From c6a3d0dfd3004777d9423e7d3430cbcabfc1c8c4 Mon Sep 17 00:00:00 2001 From: joyenjoye
| \n", + " | 0 | \n", + "1 | \n", + "
|---|---|---|
| 0 | \n", + "product_name | \n", + "description | \n", + "
| 1 | \n", + "全自动咖啡机 | \n", + "规格:\\n大型 - 尺寸:13.8'' x 17.3''。\\n中型 - 尺寸:11.5'' ... | \n", + "
| 2 | \n", + "电动牙刷 | \n", + "规格:\\n一般大小 - 高度:9.5'',宽度:1''。\\n\\n为什么我们热爱它:\\n我们的... | \n", + "
| 3 | \n", + "橙味维生素C泡腾片 | \n", + "规格:\\n每盒含有20片。\\n\\n为什么我们热爱它:\\n我们的橙味维生素C泡腾片是快速补充维... | \n", + "
| 4 | \n", + "无线蓝牙耳机 | \n", + "规格:\\n单个耳机尺寸:1.5'' x 1.3''。\\n\\n为什么我们热爱它:\\n这款无线蓝... | \n", + "
| 5 | \n", + "瑜伽垫 | \n", + "规格:\\n尺寸:24'' x 68''。\\n\\n为什么我们热爱它:\\n我们的瑜伽垫拥有出色的... | \n", + "
| 6 | \n", + "防水运动手表 | \n", + "规格:\\n表盘直径:40mm。\\n\\n为什么我们热爱它:\\n这款防水运动手表配备了心率监测和... | \n", + "
| 7 | \n", + "书籍:《机器学习基础》 | \n", + "规格:\\n页数:580页。\\n\\n为什么我们热爱它:\\n《机器学习基础》以易懂的语言讲解了机... | \n", + "
| 8 | \n", + "空气净化器 | \n", + "规格:\\n尺寸:15'' x 15'' x 20''。\\n\\n为什么我们热爱它:\\n我们的空... | \n", + "
| 9 | \n", + "陶瓷保温杯 | \n", + "规格:\\n容量:350ml。\\n\\n为什么我们热爱它:\\n我们的陶瓷保温杯设计优雅,保温效果... | \n", + "
| 10 | \n", + "宠物自动喂食器 | \n", + "规格:\\n尺寸:14'' x 9'' x 15''。\\n\\n为什么我们热爱它:\\n我们的宠物... | \n", + "
| 11 | \n", + "高清电视机 | \n", + "规格:\\n尺寸:50''。\\n\\n为什么我们热爱它:\\n我们的高清电视机拥有出色的画质和强大... | \n", + "
| 12 | \n", + "旅行背包 | \n", + "规格:\\n尺寸:18'' x 12'' x 6''。\\n\\n为什么我们热爱它:\\n我们的旅行... | \n", + "
| 13 | \n", + "太阳能庭院灯 | \n", + "规格:\\n高度:18''。\\n\\n为什么我们热爱它:\\n我们的太阳能庭院灯无需电源,只需将其... | \n", + "
| 14 | \n", + "厨房刀具套装 | \n", + "规格:\\n一套包括8把刀。\\n\\n为什么我们热爱它:\\n我们的厨房刀具套装由专业级不锈钢制成... | \n", + "
| 15 | \n", + "迷你无线蓝牙音箱 | \n", + "规格:\\n直径:3'',高度:2''。\\n\\n为什么我们热爱它:\\n我们的迷你无线蓝牙音箱体... | \n", + "
| 16 | \n", + "抗菌洗手液 | \n", + "规格:\\n容量:500ml。\\n\\n为什么我们热爱它:\\n我们的抗菌洗手液含有天然植物精华,... | \n", + "
| 17 | \n", + "纯棉T恤 | \n", + "规格:\\n尺码:S, M, L, XL, XXL。\\n\\n为什么我们热爱它:\\n我们的纯棉T... | \n", + "
| 18 | \n", + "自动咖啡机 | \n", + "规格:\\n尺寸:12'' x 8'' x 14''。\\n\\n为什么我们热爱它:\\n我们的自动... | \n", + "
| 19 | \n", + "摄像头保护套 | \n", + "规格:\\n适用于各种品牌和型号的摄像头。\\n\\n为什么我们热爱它:\\n我们的摄像头保护套可以... | \n", + "
| 20 | \n", + "玻璃保护膜 | \n", + "规格:\\n适用于各种尺寸的手机屏幕。\\n\\n为什么我们热爱它:\\n我们的玻璃保护膜可以有效防... | \n", + "
| 21 | \n", + "儿童益智玩具 | \n", + "规格:\\n适合3岁以上的儿童。\\n\\n为什么我们热爱它:\\n我们的儿童益智玩具设计独特,色彩... | \n", + "
| 22 | \n", + "迷你书架 | \n", + "规格:\\n尺寸:20'' x 8'' x 24''。\\n\\n为什么我们热爱它:\\n我们的迷你... | \n", + "
| 23 | \n", + "防滑瑜伽垫 | \n", + "规格:\\n尺寸:72'' x 24''。\\n\\n为什么我们热爱它:\\n我们的防滑瑜伽垫采用高... | \n", + "
| 24 | \n", + "LED台灯 | \n", + "规格:\\n尺寸:6'' x 6'' x 18''。\\n\\n为什么我们热爱它:\\n我们的LED... | \n", + "
| 25 | \n", + "水晶酒杯 | \n", + "规格:\\n容量:250ml。\\n\\n为什么我们热爱它:\\n我们的水晶酒杯采用高品质水晶玻璃制... | \n", + "
思考:我可以使用`sorted()`函数按姓氏和名字的顺序对客户列表进行排序。我需要为`sorted()`提供一个键函数,该函数返回一个元组,元组中按顺序包含姓氏和名字。
\n", - "\n", - "2. 模型基于思考采取行动(Action), 因为使用的工具不同,Action的输出也和之前有所不同,这里输出的为python代码\n", - "行动: 使用Python编程,输入以下代码
\n", - " \n", - " \n", - " ```python\n", - " customers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\n", - " sorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))\n", - " for customer in sorted_customers:\n", - " print(customer)\n", - " ```\n", - "观测:
\n", - " \n", - " ```\n", - " ['Jen', 'Ayai']\n", - " ['Lang', 'Chain']\n", - " ['Harrison', 'Chase']\n", - " ['Elle', 'Elem']\n", - " ['Trance', 'Former']\n", - " ['Geoff', 'Fusion']\n", - " ['Dolly', 'Too']\n", - " ```\n", - "思考: 现在客户按照姓氏和名字的顺序进行了排序。
\n", - "\n", - "5. 给出最终答案(Final Answer)\n", - "最终答案: [['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]
\n", - "6. 返回最终答案。" + "agent.run(f\"\"\"在这些客户名字转换为拼音\\\n", + "并打印输出列表: {customer_list}\\\n", + "思考过程请使用中文。\"\"\") " ] }, { + "attachments": {}, "cell_type": "markdown", "id": "16f530a5-8c50-4648-951f-4e65d15ca93f", "metadata": { @@ -497,6 +743,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "951048dc-0414-410d-a0e9-6ef32bbdda89", "metadata": { @@ -527,7 +774,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 25, "id": "92b8961b-b990-4540-a7f9-e4701dfaa616", "metadata": {}, "outputs": [ @@ -537,11 +784,11 @@ "text": [ "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor] Entering Chain run with input:\n", "\u001b[0m{\n", - " \"input\": \"Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\"\n", + " \"input\": \"在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\"\n", "}\n", "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain] Entering Chain run with input:\n", "\u001b[0m{\n", - " \"input\": \"Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\",\n", + " \"input\": \"在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\",\n", " \"agent_scratchpad\": \"\",\n", " \"stop\": [\n", " \"\\nObservation:\",\n", @@ -551,18 +798,18 @@ "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", - " \"Human: You are an agent designed to write and execute python code to answer questions.\\nYou have access to a python REPL, which you can use to execute python code.\\nIf you get an error, debug your code and try again.\\nOnly use the output of your code to answer the question. \\nYou might know the answer without running any code, but you should still run the code to get the answer.\\nIf it does not seem like you can write code to answer the question, just return \\\"I don't know\\\" as the answer.\\n\\n\\nPython REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [Python REPL]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\\nThought:\"\n", + " \"Human: You are an agent designed to write and execute python code to answer questions.\\nYou have access to a python REPL, which you can use to execute python code.\\nIf you get an error, debug your code and try again.\\nOnly use the output of your code to answer the question. \\nYou might know the answer without running any code, but you should still run the code to get the answer.\\nIf it does not seem like you can write code to answer the question, just return \\\"I don't know\\\" as the answer.\\n\\n\\nPython_REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [Python_REPL]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: 在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\\nThought:\"\n", " ]\n", "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] [12.25s] Exiting LLM run with output:\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] [2.91s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", - " \"text\": \"I can use the sorted() function to sort the list of customers by last name and then first name. I will need to provide a key function to sorted() that returns a tuple of the last name and first name in that order.\\nAction: Python REPL\\nAction Input:\\n```\\ncustomers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\\nsorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))\\nprint(sorted_customers)\\n```\",\n", + " \"text\": \"我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\",\n", " \"generation_info\": null,\n", " \"message\": {\n", - " \"content\": \"I can use the sorted() function to sort the list of customers by last name and then first name. I will need to provide a key function to sorted() that returns a tuple of the last name and first name in that order.\\nAction: Python REPL\\nAction Input:\\n```\\ncustomers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\\nsorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))\\nprint(sorted_customers)\\n```\",\n", + " \"content\": \"我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\",\n", " \"additional_kwargs\": {},\n", " \"example\": false\n", " }\n", @@ -571,29 +818,26 @@ " ],\n", " \"llm_output\": {\n", " \"token_usage\": {\n", - " \"prompt_tokens\": 327,\n", - " \"completion_tokens\": 138,\n", - " \"total_tokens\": 465\n", + " \"prompt_tokens\": 322,\n", + " \"completion_tokens\": 50,\n", + " \"total_tokens\": 372\n", " },\n", " \"model_name\": \"gpt-3.5-turbo\"\n", - " }\n", + " },\n", + " \"run\": null\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain] [12.25s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:chain:LLMChain] [2.91s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"text\": \"I can use the sorted() function to sort the list of customers by last name and then first name. I will need to provide a key function to sorted() that returns a tuple of the last name and first name in that order.\\nAction: Python REPL\\nAction Input:\\n```\\ncustomers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\\nsorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))\\nprint(sorted_customers)\\n```\"\n", + " \"text\": \"我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\"\n", "}\n", - "\u001b[32;1m\u001b[1;3m[tool/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 4:tool:Python REPL] Entering Tool run with input:\n", - "\u001b[0m\"```\n", - "customers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\n", - "sorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))\n", - "print(sorted_customers)\n", - "```\"\n", - "\u001b[36;1m\u001b[1;3m[tool/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 4:tool:Python REPL] [2.2239999999999998ms] Exiting Tool run with output:\n", - "\u001b[0m\"[['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]\"\n", + "\u001b[32;1m\u001b[1;3m[tool/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 4:tool:Python_REPL] Entering Tool run with input:\n", + "\u001b[0m\"import pinyin\"\n", + "\u001b[36;1m\u001b[1;3m[tool/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 4:tool:Python_REPL] [0.0ms] Exiting Tool run with output:\n", + "\u001b[0m\"\"\n", "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with input:\n", "\u001b[0m{\n", - " \"input\": \"Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\",\n", - " \"agent_scratchpad\": \"I can use the sorted() function to sort the list of customers by last name and then first name. I will need to provide a key function to sorted() that returns a tuple of the last name and first name in that order.\\nAction: Python REPL\\nAction Input:\\n```\\ncustomers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\\nsorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))\\nprint(sorted_customers)\\n```\\nObservation: [['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]\\n\\nThought:\",\n", + " \"input\": \"在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\",\n", + " \"agent_scratchpad\": \"我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\\nObservation: \\nThought:\",\n", " \"stop\": [\n", " \"\\nObservation:\",\n", " \"\\n\\tObservation:\"\n", @@ -602,18 +846,33 @@ "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", - " \"Human: You are an agent designed to write and execute python code to answer questions.\\nYou have access to a python REPL, which you can use to execute python code.\\nIf you get an error, debug your code and try again.\\nOnly use the output of your code to answer the question. \\nYou might know the answer without running any code, but you should still run the code to get the answer.\\nIf it does not seem like you can write code to answer the question, just return \\\"I don't know\\\" as the answer.\\n\\n\\nPython REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [Python REPL]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\\nThought:I can use the sorted() function to sort the list of customers by last name and then first name. I will need to provide a key function to sorted() that returns a tuple of the last name and first name in that order.\\nAction: Python REPL\\nAction Input:\\n```\\ncustomers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\\nsorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))\\nprint(sorted_customers)\\n```\\nObservation: [['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]\\n\\nThought:\"\n", + " \"Human: You are an agent designed to write and execute python code to answer questions.\\nYou have access to a python REPL, which you can use to execute python code.\\nIf you get an error, debug your code and try again.\\nOnly use the output of your code to answer the question. \\nYou might know the answer without running any code, but you should still run the code to get the answer.\\nIf it does not seem like you can write code to answer the question, just return \\\"I don't know\\\" as the answer.\\n\\n\\nPython_REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [Python_REPL]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: 在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\\nThought:我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\\nObservation: \\nThought:\"\n", " ]\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] [6.94s] Exiting LLM run with output:\n", + "}\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.观测: 2023-06-03
\n", + "观测: 2023-07-04
\n", " \n", " \n", " \n", @@ -830,7 +1165,7 @@ "思考: 我已成功使用 time 工具检索到了今天的日期
\n", "\n", "5. 给出最终答案(Final Answer)\n", - "最终答案: 今天的日期是2023-06-03.
\n", + "最终答案: 今天的日期是2023-07-04.
\n", "6. 返回最终答案。" ] } @@ -851,7 +1186,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.12" + "version": "3.9.16" } }, "nbformat": 4, diff --git a/content/LangChain for LLM Application Development/product_data.csv b/content/LangChain for LLM Application Development/product_data.csv new file mode 100644 index 0000000..a3f8362 --- /dev/null +++ b/content/LangChain for LLM Application Development/product_data.csv @@ -0,0 +1,434 @@ +product_name,description +全自动咖啡机,"规格: +大型 - 尺寸:13.8'' x 17.3''。 +中型 - 尺寸:11.5'' x 15.2''。 + +为什么我们热爱它: +这款全自动咖啡机是爱好者的理想选择。 一键操作,即可研磨豆子并沏制出您喜爱的咖啡。它的耐用性和一致性使它成为家庭和办公室的理想选择。 + +材质与护理: +清洁时只需轻擦。 + +构造: +由高品质不锈钢制成。 + +其他特性: +内置研磨器和滤网。 +预设多种咖啡模式。 +在中国制造。 + +有问题? 请随时联系我们的客户服务团队,他们会解答您的所有问题。" +电动牙刷,"规格: +一般大小 - 高度:9.5'',宽度:1''。 + +为什么我们热爱它: +我们的电动牙刷采用先进的刷头设计和强大的电机,为您提供超凡的清洁力和舒适的刷牙体验。 + +材质与护理: +不可水洗,只需用湿布清洁。 + +构造: +由食品级塑料和尼龙刷毛制成。 + +其他特性: +具有多种清洁模式和定时功能。 +USB充电。 +在日本制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +橙味维生素C泡腾片,"规格: +每盒含有20片。 + +为什么我们热爱它: +我们的橙味维生素C泡腾片是快速补充维生素C的理想方式。每片含有500mg的维生素C,可以帮助提升免疫力,保护您的健康。 + +材质与护理: +请存放在阴凉干燥的地方,避免阳光直射。 + +构造: +主要成分为维生素C和柠檬酸钠。 + +其他特性: +含有天然橙味。 +易于携带。 +在美国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +无线蓝牙耳机,"规格: +单个耳机尺寸:1.5'' x 1.3''。 + +为什么我们热爱它: +这款无线蓝牙耳机配备了降噪技术和长达8小时的电池续航力,让您无论在哪里都可以享受无障碍的音乐体验。 + +材质与护理: +只需用湿布清洁。 + +构造: +由耐用的塑料和金属构成,配备有软质耳塞。 + +其他特性: +快速充电功能。 +内置麦克风,支持接听电话。 +在韩国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +瑜伽垫,"规格: +尺寸:24'' x 68''。 + +为什么我们热爱它: +我们的瑜伽垫拥有出色的抓地力和舒适度,无论是做瑜伽还是健身,都是理想的选择。 + +材质与护理: +可用清水清洁,自然晾干。 + +构造: +由环保PVC材料制成。 + +其他特性: +附带便携包和绑带。 +在印度制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +防水运动手表,"规格: +表盘直径:40mm。 + +为什么我们热爱它: +这款防水运动手表配备了心率监测和计步功能,不仅适合日常佩戴,也是户外运动的理想伙伴。 + +材质与护理: +只需用湿布清洁。 + +构造: +由不锈钢和硅胶制成。 + +其他特性: +内置可充电电池。 +支持蓝牙连接手机。 +在瑞士制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +书籍:《机器学习基础》,"规格: +页数:580页。 + +为什么我们热爱它: +《机器学习基础》以易懂的语言讲解了机器学习的基本概念和方法,适合初学者阅读。 + +材质与护理: +纸质封面,存放在阴凉干燥处。 + +构造: +由环保纸张和油墨印刷。 + +其他特性: +附带实例和习题。 +在美国印刷。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +空气净化器,"规格: +尺寸:15'' x 15'' x 20''。 + +为什么我们热爱它: +我们的空气净化器采用了先进的HEPA过滤技术,能有效去除空气中的微粒和异味,为您提供清新的室内环境。 + +材质与护理: +清洁时使用干布擦拭。 + +构造: +由塑料和电子元件制成。 + +其他特性: +三档风速,附带定时功能。 +在德国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +陶瓷保温杯,"规格: +容量:350ml。 + +为什么我们热爱它: +我们的陶瓷保温杯设计优雅,保温效果好,是办公室和户外活动的理想伴侣。 + +材质与护理: +可以手洗或洗碗机清洗。 + +构造: +由高品质陶瓷和不锈钢制成。 + +其他特性: +有多种颜色可选。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +宠物自动喂食器,"规格: +尺寸:14'' x 9'' x 15''。 + +为什么我们热爱它: +我们的宠物自动喂食器可以定时定量投放食物,让您无论在家或外出都能确保宠物的饮食。 + +材质与护理: +可用湿布清洁。 + +构造: +由塑料和电子元件制成。 + +其他特性: +配备LCD屏幕,操作简单。 +可以设置多次投食。 +在美国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +高清电视机,"规格: +尺寸:50''。 + +为什么我们热爱它: +我们的高清电视机拥有出色的画质和强大的音效,带来沉浸式的观看体验。 + +材质与护理: +使用干布清洁。 + +构造: +由塑料、金属和电子元件制成。 + +其他特性: +支持网络连接,可以在线观看视频。 +配备遥控器。 +在韩国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +旅行背包,"规格: +尺寸:18'' x 12'' x 6''。 + +为什么我们热爱它: +我们的旅行背包拥有多个实用的内外袋,轻松装下您的必需品,是短途旅行的理想选择。 + +材质与护理: +可以手洗,自然晾干。 + +构造: +由防水尼龙制成。 + +其他特性: +附带可调节背带和安全锁。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +太阳能庭院灯,"规格: +高度:18''。 + +为什么我们热爱它: +我们的太阳能庭院灯无需电源,只需将其放在阳光下即可充电。到了晚上,它会自动点亮,为您的庭院增添温馨的氛围。 + +材质与护理: +可以用湿布清洁。 + +构造: +由塑料和太阳能板制成。 + +其他特性: +自动感应,日落后自动点亮。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +厨房刀具套装,"规格: +一套包括8把刀。 + +为什么我们热爱它: +我们的厨房刀具套装由专业级不锈钢制成,刀身坚固,刀刃锋利,切割效果出色,是厨房必备工具。 + +材质与护理: +建议手洗并立即擦干,以保持最佳状况。 + +构造: +由高品质不锈钢制成。 + +其他特性: +包括多种类型的刀,适合不同的用途。 +在德国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +迷你无线蓝牙音箱,"规格: +直径:3'',高度:2''。 + +为什么我们热爱它: +我们的迷你无线蓝牙音箱体积小巧,音质出色,可以轻松带给您音乐的享受。 + +材质与护理: +使用湿布擦拭清洁。 + +构造: +由塑料和金属构成。 + +其他特性: +内置可充电电池,充满后可播放5小时。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +抗菌洗手液,"规格: +容量:500ml。 + +为什么我们热爱它: +我们的抗菌洗手液含有天然植物精华,能有效杀灭细菌,同时滋润双手。 + +材质与护理: +直接涂抹在手上,然后用水冲洗。 + +构造: +由天然植物精华和抗菌成分制成。 + +其他特性: +具有清新的柠檬香味。 +在法国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +纯棉T恤,"规格: +尺码:S, M, L, XL, XXL。 + +为什么我们热爱它: +我们的纯棉T恤采用高品质棉质,柔软舒适,透气性好,是日常穿着的理想选择。 + +材质与护理: +可以机洗,建议反面洗涤,以保持颜色鲜艳。 + +构造: +由100%纯棉制成。 + +其他特性: +有多种颜色可选。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +自动咖啡机,"规格: +尺寸:12'' x 8'' x 14''。 + +为什么我们热爱它: +我们的自动咖啡机可以轻松制作美味的咖啡,只需按一下钮,就能享受到新鲜的咖啡。 + +材质与护理: +可以用湿布清洁外部,内部配件可以拆卸清洗。 + +构造: +由不锈钢和塑料制成。 + +其他特性: +可调节咖啡浓度。 +在意大利制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +摄像头保护套,"规格: +适用于各种品牌和型号的摄像头。 + +为什么我们热爱它: +我们的摄像头保护套可以有效保护您的摄像头不受灰尘和刮擦的影响。 + +材质与护理: +使用湿布擦拭。 + +构造: +由耐磨的合成纤维制成。 + +其他特性: +设计简洁,易于安装。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +玻璃保护膜,"规格: +适用于各种尺寸的手机屏幕。 + +为什么我们热爱它: +我们的玻璃保护膜可以有效防止手机屏幕刮伤和破裂,而且不影响触控的灵敏度。 + +材质与护理: +使用干布擦拭。 + +构造: +由高强度的玻璃材料制成。 + +其他特性: +安装简单,适合自行安装。 +在日本制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +儿童益智玩具,"规格: +适合3岁以上的儿童。 + +为什么我们热爱它: +我们的儿童益智玩具设计独特,色彩鲜艳,能够引发孩子的兴趣,同时训练他们的手眼协调能力和逻辑思维。 + +材质与护理: +使用湿布擦拭。 + +构造: +由环保无毒的塑料制成。 + +其他特性: +具有教育和娱乐的双重功能。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +迷你书架,"规格: +尺寸:20'' x 8'' x 24''。 + +为什么我们热爱它: +我们的迷你书架占地面积小,设计简约,能够有效存放和展示您的书籍。 + +材质与护理: +使用干布擦拭。 + +构造: +由实木制成。 + +其他特性: +拥有3层收纳空间。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +防滑瑜伽垫,"规格: +尺寸:72'' x 24''。 + +为什么我们热爱它: +我们的防滑瑜伽垫采用高品质环保材料,触感舒适,防滑效果好,是瑜伽爱好者的理想选择。 + +材质与护理: +可以用湿布清洁。 + +构造: +由环保PVC材料制成。 + +其他特性: +有多种颜色可选。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +LED台灯,"规格: +尺寸:6'' x 6'' x 18''。 + +为什么我们热爱它: +我们的LED台灯亮度可调,采用节能LED灯珠,不仅可以提供明亮的光照,还可以节省能源。 + +材质与护理: +使用湿布擦拭。 + +构造: +由金属和塑料制成。 + +其他特性: +具有触控开关和调光功能。 +在中国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" +水晶酒杯,"规格: +容量:250ml。 + +为什么我们热爱它: +我们的水晶酒杯采用高品质水晶玻璃制成,造型优雅,是享受美酒的理想选择。 + +材质与护理: +建议手洗。 + +构造: +由水晶玻璃制成。 + +其他特性: +适合各种场合使用。 +在法国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" From 155d2d14869d9c5472cfa1057d5970202d4dad33 Mon Sep 17 00:00:00 2001 From: xuhu0115 <64852985+xuhu0115@users.noreply.github.com> Date: Thu, 6 Jul 2023 10:20:53 +0800 Subject: [PATCH 3/7] Add files via upload --- 2.模型、提示和解析器.ipynb | 2179 ++++++++++++++++++++++++++++++++++++ 4.模型链.ipynb | 1693 ++++++++++++++++++++++++++++ 2 files changed, 3872 insertions(+) create mode 100644 2.模型、提示和解析器.ipynb create mode 100644 4.模型链.ipynb diff --git a/2.模型、提示和解析器.ipynb b/2.模型、提示和解析器.ipynb new file mode 100644 index 0000000..734ec03 --- /dev/null +++ b/2.模型、提示和解析器.ipynb @@ -0,0 +1,2179 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 模型,提示和输出解释器\n", + "\n", + "\n", + "**目录**\n", + "* 获取你的OpenAI API Key\n", + "* 直接调用OpenAI的API\n", + "* 通过LangChain进行的API调用:\n", + " * 提示(Prompts)\n", + " * [模型(Models)](#model)\n", + " * 输出解析器(Output parsers)\n", + " " + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "## 获取你的OpenAI API Key\n", + "\n", + "登陆[OpenAI账户获取你的API Key](https://platform.openai.com/account/api-keys) " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# 下载需要的包python-dotenv和openai\n", + "# 如果你需要查看安装过程日志,可删除 -q \n", + "!pip install -q python-dotenv\n", + "!pip install -q openai\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# 将自己的 API-KEY 导入系统环境变量(关于如何设置参考这篇文章:https://zhuanlan.zhihu.com/p/627665725)\n", + "!export OPENAI_API_KEY='api-key' #api_key替换为自己的" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import os\n", + "import openai\n", + "from dotenv import load_dotenv, find_dotenv\n", + "\n", + "# 读取本地的环境变量 \n", + "_ = load_dotenv(find_dotenv())\n", + "\n", + "# 获取环境变量 OPENAI_API_KEY\n", + "openai.api_key = os.environ['OPENAI_API_KEY'] " + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "## Chat API:OpenAI\n", + "\n", + "我们先从直接调用OpenAI的API开始。\n", + "\n", + "`get_completion`函数是基于`openai`的封装函数,对于给定提示(prompt)输出相应的回答。其包含两个参数\n", + " \n", + " - `prompt` 必需输入参数。 你给模型的**提示,可以是一个问题,可以是你需要模型帮助你做的事**(改变文本写作风格,翻译,回复消息等等)。\n", + " - `model` 非必需输入参数。默认使用gpt-3.5-turbo。你也可以选择其他模型。\n", + " \n", + "这里的提示对应我们给chatgpt的问题,函数给出的输出则对应chatpgt给我们的答案。" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n", + " \n", + " messages = [{\"role\": \"user\", \"content\": prompt}]\n", + " \n", + " response = openai.ChatCompletion.create(\n", + " model=model,\n", + " messages=messages,\n", + " temperature=0, \n", + " )\n", + " return response.choices[0].message[\"content\"]" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "### 一个简单的例子\n", + "\n", + "我们来一个简单的例子 - 分别用中英文问问模型\n", + "\n", + "- 中文提示(Prompt in Chinese): `1+1是什么?`\n", + "- 英文提示(Prompt in English): `What is 1+1?`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1+1等于2。'" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# 中文\n", + "get_completion(\"1+1是什么?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'As an AI language model, I can tell you that the answer to 1+1 is 2.'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 英文\n", + "get_completion(\"What is 1+1?\")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 复杂一点例子\n", + "\n", + "上面的简单例子,模型`gpt-3.5-turbo`对我们的关于1+1是什么的提问给出了回答。\n", + "\n", + "现在我们来看一个复杂一点的例子: \n", + "\n", + "假设我们是电商公司员工,我们的顾客是一名海盗A,他在我们的网站上买了一个榨汁机用来做奶昔,在制作奶昔的过程中,奶昔的盖子飞了出去,弄得厨房墙上到处都是。于是海盗A给我们的客服中心写来以下邮件:`customer_email`" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "customer_email = \"\"\"\n", + "Arrr, I be fuming that me blender lid \\\n", + "flew off and splattered me kitchen walls \\\n", + "with smoothie! And to make matters worse,\\\n", + "the warranty don't cover the cost of \\\n", + "cleaning up me kitchen. I need yer help \\\n", + "right now, matey!\n", + "\"\"\"" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们的客服人员对于海盗的措辞表达觉得有点难以理解。 现在我们想要实现两个小目标:\n", + "\n", + "- 让模型用美式英语的表达方式将海盗的邮件进行翻译,客服人员可以更好理解。*这里海盗的英文表达可以理解为英文的方言,其与美式英语的关系,就如四川话与普通话的关系。\n", + "- 让模型在翻译是用平和尊重的语气进行表达,客服人员的心情也会更好。\n", + "\n", + "根据这两个小目标,定义一下文本表达风格:`style`" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# 美式英语 + 平静、尊敬的语调\n", + "style = \"\"\"American English \\\n", + "in a calm and respectful tone\n", + "\"\"\"" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "下一步需要做的是将`customer_email`和`style`结合起来构造我们的提示:`prompt`" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Translate the text that is delimited by triple backticks \n", + "into a style that is American English in a calm and respectful tone\n", + ".\n", + "text: ```\n", + "Arrr, I be fuming that me blender lid flew off and splattered me kitchen walls with smoothie! And to make matters worse,the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\n", + "```\n", + "\n" + ] + } + ], + "source": [ + "# 要求模型根据给出的语调进行转化\n", + "prompt = f\"\"\"Translate the text \\\n", + "that is delimited by triple backticks \n", + "into a style that is {style}.\n", + "text: ```{customer_email}```\n", + "\"\"\"\n", + "\n", + "print(prompt)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`prompt` 构造好了,我们可以调用`get_completion`得到我们想要的结果 - 用平和尊重的语气,美式英语表达的海岛邮件" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "response = get_completion(prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'I am quite upset that my blender lid came off and caused my smoothie to splatter all over my kitchen walls. Additionally, the warranty does not cover the cost of cleaning up the mess. Would you be able to assist me, please? Thank you kindly.'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "response" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "对比语言风格转换前后,用词更为正式,替换了极端情绪的表达,并表达了感谢。\n", + "- Arrr, I be fuming(呀,我气的发抖) 换成了 I am quite upset (我有点失望)\n", + "- And to make matters worse(更糟糕地是),换成了 Additionally(还有)\n", + "- I need yer help right now, matey!(我需要你的帮助),换成了Would you be able to assist me, please? Thank you kindly.(请问您能帮我吗?非常感谢您的好意)\n", + "\n", + "\n", + "✨ 你可以尝试修改提示,看可以得到什么不一样的结果😉" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 中文" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "假设我们是电商公司员工,我们的顾客是一名海盗A,他在我们的网站上买了一个榨汁机用来做奶昔,在制作奶昔的过程中,奶昔的盖子飞了出去,弄得厨房墙上到处都是。于是海盗A给我们的客服中心写来以下邮件:`customer_email`" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# 非正式用语\n", + "customer_email = \"\"\" \n", + "阿,我很生气,\\\n", + "因为我的搅拌机盖掉了,\\\n", + "把奶昔溅到了厨房的墙上!\\\n", + "更糟糕的是,保修不包括打扫厨房的费用。\\\n", + "我现在需要你的帮助,伙计!\n", + "\"\"\"" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们的客服人员对于海盗的措辞表达觉得有点难以理解。 现在我们想要实现两个小目标:\n", + "\n", + "- 让模型用比较正式的普通话的表达方式将海盗的邮件进行翻译,客服人员可以更好理解。\n", + "- 让模型在翻译是用平和尊重的语气进行表达,客服人员的心情也会更好。\n", + "\n", + "根据这两个小目标,定义一下文本表达风格:`style`" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# 普通话 + 平静、尊敬的语调\n", + "style = \"\"\"正式普通话 \\\n", + "用一个平静、尊敬的语调\n", + "\"\"\"" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "下一步需要做的是将`customer_email`和`style`结合起来构造我们的提示:`prompt`" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "把由三个反引号分隔的文本text翻译成一种正式普通话 用一个平静、尊敬的语调\n", + "风格。\n", + "text: ``` \n", + "阿,我很生气,因为我的搅拌机盖掉了,把奶昔溅到了厨房的墙上!更糟糕的是,保修不包括打扫厨房的费用。我现在需要你的帮助,伙计!\n", + "```\n", + "\n" + ] + } + ], + "source": [ + "# 要求模型根据给出的语调进行转化\n", + "prompt = f\"\"\"把由三个反引号分隔的文本text\\\n", + "翻译成一种{style}风格。\n", + "text: ```{customer_email}```\n", + "\"\"\"\n", + "\n", + "print(prompt)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`prompt` 构造好了,我们可以调用`get_completion`得到我们想要的结果 - 用平和尊重的普通话语气去表达一封带着方言表达方式的邮件" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "response = get_completion(prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'尊敬的朋友,我感到不安,因为我的搅拌机盖子不慎掉落,导致奶昔溅到了厨房的墙壁上!更加令人糟心的是,保修并不包含厨房清洁的费用。此刻,我需要你的帮助,亲爱的朋友!'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "response" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "## Chat API:LangChain\n", + "\n", + "在前面一部分,我们通过封装函数`get_completion`直接调用了OpenAI完成了对方言邮件进行了的翻译,得到用平和尊重的语气、正式的普通话表达的邮件。\n", + "\n", + "让我们尝试使用LangChain来实现相同的功能。" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "^C\n" + ] + } + ], + "source": [ + "# 如果你需要查看安装过程日志,可删除 -q \n", + "# --upgrade 让我们可以安装到最新版本的 langchain\n", + "!pip install -q --upgrade langchain" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "### 模型\n", + "\n", + "从`langchain.chat_models`导入`OpenAI`的对话模型`ChatOpenAI`。 除去OpenAI以外,`langchain.chat_models`还集成了其他对话模型,更多细节可以查看[Langchain官方文档](https://python.langchain.com/en/latest/modules/models/chat/integrations.html)。" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from langchain.chat_models import ChatOpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ChatOpenAI(verbose=False, callbacks=None, callback_manager=None, client=| \n", + " | Product | \n", + "Review | \n", + "
|---|---|---|
| 0 | \n", + "Queen Size Sheet Set | \n", + "I ordered a king size set. My only criticism w... | \n", + "
| 1 | \n", + "Waterproof Phone Pouch | \n", + "I loved the waterproof sac, although the openi... | \n", + "
| 2 | \n", + "Luxury Air Mattress | \n", + "This mattress had a small hole in the top of i... | \n", + "
| 3 | \n", + "Pillows Insert | \n", + "This is the best throw pillow fillers on Amazo... | \n", + "
| 4 | \n", + "Milk Frother Handheld\\n | \n", + "I loved this product. But they only seem to l... | \n", + "
| \n", - " | Product | \n", - "Review | \n", - "
|---|---|---|
| 0 | \n", - "Queen Size Sheet Set | \n", - "I ordered a king size set. My only criticism w... | \n", - "
| 1 | \n", - "Waterproof Phone Pouch | \n", - "I loved the waterproof sac, although the openi... | \n", - "
| 2 | \n", - "Luxury Air Mattress | \n", - "This mattress had a small hole in the top of i... | \n", - "
| 3 | \n", - "Pillows Insert | \n", - "This is the best throw pillow fillers on Amazo... | \n", - "
| 4 | \n", - "Milk Frother Handheld\\n | \n", - "I loved this product. But they only seem to l... | \n", - "