Files
prompt-engineering-for-deve…/content/Building Systems with the ChatGPT API/5.处理输入-思维链推理 Chain of Thought Reasoning.ipynb
nowadays0421 c153d2cc35 rename
2023-07-16 13:46:03 +08:00

1 line
21 KiB
Plaintext

{"cells": [{"attachments": {}, "cell_type": "markdown", "metadata": {}, "source": ["# \u7b2c\u4e94\u7ae0 \u5904\u7406\u8f93\u5165\uff1a\u601d\u7ef4\u94fe\u63a8\u7406\n", "\n", " - [\u4e00\u3001\u73af\u5883\u914d\u7f6e](#\u4e00\u3001\u73af\u5883\u914d\u7f6e)\n", " - [\u4e8c\u3001\u601d\u7ef4\u94fe Prompt](#\u4e8c\u3001\u601d\u7ef4\u94fe-Prompt)\n", " - [\u4e09\u3001\u5185\u5fc3\u72ec\u767d\uff08Inner monologue\uff09](#\u4e09\u3001\u5185\u5fc3\u72ec\u767d\uff08Inner-monologue\uff09)\n"]}, {"attachments": {}, "cell_type": "markdown", "metadata": {}, "source": ["\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4e13\u6ce8\u4e8e\u5904\u7406\u8f93\u5165\uff0c\u5373\u901a\u8fc7\u4e00\u7cfb\u5217\u6b65\u9aa4\u751f\u6210\u6709\u7528\u7684\u8f93\u51fa\u3002\n", "\n", "\u6709\u65f6\uff0c\u6a21\u578b\u5728\u56de\u7b54\u7279\u5b9a\u95ee\u9898\u4e4b\u524d\u9700\u8981\u8fdb\u884c\u8be6\u7ec6\u5730\u63a8\u7406\u3002\u5982\u679c\u60a8\u53c2\u52a0\u8fc7\u6211\u4eec\u4e4b\u524d\u7684\u8bfe\u7a0b\uff0c\u60a8\u5c06\u770b\u5230\u8bb8\u591a\u8fd9\u6837\u7684\u4f8b\u5b50\u3002\u6709\u65f6\uff0c\u6a21\u578b\u53ef\u80fd\u4f1a\u56e0\u4e3a\u8fc7\u4e8e\u5306\u5fd9\u5f97\u51fa\u7ed3\u8bba\u800c\u5728\u63a8\u7406\u8fc7\u7a0b\u4e2d\u51fa\u9519\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u91cd\u65b0\u6784\u601d\u67e5\u8be2\uff0c\u8981\u6c42\u6a21\u578b\u5728\u7ed9\u51fa\u6700\u7ec8\u7b54\u6848\u4e4b\u524d\u63d0\u4f9b\u4e00\u7cfb\u5217\u76f8\u5173\u7684\u63a8\u7406\u6b65\u9aa4\uff0c\u8fd9\u6837\u5b83\u5c31\u53ef\u4ee5\u66f4\u957f\u65f6\u95f4\u3001\u66f4\u6df1\u5165\u5730\u601d\u8003\u95ee\u9898\u3002\n", "\n", "\u901a\u5e38\uff0c\u6211\u4eec\u79f0\u8fd9\u79cd\u8981\u6c42\u6a21\u578b\u9010\u6b65\u63a8\u7406\u95ee\u9898\u7684\u7b56\u7565\u4e3a\u601d\u7ef4\u94fe\u63a8\u7406\uff08chain of thought reasoning\uff09\u3002"]}, {"attachments": {}, "cell_type": "markdown", "metadata": {}, "source": ["## \u4e00\u3001\u73af\u5883\u914d\u7f6e\n"]}, {"cell_type": "markdown", "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, "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, "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", "metadata": {}, "source": ["## \u4e8c\u3001\u601d\u7ef4\u94fe Prompt"]}, {"attachments": {}, "cell_type": "markdown", "metadata": {}, "source": ["\u6211\u4eec\u5728\u8fd9\u91cc\u8981\u6c42\u6a21\u578b\u5728\u5f97\u51fa\u7ed3\u8bba\u4e4b\u524d\u4e00\u6b65\u4e00\u6b65\u63a8\u7406\u7b54\u6848\u3002\n"]}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": ["delimiter = \"####\"\n", "system_message = f\"\"\"\n", "Follow these steps to answer the customer queries.\n", "The customer query will be delimited with four hashtags,\\\n", "i.e. {delimiter}. \n", "\n", "Step 1:{delimiter} First decide whether the user is \\\n", "asking a question about a specific product or products. \\\n", "Product cateogry doesn't count. \n", "\n", "Step 2:{delimiter} If the user is asking about \\\n", "specific products, identify whether \\\n", "the products are in the following list.\n", "All available products: \n", "1. Product: TechPro Ultrabook\n", " Category: Computers and Laptops\n", " Brand: TechPro\n", " Model Number: TP-UB100\n", " Warranty: 1 year\n", " Rating: 4.5\n", " Features: 13.3-inch display, 8GB RAM, 256GB SSD, Intel Core i5 processor\n", " Description: A sleek and lightweight ultrabook for everyday use.\n", " Price: $799.99\n", "\n", "2. Product: BlueWave Gaming Laptop\n", " Category: Computers and Laptops\n", " Brand: BlueWave\n", " Model Number: BW-GL200\n", " Warranty: 2 years\n", " Rating: 4.7\n", " Features: 15.6-inch display, 16GB RAM, 512GB SSD, NVIDIA GeForce RTX 3060\n", " Description: A high-performance gaming laptop for an immersive experience.\n", " Price: $1199.99\n", "\n", "3. Product: PowerLite Convertible\n", " Category: Computers and Laptops\n", " Brand: PowerLite\n", " Model Number: PL-CV300\n", " Warranty: 1 year\n", " Rating: 4.3\n", " Features: 14-inch touchscreen, 8GB RAM, 256GB SSD, 360-degree hinge\n", " Description: A versatile convertible laptop with a responsive touchscreen.\n", " Price: $699.99\n", "\n", "4. Product: TechPro Desktop\n", " Category: Computers and Laptops\n", " Brand: TechPro\n", " Model Number: TP-DT500\n", " Warranty: 1 year\n", " Rating: 4.4\n", " Features: Intel Core i7 processor, 16GB RAM, 1TB HDD, NVIDIA GeForce GTX 1660\n", " Description: A powerful desktop computer for work and play.\n", " Price: $999.99\n", "\n", "5. Product: BlueWave Chromebook\n", " Category: Computers and Laptops\n", " Brand: BlueWave\n", " Model Number: BW-CB100\n", " Warranty: 1 year\n", " Rating: 4.1\n", " Features: 11.6-inch display, 4GB RAM, 32GB eMMC, Chrome OS\n", " Description: A compact and affordable Chromebook for everyday tasks.\n", " Price: $249.99\n", "\n", "Step 3:{delimiter} If the message contains products \\\n", "in the list above, list any assumptions that the \\\n", "user is making in their \\\n", "message e.g. that Laptop X is bigger than \\\n", "Laptop Y, or that Laptop Z has a 2 year warranty.\n", "\n", "Step 4:{delimiter}: If the user made any assumptions, \\\n", "figure out whether the assumption is true based on your \\\n", "product information. \n", "\n", "Step 5:{delimiter}: First, politely correct the \\\n", "customer's incorrect assumptions if applicable. \\\n", "Only mention or reference products in the list of \\\n", "5 available products, as these are the only 5 \\\n", "products that the store sells. \\\n", "Answer the customer in a friendly tone.\n", "\n", "Use the following format:\n", "Step 1:{delimiter} <step 1 reasoning>\n", "Step 2:{delimiter} <step 2 reasoning>\n", "Step 3:{delimiter} <step 3 reasoning>\n", "Step 4:{delimiter} <step 4 reasoning>\n", "Response to user:{delimiter} <response to customer>\n", "\n", "Make sure to include {delimiter} to separate every step.\n", "\"\"\""]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["delimiter = \"####\"\n", "system_message = f\"\"\"\n", "\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u56de\u7b54\u5ba2\u6237\u7684\u67e5\u8be2\u3002\u5ba2\u6237\u7684\u67e5\u8be2\u5c06\u4ee5\u56db\u4e2a\u4e95\u53f7\uff08#\uff09\u5206\u9694\uff0c\u5373 {delimiter}\u3002\n", "\n", "\u6b65\u9aa4 1:{delimiter} \u9996\u5148\u786e\u5b9a\u7528\u6237\u662f\u5426\u6b63\u5728\u8be2\u95ee\u6709\u5173\u7279\u5b9a\u4ea7\u54c1\u6216\u4ea7\u54c1\u7684\u95ee\u9898\u3002\u4ea7\u54c1\u7c7b\u522b\u4e0d\u8ba1\u5165\u8303\u56f4\u3002\n", "\n", "\u6b65\u9aa4 2:{delimiter} \u5982\u679c\u7528\u6237\u8be2\u95ee\u7279\u5b9a\u4ea7\u54c1\uff0c\u8bf7\u786e\u8ba4\u4ea7\u54c1\u662f\u5426\u5728\u4ee5\u4e0b\u5217\u8868\u4e2d\u3002\u6240\u6709\u53ef\u7528\u4ea7\u54c1\uff1a\n", "\n", "\u4ea7\u54c1\uff1aTechPro \u8d85\u6781\u672c\n", "\u7c7b\u522b\uff1a\u8ba1\u7b97\u673a\u548c\u7b14\u8bb0\u672c\u7535\u8111\n", "\u54c1\u724c\uff1aTechPro\n", "\u578b\u53f7\uff1aTP-UB100\n", "\u4fdd\u4fee\u671f\uff1a1 \u5e74\n", "\u8bc4\u5206\uff1a4.5\n", "\u7279\u70b9\uff1a13.3 \u82f1\u5bf8\u663e\u793a\u5c4f\uff0c8GB RAM\uff0c256GB SSD\uff0cIntel Core i5 \u5904\u7406\u5668\n", "\u63cf\u8ff0\uff1a\u4e00\u6b3e\u9002\u7528\u4e8e\u65e5\u5e38\u4f7f\u7528\u7684\u65f6\u5c1a\u8f7b\u4fbf\u7684\u8d85\u6781\u672c\u3002\n", "\u4ef7\u683c\uff1a$799.99\n", "\n", "\u4ea7\u54c1\uff1aBlueWave \u6e38\u620f\u7b14\u8bb0\u672c\u7535\u8111\n", "\u7c7b\u522b\uff1a\u8ba1\u7b97\u673a\u548c\u7b14\u8bb0\u672c\u7535\u8111\n", "\u54c1\u724c\uff1aBlueWave\n", "\u578b\u53f7\uff1aBW-GL200\n", "\u4fdd\u4fee\u671f\uff1a2 \u5e74\n", "\u8bc4\u5206\uff1a4.7\n", "\u7279\u70b9\uff1a15.6 \u82f1\u5bf8\u663e\u793a\u5c4f\uff0c16GB RAM\uff0c512GB SSD\uff0cNVIDIA GeForce RTX 3060\n", "\u63cf\u8ff0\uff1a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684\u6e38\u620f\u7b14\u8bb0\u672c\u7535\u8111\uff0c\u63d0\u4f9b\u6c89\u6d78\u5f0f\u4f53\u9a8c\u3002\n", "\u4ef7\u683c\uff1a$1199.99\n", "\n", "\u4ea7\u54c1\uff1aPowerLite \u53ef\u8f6c\u6362\u7b14\u8bb0\u672c\u7535\u8111\n", "\u7c7b\u522b\uff1a\u8ba1\u7b97\u673a\u548c\u7b14\u8bb0\u672c\u7535\u8111\n", "\u54c1\u724c\uff1aPowerLite\n", "\u578b\u53f7\uff1aPL-CV300\n", "\u4fdd\u4fee\u671f\uff1a1\u5e74\n", "\u8bc4\u5206\uff1a4.3\n", "\u7279\u70b9\uff1a14 \u82f1\u5bf8\u89e6\u6478\u5c4f\uff0c8GB RAM\uff0c256GB SSD\uff0c360 \u5ea6\u94f0\u94fe\n", "\u63cf\u8ff0\uff1a\u4e00\u6b3e\u591a\u529f\u80fd\u53ef\u8f6c\u6362\u7b14\u8bb0\u672c\u7535\u8111\uff0c\u5177\u6709\u54cd\u5e94\u89e6\u6478\u5c4f\u3002\n", "\u4ef7\u683c\uff1a$699.99\n", "\n", "\u4ea7\u54c1\uff1aTechPro \u53f0\u5f0f\u7535\u8111\n", "\u7c7b\u522b\uff1a\u8ba1\u7b97\u673a\u548c\u7b14\u8bb0\u672c\u7535\u8111\n", "\u54c1\u724c\uff1aTechPro\n", "\u578b\u53f7\uff1aTP-DT500\n", "\u4fdd\u4fee\u671f\uff1a1\u5e74\n", "\u8bc4\u5206\uff1a4.4\n", "\u7279\u70b9\uff1aIntel Core i7 \u5904\u7406\u5668\uff0c16GB RAM\uff0c1TB HDD\uff0cNVIDIA GeForce GTX 1660\n", "\u63cf\u8ff0\uff1a\u4e00\u6b3e\u529f\u80fd\u5f3a\u5927\u7684\u53f0\u5f0f\u7535\u8111\uff0c\u9002\u7528\u4e8e\u5de5\u4f5c\u548c\u5a31\u4e50\u3002\n", "\u4ef7\u683c\uff1a$999.99\n", "\n", "\u4ea7\u54c1\uff1aBlueWave Chromebook\n", "\u7c7b\u522b\uff1a\u8ba1\u7b97\u673a\u548c\u7b14\u8bb0\u672c\u7535\u8111\n", "\u54c1\u724c\uff1aBlueWave\n", "\u578b\u53f7\uff1aBW-CB100\n", "\u4fdd\u4fee\u671f\uff1a1 \u5e74\n", "\u8bc4\u5206\uff1a4.1\n", "\u7279\u70b9\uff1a11.6 \u82f1\u5bf8\u663e\u793a\u5c4f\uff0c4GB RAM\uff0c32GB eMMC\uff0cChrome OS\n", "\u63cf\u8ff0\uff1a\u4e00\u6b3e\u7d27\u51d1\u800c\u4ef7\u683c\u5b9e\u60e0\u7684 Chromebook\uff0c\u9002\u7528\u4e8e\u65e5\u5e38\u4efb\u52a1\u3002\n", "\u4ef7\u683c\uff1a$249.99\n", "\n", "\u6b65\u9aa4 3:{delimiter} \u5982\u679c\u6d88\u606f\u4e2d\u5305\u542b\u4e0a\u8ff0\u5217\u8868\u4e2d\u7684\u4ea7\u54c1\uff0c\u8bf7\u5217\u51fa\u7528\u6237\u5728\u6d88\u606f\u4e2d\u505a\u51fa\u7684\u4efb\u4f55\u5047\u8bbe\uff0c\u4f8b\u5982\u7b14\u8bb0\u672c\u7535\u8111 X \u6bd4\u7b14\u8bb0\u672c\u7535\u8111 Y \u5927\uff0c\u6216\u8005\u7b14\u8bb0\u672c\u7535\u8111 Z \u6709 2 \u5e74\u4fdd\u4fee\u671f\u3002\n", "\n", "\u6b65\u9aa4 4:{delimiter} \u5982\u679c\u7528\u6237\u505a\u51fa\u4e86\u4efb\u4f55\u5047\u8bbe\uff0c\u8bf7\u6839\u636e\u4ea7\u54c1\u4fe1\u606f\u786e\u5b9a\u5047\u8bbe\u662f\u5426\u6b63\u786e\u3002\n", "\n", "\u6b65\u9aa4 5:{delimiter} \u5982\u679c\u7528\u6237\u6709\u4efb\u4f55\u9519\u8bef\u7684\u5047\u8bbe\uff0c\u8bf7\u5148\u793c\u8c8c\u5730\u7ea0\u6b63\u5ba2\u6237\u7684\u9519\u8bef\u5047\u8bbe\uff08\u5982\u679c\u9002\u7528\uff09\u3002\u53ea\u63d0\u53ca\u6216\u5f15\u7528\u53ef\u7528\u4ea7\u54c1\u5217\u8868\u4e2d\u7684\u4ea7\u54c1\uff0c\u56e0\u4e3a\u8fd9\u662f\u5546\u5e97\u9500\u552e\u7684\u552f\u4e00\u4e94\u6b3e\u4ea7\u54c1\u3002\u4ee5\u53cb\u597d\u7684\u53e3\u543b\u56de\u7b54\u5ba2\u6237\u3002\n", "\n", "\u4f7f\u7528\u4ee5\u4e0b\u683c\u5f0f\u56de\u7b54\u95ee\u9898\uff1a\n", "\u6b65\u9aa4 1:{delimiter} <\u6b65\u9aa4 1\u7684\u63a8\u7406>\n", "\u6b65\u9aa4 2:{delimiter} <\u6b65\u9aa4 2 \u7684\u63a8\u7406>\n", "\u6b65\u9aa4 3:{delimiter} <\u6b65\u9aa4 3 \u7684\u63a8\u7406>\n", "\u6b65\u9aa4 4:{delimiter} <\u6b65\u9aa4 4 \u7684\u63a8\u7406>\n", "\u56de\u590d\u5ba2\u6237:{delimiter} <\u56de\u590d\u5ba2\u6237\u7684\u5185\u5bb9>\n", "\n", "\u8bf7\u786e\u4fdd\u5728\u6bcf\u4e2a\u6b65\u9aa4\u4e4b\u95f4\u4f7f\u7528 {delimiter} \u8fdb\u884c\u5206\u9694\u3002\n", "\"\"\""]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Step 1:#### The user is asking a question about two specific products, the BlueWave Chromebook and the TechPro Desktop.\n", "Step 2:#### The prices of the two products are as follows:\n", "- BlueWave Chromebook: $249.99\n", "- TechPro Desktop: $999.99\n", "Step 3:#### The user is assuming that the BlueWave Chromebook is more expensive than the TechPro Desktop.\n", "Step 4:#### The assumption is incorrect. The TechPro Desktop is actually more expensive than the BlueWave Chromebook.\n", "Response to user:#### The BlueWave Chromebook is actually less expensive than the TechPro Desktop. The BlueWave Chromebook costs $249.99 while the TechPro Desktop costs $999.99.\n"]}], "source": ["user_message = f\"\"\"\n", "by how much is the BlueWave Chromebook more expensive \\\n", "than the TechPro Desktop\"\"\"\n", "\n", "messages = [ \n", "{'role':'system', \n", " 'content': system_message}, \n", "{'role':'user', \n", " 'content': f\"{delimiter}{user_message}{delimiter}\"}, \n", "] \n", "\n", "response = get_completion_from_messages(messages)\n", "print(response)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u6b65\u9aa4 1:#### \u786e\u8ba4\u7528\u6237\u6b63\u5728\u8be2\u95ee\u6709\u5173\u7279\u5b9a\u4ea7\u54c1\u7684\u95ee\u9898\u3002\n", "\n", "\u6b65\u9aa4 2:#### \u7528\u6237\u8be2\u95ee BlueWave Chromebook \u548c TechPro \u53f0\u5f0f\u7535\u8111\u4e4b\u95f4\u7684\u4ef7\u683c\u5dee\u5f02\u3002\n", "\n", "\u6b65\u9aa4 3:#### \u7528\u6237\u5047\u8bbe BlueWave Chromebook \u7684\u4ef7\u683c\u9ad8\u4e8e TechPro \u53f0\u5f0f\u7535\u8111\u3002\n", "\n", "\u6b65\u9aa4 4:#### \u7528\u6237\u7684\u5047\u8bbe\u662f\u6b63\u786e\u7684\u3002BlueWave Chromebook \u7684\u4ef7\u683c\u4e3a $249.99\uff0c\u800c TechPro \u53f0\u5f0f\u7535\u8111\u7684\u4ef7\u683c\u4e3a $999.99\uff0c\u56e0\u6b64 BlueWave Chromebook \u7684\u4ef7\u683c\u6bd4 TechPro \u53f0\u5f0f\u7535\u8111\u4f4e $750\u3002\n", "\n", "\u56de\u590d\u5ba2\u6237:#### BlueWave Chromebook \u6bd4 TechPro \u53f0\u5f0f\u7535\u8111\u4fbf\u5b9c $750\u3002\n"]}], "source": ["user_message = f\"\"\"BlueWave Chromebook \u6bd4 TechPro \u53f0\u5f0f\u7535\u8111\u8d35\u591a\u5c11\uff1f\"\"\"\n", "\n", "messages = [ \n", "{'role':'system', \n", " 'content': system_message}, \n", "{'role':'user', \n", " 'content': f\"{delimiter}{user_message}{delimiter}\"}, \n", "] \n", "\n", "response = get_completion_from_messages(messages)\n", "print(response)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Step 1:#### The user is asking if the store sells TVs.\n", "Step 2:#### The list of available products does not include any TVs.\n", "Response to user:#### I'm sorry, but we do not sell TVs at this store. Our available products include computers and laptops.\n"]}], "source": ["user_message = f\"\"\"\n", "do you sell tvs\"\"\"\n", "messages = [ \n", "{'role':'system', \n", " 'content': system_message}, \n", "{'role':'user', \n", " 'content': f\"{delimiter}{user_message}{delimiter}\"}, \n", "] \n", "response = get_completion_from_messages(messages)\n", "print(response)"]}, {"cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u6b65\u9aa4 1:#### \u9996\u5148\u786e\u5b9a\u7528\u6237\u662f\u5426\u6b63\u5728\u8be2\u95ee\u6709\u5173\u7279\u5b9a\u4ea7\u54c1\u6216\u4ea7\u54c1\u7684\u95ee\u9898\u3002\u4ea7\u54c1\u7c7b\u522b\u4e0d\u8ba1\u5165\u8303\u56f4\u3002\n", "\n", "\u6b65\u9aa4 2:#### \u5982\u679c\u7528\u6237\u8be2\u95ee\u7279\u5b9a\u4ea7\u54c1\uff0c\u8bf7\u786e\u8ba4\u4ea7\u54c1\u662f\u5426\u5728\u4ee5\u4e0b\u5217\u8868\u4e2d\u3002\u6240\u6709\u53ef\u7528\u4ea7\u54c1\uff1a\n", "\n", "\u6211\u4eec\u5f88\u62b1\u6b49\uff0c\u6211\u4eec\u5546\u5e97\u4e0d\u51fa\u552e\u7535\u89c6\u673a\u3002\n", "\n", "\u6b65\u9aa4 3:#### \u5982\u679c\u6d88\u606f\u4e2d\u5305\u542b\u4e0a\u8ff0\u5217\u8868\u4e2d\u7684\u4ea7\u54c1\uff0c\u8bf7\u5217\u51fa\u7528\u6237\u5728\u6d88\u606f\u4e2d\u505a\u51fa\u7684\u4efb\u4f55\u5047\u8bbe\uff0c\u4f8b\u5982\u7b14\u8bb0\u672c\u7535\u8111 X \u6bd4\u7b14\u8bb0\u672c\u7535\u8111 Y \u5927\uff0c\u6216\u8005\u7b14\u8bb0\u672c\u7535\u8111 Z \u6709 2 \u5e74\u4fdd\u4fee\u671f\u3002\n", "\n", "N/A\n", "\n", "\u6b65\u9aa4 4:#### \u5982\u679c\u7528\u6237\u505a\u51fa\u4e86\u4efb\u4f55\u5047\u8bbe\uff0c\u8bf7\u6839\u636e\u4ea7\u54c1\u4fe1\u606f\u786e\u5b9a\u5047\u8bbe\u662f\u5426\u6b63\u786e\u3002\n", "\n", "N/A\n", "\n", "\u56de\u590d\u5ba2\u6237:#### \u6211\u4eec\u5f88\u62b1\u6b49\uff0c\u6211\u4eec\u5546\u5e97\u4e0d\u51fa\u552e\u7535\u89c6\u673a\u3002\n"]}], "source": ["user_message = f\"\"\"\u4f60\u6709\u7535\u89c6\u673a\u4e48\"\"\"\n", "messages = [ \n", "{'role':'system', \n", " 'content': system_message}, \n", "{'role':'user', \n", " 'content': f\"{delimiter}{user_message}{delimiter}\"}, \n", "] \n", "response = get_completion_from_messages(messages)\n", "print(response)"]}, {"attachments": {}, "cell_type": "markdown", "metadata": {}, "source": ["## \u4e09\u3001\u5185\u5fc3\u72ec\u767d\uff08Inner monologue\uff09"]}, {"attachments": {}, "cell_type": "markdown", "metadata": {}, "source": ["\u5bf9\u4e8e\u67d0\u4e9b\u5e94\u7528\u7a0b\u5e8f\uff0c\u6a21\u578b\u7684\u63a8\u7406\u8fc7\u7a0b\u53ef\u80fd\u4e0d\u9002\u5408\u4e0e\u7528\u6237\u5171\u4eab\u3002\u4f8b\u5982\uff0c\u5728\u8f85\u5bfc\u7c7b\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u9f13\u52b1\u5b66\u751f\u81ea\u884c\u89e3\u51b3\u95ee\u9898\uff0c\u4f46\u6a21\u578b\u5bf9\u5b66\u751f\u89e3\u51b3\u65b9\u6848\u7684\u63a8\u7406\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u6cc4\u9732\u7b54\u6848\u3002\n", "\n", "\u5185\u5fc3\u72ec\u767d\u662f\u4e00\u79cd\u53ef\u4ee5\u7528\u6765\u7f13\u89e3\u8fd9\u79cd\u60c5\u51b5\u7684\u7b56\u7565\uff0c\u8fd9\u662f\u4e00\u79cd\u9690\u85cf\u6a21\u578b\u63a8\u7406\u8fc7\u7a0b\u7684\u9ad8\u7ea7\u65b9\u6cd5\u3002\n", "\n", "\u5185\u5fc3\u72ec\u767d\u7684\u601d\u60f3\u662f\u8ba9\u6a21\u578b\u4ee5\u4e00\u79cd\u4e0d\u4f1a\u900f\u9732\u7b54\u6848\u7684\u65b9\u5f0f\u751f\u6210\u90e8\u5206\u8f93\u51fa\uff0c\u8fd9\u6837\u7528\u6237\u5c31\u65e0\u6cd5\u770b\u5230\u5b8c\u6574\u7684\u63a8\u7406\u8fc7\u7a0b\u3002\u76ee\u6807\u662f\u5c06\u8fd9\u4e9b\u90e8\u5206\u9690\u85cf\u5728\u4e00\u4e2a\u7ed3\u6784\u5316\u7684\u683c\u5f0f\u4e2d\uff0c\u4f7f\u5f97\u4f20\u9012\u5b83\u4eec\u53d8\u5f97\u5bb9\u6613\u3002\u7136\u540e\uff0c\u5728\u5411\u7528\u6237\u5448\u73b0\u8f93\u51fa\u4e4b\u524d\uff0c\u5bf9\u8f93\u51fa\u8fdb\u884c\u4e00\u4e9b\u8f6c\u5316\uff0c\u4f7f\u5f97\u53ea\u6709\u90e8\u5206\u8f93\u51fa\u662f\u53ef\u89c1\u7684\u3002"]}, {"cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u6211\u4eec\u5f88\u62b1\u6b49\uff0c\u6211\u4eec\u5546\u5e97\u4e0d\u51fa\u552e\u7535\u89c6\u673a\u3002\n"]}], "source": ["try:\n", " final_response = response.split(delimiter)[-1].strip()\n", "except Exception as e:\n", " final_response = \"Sorry, I'm having trouble right now, please try asking another question.\"\n", " \n", "print(final_response)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u4e00\u79cd\u5904\u7406\u590d\u6742\u4efb\u52a1\u7684\u65b0\u7b56\u7565\uff0c\u5373\u5c06\u590d\u6742\u4efb\u52a1\u5206\u89e3\u4e3a\u4e00\u7cfb\u5217\u66f4\u7b80\u5355\u7684\u5b50\u4efb\u52a1\uff0c\u800c\u4e0d\u662f\u8bd5\u56fe\u5728\u4e00\u4e2a Prompt \u4e2d\u5b8c\u6210\u6574\u4e2a\u4efb\u52a1\u3002\n", "\n"]}], "metadata": {"kernelspec": {"display_name": "Python 3", "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"}, "orig_nbformat": 4}, "nbformat": 4, "nbformat_minor": 2}