1 line
24 KiB
Plaintext
1 line
24 KiB
Plaintext
{"cells": [{"attachments": {}, "cell_type": "markdown", "id": "f99b8a44", "metadata": {}, "source": ["# \u7b2c\u4e03\u7ae0 \u68c0\u67e5\u7ed3\u679c\n", "\n", " - [\u4e00\u3001\u73af\u5883\u914d\u7f6e](#\u4e00\u3001\u73af\u5883\u914d\u7f6e)\n", " - [\u4e8c\u3001\u68c0\u67e5\u8f93\u51fa\u662f\u5426\u6709\u6f5c\u5728\u7684\u6709\u5bb3\u5185\u5bb9](#\u4e8c\u3001\u68c0\u67e5\u8f93\u51fa\u662f\u5426\u6709\u6f5c\u5728\u7684\u6709\u5bb3\u5185\u5bb9)\n", " - [\u4e09\u3001\u68c0\u67e5\u8f93\u51fa\u7ed3\u679c\u662f\u5426\u4e0e\u63d0\u4f9b\u7684\u4ea7\u54c1\u4fe1\u606f\u76f8\u7b26\u5408](#\u4e09\u3001\u68c0\u67e5\u8f93\u51fa\u7ed3\u679c\u662f\u5426\u4e0e\u63d0\u4f9b\u7684\u4ea7\u54c1\u4fe1\u606f\u76f8\u7b26\u5408)\n"]}, {"cell_type": "markdown", "id": "d8822242", "metadata": {}, "source": ["\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u5982\u4f55\u68c0\u67e5\u7cfb\u7edf\u751f\u6210\u7684\u8f93\u51fa\u3002\u5728\u5411\u7528\u6237\u5c55\u793a\u8f93\u51fa\u4e4b\u524d\uff0c\u68c0\u67e5\u8f93\u51fa\u7684\u8d28\u91cf\u3001\u76f8\u5173\u6027\u548c\u5b89\u5168\u6027\u5bf9\u4e8e\u786e\u4fdd\u63d0\u4f9b\u7684\u56de\u5e94\u975e\u5e38\u91cd\u8981\uff0c\u65e0\u8bba\u662f\u5728\u81ea\u52a8\u5316\u6d41\u7a0b\u4e2d\u8fd8\u662f\u5176\u4ed6\u573a\u666f\u4e2d\u3002\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u5ba1\u67e5 API \u6765\u8bc4\u4f30\u8f93\u51fa\uff0c\u5e76\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u989d\u5916\u7684 Prompt \u6765\u63d0\u5347\u6a21\u578b\u5728\u5c55\u793a\u8f93\u51fa\u4e4b\u524d\u7684\u8d28\u91cf\u8bc4\u4f30\u3002"]}, {"attachments": {}, "cell_type": "markdown", "id": "ca0fc5fc", "metadata": {}, "source": ["## \u4e00\u3001\u73af\u5883\u914d\u7f6e"]}, {"cell_type": "markdown", "id": "8e804bed", "metadata": {}, "source": ["\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": "5daec1c7", "metadata": {}, "outputs": [], "source": ["import openai\n", "# \u5bfc\u5165\u7b2c\u4e09\u65b9\u5e93\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": "9c40b32d", "metadata": {}, "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": "59f69c2e", "metadata": {}, "source": ["## \u4e8c\u3001\u68c0\u67e5\u8f93\u51fa\u662f\u5426\u6709\u6f5c\u5728\u7684\u6709\u5bb3\u5185\u5bb9\n", "\u4e3b\u8981\u5c31\u662f Moderation API \u7684\u4f7f\u7528"]}, {"cell_type": "code", "execution_count": null, "id": "943f5396", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["{\n", " \"categories\": {\n", " \"hate\": false,\n", " \"hate/threatening\": false,\n", " \"self-harm\": false,\n", " \"sexual\": false,\n", " \"sexual/minors\": false,\n", " \"violence\": false,\n", " \"violence/graphic\": false\n", " },\n", " \"category_scores\": {\n", " \"hate\": 2.6680607e-06,\n", " \"hate/threatening\": 1.2194433e-08,\n", " \"self-harm\": 8.294434e-07,\n", " \"sexual\": 3.41087e-05,\n", " \"sexual/minors\": 1.5462567e-07,\n", " \"violence\": 6.3285606e-06,\n", " \"violence/graphic\": 2.9102332e-06\n", " },\n", " \"flagged\": false\n", "}\n"]}], "source": ["final_response_to_customer = f\"\"\"\n", "The SmartX ProPhone has a 6.1-inch display, 128GB storage, \\\n", "12MP dual camera, and 5G. The FotoSnap DSLR Camera \\\n", "has a 24.2MP sensor, 1080p video, 3-inch LCD, and \\\n", "interchangeable lenses. We have a variety of TVs, including \\\n", "the CineView 4K TV with a 55-inch display, 4K resolution, \\\n", "HDR, and smart TV features. We also have the SoundMax \\\n", "Home Theater system with 5.1 channel, 1000W output, wireless \\\n", "subwoofer, and Bluetooth. Do you have any specific questions \\\n", "about these products or any other products we offer?\n", "\"\"\"\n", "# Moderation \u662f OpenAI \u7684\u5185\u5bb9\u5ba1\u6838\u51fd\u6570\uff0c\u7528\u4e8e\u68c0\u6d4b\u8fd9\u6bb5\u5185\u5bb9\u7684\u5371\u5bb3\u542b\u91cf\n", "\n", "response = openai.Moderation.create(\n", " input=final_response_to_customer\n", ")\n", "moderation_output = response[\"results\"][0]\n", "print(moderation_output)"]}, {"cell_type": "code", "execution_count": 4, "id": "943f5396", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["{\n", " \"categories\": {\n", " \"hate\": false,\n", " \"hate/threatening\": false,\n", " \"self-harm\": false,\n", " \"sexual\": false,\n", " \"sexual/minors\": false,\n", " \"violence\": false,\n", " \"violence/graphic\": false\n", " },\n", " \"category_scores\": {\n", " \"hate\": 2.6680607e-06,\n", " \"hate/threatening\": 1.2194433e-08,\n", " \"self-harm\": 8.294434e-07,\n", " \"sexual\": 3.41087e-05,\n", " \"sexual/minors\": 1.5462567e-07,\n", " \"violence\": 6.3285606e-06,\n", " \"violence/graphic\": 2.9102332e-06\n", " },\n", " \"flagged\": false\n", "}\n"]}], "source": ["final_response_to_customer = f\"\"\"\n", "SmartX ProPhone \u6709\u4e00\u4e2a 6.1 \u82f1\u5bf8\u7684\u663e\u793a\u5c4f\uff0c128GB \u5b58\u50a8\u3001\\\n", "1200 \u4e07\u50cf\u7d20\u7684\u53cc\u6444\u50cf\u5934\uff0c\u4ee5\u53ca 5G\u3002FotoSnap \u5355\u53cd\u76f8\u673a\\\n", "\u6709\u4e00\u4e2a 2420 \u4e07\u50cf\u7d20\u7684\u4f20\u611f\u5668\uff0c1080p \u89c6\u9891\uff0c3 \u82f1\u5bf8 LCD \u548c\\\n", "\u53ef\u66f4\u6362\u7684\u955c\u5934\u3002\u6211\u4eec\u6709\u5404\u79cd\u7535\u89c6\uff0c\u5305\u62ec CineView 4K \u7535\u89c6\uff0c\\\n", "55 \u82f1\u5bf8\u663e\u793a\u5c4f\uff0c4K \u5206\u8fa8\u7387\u3001HDR\uff0c\u4ee5\u53ca\u667a\u80fd\u7535\u89c6\u529f\u80fd\u3002\\\n", "\u6211\u4eec\u4e5f\u6709 SoundMax \u5bb6\u5ead\u5f71\u9662\u7cfb\u7edf\uff0c\u5177\u6709 5.1 \u58f0\u9053\uff0c\\\n", "1000W \u8f93\u51fa\uff0c\u65e0\u7ebf\u91cd\u4f4e\u97f3\u626c\u58f0\u5668\u548c\u84dd\u7259\u3002\u5173\u4e8e\u8fd9\u4e9b\u4ea7\u54c1\u6216\\\n", "\u6211\u4eec\u63d0\u4f9b\u7684\u4efb\u4f55\u5176\u4ed6\u4ea7\u54c1\u60a8\u662f\u5426\u6709\u4efb\u4f55\u5177\u4f53\u95ee\u9898\uff1f\n", "\"\"\"\n", "\n", "response = openai.Moderation.create(\n", " input=final_response_to_customer\n", ")\n", "moderation_output = response[\"results\"][0]\n", "print(moderation_output)"]}, {"cell_type": "markdown", "id": "b1f1399a", "metadata": {}, "source": ["\u6b63\u5982\u60a8\u6240\u89c1\uff0c\u8fd9\u4e2a\u8f93\u51fa\u6ca1\u6709\u88ab\u6807\u8bb0\uff0c\u5e76\u4e14\u5728\u6240\u6709\u7c7b\u522b\u4e2d\u90fd\u83b7\u5f97\u4e86\u975e\u5e38\u4f4e\u7684\u5206\u6570\uff0c\u8bf4\u660e\u7ed9\u5b9a\u7684\u56de\u5e94\u662f\u5408\u7406\u7684\u3002\n", "\n", "\u603b\u7684\u6765\u8bf4\uff0c\u68c0\u67e5\u8f93\u51fa\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6b63\u5728\u4e3a\u654f\u611f\u7684\u53d7\u4f17\u521b\u5efa\u4e00\u4e2a\u804a\u5929\u673a\u5668\u4eba\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u66f4\u4f4e\u7684\u9608\u503c\u6765\u6807\u8bb0\u8f93\u51fa\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u5ba1\u67e5\u8f93\u51fa\u8868\u660e\u5185\u5bb9\u88ab\u6807\u8bb0\uff0c\u60a8\u53ef\u4ee5\u91c7\u53d6\u9002\u5f53\u7684\u884c\u52a8\uff0c\u4f8b\u5982\u56de\u5e94\u4e00\u4e2a\u5907\u7528\u7b54\u6848\u6216\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u56de\u5e94\u3002\n", "\n", "\u8bf7\u6ce8\u610f\uff0c\u968f\u7740\u6211\u4eec\u6539\u8fdb\u6a21\u578b\uff0c\u5b83\u4eec\u4e5f\u8d8a\u6765\u8d8a\u4e0d\u592a\u53ef\u80fd\u8fd4\u56de\u4efb\u4f55\u6709\u5bb3\u7684\u8f93\u51fa\u3002\n", "\n", "\u53e6\u4e00\u79cd\u68c0\u67e5\u8f93\u51fa\u7684\u65b9\u6cd5\u662f\u8be2\u95ee\u6a21\u578b\u672c\u8eab\u751f\u6210\u7684\u7ed3\u679c\u662f\u5426\u4ee4\u4eba\u6ee1\u610f\uff0c\u662f\u5426\u7b26\u5408\u60a8\u6240\u5b9a\u4e49\u7684\u6807\u51c6\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5c06\u751f\u6210\u7684\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u7684\u4e00\u90e8\u5206\u63d0\u4f9b\u7ed9\u6a21\u578b\uff0c\u5e76\u8981\u6c42\u5b83\u8bc4\u4f30\u8f93\u51fa\u7684\u8d28\u91cf\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u4ee5\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u8fd9\u6837\u7684\u64cd\u4f5c\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002"]}, {"attachments": {}, "cell_type": "markdown", "id": "f57f8dad", "metadata": {}, "source": ["## \u4e09\u3001\u68c0\u67e5\u8f93\u51fa\u7ed3\u679c\u662f\u5426\u4e0e\u63d0\u4f9b\u7684\u4ea7\u54c1\u4fe1\u606f\u76f8\u7b26\u5408"]}, {"cell_type": "code", "execution_count": 7, "id": "552e3d8c", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Y\n"]}], "source": ["# \u8fd9\u662f\u4e00\u6bb5\u7535\u5b50\u4ea7\u54c1\u76f8\u5173\u7684\u4fe1\u606f\n", "system_message = f\"\"\"\n", "You are an assistant that evaluates whether \\\n", "customer service agent responses sufficiently \\\n", "answer customer questions, and also validates that \\\n", "all the facts the assistant cites from the product \\\n", "information are correct.\n", "The product information and user and customer \\\n", "service agent messages will be delimited by \\\n", "3 backticks, i.e. ```.\n", "Respond with a Y or N character, with no punctuation:\n", "Y - if the output sufficiently answers the question \\\n", "AND the response correctly uses product information\n", "N - otherwise\n", "\n", "Output a single letter only.\n", "\"\"\"\n", "\n", "#\u8fd9\u662f\u987e\u5ba2\u7684\u63d0\u95ee\n", "customer_message = f\"\"\"\n", "tell me about the smartx pro phone and \\\n", "the fotosnap camera, the dslr one. \\\n", "Also tell me about your tvs\"\"\"\n", "product_information = \"\"\"{ \"name\": \"SmartX ProPhone\", \"category\": \"Smartphones and Accessories\", \"brand\": \"SmartX\", \"model_number\": \"SX-PP10\", \"warranty\": \"1 year\", \"rating\": 4.6, \"features\": [ \"6.1-inch display\", \"128GB storage\", \"12MP dual camera\", \"5G\" ], \"description\": \"A powerful smartphone with advanced camera features.\", \"price\": 899.99 } { \"name\": \"FotoSnap DSLR Camera\", \"category\": \"Cameras and Camcorders\", \"brand\": \"FotoSnap\", \"model_number\": \"FS-DSLR200\", \"warranty\": \"1 year\", \"rating\": 4.7, \"features\": [ \"24.2MP sensor\", \"1080p video\", \"3-inch LCD\", \"Interchangeable lenses\" ], \"description\": \"Capture stunning photos and videos with this versatile DSLR camera.\", \"price\": 599.99 } { \"name\": \"CineView 4K TV\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"CineView\", \"model_number\": \"CV-4K55\", \"warranty\": \"2 years\", \"rating\": 4.8, \"features\": [ \"55-inch display\", \"4K resolution\", \"HDR\", \"Smart TV\" ], \"description\": \"A stunning 4K TV with vibrant colors and smart features.\", \"price\": 599.99 } { \"name\": \"SoundMax Home Theater\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"SoundMax\", \"model_number\": \"SM-HT100\", \"warranty\": \"1 year\", \"rating\": 4.4, \"features\": [ \"5.1 channel\", \"1000W output\", \"Wireless subwoofer\", \"Bluetooth\" ], \"description\": \"A powerful home theater system for an immersive audio experience.\", \"price\": 399.99 } { \"name\": \"CineView 8K TV\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"CineView\", \"model_number\": \"CV-8K65\", \"warranty\": \"2 years\", \"rating\": 4.9, \"features\": [ \"65-inch display\", \"8K resolution\", \"HDR\", \"Smart TV\" ], \"description\": \"Experience the future of television with this stunning 8K TV.\", \"price\": 2999.99 } { \"name\": \"SoundMax Soundbar\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"SoundMax\", \"model_number\": \"SM-SB50\", \"warranty\": \"1 year\", \"rating\": 4.3, \"features\": [ \"2.1 channel\", \"300W output\", \"Wireless subwoofer\", \"Bluetooth\" ], \"description\": \"Upgrade your TV's audio with this sleek and powerful soundbar.\", \"price\": 199.99 } { \"name\": \"CineView OLED TV\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"CineView\", \"model_number\": \"CV-OLED55\", \"warranty\": \"2 years\", \"rating\": 4.7, \"features\": [ \"55-inch display\", \"4K resolution\", \"HDR\", \"Smart TV\" ], \"description\": \"Experience true blacks and vibrant colors with this OLED TV.\", \"price\": 1499.99 }\"\"\"\n", "\n", "q_a_pair = f\"\"\"\n", "Customer message: ```{customer_message}```\n", "Product information: ```{product_information}```\n", "Agent response: ```{final_response_to_customer}```\n", "\n", "Does the response use the retrieved information correctly?\n", "Does the response sufficiently answer the question?\n", "\n", "Output Y or N\n", "\"\"\"\n", "#\u5224\u65ad\u76f8\u5173\u6027\n", "messages = [\n", " {'role': 'system', 'content': system_message},\n", " {'role': 'user', 'content': q_a_pair}\n", "]\n", "\n", "response = get_completion_from_messages(messages, max_tokens=1)\n", "print(response)"]}, {"cell_type": "code", "execution_count": null, "id": "552e3d8c", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Y\n"]}], "source": ["# \u8fd9\u662f\u4e00\u6bb5\u7535\u5b50\u4ea7\u54c1\u76f8\u5173\u7684\u4fe1\u606f\n", "system_message = f\"\"\"\n", "\u60a8\u662f\u4e00\u4e2a\u52a9\u7406\uff0c\u7528\u4e8e\u8bc4\u4f30\u5ba2\u670d\u4ee3\u7406\u7684\u56de\u590d\u662f\u5426\u5145\u5206\u56de\u7b54\u4e86\u5ba2\u6237\u95ee\u9898\uff0c\\\n", "\u5e76\u9a8c\u8bc1\u52a9\u7406\u4ece\u4ea7\u54c1\u4fe1\u606f\u4e2d\u5f15\u7528\u7684\u6240\u6709\u4e8b\u5b9e\u662f\u5426\u6b63\u786e\u3002 \n", "\u4ea7\u54c1\u4fe1\u606f\u3001\u7528\u6237\u548c\u5ba2\u670d\u4ee3\u7406\u7684\u4fe1\u606f\u5c06\u4f7f\u7528\u4e09\u4e2a\u53cd\u5f15\u53f7\uff08\u5373 ```)\\\n", "\u8fdb\u884c\u5206\u9694\u3002 \n", "\u8bf7\u4ee5 Y \u6216 N \u7684\u5b57\u7b26\u5f62\u5f0f\u8fdb\u884c\u56de\u590d\uff0c\u4e0d\u8981\u5305\u542b\u6807\u70b9\u7b26\u53f7\uff1a\\\n", "Y - \u5982\u679c\u8f93\u51fa\u5145\u5206\u56de\u7b54\u4e86\u95ee\u9898\u5e76\u4e14\u56de\u590d\u6b63\u786e\u5730\u4f7f\u7528\u4e86\u4ea7\u54c1\u4fe1\u606f\\\n", "N - \u5176\u4ed6\u60c5\u51b5\u3002\n", "\n", "\u4ec5\u8f93\u51fa\u5355\u4e2a\u5b57\u6bcd\u3002\n", "\"\"\"\n", "\n", "#\u8fd9\u662f\u987e\u5ba2\u7684\u63d0\u95ee\n", "customer_message = f\"\"\"\n", "\u544a\u8bc9\u6211\u6709\u5173 smartx pro \u624b\u673a\\\n", "\u548c fotosnap \u76f8\u673a\uff08\u5355\u53cd\u76f8\u673a\uff09\u7684\u4fe1\u606f\u3002\\\n", "\u8fd8\u6709\u60a8\u7535\u89c6\u7684\u4fe1\u606f\u3002\n", "\"\"\"\n", "product_information = \"\"\"{ \"name\": \"SmartX ProPhone\", \"category\": \"Smartphones and Accessories\", \"brand\": \"SmartX\", \"model_number\": \"SX-PP10\", \"warranty\": \"1 year\", \"rating\": 4.6, \"features\": [ \"6.1-inch display\", \"128GB storage\", \"12MP dual camera\", \"5G\" ], \"description\": \"A powerful smartphone with advanced camera features.\", \"price\": 899.99 } { \"name\": \"FotoSnap DSLR Camera\", \"category\": \"Cameras and Camcorders\", \"brand\": \"FotoSnap\", \"model_number\": \"FS-DSLR200\", \"warranty\": \"1 year\", \"rating\": 4.7, \"features\": [ \"24.2MP sensor\", \"1080p video\", \"3-inch LCD\", \"Interchangeable lenses\" ], \"description\": \"Capture stunning photos and videos with this versatile DSLR camera.\", \"price\": 599.99 } { \"name\": \"CineView 4K TV\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"CineView\", \"model_number\": \"CV-4K55\", \"warranty\": \"2 years\", \"rating\": 4.8, \"features\": [ \"55-inch display\", \"4K resolution\", \"HDR\", \"Smart TV\" ], \"description\": \"A stunning 4K TV with vibrant colors and smart features.\", \"price\": 599.99 } { \"name\": \"SoundMax Home Theater\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"SoundMax\", \"model_number\": \"SM-HT100\", \"warranty\": \"1 year\", \"rating\": 4.4, \"features\": [ \"5.1 channel\", \"1000W output\", \"Wireless subwoofer\", \"Bluetooth\" ], \"description\": \"A powerful home theater system for an immersive audio experience.\", \"price\": 399.99 } { \"name\": \"CineView 8K TV\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"CineView\", \"model_number\": \"CV-8K65\", \"warranty\": \"2 years\", \"rating\": 4.9, \"features\": [ \"65-inch display\", \"8K resolution\", \"HDR\", \"Smart TV\" ], \"description\": \"Experience the future of television with this stunning 8K TV.\", \"price\": 2999.99 } { \"name\": \"SoundMax Soundbar\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"SoundMax\", \"model_number\": \"SM-SB50\", \"warranty\": \"1 year\", \"rating\": 4.3, \"features\": [ \"2.1 channel\", \"300W output\", \"Wireless subwoofer\", \"Bluetooth\" ], \"description\": \"Upgrade your TV's audio with this sleek and powerful soundbar.\", \"price\": 199.99 } { \"name\": \"CineView OLED TV\", \"category\": \"Televisions and Home Theater Systems\", \"brand\": \"CineView\", \"model_number\": \"CV-OLED55\", \"warranty\": \"2 years\", \"rating\": 4.7, \"features\": [ \"55-inch display\", \"4K resolution\", \"HDR\", \"Smart TV\" ], \"description\": \"Experience true blacks and vibrant colors with this OLED TV.\", \"price\": 1499.99 }\"\"\"\n", "\n", "q_a_pair = f\"\"\"\n", "\u987e\u5ba2\u7684\u4fe1\u606f: ```{customer_message}```\n", "\u4ea7\u54c1\u4fe1\u606f: ```{product_information}```\n", "\u4ee3\u7406\u7684\u56de\u590d: ```{final_response_to_customer}```\n", "\n", "\u56de\u590d\u662f\u5426\u6b63\u786e\u4f7f\u7528\u4e86\u68c0\u7d22\u7684\u4fe1\u606f\uff1f\n", "\u56de\u590d\u662f\u5426\u5145\u5206\u5730\u56de\u7b54\u4e86\u95ee\u9898\uff1f\n", "\n", "\u8f93\u51fa Y \u6216 N\n", "\"\"\"\n", "#\u5224\u65ad\u76f8\u5173\u6027\n", "messages = [\n", " {'role': 'system', 'content': system_message},\n", " {'role': 'user', 'content': q_a_pair}\n", "]\n", "\n", "response = get_completion_from_messages(messages, max_tokens=1)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 6, "id": "afb1b82f", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["N\n"]}], "source": ["another_response = \"life is like a box of chocolates\"\n", "q_a_pair = f\"\"\"\n", "Customer message: ```{customer_message}```\n", "Product information: ```{product_information}```\n", "Agent response: ```{another_response}```\n", "\n", "Does the response use the retrieved information correctly?\n", "Does the response sufficiently answer the question?\n", "\n", "Output Y or N\n", "\"\"\"\n", "messages = [\n", " {'role': 'system', 'content': system_message},\n", " {'role': 'user', 'content': q_a_pair}\n", "]\n", "\n", "response = get_completion_from_messages(messages)\n", "print(response)"]}, {"cell_type": "code", "execution_count": null, "id": "afb1b82f", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["N\n"]}], "source": ["another_response = \"\u751f\u6d3b\u5c31\u50cf\u4e00\u76d2\u5de7\u514b\u529b\"\n", "q_a_pair = f\"\"\"\n", "\u987e\u5ba2\u7684\u4fe1\u606f: ```{customer_message}```\n", "\u4ea7\u54c1\u4fe1\u606f: ```{product_information}```\n", "\u4ee3\u7406\u7684\u56de\u590d: ```{final_response_to_customer}```\n", "\n", "\u56de\u590d\u662f\u5426\u6b63\u786e\u4f7f\u7528\u4e86\u68c0\u7d22\u7684\u4fe1\u606f\uff1f\n", "\u56de\u590d\u662f\u5426\u5145\u5206\u5730\u56de\u7b54\u4e86\u95ee\u9898\uff1f\n", "\n", "\u8f93\u51fa Y \u6216 N\n", "\"\"\"\n", "messages = [\n", " {'role': 'system', 'content': system_message},\n", " {'role': 'user', 'content': q_a_pair}\n", "]\n", "\n", "response = get_completion_from_messages(messages)\n", "print(response)"]}, {"cell_type": "markdown", "id": "51dd8979", "metadata": {}, "source": ["\u56e0\u6b64\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\uff0c\u6a21\u578b\u80fd\u591f\u63d0\u4f9b\u5173\u4e8e\u751f\u6210\u8f93\u51fa\u8d28\u91cf\u7684\u53cd\u9988\u3002\u60a8\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u53cd\u9988\u6765\u51b3\u5b9a\u662f\u5426\u5c55\u793a\u8f93\u51fa\u7ed9\u7528\u6237\u6216\u751f\u6210\u65b0\u7684\u56de\u5e94\u3002\u751a\u81f3\u53ef\u4ee5\u5c1d\u8bd5\u4e3a\u6bcf\u4e2a\u7528\u6237\u67e5\u8be2\u751f\u6210\u591a\u4e2a\u6a21\u578b\u56de\u5e94\uff0c\u7136\u540e\u9009\u62e9\u6700\u4f73\u7684\u56de\u5e94\u5c55\u793a\u7ed9\u7528\u6237\u3002\u56e0\u6b64\uff0c\u60a8\u6709\u591a\u79cd\u5c1d\u8bd5\u7684\u65b9\u5f0f\u3002\n", "\n", "\u603b\u7684\u6765\u8bf4\uff0c\u4f7f\u7528\u5ba1\u67e5 API \u6765\u68c0\u67e5\u8f93\u51fa\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u505a\u6cd5\u3002\u4f46\u662f\uff0c\u6211\u8ba4\u4e3a\u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u8fd9\u53ef\u80fd\u662f\u4e0d\u5fc5\u8981\u7684\uff0c\u5c24\u5176\u662f\u5f53\u60a8\u4f7f\u7528\u66f4\u5148\u8fdb\u7684\u6a21\u578b\uff0c\u4f8b\u5982 GPT-4 \u65f6\u3002\n", "\n", "\u4e8b\u5b9e\u4e0a\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u770b\u5230\u5f88\u591a\u4eba\u5728\u5b9e\u9645\u751f\u4ea7\u73af\u5883\u4e2d\u91c7\u53d6\u8fd9\u79cd\u505a\u6cd5\u3002\u8fd9\u4e5f\u4f1a\u589e\u52a0\u7cfb\u7edf\u7684\u5ef6\u8fdf\u548c\u6210\u672c\uff0c\u56e0\u4e3a\u60a8\u5fc5\u987b\u7b49\u5f85\u989d\u5916\u7684\u8c03\u7528\uff0c\u8fd8\u9700\u8981\u989d\u5916\u7684 tokens\u3002\u5982\u679c\u60a8\u7684\u5e94\u7528\u6216\u4ea7\u54c1\u7684\u9519\u8bef\u7387\u53ea\u6709 0.0000001%\uff0c\u90a3\u4e48\u6216\u8bb8\u60a8\u53ef\u4ee5\u5c1d\u8bd5\u8fd9\u79cd\u65b9\u6cd5\u3002\u4f46\u603b\u7684\u6765\u8bf4\uff0c\u6211\u4eec\u4e0d\u5efa\u8bae\u60a8\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f\u3002\n", "\n", "\u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u6211\u4eec\u5728\u8bc4\u4f30\u8f93\u5165\u90e8\u5206\u3001\u5904\u7406\u90e8\u5206\u548c\u68c0\u67e5\u8f93\u51fa\u4e2d\u5b66\u5230\u7684\u6240\u6709\u5185\u5bb9\u7ed3\u5408\u8d77\u6765\uff0c\u6784\u5efa\u4e00\u4e2a\u7aef\u5230\u7aef\u7684\u7cfb\u7edf\u3002\n", "\n"]}], "metadata": {"kernelspec": {"display_name": "Python 3.9.6 64-bit", "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.9.6"}, "vscode": {"interpreter": {"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"}}}, "nbformat": 4, "nbformat_minor": 5} |