1 line
56 KiB
Plaintext
1 line
56 KiB
Plaintext
{"cells": [{"attachments": {}, "cell_type": "markdown", "id": "aa3de8c6", "metadata": {"height": 30}, "source": ["# \u7b2c\u4e5d\u7ae0 \u8bc4\u4f30\uff08\u4e0a\uff09\u2014\u2014\u5b58\u5728\u4e00\u4e2a\u7b80\u5355\u7684\u6b63\u786e\u7b54\u6848\u65f6\n", "\n", " - [\u4e00\u3001\u73af\u5883\u914d\u7f6e](#\u4e00\u3001\u73af\u5883\u914d\u7f6e)\n", " - [1.1 \u52a0\u8f7d API \u5bc6\u94a5\u548c\u4e00\u4e9b Python \u5e93\u3002](#1.1-\u52a0\u8f7d-API-\u5bc6\u94a5\u548c\u4e00\u4e9b-Python-\u5e93\u3002)\n", " - [1.2 \u83b7\u53d6\u76f8\u5173\u4ea7\u54c1\u548c\u7c7b\u522b](#1.2-\u83b7\u53d6\u76f8\u5173\u4ea7\u54c1\u548c\u7c7b\u522b)\n", " - [\u4e8c\u3001\u627e\u51fa\u76f8\u5173\u4ea7\u54c1\u548c\u7c7b\u522b\u540d\u79f0](#\u4e8c\u3001\u627e\u51fa\u76f8\u5173\u4ea7\u54c1\u548c\u7c7b\u522b\u540d\u79f0)\n", " - [\u4e09\u3001\u5728\u4e00\u4e9b\u67e5\u8be2\u4e0a\u8fdb\u884c\u8bc4\u4f30](#\u4e09\u3001\u5728\u4e00\u4e9b\u67e5\u8be2\u4e0a\u8fdb\u884c\u8bc4\u4f30)\n", " - [\u56db\u3001\u66f4\u96be\u7684\u6d4b\u8bd5\u7528\u4f8b](#\u56db\u3001\u66f4\u96be\u7684\u6d4b\u8bd5\u7528\u4f8b)\n", " - [\u4e94\u3001\u4fee\u6539\u6307\u4ee4\u4ee5\u5904\u7406\u96be\u6d4b\u8bd5\u7528\u4f8b](#\u4e94\u3001\u4fee\u6539\u6307\u4ee4\u4ee5\u5904\u7406\u96be\u6d4b\u8bd5\u7528\u4f8b)\n", " - [\u516d\u3001\u5728\u96be\u6d4b\u8bd5\u7528\u4f8b\u4e0a\u8bc4\u4f30\u4fee\u6539\u540e\u7684\u6307\u4ee4](#\u516d\u3001\u5728\u96be\u6d4b\u8bd5\u7528\u4f8b\u4e0a\u8bc4\u4f30\u4fee\u6539\u540e\u7684\u6307\u4ee4)\n", " - [\u4e03\u3001\u56de\u5f52\u6d4b\u8bd5\uff1a\u9a8c\u8bc1\u6a21\u578b\u5728\u4ee5\u524d\u7684\u6d4b\u8bd5\u7528\u4f8b\u4e0a\u4ecd\u7136\u6709\u6548](#\u4e03\u3001\u56de\u5f52\u6d4b\u8bd5\uff1a\u9a8c\u8bc1\u6a21\u578b\u5728\u4ee5\u524d\u7684\u6d4b\u8bd5\u7528\u4f8b\u4e0a\u4ecd\u7136\u6709\u6548)\n", " - [\u516b\u3001\u6536\u96c6\u5f00\u53d1\u96c6\u8fdb\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5](#\u516b\u3001\u6536\u96c6\u5f00\u53d1\u96c6\u8fdb\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5)\n", " - [\u4e5d\u3001\u901a\u8fc7\u4e0e\u7406\u60f3\u7b54\u6848\u6bd4\u8f83\u6765\u8bc4\u4f30\u6d4b\u8bd5\u7528\u4f8b](#\u4e5d\u3001\u901a\u8fc7\u4e0e\u7406\u60f3\u7b54\u6848\u6bd4\u8f83\u6765\u8bc4\u4f30\u6d4b\u8bd5\u7528\u4f8b)\n", " - [\u5341\u3001\u5728\u6240\u6709\u6d4b\u8bd5\u7528\u4f8b\u4e0a\u8fd0\u884c\u8bc4\u4f30\uff0c\u5e76\u8ba1\u7b97\u6b63\u786e\u7684\u7528\u4f8b\u6bd4\u4f8b](#\u5341\u3001\u5728\u6240\u6709\u6d4b\u8bd5\u7528\u4f8b\u4e0a\u8fd0\u884c\u8bc4\u4f30\uff0c\u5e76\u8ba1\u7b97\u6b63\u786e\u7684\u7528\u4f8b\u6bd4\u4f8b)\n"]}, {"attachments": {}, "cell_type": "markdown", "id": "c768620b", "metadata": {}, "source": ["\u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 LLM \u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u8bc4\u4f30\u8f93\u5165\u3001\u5904\u7406\u8f93\u5165\u4ee5\u53ca\u5728\u5411\u7528\u6237\u663e\u793a\u8f93\u51fa\u4e4b\u524d\u8fdb\u884c\u6700\u7ec8\u8f93\u51fa\u68c0\u67e5\u3002\n", "\n", "\u6784\u5efa\u8fd9\u6837\u7684\u7cfb\u7edf\u540e\uff0c\u5982\u4f55\u77e5\u9053\u5b83\u7684\u5de5\u4f5c\u60c5\u51b5\uff1f\u751a\u81f3\u5728\u90e8\u7f72\u540e\u5e76\u8ba9\u7528\u6237\u4f7f\u7528\u5b83\u65f6\uff0c\u5982\u4f55\u8ddf\u8e2a\u5b83\u7684\u8fd0\u884c\u60c5\u51b5\uff0c\u53d1\u73b0\u4efb\u4f55\u7f3a\u9677\uff0c\u5e76\u6301\u7eed\u6539\u8fdb\u7cfb\u7edf\u7684\u7b54\u6848\u8d28\u91cf\uff1f\n", "\n", "\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u60f3\u4e0e\u60a8\u5206\u4eab\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u7528\u4e8e\u8bc4\u4f30 LLM \u7684\u8f93\u51fa\u3002\n", "\n", "\u6784\u5efa\u57fa\u4e8e LLM \u7684\u5e94\u7528\u7a0b\u5e8f\u4e0e\u4f20\u7edf\u7684\u76d1\u7763\u5b66\u4e60\u5e94\u7528\u7a0b\u5e8f\u6709\u6240\u4e0d\u540c\u3002\u7531\u4e8e\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u57fa\u4e8e LLM \u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u56e0\u6b64\u8bc4\u4f30\u65b9\u6cd5\u901a\u5e38\u4e0d\u4ece\u6d4b\u8bd5\u96c6\u5f00\u59cb\u3002\u76f8\u53cd\uff0c\u901a\u5e38\u4f1a\u9010\u6e10\u5efa\u7acb\u4e00\u7ec4\u6d4b\u8bd5\u793a\u4f8b\u3002\n", "\n", "\u5728\u4f20\u7edf\u7684\u76d1\u7763\u5b66\u4e60\u73af\u5883\u4e2d\uff0c\u9700\u8981\u6536\u96c6\u8bad\u7ec3\u96c6\u3001\u5f00\u53d1\u96c6\u6216\u4fdd\u7559\u4ea4\u53c9\u9a8c\u8bc1\u96c6\uff0c\u7136\u540e\u5728\u6574\u4e2a\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002\n", "\n", "\u7136\u800c\uff0c\u5982\u679c\u80fd\u591f\u5728\u51e0\u5206\u949f\u5185\u6307\u5b9a Prompt\uff0c\u5e76\u5728\u51e0\u4e2a\u5c0f\u65f6\u5185\u5f97\u5230\u76f8\u5e94\u7ed3\u679c\uff0c\u90a3\u4e48\u6682\u505c\u5f88\u957f\u65f6\u95f4\u53bb\u6536\u96c6\u4e00\u5343\u4e2a\u6d4b\u8bd5\u6837\u672c\u5c06\u662f\u4e00\u4ef6\u6781\u5176\u75db\u82e6\u7684\u4e8b\u60c5\u3002\u56e0\u4e3a\u73b0\u5728\uff0c\u53ef\u4ee5\u5728\u96f6\u4e2a\u8bad\u7ec3\u6837\u672c\u7684\u60c5\u51b5\u4e0b\u83b7\u5f97\u8fd9\u4e2a\u6210\u679c\u3002\n", "\n", "\u56e0\u6b64\uff0c\u5728\u4f7f\u7528 LLM \u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u60a8\u5c06\u4f53\u4f1a\u5230\u5982\u4e0b\u7684\u8fc7\u7a0b\uff1a\n", "\n", "\u9996\u5148\uff0c\u60a8\u4f1a\u5728\u53ea\u6709\u4e00\u5230\u4e09\u4e2a\u6837\u672c\u7684\u5c0f\u6837\u672c\u4e2d\u8c03\u6574 Prompt\uff0c\u5e76\u5c1d\u8bd5\u8ba9 Prompt \u5728\u5b83\u4eec\u8eab\u4e0a\u8d77\u4f5c\u7528\u3002\n", "\n", "\u7136\u540e\uff0c\u5f53\u7cfb\u7edf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u6d4b\u8bd5\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u68d8\u624b\u7684\u4f8b\u5b50\u3002Prompt \u5728\u5b83\u4eec\u8eab\u4e0a\u4e0d\u8d77\u4f5c\u7528\uff0c\u6216\u8005\u7b97\u6cd5\u5728\u5b83\u4eec\u8eab\u4e0a\u4e0d\u8d77\u4f5c\u7528\u3002\n", "\n", "\u8fd9\u5c31\u662f\u4f7f\u7528 ChatGPT API \u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u8005\u6240\u7ecf\u5386\u7684\u6311\u6218\u3002\n", "\n", "\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u989d\u5916\u7684\u51e0\u4e2a\u793a\u4f8b\u6dfb\u52a0\u5230\u60a8\u6b63\u5728\u6d4b\u8bd5\u7684\u96c6\u5408\u4e2d\uff0c\u4ee5\u673a\u4f1a\u4e3b\u4e49\u5730\u6dfb\u52a0\u5176\u4ed6\u68d8\u624b\u7684\u793a\u4f8b\u3002\n", "\n", "\u6700\u7ec8\uff0c\u60a8\u5df2\u7ecf\u6dfb\u52a0\u4e86\u8db3\u591f\u7684\u8fd9\u4e9b\u793a\u4f8b\u5230\u60a8\u7f13\u6162\u589e\u957f\u7684\u5f00\u53d1\u96c6\u4e2d\uff0c\u4ee5\u81f3\u4e8e\u901a\u8fc7\u624b\u52a8\u8fd0\u884c\u6bcf\u4e2a\u793a\u4f8b\u6765\u6d4b\u8bd5 Prompt \u53d8\u5f97\u6709\u4e9b\u4e0d\u65b9\u4fbf\u3002\n", "\n", "\u7136\u540e\uff0c\u60a8\u5f00\u59cb\u5f00\u53d1\u5728\u8fd9\u4e9b\u5c0f\u793a\u4f8b\u96c6\u4e0a\u7528\u4e8e\u8861\u91cf\u6027\u80fd\u7684\u6307\u6807\uff0c\u4f8b\u5982\u5e73\u5747\u51c6\u786e\u6027\u3002\n", "\n", "\u8fd9\u4e2a\u8fc7\u7a0b\u7684\u4e00\u4e2a\u6709\u8da3\u65b9\u9762\u662f\uff0c\u5982\u679c\u60a8\u89c9\u5f97\u60a8\u7684\u7cfb\u7edf\u5df2\u7ecf\u8db3\u591f\u597d\u4e86\uff0c\u60a8\u53ef\u4ee5\u968f\u65f6\u505c\u5728\u90a3\u91cc\uff0c\u4e0d\u518d\u6539\u8fdb\u5b83\u3002\u4e8b\u5b9e\u4e0a\uff0c\u8bb8\u591a\u5df2\u90e8\u7f72\u7684\u5e94\u7528\u7a0b\u5e8f\u505c\u5728\u7b2c\u4e00\u6216\u7b2c\u4e8c\u4e2a\u6b65\u9aa4\uff0c\u5e76\u4e14\u8fd0\u884c\u5f97\u975e\u5e38\u597d\u3002\n", "\n", "\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6709\u5f88\u591a\u5927\u6a21\u578b\u7684\u5e94\u7528\u7a0b\u5e8f\u6ca1\u6709\u5b9e\u8d28\u6027\u7684\u98ce\u9669\uff0c\u5373\u4f7f\u5b83\u6ca1\u6709\u7ed9\u51fa\u5b8c\u5168\u6b63\u786e\u7684\u7b54\u6848\u3002\n", "\n", "\u4f46\u662f\uff0c\u5bf9\u4e8e\u90e8\u5206\u9ad8\u98ce\u9669\u5e94\u7528\uff0c\u5982\u679c\u5b58\u5728\u504f\u89c1\u6216\u4e0d\u9002\u5f53\u7684\u8f93\u51fa\u53ef\u80fd\u5bf9\u67d0\u4eba\u9020\u6210\u4f24\u5bb3\uff0c\u90a3\u4e48\u6536\u96c6\u6d4b\u8bd5\u96c6\u3001\u4e25\u683c\u8bc4\u4f30\u7cfb\u7edf\u7684\u6027\u80fd\u3001\u786e\u4fdd\u5728\u4f7f\u7528\u4e4b\u524d\u5b83\u80fd\u591f\u505a\u6b63\u786e\u7684\u4e8b\u60c5\uff0c\u5c31\u53d8\u5f97\u66f4\u52a0\u91cd\u8981\u3002\n", "\n", "\u4f46\u662f\uff0c\u5982\u679c\u60a8\u53ea\u662f\u4f7f\u7528\u5b83\u6765\u603b\u7ed3\u6587\u7ae0\u4f9b\u81ea\u5df1\u9605\u8bfb\uff0c\u800c\u4e0d\u662f\u7ed9\u522b\u4eba\u770b\uff0c\u90a3\u4e48\u53ef\u80fd\u9020\u6210\u7684\u5371\u5bb3\u98ce\u9669\u66f4\u5c0f\uff0c\u60a8\u53ef\u4ee5\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u65e9\u65e9\u505c\u6b62\uff0c\u800c\u4e0d\u5fc5\u53bb\u82b1\u8d39\u66f4\u5927\u7684\u4ee3\u4ef7\u53bb\u6536\u96c6\u66f4\u5927\u7684\u6570\u636e\u96c6\u3002"]}, {"attachments": {}, "cell_type": "markdown", "id": "b0582759", "metadata": {"height": 30}, "source": ["## \u4e00\u3001\u73af\u5883\u914d\u7f6e"]}, {"cell_type": "markdown", "id": "e3eec945", "metadata": {}, "source": ["### 1.1 \u52a0\u8f7d API \u5bc6\u94a5\u548c\u4e00\u4e9b Python \u5e93\u3002\n", "\n", "\u540c\u4e0a\u4e00\u7ae0\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u914d\u7f6e\u4f7f\u7528 OpenAI API \u7684\u73af\u5883"]}, {"cell_type": "code", "execution_count": 1, "id": "a9726b15", "metadata": {"height": 166}, "outputs": [], "source": ["import os\n", "import openai\n", "import sys\n", "import time\n", "sys.path.append('../..')\n", "import utils_en\n", "import utils_zh\n", "\n", "openai.api_key = \"sk-...\"\n", "# \u8bbe\u7f6e API_KEY, \u8bf7\u66ff\u6362\u6210\u60a8\u81ea\u5df1\u7684 API_KEY\n", "\n", "# \u4ee5\u4e0b\u4e3a\u57fa\u4e8e\u73af\u5883\u53d8\u91cf\u7684\u914d\u7f6e\u65b9\u6cd5\u793a\u4f8b\uff0c\u8fd9\u6837\u66f4\u52a0\u5b89\u5168\u3002\u4ec5\u4f9b\u53c2\u8003\uff0c\u540e\u7eed\u5c06\u4e0d\u518d\u6d89\u53ca\u3002\n", "# import openai\n", "# import os\n", "# OPENAI_API_KEY = os.environ.get(\"OPENAI_API_KEY\")\n", "# openai.api_key = OPENAI_API_KEY"]}, {"cell_type": "code", "execution_count": 2, "id": "458993db", "metadata": {"height": 149}, "outputs": [], "source": ["def get_completion_from_messages(messages, \n", " model=\"gpt-3.5-turbo\", \n", " temperature=0, \n", " max_tokens=500):\n", " '''\n", " \u5c01\u88c5\u4e00\u4e2a\u8bbf\u95ee OpenAI GPT3.5 \u7684\u51fd\u6570\n", "\n", " \u53c2\u6570: \n", " messages: \u8fd9\u662f\u4e00\u4e2a\u6d88\u606f\u5217\u8868\uff0c\u6bcf\u4e2a\u6d88\u606f\u90fd\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b role(\u89d2\u8272\uff09\u548c content(\u5185\u5bb9)\u3002\u89d2\u8272\u53ef\u4ee5\u662f'system'\u3001'user' \u6216 'assistant\u2019\uff0c\u5185\u5bb9\u662f\u89d2\u8272\u7684\u6d88\u606f\u3002\n", " model: \u8c03\u7528\u7684\u6a21\u578b\uff0c\u9ed8\u8ba4\u4e3a gpt-3.5-turbo(ChatGPT)\uff0c\u6709\u5185\u6d4b\u8d44\u683c\u7684\u7528\u6237\u53ef\u4ee5\u9009\u62e9 gpt-4\n", " temperature: \u8fd9\u51b3\u5b9a\u6a21\u578b\u8f93\u51fa\u7684\u968f\u673a\u7a0b\u5ea6\uff0c\u9ed8\u8ba4\u4e3a0\uff0c\u8868\u793a\u8f93\u51fa\u5c06\u975e\u5e38\u786e\u5b9a\u3002\u589e\u52a0\u6e29\u5ea6\u4f1a\u4f7f\u8f93\u51fa\u66f4\u968f\u673a\u3002\n", " max_tokens: \u8fd9\u51b3\u5b9a\u6a21\u578b\u8f93\u51fa\u7684\u6700\u5927\u7684 token \u6570\u3002\n", " '''\n", " response = openai.ChatCompletion.create(\n", " model=model,\n", " messages=messages,\n", " temperature=temperature, # \u8fd9\u51b3\u5b9a\u6a21\u578b\u8f93\u51fa\u7684\u968f\u673a\u7a0b\u5ea6\n", " max_tokens=max_tokens, # \u8fd9\u51b3\u5b9a\u6a21\u578b\u8f93\u51fa\u7684\u6700\u5927\u7684 token \u6570\n", " )\n", " return response.choices[0].message[\"content\"]"]}, {"attachments": {}, "cell_type": "markdown", "id": "3b6a4c17", "metadata": {"height": 30}, "source": ["### 1.2 \u83b7\u53d6\u76f8\u5173\u4ea7\u54c1\u548c\u7c7b\u522b\n", "\n", "\u6211\u4eec\u8981\u83b7\u53d6\u524d\u51e0\u7ae0\u4e2d\u63d0\u5230\u7684\u4ea7\u54c1\u76ee\u5f55\u4e2d\u7684\u4ea7\u54c1\u548c\u7c7b\u522b\u5217\u8868\u3002"]}, {"cell_type": "code", "execution_count": 5, "id": "6f4062ea", "metadata": {"height": 47}, "outputs": [{"data": {"text/plain": ["{'Computers and Laptops': ['TechPro Ultrabook',\n", " 'BlueWave Gaming Laptop',\n", " 'PowerLite Convertible',\n", " 'TechPro Desktop',\n", " 'BlueWave Chromebook'],\n", " 'Smartphones and Accessories': ['SmartX ProPhone',\n", " 'MobiTech PowerCase',\n", " 'SmartX MiniPhone',\n", " 'MobiTech Wireless Charger',\n", " 'SmartX EarBuds'],\n", " 'Televisions and Home Theater Systems': ['CineView 4K TV',\n", " 'SoundMax Home Theater',\n", " 'CineView 8K TV',\n", " 'SoundMax Soundbar',\n", " 'CineView OLED TV'],\n", " 'Gaming Consoles and Accessories': ['GameSphere X',\n", " 'ProGamer Controller',\n", " 'GameSphere Y',\n", " 'ProGamer Racing Wheel',\n", " 'GameSphere VR Headset'],\n", " 'Audio Equipment': ['AudioPhonic Noise-Canceling Headphones',\n", " 'WaveSound Bluetooth Speaker',\n", " 'AudioPhonic True Wireless Earbuds',\n", " 'WaveSound Soundbar',\n", " 'AudioPhonic Turntable'],\n", " 'Cameras and Camcorders': ['FotoSnap DSLR Camera',\n", " 'ActionCam 4K',\n", " 'FotoSnap Mirrorless Camera',\n", " 'ZoomMaster Camcorder',\n", " 'FotoSnap Instant Camera']}"]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["products_and_category = utils_en.get_products_and_category()\n", "products_and_category"]}, {"attachments": {}, "cell_type": "markdown", "id": "d91f5384", "metadata": {"height": 30}, "source": ["## \u4e8c\u3001\u627e\u51fa\u76f8\u5173\u4ea7\u54c1\u548c\u7c7b\u522b\u540d\u79f0"]}, {"cell_type": "code", "execution_count": 4, "id": "e426619a", "metadata": {"height": 744}, "outputs": [], "source": ["def find_category_and_product_v1(user_input, products_and_category):\n", " \"\"\"\n", " \u4ece\u7528\u6237\u8f93\u5165\u4e2d\u83b7\u53d6\u5230\u4ea7\u54c1\u548c\u7c7b\u522b\n", "\n", " \u53c2\u6570\uff1a\n", " user_input\uff1a\u7528\u6237\u7684\u67e5\u8be2\n", " products_and_category\uff1a\u4ea7\u54c1\u7c7b\u578b\u548c\u5bf9\u5e94\u4ea7\u54c1\u7684\u5b57\u5178\n", " \"\"\"\n", "\n", " # \u5206\u9694\u7b26\n", " delimiter = \"####\"\n", " # \u5b9a\u4e49\u7684\u7cfb\u7edf\u4fe1\u606f\uff0c\u9648\u8ff0\u4e86\u9700\u8981 GPT \u5b8c\u6210\u7684\u5de5\u4f5c\n", " system_message = f\"\"\"\n", " You will be provided with customer service queries. \\\n", " The customer service query will be delimited with {delimiter} characters.\n", " Output a Python list of json objects, where each object has the following format:\n", " 'category': <one of Computers and Laptops, Smartphones and Accessories, Televisions and Home Theater Systems, \\\n", " Gaming Consoles and Accessories, Audio Equipment, Cameras and Camcorders>,\n", " AND\n", " 'products': <a list of products that must be found in the allowed products below>\n", "\n", "\n", " Where the categories and products must be found in the customer service query.\n", " If a product is mentioned, it must be associated with the correct category in the allowed products list below.\n", " If no products or categories are found, output an empty list.\n", " \n", "\n", " List out all products that are relevant to the customer service query based on how closely it relates\n", " to the product name and product category.\n", " Do not assume, from the name of the product, any features or attributes such as relative quality or price.\n", "\n", " The allowed products are provided in JSON format.\n", " The keys of each item represent the category.\n", " The values of each item is a list of products that are within that category.\n", " Allowed products: {products_and_category}\n", " \n", "\n", " \"\"\"\n", " # \u7ed9\u51fa\u51e0\u4e2a\u793a\u4f8b\n", " few_shot_user_1 = \"\"\"I want the most expensive computer.\"\"\"\n", " few_shot_assistant_1 = \"\"\" \n", " [{'category': 'Computers and Laptops', \\\n", "'products': ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook']}]\n", " \"\"\"\n", " \n", " messages = [ \n", " {'role':'system', 'content': system_message}, \n", " {'role':'user', 'content': f\"{delimiter}{few_shot_user_1}{delimiter}\"}, \n", " {'role':'assistant', 'content': few_shot_assistant_1 },\n", " {'role':'user', 'content': f\"{delimiter}{user_input}{delimiter}\"}, \n", " ] \n", " return get_completion_from_messages(messages)\n"]}, {"cell_type": "code", "execution_count": 3, "id": "ac683bfb", "metadata": {}, "outputs": [], "source": ["def find_category_and_product_v1(user_input,products_and_category):\n", " \"\"\"\n", " \u4ece\u7528\u6237\u8f93\u5165\u4e2d\u83b7\u53d6\u5230\u4ea7\u54c1\u548c\u7c7b\u522b\n", "\n", " \u53c2\u6570\uff1a\n", " user_input\uff1a\u7528\u6237\u7684\u67e5\u8be2\n", " products_and_category\uff1a\u4ea7\u54c1\u7c7b\u578b\u548c\u5bf9\u5e94\u4ea7\u54c1\u7684\u5b57\u5178\n", " \"\"\"\n", " \n", " delimiter = \"####\"\n", " system_message = f\"\"\"\n", " \u60a8\u5c06\u63d0\u4f9b\u5ba2\u6237\u670d\u52a1\u67e5\u8be2\u3002\\\n", " \u5ba2\u6237\u670d\u52a1\u67e5\u8be2\u5c06\u7528{delimiter}\u5b57\u7b26\u5206\u9694\u3002\n", " \u8f93\u51fa\u4e00\u4e2a Python \u5217\u8868\uff0c\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u662f Json \u5bf9\u8c61\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u7684\u683c\u5f0f\u5982\u4e0b\uff1a\n", " 'category': <Computers and Laptops, Smartphones and Accessories, Televisions and Home Theater Systems, \\\n", " Gaming Consoles and Accessories, Audio Equipment, Cameras and Camcorders\u4e2d\u7684\u4e00\u4e2a>,\n", " \u4ee5\u53ca\n", " 'products': <\u5fc5\u987b\u5728\u4e0b\u9762\u5141\u8bb8\u7684\u4ea7\u54c1\u4e2d\u627e\u5230\u7684\u4ea7\u54c1\u5217\u8868>\n", " \n", " \u5176\u4e2d\u7c7b\u522b\u548c\u4ea7\u54c1\u5fc5\u987b\u5728\u5ba2\u6237\u670d\u52a1\u67e5\u8be2\u4e2d\u627e\u5230\u3002\n", " \u5982\u679c\u63d0\u5230\u4e86\u4e00\u4e2a\u4ea7\u54c1\uff0c\u5b83\u5fc5\u987b\u4e0e\u4e0b\u9762\u5141\u8bb8\u7684\u4ea7\u54c1\u5217\u8868\u4e2d\u7684\u6b63\u786e\u7c7b\u522b\u5173\u8054\u3002\n", " \u5982\u679c\u6ca1\u6709\u627e\u5230\u4ea7\u54c1\u6216\u7c7b\u522b\uff0c\u8f93\u51fa\u4e00\u4e2a\u7a7a\u5217\u8868\u3002\n", " \n", " \u6839\u636e\u4ea7\u54c1\u540d\u79f0\u548c\u4ea7\u54c1\u7c7b\u522b\u4e0e\u5ba2\u6237\u670d\u52a1\u67e5\u8be2\u7684\u76f8\u5173\u6027\uff0c\u5217\u51fa\u6240\u6709\u76f8\u5173\u7684\u4ea7\u54c1\u3002\n", " \u4e0d\u8981\u4ece\u4ea7\u54c1\u7684\u540d\u79f0\u4e2d\u5047\u8bbe\u4efb\u4f55\u7279\u6027\u6216\u5c5e\u6027\uff0c\u5982\u76f8\u5bf9\u8d28\u91cf\u6216\u4ef7\u683c\u3002\n", " \n", " \u5141\u8bb8\u7684\u4ea7\u54c1\u4ee5 JSON \u683c\u5f0f\u63d0\u4f9b\u3002\n", " \u6bcf\u4e2a\u9879\u76ee\u7684\u952e\u4ee3\u8868\u7c7b\u522b\u3002\n", " \u6bcf\u4e2a\u9879\u76ee\u7684\u503c\u662f\u8be5\u7c7b\u522b\u4e2d\u7684\u4ea7\u54c1\u5217\u8868\u3002\n", " \u5141\u8bb8\u7684\u4ea7\u54c1\uff1a{products_and_category}\n", " \n", " \"\"\"\n", " \n", " few_shot_user_1 = \"\"\"\u6211\u60f3\u8981\u6700\u8d35\u7684\u7535\u8111\u3002\"\"\"\n", " few_shot_assistant_1 = \"\"\" \n", " [{'category': 'Computers and Laptops', \\\n", "'products': ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook']}]\n", " \"\"\"\n", " \n", " messages = [ \n", " {'role':'system', 'content': system_message}, \n", " {'role':'user', 'content': f\"{delimiter}{few_shot_user_1}{delimiter}\"}, \n", " {'role':'assistant', 'content': few_shot_assistant_1 },\n", " {'role':'user', 'content': f\"{delimiter}{user_input}{delimiter}\"}, \n", " ] \n", " return get_completion_from_messages(messages)"]}, {"attachments": {}, "cell_type": "markdown", "id": "aca82030", "metadata": {"height": 30}, "source": ["## \u4e09\u3001\u5728\u4e00\u4e9b\u67e5\u8be2\u4e0a\u8fdb\u884c\u8bc4\u4f30"]}, {"cell_type": "code", "execution_count": 5, "id": "09cb58f3", "metadata": {"height": 98}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]\n"]}], "source": ["# \u7b2c\u4e00\u4e2a\u8bc4\u4f30\u7684\u67e5\u8be2\n", "customer_msg_0 = f\"\"\"Which TV can I buy if I'm on a budget?\"\"\"\n", "\n", "products_by_category_0 = find_category_and_product_v1(customer_msg_0,\n", " products_and_category)\n", "print(products_by_category_0)"]}, {"cell_type": "code", "execution_count": null, "id": "cacb96b2", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'SoundMax Soundbar', 'CineView OLED TV']}]\n"]}], "source": ["# \u7b2c\u4e00\u4e2a\u8bc4\u4f30\u7684\u67e5\u8be2\n", "customer_msg_0 = f\"\"\"\u5982\u679c\u6211\u9884\u7b97\u6709\u9650\uff0c\u6211\u53ef\u4ee5\u4e70\u54ea\u6b3e\u7535\u89c6\uff1f\"\"\"\n", "\n", "products_by_category_0 = find_category_and_product_v1(customer_msg_0,\n", " products_and_category)\n", "print(products_by_category_0)"]}, {"cell_type": "code", "execution_count": 6, "id": "d2160d28", "metadata": {"height": 98}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Smartphones and Accessories', 'products': ['MobiTech PowerCase', 'MobiTech Wireless Charger', 'SmartX EarBuds']}]\n", "\n"]}], "source": ["# \u7b2c\u4e8c\u4e2a\u8bc4\u4f30\u7684\u67e5\u8be2\n", "customer_msg_1 = f\"\"\"I need a charger for my smartphone\"\"\"\n", "\n", "products_by_category_1 = find_category_and_product_v1(customer_msg_1,\n", " products_and_category)\n", "print(products_by_category_1)"]}, {"cell_type": "code", "execution_count": null, "id": "04364405", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Smartphones and Accessories', 'products': ['MobiTech PowerCase', 'MobiTech Wireless Charger', 'SmartX EarBuds']}]\n", "\n"]}], "source": ["customer_msg_1 = f\"\"\"\u6211\u9700\u8981\u4e00\u4e2a\u667a\u80fd\u624b\u673a\u7684\u5145\u7535\u5668\"\"\"\n", "\n", "products_by_category_1 = find_category_and_product_v1(customer_msg_1,\n", " products_and_category)\n", "print(products_by_category_1)"]}, {"cell_type": "code", "execution_count": 7, "id": "4de5c246", "metadata": {"height": 115}, "outputs": [{"data": {"text/plain": ["\" [{'category': 'Computers and Laptops', 'products': ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook']}]\""]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u7b2c\u4e09\u4e2a\u8bc4\u4f30\u67e5\u8be2\n", "customer_msg_2 = f\"\"\"\n", "What computers do you have?\"\"\"\n", "\n", "products_by_category_2 = find_category_and_product_v1(customer_msg_2,\n", " products_and_category)\n", "products_by_category_2"]}, {"cell_type": "code", "execution_count": null, "id": "66e9ecd0", "metadata": {}, "outputs": [{"data": {"text/plain": ["\" [{'category': 'Computers and Laptops', 'products': ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook']}]\""]}, "metadata": {}, "output_type": "display_data"}], "source": ["customer_msg_2 = f\"\"\"\n", "\u4f60\u4eec\u6709\u54ea\u4e9b\u7535\u8111\uff1f\"\"\"\n", "\n", "products_by_category_2 = find_category_and_product_v1(customer_msg_2,\n", " products_and_category)\n", "products_by_category_2"]}, {"cell_type": "code", "execution_count": 10, "id": "74f16345", "metadata": {"height": 132}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Smartphones and Accessories', 'products': ['SmartX ProPhone']},\n", " {'category': 'Cameras and Camcorders', 'products': ['FotoSnap DSLR Camera']},\n", " {'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]\n", " \n", " Note: The query mentions \"smartx pro phone\" and \"fotosnap camera, the dslr one\", so the output includes the relevant categories and products. The query also asks about TVs, so the relevant category is included in the output.\n"]}], "source": ["# \u7b2c\u56db\u4e2a\u67e5\u8be2\uff0c\u66f4\u590d\u6742\n", "customer_msg_3 = f\"\"\"\n", "tell me about the smartx pro phone and the fotosnap camera, the dslr one.\n", "Also, what TVs do you have?\"\"\"\n", "\n", "products_by_category_3 = find_category_and_product_v1(customer_msg_3,\n", " products_and_category)\n", "print(products_by_category_3)"]}, {"cell_type": "code", "execution_count": null, "id": "112cfd5f", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Smartphones and Accessories', 'products': ['SmartX ProPhone']}, {'category': 'Cameras and Camcorders', 'products': ['FotoSnap DSLR Camera']}]\n", " \n", " {'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}\n"]}], "source": ["customer_msg_3 = f\"\"\"\n", "\u544a\u8bc9\u6211\u5173\u4e8esmartx pro\u624b\u673a\u548cfotosnap\u76f8\u673a\u7684\u4fe1\u606f\uff0c\u90a3\u6b3eDSLR\u7684\u3002\n", "\u53e6\u5916\uff0c\u4f60\u4eec\u6709\u54ea\u4e9b\u7535\u89c6\uff1f\"\"\"\n", "\n", "products_by_category_3 = find_category_and_product_v1(customer_msg_3,\n", " products_and_category)\n", "print(products_by_category_3)"]}, {"attachments": {}, "cell_type": "markdown", "id": "d58f15be", "metadata": {}, "source": ["\u5b83\u770b\u8d77\u6765\u50cf\u662f\u8f93\u51fa\u4e86\u6b63\u786e\u7684\u6570\u636e\uff0c\u4f46\u5b83\u4e5f\u8f93\u51fa\u4e86\u4e00\u5806\u6587\u672c\uff0c\u8fd9\u4e9b\u662f\u591a\u4f59\u7684\u3002\u8fd9\u4f7f\u5f97\u5c06\u5176\u89e3\u6790\u4e3a Python \u5b57\u5178\u5217\u8868\u66f4\u52a0\u56f0\u96be\u3002"]}, {"attachments": {}, "cell_type": "markdown", "id": "ff2af235", "metadata": {"height": 30}, "source": ["## \u56db\u3001\u66f4\u96be\u7684\u6d4b\u8bd5\u7528\u4f8b\n", "\n", "\u627e\u51fa\u4e00\u4e9b\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6a21\u578b\u8868\u73b0\u4e0d\u5982\u9884\u671f\u7684\u67e5\u8be2\u3002"]}, {"cell_type": "code", "execution_count": 9, "id": "4cbf55cd", "metadata": {"height": 132}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Televisions and Home Theater Systems', 'products': ['CineView 8K TV']},\n", " {'category': 'Gaming Consoles and Accessories', 'products': ['GameSphere X']},\n", " {'category': 'Computers and Laptops', 'products': ['BlueWave Chromebook']}]\n", " \n", " Note: The CineView TV mentioned is the 8K one, and the Gamesphere console mentioned is the X one. \n", " For the computer category, since the customer mentioned being on a budget, we cannot determine which specific product to recommend. \n", " Therefore, we have included all the products in the Computers and Laptops category in the output.\n"]}], "source": ["customer_msg_4 = f\"\"\"\n", "tell me about the CineView TV, the 8K one, Gamesphere console, the X one.\n", "I'm on a budget, what computers do you have?\"\"\"\n", "\n", "products_by_category_4 = find_category_and_product_v1(customer_msg_4,\n", " products_and_category)\n", "print(products_by_category_4)"]}, {"cell_type": "code", "execution_count": 10, "id": "5b11172f", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Televisions and Home Theater Systems', 'products': ['CineView 8K TV']}, {'category': 'Gaming Consoles and Accessories', 'products': ['GameSphere X']}, {'category': 'Computers and Laptops', 'products': ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook']}]\n", " \n", " \u5177\u4f53\u6765\u8bf4\uff0cCineView 8K\u7535\u89c6\u662f\u4e00\u6b3e\u9ad8\u7aef\u7535\u89c6\uff0c\u5177\u67098K\u5206\u8fa8\u7387\u548cOLED\u663e\u793a\u5c4f\u3002GameSphere X\u662f\u4e00\u6b3e\u6e38\u620f\u673a\uff0c\u5177\u6709\u9ad8\u6027\u80fd\u548c\u591a\u79cd\u6e38\u620f\u9009\u62e9\u3002\u5bf9\u4e8e\u9884\u7b97\u6709\u9650\u7684\u7535\u8111\uff0c\u60a8\u53ef\u4ee5\u8003\u8651TechPro Chromebook\u6216TechPro Ultrabook\uff0c\u5b83\u4eec\u90fd\u662f\u8f83\u4e3a\u7ecf\u6d4e\u5b9e\u60e0\u7684\u9009\u62e9\u3002\n"]}], "source": ["customer_msg_4 = f\"\"\"\n", "\u544a\u8bc9\u6211\u5173\u4e8eCineView\u7535\u89c6\u7684\u4fe1\u606f\uff0c\u90a3\u6b3e8K\u7684\uff0c\u8fd8\u6709Gamesphere\u6e38\u620f\u673a\uff0cX\u6b3e\u7684\u3002\n", "\u6211\u9884\u7b97\u6709\u9650\uff0c\u4f60\u4eec\u6709\u54ea\u4e9b\u7535\u8111\uff1f\"\"\"\n", "\n", "products_by_category_4 = find_category_and_product_v1(customer_msg_4,products_and_category)\n", "print(products_by_category_4)"]}, {"attachments": {}, "cell_type": "markdown", "id": "92b63d8b", "metadata": {"height": 30}, "source": ["## \u4e94\u3001\u4fee\u6539\u6307\u4ee4\u4ee5\u5904\u7406\u96be\u6d4b\u8bd5\u7528\u4f8b"]}, {"attachments": {}, "cell_type": "markdown", "id": "ddcee6a5", "metadata": {}, "source": ["\u6211\u4eec\u5728\u63d0\u793a\u4e2d\u6dfb\u52a0\u4e86\u4ee5\u4e0b\u5185\u5bb9\uff0c\u4e0d\u8981\u8f93\u51fa\u4efb\u4f55\u4e0d\u5728 JSON \u683c\u5f0f\u4e2d\u7684\u9644\u52a0\u6587\u672c\uff0c\u5e76\u6dfb\u52a0\u4e86\u7b2c\u4e8c\u4e2a\u793a\u4f8b\uff0c\u4f7f\u7528\u7528\u6237\u548c\u52a9\u624b\u6d88\u606f\u8fdb\u884c few-shot \u63d0\u793a\u3002"]}, {"cell_type": "code", "execution_count": 11, "id": "5954e112", "metadata": {"height": 1016}, "outputs": [], "source": ["def find_category_and_product_v2(user_input, products_and_category):\n", " \"\"\"\n", " \u4ece\u7528\u6237\u8f93\u5165\u4e2d\u83b7\u53d6\u5230\u4ea7\u54c1\u548c\u7c7b\u522b\n", " \u6dfb\u52a0\uff1a\u4e0d\u8981\u8f93\u51fa\u4efb\u4f55\u4e0d\u7b26\u5408 JSON \u683c\u5f0f\u7684\u989d\u5916\u6587\u672c\u3002\n", " \u6dfb\u52a0\u4e86\u7b2c\u4e8c\u4e2a\u793a\u4f8b\uff08\u7528\u4e8e few-shot \u63d0\u793a\uff09\uff0c\u7528\u6237\u8be2\u95ee\u6700\u4fbf\u5b9c\u7684\u8ba1\u7b97\u673a\u3002\n", " \u5728\u8fd9\u4e24\u4e2a few-shot \u793a\u4f8b\u4e2d\uff0c\u663e\u793a\u7684\u54cd\u5e94\u53ea\u662f JSON \u683c\u5f0f\u7684\u5b8c\u6574\u4ea7\u54c1\u5217\u8868\u3002\n", "\n", " \u53c2\u6570\uff1a\n", " user_input\uff1a\u7528\u6237\u7684\u67e5\u8be2\n", " products_and_category\uff1a\u4ea7\u54c1\u7c7b\u578b\u548c\u5bf9\u5e94\u4ea7\u54c1\u7684\u5b57\u5178\n", " \"\"\"\n", " delimiter = \"####\"\n", " system_message = f\"\"\"\n", " You will be provided with customer service queries. \\\n", " The customer service query will be delimited with {delimiter} characters.\n", " Output a Python list of JSON objects, where each object has the following format:\n", " 'category': <one of Computers and Laptops, Smartphones and Accessories, Televisions and Home Theater Systems, \\\n", " Gaming Consoles and Accessories, Audio Equipment, Cameras and Camcorders>,\n", " AND\n", " 'products': <a list of products that must be found in the allowed products below>\n", " Do not output any additional text that is not in JSON format.\n", " Do not write any explanatory text after outputting the requested JSON.\n", "\n", "\n", " Where the categories and products must be found in the customer service query.\n", " If a product is mentioned, it must be associated with the correct category in the allowed products list below.\n", " If no products or categories are found, output an empty list.\n", " \n", "\n", " List out all products that are relevant to the customer service query based on how closely it relates\n", " to the product name and product category.\n", " Do not assume, from the name of the product, any features or attributes such as relative quality or price.\n", "\n", " The allowed products are provided in JSON format.\n", " The keys of each item represent the category.\n", " The values of each item is a list of products that are within that category.\n", " Allowed products: {products_and_category}\n", " \n", "\n", " \"\"\"\n", " \n", " few_shot_user_1 = \"\"\"I want the most expensive computer. What do you recommend?\"\"\"\n", " few_shot_assistant_1 = \"\"\" \n", " [{'category': 'Computers and Laptops', \\\n", "'products': ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook']}]\n", " \"\"\"\n", " \n", " few_shot_user_2 = \"\"\"I want the most cheapest computer. What do you recommend?\"\"\"\n", " few_shot_assistant_2 = \"\"\" \n", " [{'category': 'Computers and Laptops', \\\n", "'products': ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook']}]\n", " \"\"\"\n", " \n", " messages = [ \n", " {'role':'system', 'content': system_message}, \n", " {'role':'user', 'content': f\"{delimiter}{few_shot_user_1}{delimiter}\"}, \n", " {'role':'assistant', 'content': few_shot_assistant_1 },\n", " {'role':'user', 'content': f\"{delimiter}{few_shot_user_2}{delimiter}\"}, \n", " {'role':'assistant', 'content': few_shot_assistant_2 },\n", " {'role':'user', 'content': f\"{delimiter}{user_input}{delimiter}\"}, \n", " ] \n", " return get_completion_from_messages(messages)\n"]}, {"cell_type": "code", "execution_count": 11, "id": "d3b183bf", "metadata": {}, "outputs": [], "source": ["def find_category_and_product_v2(user_input,products_and_category):\n", " \"\"\"\n", " \u4ece\u7528\u6237\u8f93\u5165\u4e2d\u83b7\u53d6\u5230\u4ea7\u54c1\u548c\u7c7b\u522b\n", "\n", " \u6dfb\u52a0\uff1a\u4e0d\u8981\u8f93\u51fa\u4efb\u4f55\u4e0d\u7b26\u5408 JSON \u683c\u5f0f\u7684\u989d\u5916\u6587\u672c\u3002\n", " \u6dfb\u52a0\u4e86\u7b2c\u4e8c\u4e2a\u793a\u4f8b\uff08\u7528\u4e8e few-shot \u63d0\u793a\uff09\uff0c\u7528\u6237\u8be2\u95ee\u6700\u4fbf\u5b9c\u7684\u8ba1\u7b97\u673a\u3002\n", " \u5728\u8fd9\u4e24\u4e2a few-shot \u793a\u4f8b\u4e2d\uff0c\u663e\u793a\u7684\u54cd\u5e94\u53ea\u662f JSON \u683c\u5f0f\u7684\u5b8c\u6574\u4ea7\u54c1\u5217\u8868\u3002\n", "\n", " \u53c2\u6570\uff1a\n", " user_input\uff1a\u7528\u6237\u7684\u67e5\u8be2\n", " products_and_category\uff1a\u4ea7\u54c1\u7c7b\u578b\u548c\u5bf9\u5e94\u4ea7\u54c1\u7684\u5b57\u5178 \n", " \"\"\"\n", " delimiter = \"####\"\n", " system_message = f\"\"\"\n", " \u60a8\u5c06\u63d0\u4f9b\u5ba2\u6237\u670d\u52a1\u67e5\u8be2\u3002\\\n", " \u5ba2\u6237\u670d\u52a1\u67e5\u8be2\u5c06\u7528{delimiter}\u5b57\u7b26\u5206\u9694\u3002\n", " \u8f93\u51fa\u4e00\u4e2a Python\u5217\u8868\uff0c\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u662f JSON \u5bf9\u8c61\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u7684\u683c\u5f0f\u5982\u4e0b\uff1a\n", " 'category': <Computers and Laptops, Smartphones and Accessories, Televisions and Home Theater Systems, \\\n", " Gaming Consoles and Accessories, Audio Equipment, Cameras and Camcorders\u4e2d\u7684\u4e00\u4e2a>,\n", " AND\n", " 'products': <\u5fc5\u987b\u5728\u4e0b\u9762\u5141\u8bb8\u7684\u4ea7\u54c1\u4e2d\u627e\u5230\u7684\u4ea7\u54c1\u5217\u8868>\n", " \u4e0d\u8981\u8f93\u51fa\u4efb\u4f55\u4e0d\u662f JSON \u683c\u5f0f\u7684\u989d\u5916\u6587\u672c\u3002\n", " \u8f93\u51fa\u8bf7\u6c42\u7684 JSON \u540e\uff0c\u4e0d\u8981\u5199\u4efb\u4f55\u89e3\u91ca\u6027\u7684\u6587\u672c\u3002\n", " \n", " \u5176\u4e2d\u7c7b\u522b\u548c\u4ea7\u54c1\u5fc5\u987b\u5728\u5ba2\u6237\u670d\u52a1\u67e5\u8be2\u4e2d\u627e\u5230\u3002\n", " \u5982\u679c\u63d0\u5230\u4e86\u4e00\u4e2a\u4ea7\u54c1\uff0c\u5b83\u5fc5\u987b\u4e0e\u4e0b\u9762\u5141\u8bb8\u7684\u4ea7\u54c1\u5217\u8868\u4e2d\u7684\u6b63\u786e\u7c7b\u522b\u5173\u8054\u3002\n", " \u5982\u679c\u6ca1\u6709\u627e\u5230\u4ea7\u54c1\u6216\u7c7b\u522b\uff0c\u8f93\u51fa\u4e00\u4e2a\u7a7a\u5217\u8868\u3002\n", " \n", " \u6839\u636e\u4ea7\u54c1\u540d\u79f0\u548c\u4ea7\u54c1\u7c7b\u522b\u4e0e\u5ba2\u6237\u670d\u52a1\u67e5\u8be2\u7684\u76f8\u5173\u6027\uff0c\u5217\u51fa\u6240\u6709\u76f8\u5173\u7684\u4ea7\u54c1\u3002\n", " \u4e0d\u8981\u4ece\u4ea7\u54c1\u7684\u540d\u79f0\u4e2d\u5047\u8bbe\u4efb\u4f55\u7279\u6027\u6216\u5c5e\u6027\uff0c\u5982\u76f8\u5bf9\u8d28\u91cf\u6216\u4ef7\u683c\u3002\n", " \n", " \u5141\u8bb8\u7684\u4ea7\u54c1\u4ee5 JSON \u683c\u5f0f\u63d0\u4f9b\u3002\n", " \u6bcf\u4e2a\u9879\u76ee\u7684\u952e\u4ee3\u8868\u7c7b\u522b\u3002\n", " \u6bcf\u4e2a\u9879\u76ee\u7684\u503c\u662f\u8be5\u7c7b\u522b\u4e2d\u7684\u4ea7\u54c1\u5217\u8868\u3002\n", " \u5141\u8bb8\u7684\u4ea7\u54c1\uff1a{products_and_category}\n", " \n", " \"\"\"\n", " \n", " few_shot_user_1 = \"\"\"\u6211\u60f3\u8981\u6700\u8d35\u7684\u7535\u8111\u3002\u4f60\u63a8\u8350\u54ea\u6b3e\uff1f\"\"\"\n", " few_shot_assistant_1 = \"\"\" \n", " [{'category': 'Computers and Laptops', \\\n", "'products': ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook']}]\n", " \"\"\"\n", " \n", " few_shot_user_2 = \"\"\"\u6211\u60f3\u8981\u6700\u4fbf\u5b9c\u7684\u7535\u8111\u3002\u4f60\u63a8\u8350\u54ea\u6b3e\uff1f\"\"\"\n", " few_shot_assistant_2 = \"\"\" \n", " [{'category': 'Computers and Laptops', \\\n", "'products': ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook']}]\n", " \"\"\"\n", " \n", " messages = [ \n", " {'role':'system', 'content': system_message}, \n", " {'role':'user', 'content': f\"{delimiter}{few_shot_user_1}{delimiter}\"}, \n", " {'role':'assistant', 'content': few_shot_assistant_1 },\n", " {'role':'user', 'content': f\"{delimiter}{few_shot_user_2}{delimiter}\"}, \n", " {'role':'assistant', 'content': few_shot_assistant_2 },\n", " {'role':'user', 'content': f\"{delimiter}{user_input}{delimiter}\"}, \n", " ] \n", " return get_completion_from_messages(messages)"]}, {"attachments": {}, "cell_type": "markdown", "id": "83e8ab86", "metadata": {"height": 30}, "source": ["## \u516d\u3001\u5728\u96be\u6d4b\u8bd5\u7528\u4f8b\u4e0a\u8bc4\u4f30\u4fee\u6539\u540e\u7684\u6307\u4ee4"]}, {"cell_type": "code", "execution_count": 12, "id": "1e876345", "metadata": {"height": 132}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Smartphones and Accessories', 'products': ['SmartX ProPhone']}, {'category': 'Cameras and Camcorders', 'products': ['FotoSnap DSLR Camera']}, {'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]\n", "\n"]}], "source": ["customer_msg_3 = f\"\"\"\n", "tell me about the smartx pro phone and the fotosnap camera, the dslr one.\n", "Also, what TVs do you have?\"\"\"\n", "\n", "products_by_category_3 = find_category_and_product_v2(customer_msg_3,\n", " products_and_category)\n", "print(products_by_category_3)"]}, {"cell_type": "code", "execution_count": 12, "id": "4a547b34", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Smartphones and Accessories', 'products': ['SmartX ProPhone']}, {'category': 'Cameras and Camcorders', 'products': ['FotoSnap DSLR Camera']}, {'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]\n", "\n"]}], "source": ["customer_msg_3 = f\"\"\"\n", "\u544a\u8bc9\u6211\u5173\u4e8esmartx pro\u624b\u673a\u548cfotosnap\u76f8\u673a\u7684\u4fe1\u606f\uff0c\u90a3\u6b3eDSLR\u7684\u3002\n", "\u53e6\u5916\uff0c\u4f60\u4eec\u6709\u54ea\u4e9b\u7535\u89c6\uff1f\"\"\"\n", "\n", "products_by_category_3 = find_category_and_product_v2(customer_msg_3,\n", " products_and_category)\n", "print(products_by_category_3)"]}, {"attachments": {}, "cell_type": "markdown", "id": "22a0a17b", "metadata": {"height": 30}, "source": ["## \u4e03\u3001\u56de\u5f52\u6d4b\u8bd5\uff1a\u9a8c\u8bc1\u6a21\u578b\u5728\u4ee5\u524d\u7684\u6d4b\u8bd5\u7528\u4f8b\u4e0a\u4ecd\u7136\u6709\u6548\n", "\n", "\u68c0\u67e5\u5e76\u4fee\u590d\u6a21\u578b\u4ee5\u63d0\u9ad8\u96be\u4ee5\u6d4b\u8bd5\u7684\u7528\u4f8b\u6548\u679c\uff0c\u540c\u65f6\u786e\u4fdd\u6b64\u4fee\u6b63\u4e0d\u4f1a\u5bf9\u5148\u524d\u7684\u6d4b\u8bd5\u7528\u4f8b\u6027\u80fd\u9020\u6210\u8d1f\u9762\u5f71\u54cd\u3002"]}, {"cell_type": "code", "execution_count": 13, "id": "f2a46445", "metadata": {"height": 98}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" [{'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]\n", "\n"]}], "source": ["customer_msg_0 = f\"\"\"Which TV can I buy if I'm on a budget?\"\"\"\n", "\n", "products_by_category_0 = find_category_and_product_v2(customer_msg_0,\n", " products_and_category)\n", "print(products_by_category_0)"]}, {"cell_type": "code", "execution_count": 13, "id": "b5ba773b", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": [" \n", "\n", " [{'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]\n", " \n", " \u5982\u679c\u60a8\u7684\u9884\u7b97\u6709\u9650\uff0c\u6211\u4eec\u5efa\u8bae\u60a8\u8d2d\u4e70CineView 4K\u7535\u89c6\u6216SoundMax\u5bb6\u5ead\u5f71\u9662\u3002\n"]}], "source": ["customer_msg_0 = f\"\"\"\u5982\u679c\u6211\u9884\u7b97\u6709\u9650\uff0c\u6211\u53ef\u4ee5\u4e70\u54ea\u6b3e\u7535\u89c6\uff1f\"\"\"\n", "\n", "products_by_category_0 = find_category_and_product_v2(customer_msg_0,\n", " products_and_category)\n", "print(products_by_category_0)"]}, {"attachments": {}, "cell_type": "markdown", "id": "4440ce1f", "metadata": {"height": 30}, "source": ["## \u516b\u3001\u6536\u96c6\u5f00\u53d1\u96c6\u8fdb\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5"]}, {"attachments": {}, "cell_type": "markdown", "id": "2af63218", "metadata": {}, "source": ["\u5f53\u60a8\u8981\u8c03\u6574\u7684\u5f00\u53d1\u96c6\u4e0d\u4ec5\u4ec5\u662f\u4e00\u5c0f\u90e8\u5206\u793a\u4f8b\u65f6\uff0c\u5f00\u59cb\u81ea\u52a8\u5316\u6d4b\u8bd5\u8fc7\u7a0b\u5c31\u53d8\u5f97\u6709\u7528\u4e86\u3002"]}, {"cell_type": "code", "execution_count": 14, "id": "8a0b751f", "metadata": {"height": 207}, "outputs": [], "source": ["msg_ideal_pairs_set = [\n", " \n", " # eg 0\n", " {'customer_msg':\"\"\"Which TV can I buy if I'm on a budget?\"\"\",\n", " 'ideal_answer':{\n", " 'Televisions and Home Theater Systems':set(\n", " ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']\n", " )}\n", " },\n", "\n", " # eg 1\n", " {'customer_msg':\"\"\"I need a charger for my smartphone\"\"\",\n", " 'ideal_answer':{\n", " 'Smartphones and Accessories':set(\n", " ['MobiTech PowerCase', 'MobiTech Wireless Charger', 'SmartX EarBuds']\n", " )}\n", " },\n", " # eg 2\n", " {'customer_msg':f\"\"\"What computers do you have?\"\"\",\n", " 'ideal_answer':{\n", " 'Computers and Laptops':set(\n", " ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook'\n", " ])\n", " }\n", " },\n", "\n", " # eg 3\n", " {'customer_msg':f\"\"\"tell me about the smartx pro phone and \\\n", " the fotosnap camera, the dslr one.\\\n", " Also, what TVs do you have?\"\"\",\n", " 'ideal_answer':{\n", " 'Smartphones and Accessories':set(\n", " ['SmartX ProPhone']),\n", " 'Cameras and Camcorders':set(\n", " ['FotoSnap DSLR Camera']),\n", " 'Televisions and Home Theater Systems':set(\n", " ['CineView 4K TV', 'SoundMax Home Theater','CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV'])\n", " }\n", " }, \n", " \n", " # eg 4\n", " {'customer_msg':\"\"\"tell me about the CineView TV, the 8K one, Gamesphere console, the X one.\n", "I'm on a budget, what computers do you have?\"\"\",\n", " 'ideal_answer':{\n", " 'Televisions and Home Theater Systems':set(\n", " ['CineView 8K TV']),\n", " 'Gaming Consoles and Accessories':set(\n", " ['GameSphere X']),\n", " 'Computers and Laptops':set(\n", " ['TechPro Ultrabook', 'BlueWave Gaming Laptop', 'PowerLite Convertible', 'TechPro Desktop', 'BlueWave Chromebook'])\n", " }\n", " },\n", " \n", " # eg 5\n", " {'customer_msg':f\"\"\"What smartphones do you have?\"\"\",\n", " 'ideal_answer':{\n", " 'Smartphones and Accessories':set(\n", " ['SmartX ProPhone', 'MobiTech PowerCase', 'SmartX MiniPhone', 'MobiTech Wireless Charger', 'SmartX EarBuds'\n", " ])\n", " }\n", " },\n", " # eg 6\n", " {'customer_msg':f\"\"\"I'm on a budget. Can you recommend some smartphones to me?\"\"\",\n", " 'ideal_answer':{\n", " 'Smartphones and Accessories':set(\n", " ['SmartX EarBuds', 'SmartX MiniPhone', 'MobiTech PowerCase', 'SmartX ProPhone', 'MobiTech Wireless Charger']\n", " )}\n", " },\n", "\n", " # eg 7 # this will output a subset of the ideal answer\n", " {'customer_msg':f\"\"\"What Gaming consoles would be good for my friend who is into racing games?\"\"\",\n", " 'ideal_answer':{\n", " 'Gaming Consoles and Accessories':set([\n", " 'GameSphere X',\n", " 'ProGamer Controller',\n", " 'GameSphere Y',\n", " 'ProGamer Racing Wheel',\n", " 'GameSphere VR Headset'\n", " ])}\n", " },\n", " # eg 8\n", " {'customer_msg':f\"\"\"What could be a good present for my videographer friend?\"\"\",\n", " 'ideal_answer': {\n", " 'Cameras and Camcorders':set([\n", " 'FotoSnap DSLR Camera', 'ActionCam 4K', 'FotoSnap Mirrorless Camera', 'ZoomMaster Camcorder', 'FotoSnap Instant Camera'\n", " ])}\n", " },\n", " \n", " # eg 9\n", " {'customer_msg':f\"\"\"I would like a hot tub time machine.\"\"\",\n", " 'ideal_answer': []\n", " }\n", " \n", "]\n"]}, {"attachments": {}, "cell_type": "markdown", "id": "6e0f1db4", "metadata": {"height": 30}, "source": ["## \u4e5d\u3001\u901a\u8fc7\u4e0e\u7406\u60f3\u7b54\u6848\u6bd4\u8f83\u6765\u8bc4\u4f30\u6d4b\u8bd5\u7528\u4f8b"]}, {"cell_type": "code", "execution_count": 16, "id": "d9530285", "metadata": {}, "outputs": [], "source": ["import json\n", "def eval_response_with_ideal(response,\n", " ideal,\n", " debug=False):\n", " \"\"\"\n", " \u8bc4\u4f30\u56de\u590d\u662f\u5426\u4e0e\u7406\u60f3\u7b54\u6848\u5339\u914d\n", " \n", " \u53c2\u6570\uff1a\n", " response: \u56de\u590d\u7684\u5185\u5bb9\n", " ideal: \u7406\u60f3\u7684\u7b54\u6848\n", " debug: \u662f\u5426\u6253\u5370\u8c03\u8bd5\u4fe1\u606f\n", " \"\"\"\n", " if debug:\n", " print(\"\u56de\u590d\uff1a\")\n", " print(response)\n", " \n", " # json.loads() \u53ea\u80fd\u89e3\u6790\u53cc\u5f15\u53f7\uff0c\u56e0\u6b64\u6b64\u5904\u5c06\u5355\u5f15\u53f7\u66ff\u6362\u4e3a\u53cc\u5f15\u53f7\n", " json_like_str = response.replace(\"'\",'\"')\n", " \n", " # \u89e3\u6790\u4e3a\u4e00\u7cfb\u5217\u7684\u5b57\u5178\n", " l_of_d = json.loads(json_like_str)\n", " \n", " # \u5f53\u54cd\u5e94\u4e3a\u7a7a\uff0c\u5373\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u5546\u54c1\u65f6\n", " if l_of_d == [] and ideal == []:\n", " return 1\n", " \n", " # \u53e6\u5916\u4e00\u79cd\u5f02\u5e38\u60c5\u51b5\u662f\uff0c\u6807\u51c6\u7b54\u6848\u6570\u91cf\u4e0e\u56de\u590d\u7b54\u6848\u6570\u91cf\u4e0d\u5339\u914d\n", " elif l_of_d == [] or ideal == []:\n", " return 0\n", " \n", " # \u7edf\u8ba1\u6b63\u786e\u7b54\u6848\u6570\u91cf\n", " correct = 0 \n", " \n", " if debug:\n", " print(\"l_of_d is\")\n", " print(l_of_d)\n", "\n", " # \u5bf9\u6bcf\u4e00\u4e2a\u95ee\u7b54\u5bf9 \n", " for d in l_of_d:\n", "\n", " # \u83b7\u53d6\u4ea7\u54c1\u548c\u76ee\u5f55\n", " cat = d.get('category')\n", " prod_l = d.get('products')\n", " # \u6709\u83b7\u53d6\u5230\u4ea7\u54c1\u548c\u76ee\u5f55\n", " if cat and prod_l:\n", " # convert list to set for comparison\n", " prod_set = set(prod_l)\n", " # get ideal set of products\n", " ideal_cat = ideal.get(cat)\n", " if ideal_cat:\n", " prod_set_ideal = set(ideal.get(cat))\n", " else:\n", " if debug:\n", " print(f\"\u6ca1\u6709\u5728\u6807\u51c6\u7b54\u6848\u4e2d\u627e\u5230\u76ee\u5f55 {cat}\")\n", " print(f\"\u6807\u51c6\u7b54\u6848: {ideal}\")\n", " continue\n", " \n", " if debug:\n", " print(\"\u4ea7\u54c1\u96c6\u5408\uff1a\\n\",prod_set)\n", " print()\n", " print(\"\u6807\u51c6\u7b54\u6848\u7684\u4ea7\u54c1\u96c6\u5408\uff1a\\n\",prod_set_ideal)\n", "\n", " # \u67e5\u627e\u5230\u7684\u4ea7\u54c1\u96c6\u5408\u548c\u6807\u51c6\u7684\u4ea7\u54c1\u96c6\u5408\u4e00\u81f4\n", " if prod_set == prod_set_ideal:\n", " if debug:\n", " print(\"\u6b63\u786e\")\n", " correct +=1\n", " else:\n", " print(\"\u9519\u8bef\")\n", " print(f\"\u4ea7\u54c1\u96c6\u5408: {prod_set}\")\n", " print(f\"\u6807\u51c6\u7684\u4ea7\u54c1\u96c6\u5408: {prod_set_ideal}\")\n", " if prod_set <= prod_set_ideal:\n", " print(\"\u56de\u7b54\u662f\u6807\u51c6\u7b54\u6848\u7684\u4e00\u4e2a\u5b50\u96c6\")\n", " elif prod_set >= prod_set_ideal:\n", " print(\"\u56de\u7b54\u662f\u6807\u51c6\u7b54\u6848\u7684\u4e00\u4e2a\u8d85\u96c6\")\n", "\n", " # \u8ba1\u7b97\u6b63\u786e\u7b54\u6848\u6570\n", " pc_correct = correct / len(l_of_d)\n", " \n", " return pc_correct"]}, {"cell_type": "code", "execution_count": 16, "id": "e06d9fe3", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u7528\u6237\u63d0\u95ee: What Gaming consoles would be good for my friend who is into racing games?\n", "\u6807\u51c6\u7b54\u6848: {'Gaming Consoles and Accessories': {'GameSphere VR Headset', 'GameSphere X', 'ProGamer Controller', 'ProGamer Racing Wheel', 'GameSphere Y'}}\n"]}], "source": ["print(f'\u7528\u6237\u63d0\u95ee: {msg_ideal_pairs_set[7][\"customer_msg\"]}')\n", "print(f'\u6807\u51c6\u7b54\u6848: {msg_ideal_pairs_set[7][\"ideal_answer\"]}')"]}, {"cell_type": "code", "execution_count": 17, "id": "2ff332b4", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u56de\u7b54: [{'category': 'Gaming Consoles and Accessories', 'products': ['ProGamer Controller', 'ProGamer Racing Wheel', 'GameSphere VR Headset']}]\n", "\u9519\u8bef\n", "\u4ea7\u54c1\u96c6\u5408: {'ProGamer Racing Wheel', 'ProGamer Controller', 'GameSphere VR Headset'}\n", "\u6807\u51c6\u7684\u4ea7\u54c1\u96c6\u5408: {'GameSphere VR Headset', 'GameSphere X', 'ProGamer Racing Wheel', 'ProGamer Controller', 'GameSphere Y'}\n", "\u56de\u7b54\u662f\u6807\u51c6\u7b54\u6848\u7684\u4e00\u4e2a\u5b50\u96c6\n"]}, {"data": {"text/plain": ["0.0"]}, "execution_count": 17, "metadata": {}, "output_type": "execute_result"}], "source": ["response = find_category_and_product_v2(msg_ideal_pairs_set[7][\"customer_msg\"],\n", " products_and_category)\n", "print(f'\u56de\u7b54: {response}')\n", "\n", "eval_response_with_ideal(response,\n", " msg_ideal_pairs_set[7][\"ideal_answer\"])"]}, {"cell_type": "code", "execution_count": 17, "id": "bb7f5a2f", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u56de\u7b54: [{'category': 'Gaming Consoles and Accessories', 'products': ['GameSphere X', 'ProGamer Controller', 'GameSphere Y', 'ProGamer Racing Wheel', 'GameSphere VR Headset']}]\n"]}, {"data": {"text/plain": ["0.0"]}, "execution_count": 17, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u8c03\u7528\u4e2d\u6587 Prompt\n", "response = find_category_and_product_v2(msg_ideal_pairs_set[7][\"customer_msg\"],\n", " products_and_category)\n", "print(f'\u56de\u7b54: {response}')\n", "\n", "eval_response_with_ideal(response,\n", " msg_ideal_pairs_set[7][\"ideal_answer\"])"]}, {"attachments": {}, "cell_type": "markdown", "id": "d1313b17", "metadata": {"height": 30}, "source": ["## \u5341\u3001\u5728\u6240\u6709\u6d4b\u8bd5\u7528\u4f8b\u4e0a\u8fd0\u884c\u8bc4\u4f30\uff0c\u5e76\u8ba1\u7b97\u6b63\u786e\u7684\u7528\u4f8b\u6bd4\u4f8b\n", "\n", "\u6ce8\u610f\uff1a\u5982\u679c\u4efb\u4f55 API \u8c03\u7528\u8d85\u65f6\uff0c\u5c06\u65e0\u6cd5\u8fd0\u884c"]}, {"cell_type": "code", "execution_count": 20, "id": "d39407c0", "metadata": {"height": 30}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u793a\u4f8b 0\n", "0: 1.0\n", "\u793a\u4f8b 1\n", "1: 1.0\n", "\u793a\u4f8b 2\n", "2: 1.0\n", "\u793a\u4f8b 3\n", "3: 1.0\n", "\u793a\u4f8b 4\n", "4: 1.0\n", "\u793a\u4f8b 5\n", "5: 1.0\n", "\u793a\u4f8b 6\n", "6: 1.0\n", "\u793a\u4f8b 7\n", "\u9519\u8bef\n", "\u4ea7\u54c1\u96c6\u5408: {'ProGamer Racing Wheel', 'ProGamer Controller', 'GameSphere VR Headset'}\n", "\u6807\u51c6\u7684\u4ea7\u54c1\u96c6\u5408: {'GameSphere VR Headset', 'GameSphere X', 'ProGamer Racing Wheel', 'ProGamer Controller', 'GameSphere Y'}\n", "\u56de\u7b54\u662f\u6807\u51c6\u7b54\u6848\u7684\u4e00\u4e2a\u5b50\u96c6\n", "7: 0.0\n", "\u793a\u4f8b 8\n", "8: 1.0\n", "\u793a\u4f8b 9\n", "9: 1\n", "\u6b63\u786e\u6bd4\u4f8b\u4e3a 10: 0.9\n"]}], "source": ["score_accum = 0\n", "for i, pair in enumerate(msg_ideal_pairs_set):\n", " time.sleep(20)\n", " print(f\"\u793a\u4f8b {i}\")\n", " \n", " customer_msg = pair['customer_msg']\n", " ideal = pair['ideal_answer']\n", " \n", " # print(\"Customer message\",customer_msg)\n", " # print(\"ideal:\",ideal)\n", " response = find_category_and_product_v2(customer_msg,\n", " products_and_category)\n", "\n", " \n", " # print(\"products_by_category\",products_by_category)\n", " score = eval_response_with_ideal(response,ideal,debug=False)\n", " print(f\"{i}: {score}\")\n", " score_accum += score\n", " \n", "\n", "n_examples = len(msg_ideal_pairs_set)\n", "fraction_correct = score_accum / n_examples\n", "print(f\"\u6b63\u786e\u6bd4\u4f8b\u4e3a {n_examples}: {fraction_correct}\")"]}, {"attachments": {}, "cell_type": "markdown", "id": "5d885db6", "metadata": {}, "source": ["\u4f7f\u7528 Prompt \u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u4f5c\u6d41\u7a0b\u4e0e\u4f7f\u7528\u76d1\u7763\u5b66\u4e60\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u4f5c\u6d41\u7a0b\u975e\u5e38\u4e0d\u540c\u3002\n", "\n", "\u56e0\u6b64\uff0c\u6211\u4eec\u8ba4\u4e3a\u8fd9\u662f\u9700\u8981\u8bb0\u4f4f\u7684\u4e00\u4ef6\u597d\u4e8b\uff0c\u5f53\u60a8\u6b63\u5728\u6784\u5efa\u76d1\u7763\u5b66\u4e60\u6a21\u578b\u65f6\uff0c\u4f1a\u611f\u89c9\u5230\u8fed\u4ee3\u901f\u5ea6\u5feb\u4e86\u5f88\u591a\u3002\n", "\n", "\u5982\u679c\u60a8\u5e76\u672a\u4eb2\u8eab\u4f53\u9a8c\uff0c\u53ef\u80fd\u4f1a\u60ca\u53f9\u4e8e\u4ec5\u6709\u624b\u52a8\u6784\u5efa\u7684\u6781\u5c11\u6837\u672c\uff0c\u5c31\u53ef\u4ee5\u4ea7\u751f\u9ad8\u6548\u7684\u8bc4\u4f30\u65b9\u6cd5\u3002\u60a8\u53ef\u80fd\u4f1a\u8ba4\u4e3a\uff0c\u4ec5\u6709 10 \u4e2a\u6837\u672c\u662f\u4e0d\u5177\u5907\u7edf\u8ba1\u610f\u4e49\u7684\u3002\u4f46\u5f53\u60a8\u771f\u6b63\u8fd0\u7528\u8fd9\u79cd\u65b9\u5f0f\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u5bf9\u5411\u5f00\u53d1\u96c6\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u590d\u6742\u6837\u672c\u6240\u5e26\u6765\u7684\u6548\u679c\u63d0\u5347\u611f\u5230\u60ca\u8bb6\u3002\n", "\n", "\u8fd9\u5bf9\u4e8e\u5e2e\u52a9\u60a8\u548c\u60a8\u7684\u56e2\u961f\u627e\u5230\u6709\u6548\u7684 Prompt \u548c\u6709\u6548\u7684\u7cfb\u7edf\u975e\u5e38\u6709\u5e2e\u52a9\u3002\n", "\n", "\u5728\u672c\u7ae0\u4e2d\uff0c\u8f93\u51fa\u53ef\u4ee5\u88ab\u5b9a\u91cf\u8bc4\u4f30\uff0c\u5c31\u50cf\u6709\u4e00\u4e2a\u671f\u671b\u7684\u8f93\u51fa\u4e00\u6837\uff0c\u60a8\u53ef\u4ee5\u5224\u65ad\u5b83\u662f\u5426\u7ed9\u51fa\u4e86\u8fd9\u4e2a\u671f\u671b\u7684\u8f93\u51fa\u3002\n", "\n", "\u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u5728\u66f4\u52a0\u6a21\u7cca\u7684\u60c5\u51b5\u4e0b\u8bc4\u4f30\u6211\u4eec\u7684\u8f93\u51fa\u3002\u5373\u6b63\u786e\u7b54\u6848\u53ef\u80fd\u4e0d\u90a3\u4e48\u660e\u786e\u7684\u60c5\u51b5\u3002"]}], "metadata": {"kernelspec": {"display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.11"}}, "nbformat": 4, "nbformat_minor": 5} |