diff --git a/content/LangChain for LLM Application Development/1.开篇介绍.md b/content/LangChain for LLM Application Development/1.开篇介绍.md deleted file mode 100644 index 67e9cbd..0000000 --- a/content/LangChain for LLM Application Development/1.开篇介绍.md +++ /dev/null @@ -1,33 +0,0 @@ -## 吴恩达 LangChain大模型应用开发 开端篇 - -## LangChain for LLM Application Development - -欢迎来到LangChain大模型应用开发短期课程👏🏻👏🏻 - -本课程由哈里森·蔡斯 (Harrison Chase,LangChain作者)与Deeplearning.ai合作开发,旨在教大家使用这个神奇工具。 - -### 🚀 LangChain的诞生和发展 - -通过对LLM或大型语言模型给出提示(prompt),现在可以比以往更快地开发AI应用程序,但是一个应用程序可能需要进行多轮提示以及解析输出。 - -在此过程有很多胶水代码需要编写,基于此需求,哈里森·蔡斯 (Harrison Chase) 创建了LangChain,使开发过程变得更加丝滑。 - -LangChain开源社区快速发展,贡献者已达数百人,正以惊人的速度更新代码和功能。 - - -### 📚 课程基本内容 - -LangChain是用于构建大模型应用程序的开源框架,有Python和JavaScript两个不同版本的包。LangChain基于模块化组合,有许多单独的组件,可以一起使用或单独使用。此外LangChain还拥有很多应用案例,帮助我们了解如何将这些模块化组件以链式方式组合,以形成更多端到端的应用程序 。 - -在本课程中,我们将介绍LandChain的常见组件。具体而言我们会讨论一下几个方面 -- 模型(Models) -- 提示(Prompts): 使模型执行操作的方式 -- 索引(Indexes): 获取数据的方式,可以与模型结合使用 -- 链式(Chains): 端到端功能实现 -- 代理(Agents): 使用模型作为推理引擎 - - - -### 🌹致谢课程重要贡献者 - -最后特别感谢Ankush Gholar(LandChain的联合作者)、Geoff Ladwig,、Eddy Shyu 以及 Diala Ezzedine,他们也为本课程内容贡献颇多~ \ No newline at end of file diff --git a/content/LangChain for LLM Application Development/1.简介.ipynb b/content/LangChain for LLM Application Development/1.简介.ipynb new file mode 100644 index 0000000..62cee6b --- /dev/null +++ b/content/LangChain for LLM Application Development/1.简介.ipynb @@ -0,0 +1,87 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "cfab521b-77fa-41be-a964-1f50f2ef4689", + "metadata": {}, + "source": [ + "# 1. 简介\n", + "
\n", + "\n", + "欢迎来到LangChain大模型应用开发短期课程👏🏻👏🏻\n", + "\n", + "本课程由哈里森·蔡斯 (Harrison Chase,LangChain作者)与Deeplearning.ai合作开发,旨在教大家使用这个神奇工具。\n", + "\n", + "\n", + "\n", + "## 1.1 LangChain的诞生和发展\n", + "\n", + "通过对LLM或大型语言模型给出提示(prompt),现在可以比以往更快地开发AI应用程序,但是一个应用程序可能需要进行多轮提示以及解析输出。\n", + "\n", + "在此过程有很多胶水代码需要编写,基于此需求,哈里森·蔡斯 (Harrison Chase) 创建了LangChain,使开发过程变得更加丝滑。\n", + "\n", + "LangChain开源社区快速发展,贡献者已达数百人,正以惊人的速度更新代码和功能。\n", + "\n", + "\n", + "## 1.2 课程基本内容\n", + "\n", + "LangChain是用于构建大模型应用程序的开源框架,有Python和JavaScript两个不同版本的包。LangChain基于模块化组合,有许多单独的组件,可以一起使用或单独使用。此外LangChain还拥有很多应用案例,帮助我们了解如何将这些模块化组件以链式方式组合,以形成更多端到端的应用程序 。\n", + "\n", + "在本课程中,我们将介绍LandChain的常见组件。具体而言我们会讨论一下几个方面\n", + "- 模型(Models)\n", + "- 提示(Prompts): 使模型执行操作的方式\n", + "- 索引(Indexes): 获取数据的方式,可以与模型结合使用\n", + "- 链式(Chains): 端到端功能实现\n", + "- 代理(Agents): 使用模型作为推理引擎\n", + "\n", + " \n", + "\n", + "## 1.3 致谢课程重要贡献者\n", + "\n", + "最后特别感谢Ankush Gholar(LandChain的联合作者)、Geoff Ladwig,、Eddy Shyu 以及 Diala Ezzedine,他们也为本课程内容贡献颇多~ " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e3618ca8", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.9.12" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/content/LangChain for LLM Application Development/2.模型、提示和解析器.ipynb b/content/LangChain for LLM Application Development/2.模型、提示和解析器.ipynb index d06b746..734ec03 100644 --- a/content/LangChain for LLM Application Development/2.模型、提示和解析器.ipynb +++ b/content/LangChain for LLM Application Development/2.模型、提示和解析器.ipynb @@ -32,19 +32,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# 下载需要的包python-dotenv和openai\n", "# 如果你需要查看安装过程日志,可删除 -q \n", "!pip install -q python-dotenv\n", - "!pip install -q openai" + "!pip install -q openai\n" ] }, { "cell_type": "code", - "execution_count": 2, + "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": [] }, @@ -52,11 +62,13 @@ "source": [ "import os\n", "import openai\n", - "# 运行此API配置,需要将目录中的.env中api_key替换为自己的\n", "from dotenv import load_dotenv, find_dotenv\n", - "_ = load_dotenv(find_dotenv()) # read local .env file\n", - "# 导入 OpenAI API_KEY\n", - "openai.api_key = os.environ['OPENAI_API_KEY']" + "\n", + "# 读取本地的环境变量 \n", + "_ = load_dotenv(find_dotenv())\n", + "\n", + "# 获取环境变量 OPENAI_API_KEY\n", + "openai.api_key = os.environ['OPENAI_API_KEY'] " ] }, { @@ -72,7 +84,7 @@ "\n", "`get_completion`函数是基于`openai`的封装函数,对于给定提示(prompt)输出相应的回答。其包含两个参数\n", " \n", - " - `prompt` 必需输入参数。 你给模型的提示,可以是一个问题,可以是你需要模型帮助你做的事(改变文本写作风格,翻译,回复消息等等)。\n", + " - `prompt` 必需输入参数。 你给模型的**提示,可以是一个问题,可以是你需要模型帮助你做的事**(改变文本写作风格,翻译,回复消息等等)。\n", " - `model` 非必需输入参数。默认使用gpt-3.5-turbo。你也可以选择其他模型。\n", " \n", "这里的提示对应我们给chatgpt的问题,函数给出的输出则对应chatpgt给我们的答案。" @@ -80,7 +92,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": { "tags": [] }, @@ -115,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -124,12 +136,12 @@ "'1+1等于2。'" ] }, - "execution_count": 5, "metadata": {}, - "output_type": "execute_result" + "output_type": "display_data" } ], "source": [ + "# 中文\n", "get_completion(\"1+1是什么?\")" ] }, @@ -152,6 +164,7 @@ } ], "source": [ + "# 英文\n", "get_completion(\"What is 1+1?\")" ] }, @@ -264,10 +277,8 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "tags": [] - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "response = get_completion(prompt)" @@ -309,6 +320,136 @@ "✨ 你可以尝试修改提示,看可以得到什么不一样的结果😉" ] }, + { + "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", @@ -318,18 +459,26 @@ "source": [ "## Chat API:LangChain\n", "\n", - "在前面一部分,我们通过封装函数`get_completion`直接调用了OpenAI完成了对海岛邮件进行了翻译,得到用平和尊重的语气、美式英语表达的邮件。\n", + "在前面一部分,我们通过封装函数`get_completion`直接调用了OpenAI完成了对方言邮件进行了的翻译,得到用平和尊重的语气、正式的普通话表达的邮件。\n", "\n", "让我们尝试使用LangChain来实现相同的功能。" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "^C\n" + ] + } + ], "source": [ "# 如果你需要查看安装过程日志,可删除 -q \n", "# --upgrade 让我们可以安装到最新版本的 langchain\n", @@ -350,7 +499,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 15, "metadata": { "tags": [] }, @@ -361,7 +510,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 13, "metadata": { "tags": [] }, @@ -369,10 +518,10 @@ { "data": { "text/plain": [ - "ChatOpenAI(verbose=False, callbacks=None, callback_manager=None, client=, model_name='gpt-3.5-turbo', temperature=0.0, model_kwargs={}, openai_api_key=None, openai_api_base=None, openai_organization=None, openai_proxy=None, request_timeout=None, max_retries=6, streaming=False, n=1, max_tokens=None)" + "ChatOpenAI(verbose=False, callbacks=None, callback_manager=None, client=, model_name='gpt-3.5-turbo', temperature=0.0, model_kwargs={}, openai_api_key=None, openai_api_base=None, openai_organization=None, request_timeout=None, max_retries=6, streaming=False, n=1, max_tokens=None)" ] }, - "execution_count": 7, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -416,7 +565,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### 📚 使用LongChain提示模版\n", + "#### 📚 使用LangChain提示模版\n", "##### 1️⃣ 构造提示模版字符串\n", "我们构造一个提示模版字符串:`template_string`" ] @@ -436,6 +585,19 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "template_string = \"\"\"把由三个反引号分隔的文本text\\\n", + "翻译成一种{style}风格。\\\n", + "text: ```{text}```\n", + "\"\"\"" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -447,7 +609,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 18, "metadata": { "tags": [] }, @@ -480,6 +642,27 @@ "prompt_template.messages[0].prompt" ] }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "PromptTemplate(input_variables=['style', 'text'], output_parser=None, partial_variables={}, template='把由三个反引号分隔的文本text翻译成一种{style}风格。text: ```{text}```\\n', template_format='f-string', validate_template=True)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 中文\n", + "prompt_template.messages[0].prompt" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -535,6 +718,18 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "customer_style = \"\"\"正式普通话 \\\n", + "用一个平静、尊敬的语气\n", + "\"\"\"" + ] + }, { "cell_type": "code", "execution_count": 12, @@ -553,6 +748,22 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "customer_email = \"\"\"\n", + "阿,我很生气,\\\n", + "因为我的搅拌机盖掉了,\\\n", + "把奶昔溅到了厨房的墙上!\\\n", + "更糟糕的是,保修不包括打扫厨房的费用。\\\n", + "我现在需要你的帮助,伙计!\n", + "\"\"\"" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -563,7 +774,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 22, "metadata": { "tags": [] }, @@ -624,6 +835,24 @@ "print(customer_messages[0])" ] }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "content='把由三个反引号分隔的文本text翻译成一种正式普通话 用一个平静、尊敬的语气\\n风格。text: ```\\n阿,我很生气,因为我的搅拌机盖掉了,把奶昔溅到了厨房的墙上!更糟糕的是,保修不包括打扫厨房的费用。我现在需要你的帮助,伙计!\\n```\\n' additional_kwargs={} example=False\n" + ] + } + ], + "source": [ + "# 中文\n", + "print(customer_messages[0])" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -636,7 +865,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 25, "metadata": { "tags": [] }, @@ -664,6 +893,23 @@ "print(customer_response.content)" ] }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "尊敬的伙计,我感到非常不安,因为我的搅拌机盖子不慎掉落,导致奶昔溅到了厨房的墙壁上!更加令人糟心的是,保修并不包括厨房清洁的费用。现在,我非常需要您的帮助!\n" + ] + } + ], + "source": [ + "print(customer_response.content)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -697,10 +943,24 @@ }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "tags": [] - }, + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "service_reply = \"\"\"嘿,顾客, \\\n", + "保修不包括厨房的清洁费用, \\\n", + "因为您在启动搅拌机之前 \\\n", + "忘记盖上盖子而误用搅拌机, \\\n", + "这是您的错。 \\\n", + "倒霉! 再见!\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "service_style_pirate = \"\"\"\\\n", @@ -709,6 +969,21 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# 中文\n", + "service_style_pirate = \"\"\"\\\n", + "一个有礼貌的语气 \\\n", + "使用正式的普通话\\\n", + "\"\"\"" + ] + }, { "cell_type": "code", "execution_count": 18, @@ -734,6 +1009,29 @@ "print(service_messages[0].content)" ] }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "把由三个反引号分隔的文本text翻译成一种一个有礼貌的语气 使用正式的普通话风格。text: ```嘿,顾客, 保修不包括厨房的清洁费用, 因为您在启动搅拌机之前 忘记盖上盖子而误用搅拌机, 这是您的错。 倒霉! 再见!\n", + "```\n", + "\n" + ] + } + ], + "source": [ + "service_messages = prompt_template.format_messages(\n", + " style=service_style_pirate,\n", + " text=service_reply)\n", + "\n", + "print(service_messages[0].content)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -764,6 +1062,24 @@ "print(service_response.content)" ] }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "尊敬的顾客,根据保修条款,厨房清洁费用不在保修范围内。这是因为在使用搅拌机之前,您忘记盖上盖子而导致误用搅拌机,这属于您的疏忽。非常抱歉给您带来困扰!祝您好运!再见!\n" + ] + } + ], + "source": [ + "service_response = chat(service_messages)\n", + "print(service_response.content)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -782,7 +1098,7 @@ "metadata": {}, "source": [ "\n", - "在应用于比较复杂的场景时,提示可能会非常长并且包含涉及许多细节。使用提示模版,可以让我们更为方便地重复使用设计好的提示。\n", + "在应用于比较复杂的场景时,提示可能会非常长并且包含涉及许多细节。**使用提示模版,可以让我们更为方便地重复使用设计好的提示**。\n", "\n", "下面给出了一个比较长的提示模版案例。学生们线上学习并提交作业,通过以下的提示来实现对学生的提交的作业的评分。\n", "\n", @@ -831,6 +1147,56 @@ " \"\"\"\n", "```\n", "\n", + "# 中文\n", + "\n", + "```python\n", + " prompt = \"\"\" 你的任务是判断学生的解决方案是正确的还是不正确的\n", + "\n", + " 要解决该问题,请执行以下操作:\n", + " - 首先,制定自己的问题解决方案\n", + " - 然后将您的解决方案与学生的解决方案进行比较\n", + " 并评估学生的解决方案是否正确。\n", + " ...\n", + " 使用下面的格式:\n", + "\n", + " 问题:\n", + " ```\n", + " 问题文本\n", + " ```\n", + " 学生的解决方案:\n", + " ```\n", + " 学生的解决方案文本\n", + " ```\n", + " 实际解决方案:\n", + " ```\n", + " ...\n", + " 制定解决方案的步骤以及您的解决方案请参见此处\n", + " ```\n", + " 学生的解决方案和实际解决方案是否相同 \\\n", + " 只计算:\n", + " ```\n", + " 是或者不是\n", + " ```\n", + " 学生的成绩\n", + " ```\n", + " 正确或者不正确\n", + " ```\n", + " \n", + " 问题:\n", + " ```\n", + " {question}\n", + " ```\n", + " 学生的解决方案:\n", + " ```\n", + " {student's solution}\n", + " ```\n", + " 实际解决方案:\n", + " \n", + " \"\"\"\n", + "```\n", + "\n", + "\n", + "\n", "此外,LangChain还提供了提示模版用于一些常用场景。比如summarization, Question answering, or connect to sql databases, or connect to different APIs. 通过使用LongChain内置的提示模版,你可以快速建立自己的大模型应用,而不需要花时间去设计和构造提示。\n", "\n", "最后,我们在建立大模型应用时,通常希望模型的输出为给定的格式,比如在输出使用特定的关键词来让输出结构化。 下面为一个使用大模型进行链式思考推理例子,对于问题:`What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?`, 通过使用LangChain库函数,输出采用\"Thought\"(思考)、\"Action\"(行动)、\"Observation\"(观察)作为链式思考推理的关键词,让输出结构化。在[补充材料](#reason_act)中,可以查看使用LangChain和OpenAI进行链式思考推理的另一个代码实例。\n", @@ -857,6 +1223,31 @@ "Action: Finish[1,800 to 7,000 ft]\n", "\n", "\"\"\"\n", + "```\n", + "\n", + "\n", + "```python\n", + "\"\"\"\n", + "想法:我需要搜索科罗拉多造山带,找到科罗拉多造山带东段延伸到的区域,然后找到该区域的高程范围。\n", + "行动:搜索[科罗拉多造山运动]\n", + "观察:科罗拉多造山运动是科罗拉多州及周边地区造山运动(造山运动)的一次事件。\n", + "\n", + "想法:它没有提到东区。 所以我需要查找东区。\n", + "行动:查找[东区]\n", + "观察:(结果1 / 1)东段延伸至高原,称为中原造山运动。\n", + "\n", + "想法:科罗拉多造山运动的东段延伸至高原。 所以我需要搜索高原并找到它的海拔范围。\n", + "行动:搜索[高地平原]\n", + "观察:高原是指两个不同的陆地区域之一\n", + "\n", + "想法:我需要搜索高地平原(美国)。\n", + "行动:搜索[高地平原(美国)]\n", + "观察:高地平原是大平原的一个分区。 从东到西,高原的海拔从 1,800 英尺左右上升到 7,000 英尺(550 到 2,130 米)。[3]\n", + "\n", + "想法:高原的海拔从大约 1,800 英尺上升到 7,000 英尺,所以答案是 1,800 到 7,000 英尺。\n", + "动作:完成[1,800 至 7,000 英尺]\n", + "\n", + "\"\"\"\n", "```\n" ] }, @@ -905,6 +1296,26 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "customer_review = \"\"\"\\\n", + "这款吹叶机非常神奇。 它有四个设置:\\\n", + "吹蜡烛、微风、风城、龙卷风。 \\\n", + "两天后就到了,正好赶上我妻子的\\\n", + "周年纪念礼物。 \\\n", + "我想我的妻子会喜欢它到说不出话来。 \\\n", + "到目前为止,我是唯一一个使用它的人,而且我一直\\\n", + "每隔一天早上用它来清理草坪上的叶子。 \\\n", + "它比其他吹叶机稍微贵一点,\\\n", + "但我认为它的额外功能是值得的。\n", + "\"\"\"" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -940,6 +1351,34 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "review_template = \"\"\"\\\n", + "对于以下文本,请从中提取以下信息:\n", + "\n", + "礼物:该商品是作为礼物送给别人的吗? \\\n", + "如果是,则回答 是的;如果否或未知,则回答 不是。\n", + "\n", + "交货天数:产品需要多少天\\\n", + "到达? 如果没有找到该信息,则输出-1。\n", + "\n", + "价钱:提取有关价值或价格的任何句子,\\\n", + "并将它们输出为逗号分隔的 Python 列表。\n", + "\n", + "使用以下键将输出格式化为 JSON:\n", + "礼物\n", + "交货天数\n", + "价钱\n", + "\n", + "文本: {text}\n", + "\"\"\"" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -967,6 +1406,25 @@ "print(prompt_template)" ] }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "input_variables=['text'] output_parser=None partial_variables={} messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['text'], output_parser=None, partial_variables={}, template='对于以下文本,请从中提取以下信息:\\n\\n礼物:该商品是作为礼物送给别人的吗? 如果是,则回答 是的;如果否或未知,则回答 不是。\\n\\n交货天数:产品需要多少天到达? 如果没有找到该信息,则输出-1。\\n\\n价钱:提取有关价值或价格的任何句子,并将它们输出为逗号分隔的 Python 列表。\\n\\n使用以下键将输出格式化为 JSON:\\n礼物\\n交货天数\\n价钱\\n\\n文本: {text}\\n', template_format='f-string', validate_template=True), additional_kwargs={})]\n" + ] + } + ], + "source": [ + "from langchain.prompts import ChatPromptTemplate\n", + "prompt_template = ChatPromptTemplate.from_template(review_template)\n", + "print(prompt_template)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -977,7 +1435,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ @@ -1015,6 +1473,29 @@ "print(response.content)" ] }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"礼物\": \"是的\",\n", + " \"交货天数\": 2,\n", + " \"价钱\": [\"它比其他吹叶机稍微贵一点\"]\n", + "}\n" + ] + } + ], + "source": [ + "chat = ChatOpenAI(temperature=0.0)\n", + "response = chat(messages)\n", + "print(response.content)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -1105,6 +1586,29 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "review_template_2 = \"\"\"\\\n", + "对于以下文本,请从中提取以下信息::\n", + "\n", + "礼物:该商品是作为礼物送给别人的吗?\n", + "如果是,则回答 是的;如果否或未知,则回答 不是。\n", + "\n", + "交货天数:产品到达需要多少天? 如果没有找到该信息,则输出-1。\n", + "\n", + "价钱:提取有关价值或价格的任何句子,并将它们输出为逗号分隔的 Python 列表。\n", + "\n", + "文本: {text}\n", + "\n", + "{format_instructions}\n", + "\"\"\"" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -1115,7 +1619,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ @@ -1174,6 +1678,56 @@ " price, and output them as a \\\n", " comma separated Python list.\")\n", "\n", + "\n", + "response_schemas = [gift_schema, \n", + " delivery_days_schema,\n", + " price_value_schema]\n", + "output_parser = StructuredOutputParser.from_response_schemas(response_schemas)\n", + "format_instructions = output_parser.get_format_instructions()\n", + "print(format_instructions)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The output should be a markdown code snippet formatted in the following schema, including the leading and trailing \"\\`\\`\\`json\" and \"\\`\\`\\`\":\n", + "\n", + "```json\n", + "{\n", + "\t\"礼物\": string // 这件物品是作为礼物送给别人的吗? 如果是,则回答 是的, 如果否或未知,则回答 不是。\n", + "\t\"交货天数\": string // 产品需要多少天才能到达? 如果没有找到该信息,则输出-1。\n", + "\t\"价钱\": string // 提取有关价值或价格的任何句子, 并将它们输出为逗号分隔的 Python 列表\n", + "}\n", + "```\n" + ] + } + ], + "source": [ + "# 中文\n", + "from langchain.output_parsers import ResponseSchema\n", + "from langchain.output_parsers import StructuredOutputParser\n", + "\n", + "gift_schema = ResponseSchema(name=\"礼物\",\n", + " description=\"这件物品是作为礼物送给别人的吗?\\\n", + " 如果是,则回答 是的,\\\n", + " 如果否或未知,则回答 不是。\")\n", + "\n", + "delivery_days_schema = ResponseSchema(name=\"交货天数\",\n", + " description=\"产品需要多少天才能到达?\\\n", + " 如果没有找到该信息,则输出-1。\")\n", + "\n", + "price_value_schema = ResponseSchema(name=\"价钱\",\n", + " description=\"提取有关价值或价格的任何句子,\\\n", + " 并将它们输出为逗号分隔的 Python 列表\")\n", + "\n", + "\n", "response_schemas = [gift_schema, \n", " delivery_days_schema,\n", " price_value_schema]\n", @@ -1192,7 +1746,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ @@ -1236,6 +1790,45 @@ "print(messages[0].content)" ] }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "对于以下文本,请从中提取以下信息::\n", + "\n", + "礼物:该商品是作为礼物送给别人的吗?\n", + "如果是,则回答 是的;如果否或未知,则回答 不是。\n", + "\n", + "交货天数:产品到达需要多少天? 如果没有找到该信息,则输出-1。\n", + "\n", + "价钱:提取有关价值或价格的任何句子,并将它们输出为逗号分隔的 Python 列表。\n", + "\n", + "文本: 这款吹叶机非常神奇。 它有四个设置:吹蜡烛、微风、风城、龙卷风。 两天后就到了,正好赶上我妻子的周年纪念礼物。 我想我的妻子会喜欢它到说不出话来。 到目前为止,我是唯一一个使用它的人,而且我一直每隔一天早上用它来清理草坪上的叶子。 它比其他吹叶机稍微贵一点,但我认为它的额外功能是值得的。\n", + "\n", + "\n", + "The output should be a markdown code snippet formatted in the following schema, including the leading and trailing \"\\`\\`\\`json\" and \"\\`\\`\\`\":\n", + "\n", + "```json\n", + "{\n", + "\t\"礼物\": string // 这件物品是作为礼物送给别人的吗? 如果是,则回答 是的, 如果否或未知,则回答 不是。\n", + "\t\"交货天数\": string // 产品需要多少天才能到达? 如果没有找到该信息,则输出-1。\n", + "\t\"价钱\": string // 提取有关价值或价格的任何句子, 并将它们输出为逗号分隔的 Python 列表\n", + "}\n", + "```\n", + "\n" + ] + } + ], + "source": [ + "# 中文\n", + "print(messages[0].content)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -1268,6 +1861,31 @@ "print(response.content)" ] }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "```json\n", + "{\n", + "\t\"礼物\": \"不是\",\n", + "\t\"交货天数\": \"两天后就到了\",\n", + "\t\"价钱\": \"它比其他吹叶机稍微贵一点\"\n", + "}\n", + "```\n" + ] + } + ], + "source": [ + "# 中文\n", + "response = chat(messages)\n", + "print(response.content)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -1299,6 +1917,27 @@ "output_dict" ] }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'礼物': '不是', '交货天数': '两天后就到了', '价钱': '它比其他吹叶机稍微贵一点'}" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "output_dict = output_parser.parse(response.content)\n", + "output_dict" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -1376,9 +2015,99 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3mThought: I need to search David Chanoff and find the U.S. Navy admiral he collaborated with. Then I need to find the president under whom the admiral served as the ambassador to the United Kingdom.\n", + "Action: Search[David Chanoff]\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mDavid Chanoff is a noted author of non-fiction work. His work has typically involved collaborations with the principal protagonist of the work concerned. His collaborators have included; Augustus A. White, Joycelyn Elders, Đoàn Văn Toại, William J. Crowe, Ariel Sharon, Kenneth Good and Felix Zandman. He has also written about a wide range of subjects including literary history, education and foreign for The Washington Post, The New Republic and The New York Times Magazine. He has published more than twelve books.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mDavid Chanoff has collaborated with several individuals, including Augustus A. White, Joycelyn Elders, Đoàn Văn Toại, William J. Crowe, Ariel Sharon, Kenneth Good, and Felix Zandman. I need to search each of these individuals to find the U.S. Navy admiral. \n", + "Action: Search[Augustus A. White]\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mAugustus A. White III (born June 4, 1936) is the Ellen and Melvin Gordon Distinguished Professor of Medical Education and Professor of Orthopedic Surgery at Harvard Medical School and a former Orthopaedic Surgeon-in-Chief at Beth Israel Hospital, Boston, Massachusetts. White was the first African American medical student at Stanford, surgical resident at Yale University, professor of medicine at Yale, and department head at a Harvard-affiliated hospital (Beth Israel Hospital).\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mAugustus A. White III is not a U.S. Navy admiral. I need to search the next individual, Joycelyn Elders.\n", + "Action: Search[Joycelyn Elders]\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mMinnie Joycelyn Elders (born Minnie Lee Jones; August 13, 1933) is an American pediatrician and public health administrator who served as Surgeon General of the United States from 1993 to 1994. A vice admiral in the Public Health Service Commissioned Corps, she was the second woman, second person of color, and first African American to serve as Surgeon General. \n", + "Elders is best known for her frank discussion of her views on controversial issues such as drug legalization, masturbation, and distributing contraception in schools. She was forced to resign in December 1994 amidst controversy as a result of her views. She is currently a professor emerita of pediatrics at the University of Arkansas for Medical Sciences.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mJoycelyn Elders is a pediatrician and public health administrator, not a U.S. Navy admiral. I need to search the next individual, Đoàn Văn Toại.\n", + "Action: Search[Đoàn Văn Toại]\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mĐoàn Văn Toại (1945 in Vietnam – November 2017 in California) was a Vietnamese-born naturalized American activist and the author of The Vietnamese Gulag (Simon & Schuster, 1986).\u001b[0m\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.llms.openai.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.llms.openai.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.llms.openai.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mĐoàn Văn Toại is an activist and author, not a U.S. Navy admiral. I need to search the next individual, William J. Crowe.\n", + "Action: Search[William J. Crowe]\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mWilliam James Crowe Jr. (January 2, 1925 – October 18, 2007) was a United States Navy admiral and diplomat who served as the 11th chairman of the Joint Chiefs of Staff under Presidents Ronald Reagan and George H. W. Bush, and as the ambassador to the United Kingdom and Chair of the Intelligence Oversight Board under President Bill Clinton.\u001b[0m\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.llms.openai.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.llms.openai.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.llms.openai.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mWilliam J. Crowe is a U.S. Navy admiral and diplomat. I need to find the president under whom he served as the ambassador to the United Kingdom.\n", + "Action: Search[William J. Crowe ambassador to United Kingdom]\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mWilliam James Crowe Jr. (January 2, 1925 – October 18, 2007) was a United States Navy admiral and diplomat who served as the 11th chairman of the Joint Chiefs of Staff under Presidents Ronald Reagan and George H. W. Bush, and as the ambassador to the United Kingdom and Chair of the Intelligence Oversight Board under President Bill Clinton.\u001b[0m\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.llms.openai.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.llms.openai.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mWilliam J. Crowe served as the ambassador to the United Kingdom under President Bill Clinton. So the answer is Bill Clinton.\n", + "Action: Finish[Bill Clinton]\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'Bill Clinton'" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from langchain.docstore.wikipedia import Wikipedia\n", "from langchain.llms import OpenAI\n", @@ -1417,6 +2146,13 @@ "question = \"Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?\"\n", "agent_executor.run(question)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -1435,7 +2171,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/content/LangChain for LLM Application Development/3.存储 .ipynb b/content/LangChain for LLM Application Development/3.存储.ipynb similarity index 97% rename from content/LangChain for LLM Application Development/3.存储 .ipynb rename to content/LangChain for LLM Application Development/3.存储.ipynb index 72f5059..305bc33 100644 --- a/content/LangChain for LLM Application Development/3.存储 .ipynb +++ b/content/LangChain for LLM Application Development/3.存储.ipynb @@ -1,12 +1,14 @@ { "cells": [ { - "attachments": {}, "cell_type": "markdown", "id": "a786c77c", "metadata": {}, "source": [ - "# LangChain: Memory(记忆)\n", + "# 3. 储存\n", + "\n", + "\n", + "\n", "当你与那些语言模型进行交互的时候,他们不会记得你之前和他进行的交流内容,这在我们构建一些应用程序(如聊天机器人)的时候,是一个很大的问题---显得不够智能!\n", "\n", "因此,在本节中我们将介绍LangChain 中的 **Memory(记忆)** 模块,即他是如何将先前的对话嵌入到语言模型中的,使其具有连续对话的能力\n", @@ -29,7 +31,7 @@ " \n", "此次课程主要介绍其中四种记忆模块,其他模块可查看文档学习。\n", "\n", - "## 大纲\n", + "\n", "* ConversationBufferMemory(对话缓存记忆)\n", "* ConversationBufferWindowMemory(对话缓存窗口记忆)\n", "* ConversationTokenBufferMemory(对话令牌缓存记忆)\n", @@ -37,7 +39,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "7e10db6f", "metadata": {}, @@ -48,18 +49,16 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "1297dcd5", "metadata": {}, "source": [ - "## ConversationBufferMemory(对话缓存记忆) \n", + "## 3.1 对话缓存储存 \n", " \n", "这种记忆允许存储消息,然后从变量中提取消息。" ] }, { - "attachments": {}, "cell_type": "markdown", "id": "0768ca9b", "metadata": {}, @@ -68,7 +67,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "d76f6ba7", "metadata": {}, @@ -133,7 +131,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "dea83837", "metadata": {}, @@ -142,7 +139,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "1a3b4c42", "metadata": {}, @@ -232,7 +228,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "e71564ad", "metadata": {}, @@ -241,7 +236,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "54d006bd", "metadata": {}, @@ -345,7 +339,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "33cb734b", "metadata": {}, @@ -354,7 +347,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "0393df3d", "metadata": {}, @@ -462,7 +454,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "5a96a8d9", "metadata": {}, @@ -532,7 +523,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "6bd222c3", "metadata": {}, @@ -541,7 +531,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "0b5de846", "metadata": {}, @@ -595,7 +584,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "07d2e892", "metadata": {}, @@ -698,7 +686,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "2ac544f2", "metadata": {}, @@ -767,7 +754,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "8839314a", "metadata": {}, @@ -778,12 +764,11 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "cf98e9ff", "metadata": {}, "source": [ - "## ConversationBufferWindowMemory(对话缓存窗口记忆)\n", + "## 3.2 对话缓存窗口储存\n", " \n", "随着对话变得越来越长,所需的内存量也变得非常长。将大量的tokens发送到LLM的成本,也会变得更加昂贵,这也就是为什么API的调用费用,通常是基于它需要处理的tokens数量而收费的。\n", " \n", @@ -804,7 +789,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "641477a4", "metadata": {}, @@ -863,7 +847,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "9b401f0b", "metadata": {}, @@ -899,7 +882,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "63bda148", "metadata": {}, @@ -927,7 +909,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "b6d661e3", "metadata": {}, @@ -1005,7 +986,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "a1080168", "metadata": {}, @@ -1038,16 +1018,14 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "d2931b92", "metadata": {}, "source": [ - "## ConversationTokenBufferMemory(对话token缓存记忆)" + "## 3.3 对话token缓存储存" ] }, { - "attachments": {}, "cell_type": "markdown", "id": "dff5b4c7", "metadata": {}, @@ -1069,7 +1047,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "2187cfe6", "metadata": {}, @@ -1094,7 +1071,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "f3a84112", "metadata": {}, @@ -1121,7 +1097,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "7b62b2e1", "metadata": {}, @@ -1153,7 +1128,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "f7f6be43", "metadata": {}, @@ -1188,7 +1162,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "5e4d918b", "metadata": {}, @@ -1204,16 +1177,14 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "5ff55d5d", "metadata": {}, "source": [ - "## ConversationSummaryBufferMemory(对话摘要缓存记忆)" + "## 3.4 对话摘要缓存储存" ] }, { - "attachments": {}, "cell_type": "markdown", "id": "7d39b83a", "metadata": {}, @@ -1247,7 +1218,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "6572ef39", "metadata": {}, @@ -1305,7 +1275,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "7ccb97b6", "metadata": {}, @@ -1396,7 +1365,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "4ba827aa", "metadata": { @@ -1530,7 +1498,20 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.9.12" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true } }, "nbformat": 4, diff --git a/content/LangChain for LLM Application Development/4.模型链.ipynb b/content/LangChain for LLM Application Development/4.模型链.ipynb index 4eadf48..7477286 100644 --- a/content/LangChain for LLM Application Development/4.模型链.ipynb +++ b/content/LangChain for LLM Application Development/4.模型链.ipynb @@ -29,7 +29,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 44, "id": "541eb2f1", "metadata": {}, "outputs": [], @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 45, "id": "b7ed03ed-1322-49e3-b2a2-33e94fb592ef", "metadata": { "tags": [] @@ -48,14 +48,18 @@ "outputs": [], "source": [ "import os\n", - "\n", + "import openai\n", "from dotenv import load_dotenv, find_dotenv\n", - "_ = load_dotenv(find_dotenv()) # 读取本地 .env 文件" + "\n", + "_ = load_dotenv(find_dotenv()) # 读取本地 .env 文件\n", + "\n", + "# 获取环境变量 OPENAI_API_KEY\n", + "openai.api_key = os.environ['OPENAI_API_KEY'] #\"填入你的专属的API key\"" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "b84e441b", "metadata": {}, "outputs": [], @@ -74,7 +78,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 46, "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", "metadata": {}, "outputs": [], @@ -85,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 47, "id": "b7a09c35", "metadata": {}, "outputs": [ @@ -153,7 +157,7 @@ "4 Milk Frother Handheld\\n  I loved this product. But they only seem to l..." ] }, - "execution_count": 11, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -182,7 +186,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 48, "id": "e92dff22", "metadata": {}, "outputs": [], @@ -203,13 +207,12 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 9, "id": "943237a7", "metadata": {}, "outputs": [], "source": [ - "OPENAI_API_KEY = \"********\" #\"填入你的专属的API key\"\n", - "llm = ChatOpenAI(temperature=0.0,openai_api_key=OPENAI_API_KEY) #预测下一个token时,概率越大的值就越平滑(平滑也就是让差异大的值之间的差异变得没那么大),temperature值越小则生成的内容越稳定" + "llm = ChatOpenAI(temperature=0.0) #预测下一个token时,概率越大的值就越平滑(平滑也就是让差异大的值之间的差异变得没那么大),temperature值越小则生成的内容越稳定" ] }, { @@ -295,17 +298,17 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 14, "id": "2181be10", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'蓝耳科技 (BlueEar Technologies)'" + "'\"豪华床纺\"'" ] }, - "execution_count": 12, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -313,10 +316,10 @@ "source": [ "# 中文\n", "prompt = ChatPromptTemplate.from_template( \n", - " \"描述制造{product}的公司的最佳名称是什么?\"\n", + " \"描述制造{product}的一个公司的最佳名称是什么?\"\n", ")\n", "chain = LLMChain(llm=llm, prompt=prompt)\n", - "product = \"蓝牙耳机\"\n", + "product = \"大号床单套装\"\n", "chain.run(product)" ] }, @@ -342,7 +345,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 15, "id": "febee243", "metadata": {}, "outputs": [], @@ -352,13 +355,12 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 16, "id": "5d019d6f", "metadata": {}, "outputs": [], "source": [ - "OPENAI_API_KEY = \"********\" #\"填入你的专属的API key\"\n", - "llm = ChatOpenAI(temperature=0.9,openai_api_key=OPENAI_API_KEY)" + "llm = ChatOpenAI(temperature=0.9)" ] }, { @@ -481,7 +483,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 19, "id": "c7c32997", "metadata": {}, "outputs": [ @@ -492,8 +494,8 @@ "\n", "\n", "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", - "\u001b[36;1m\u001b[1;3m“蓝牙耳机制造公司”或“蓝耳制造公司”均可。具体名称应根据公司的定位、目标市场、品牌理念等因素来考虑。\u001b[0m\n", - "\u001b[33;1m\u001b[1;3m专业生产蓝牙耳机的公司,致力于提供高品质、时尚设计、舒适佩戴的产品,满足用户多样化的需求。\u001b[0m\n", + "\u001b[36;1m\u001b[1;3m\"尺寸王床品有限公司\"\u001b[0m\n", + "\u001b[33;1m\u001b[1;3m尺寸王床品有限公司是一家专注于床上用品生产的公司。\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -501,18 +503,19 @@ { "data": { "text/plain": [ - "'专业生产蓝牙耳机的公司,致力于提供高品质、时尚设计、舒适佩戴的产品,满足用户多样化的需求。'" + "'尺寸王床品有限公司是一家专注于床上用品生产的公司。'" ] }, - "execution_count": 23, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 中文\n", + "\n", "first_prompt = ChatPromptTemplate.from_template( \n", - " \"描述制造{product}的公司的最佳名称是什么?\"\n", + " \"描述制造{product}的一个公司的最好的名称是什么\"\n", ")\n", "chain_one = LLMChain(llm=llm, prompt=first_prompt)\n", "\n", @@ -522,10 +525,11 @@ ")\n", "chain_two = LLMChain(llm=llm, prompt=second_prompt)\n", "\n", + "\n", "overall_simple_chain = SimpleSequentialChain(chains=[chain_one, chain_two],\n", " verbose=True\n", " )\n", - "product = \"蓝牙耳机\"\n", + "product = \"大号床单套装\"\n", "overall_simple_chain.run(product)" ] }, @@ -551,7 +555,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "4c129ef6", "metadata": {}, "outputs": [], @@ -573,13 +577,12 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "id": "03a8e203", "metadata": {}, "outputs": [], "source": [ - "OPENAI_API_KEY = \"********\" #\"填入你的专属的API key\"\n", - "llm = ChatOpenAI(temperature=0.9,openai_api_key=OPENAI_API_KEY)" + "llm = ChatOpenAI(temperature=0.9)" ] }, { @@ -593,7 +596,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 23, "id": "016187ac", "metadata": {}, "outputs": [], @@ -613,7 +616,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 25, "id": "0fb0730e", "metadata": {}, "outputs": [], @@ -633,7 +636,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 26, "id": "6accf92d", "metadata": {}, "outputs": [], @@ -652,7 +655,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 27, "id": "c7a46121", "metadata": {}, "outputs": [], @@ -673,7 +676,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 28, "id": "89603117", "metadata": {}, "outputs": [], @@ -750,6 +753,121 @@ "overall_chain(review)" ] }, + { + "cell_type": "code", + "execution_count": 9, + "id": "31624a7c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "{'Review': \"Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !?\",\n", + " 'English_Review': \"I find the taste mediocre. The foam doesn't last, it's strange. I buy the same ones in stores and the taste is much better...\\nOld batch or counterfeit?!\",\n", + " 'summary': \"The reviewer finds the taste mediocre and suspects that either the product is from an old batch or it might be counterfeit, as the foam doesn't last and the taste is not as good as the ones purchased in stores.\",\n", + " 'followup_message': \"后续回复: Bonjour! Je vous remercie de votre commentaire concernant notre produit. Nous sommes désolés d'apprendre que vous avez trouvé le goût moyen et que vous soupçonnez qu'il s'agit peut-être d'un ancien lot ou d'une contrefaçon, car la mousse ne dure pas et le goût n'est pas aussi bon que ceux achetés en magasin. Nous apprécions vos préoccupations et nous aimerions enquêter davantage sur cette situation. Pourriez-vous s'il vous plaît nous fournir plus de détails, tels que la date d'achat et le numéro de lot du produit? Nous sommes déterminés à offrir la meilleure qualité à nos clients et nous ferons tout notre possible pour résoudre ce problème. Merci encore pour votre commentaire et nous attendons votre réponse avec impatience. Cordialement.\"}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 中文\n", + "\n", + "#子链1\n", + "\n", + "# prompt模板 1: 翻译成英语(把下面的review翻译成英语)\n", + "first_prompt = ChatPromptTemplate.from_template(\n", + " \"把下面的评论review翻译成英文:\"\n", + " \"\\n\\n{Review}\"\n", + ")\n", + "# chain 1: 输入:Review 输出:英文的 Review\n", + "chain_one = LLMChain(llm=llm, prompt=first_prompt, \n", + " output_key=\"English_Review\"\n", + " )\n", + "\n", + "#子链2\n", + "\n", + "# prompt模板 2: 用一句话总结下面的 review\n", + "second_prompt = ChatPromptTemplate.from_template(\n", + " \"请你用一句话来总结下面的评论review:\"\n", + " \"\\n\\n{English_Review}\"\n", + ")\n", + "# chain 2: 输入:英文的Review 输出:总结\n", + "chain_two = LLMChain(llm=llm, prompt=second_prompt, \n", + " output_key=\"summary\"\n", + " )\n", + "\n", + "\n", + "#子链3\n", + "\n", + "# prompt模板 3: 下面review使用的什么语言\n", + "third_prompt = ChatPromptTemplate.from_template(\n", + " \"下面的评论review使用的什么语言:\\n\\n{Review}\"\n", + ")\n", + "# chain 3: 输入:Review 输出:语言\n", + "chain_three = LLMChain(llm=llm, prompt=third_prompt,\n", + " output_key=\"language\"\n", + " )\n", + "\n", + "\n", + "#子链4\n", + "\n", + "# prompt模板 4: 使用特定的语言对下面的总结写一个后续回复\n", + "fourth_prompt = ChatPromptTemplate.from_template(\n", + " \"使用特定的语言对下面的总结写一个后续回复:\"\n", + " \"\\n\\n总结: {summary}\\n\\n语言: {language}\"\n", + ")\n", + "# chain 4: 输入: 总结, 语言 输出: 后续回复\n", + "chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n", + " output_key=\"followup_message\"\n", + " )\n", + "\n", + "\n", + "# 对四个子链进行组合\n", + "\n", + "#输入:review 输出:英文review,总结,后续回复 \n", + "overall_chain = SequentialChain(\n", + " chains=[chain_one, chain_two, chain_three, chain_four],\n", + " input_variables=[\"Review\"],\n", + " output_variables=[\"English_Review\", \"summary\",\"followup_message\"],\n", + " verbose=True\n", + ")\n", + "\n", + "\n", + "review = df.Review[5]\n", + "overall_chain(review)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -788,7 +906,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 49, "id": "ade83f4f", "metadata": {}, "outputs": [], @@ -845,6 +963,59 @@ "{input}\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 51, + "id": "f7fade7a", + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "#第一个提示适合回答物理问题\n", + "physics_template = \"\"\"你是一个非常聪明的物理专家。 \\\n", + "你擅长用一种简洁并且易于理解的方式去回答问题。\\\n", + "当你不知道问题的答案时,你承认\\\n", + "你不知道.\n", + "\n", + "这是一个问题:\n", + "{input}\"\"\"\n", + "\n", + "\n", + "#第二个提示适合回答数学问题\n", + "math_template = \"\"\"你是一个非常优秀的数学家。 \\\n", + "你擅长回答数学问题。 \\\n", + "你之所以如此优秀, \\\n", + "是因为你能够将棘手的问题分解为组成部分,\\\n", + "回答组成部分,然后将它们组合在一起,回答更广泛的问题。\n", + "\n", + "这是一个问题:\n", + "{input}\"\"\"\n", + "\n", + "\n", + "#第三个适合回答历史问题\n", + "history_template = \"\"\"你是以为非常优秀的历史学家。 \\\n", + "你对一系列历史时期的人物、事件和背景有着极好的学识和理解\\\n", + "你有能力思考、反思、辩证、讨论和评估过去。\\\n", + "你尊重历史证据,并有能力利用它来支持你的解释和判断。\n", + "\n", + "这是一个问题:\n", + "{input}\"\"\"\n", + "\n", + "\n", + "#第四个适合回答计算机问题\n", + "computerscience_template = \"\"\" 你是一个成功的计算机科学专家。\\\n", + "你有创造力、协作精神、\\\n", + "前瞻性思维、自信、解决问题的能力、\\\n", + "对理论和算法的理解以及出色的沟通技巧。\\\n", + "你非常擅长回答编程问题。\\\n", + "你之所以如此优秀,是因为你知道 \\\n", + "如何通过以机器可以轻松解释的命令式步骤描述解决方案来解决问题,\\\n", + "并且你知道如何选择在时间复杂性和空间复杂性之间取得良好平衡的解决方案。\n", + "\n", + "这还是一个输入:\n", + "{input}\"\"\"" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -856,8 +1027,7 @@ }, { "cell_type": "code", - "execution_count": 32, - "id": "5f590e9f", + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -885,6 +1055,38 @@ "]" ] }, + { + "cell_type": "code", + "execution_count": 52, + "id": "deb8aafc", + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "prompt_infos = [\n", + " {\n", + " \"名字\": \"物理学\", \n", + " \"描述\": \"擅长回答关于物理学的问题\", \n", + " \"提示模板\": physics_template\n", + " },\n", + " {\n", + " \"名字\": \"数学\", \n", + " \"描述\": \"擅长回答数学问题\", \n", + " \"提示模板\": math_template\n", + " },\n", + " {\n", + " \"名字\": \"历史\", \n", + " \"描述\": \"擅长回答历史问题\", \n", + " \"提示模板\": history_template\n", + " },\n", + " {\n", + " \"名字\": \"计算机科学\", \n", + " \"描述\": \"擅长回答计算机科学问题\", \n", + " \"提示模板\": computerscience_template\n", + " }\n", + "]\n" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -896,7 +1098,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 28, "id": "31b06fc8", "metadata": {}, "outputs": [], @@ -917,13 +1119,12 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 29, "id": "f3f50bcc", "metadata": {}, "outputs": [], "source": [ - "OPENAI_API_KEY = \"********\" #\"填入你的专属的API key\"\n", - "llm = ChatOpenAI(temperature=0,openai_api_key=OPENAI_API_KEY)" + "llm = ChatOpenAI(temperature=0)" ] }, { @@ -953,12 +1154,11 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 30, "id": "8eefec24", "metadata": {}, "outputs": [], "source": [ - "\n", "destination_chains = {}\n", "for p_info in prompt_infos:\n", " name = p_info[\"name\"]\n", @@ -971,6 +1171,26 @@ "destinations_str = \"\\n\".join(destinations)" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "fd6eb641", + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "destination_chains = {}\n", + "for p_info in prompt_infos:\n", + " name = p_info[\"名字\"]\n", + " prompt_template = p_info[\"提示模板\"]\n", + " prompt = ChatPromptTemplate.from_template(template=prompt_template)\n", + " chain = LLMChain(llm=llm, prompt=prompt)\n", + " destination_chains[name] = chain \n", + " \n", + "destinations = [f\"{p['名字']}: {p['描述']}\" for p in prompt_infos]\n", + "destinations_str = \"\\n\".join(destinations)" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -983,7 +1203,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 31, "id": "9f98018a", "metadata": {}, "outputs": [], @@ -1002,9 +1222,28 @@ "这包括要完成的任务的说明以及输出应该采用的特定格式。" ] }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "24f30c2c", + "metadata": {}, + "source": [ + "注意:此处在原教程的基础上添加了一个示例,主要是因为\"gpt-3.5-turbo\"模型不能很好适应理解模板的意思,使用 \"text-davinci-003\" 或者\"gpt-4-0613\"可以很好的工作,因此在这里多加了示例提示让其更好的学习。\n", + "eg:\n", + "<< INPUT >>\n", + "\"What is black body radiation?\"\n", + "<< OUTPUT >>\n", + "```json\n", + "{{{{\n", + " \"destination\": string \\ name of the prompt to use or \"DEFAULT\"\n", + " \"next_inputs\": string \\ a potentially modified version of the original input\n", + "}}}}\n", + "```" + ] + }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 32, "id": "11b2e2ba", "metadata": {}, "outputs": [], @@ -1037,7 +1276,75 @@ "<< INPUT >>\n", "{{input}}\n", "\n", - "<< OUTPUT (remember to include the ```json)>>\"\"\"" + "<< OUTPUT (remember to include the ```json)>>\n", + "\n", + "eg:\n", + "<< INPUT >>\n", + "\"What is black body radiation?\"\n", + "<< OUTPUT >>\n", + "```json\n", + "{{{{\n", + " \"destination\": string \\ name of the prompt to use or \"DEFAULT\"\n", + " \"next_inputs\": string \\ a potentially modified version of the original input\n", + "}}}}\n", + "```\n", + "\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a7aae035", + "metadata": {}, + "outputs": [], + "source": [ + "# 中文\n", + "\n", + "# 多提示路由模板\n", + "MULTI_PROMPT_ROUTER_TEMPLATE = \"\"\"给语言模型一个原始文本输入,\\\n", + "让其选择最适合输入的模型提示。\\\n", + "系统将为您提供可用提示的名称以及最适合改提示的描述。\\\n", + "如果你认为修改原始输入最终会导致语言模型做出更好的响应,\\\n", + "你也可以修改原始输入。\n", + "\n", + "\n", + "<< 格式 >>\n", + "返回一个带有JSON对象的markdown代码片段,该JSON对象的格式如下:\n", + "```json\n", + "{{{{\n", + " \"destination\": 字符串 \\ 使用的提示名字或者使用 \"DEFAULT\"\n", + " \"next_inputs\": 字符串 \\ 原始输入的改进版本\n", + "}}}}\n", + "```\n", + "\n", + "\n", + "记住:“destination”必须是下面指定的候选提示名称之一,\\\n", + "或者如果输入不太适合任何候选提示,\\\n", + "则可以是 “DEFAULT” 。\n", + "记住:如果您认为不需要任何修改,\\\n", + "则 “next_inputs” 可以只是原始输入。\n", + "\n", + "<< 候选提示 >>\n", + "{destinations}\n", + "\n", + "<< 输入 >>\n", + "{{input}}\n", + "\n", + "<< 输出 (记得要包含 ```json)>>\n", + "\n", + "样例:\n", + "<< 输入 >>\n", + "\"什么是黑体辐射?\"\n", + "<< 输出 >>\n", + "```json\n", + "{{{{\n", + " \"destination\": 字符串 \\ 使用的提示名字或者使用 \"DEFAULT\"\n", + " \"next_inputs\": 字符串 \\ 原始输入的改进版本\n", + "}}}}\n", + "```\n", + "\n", + "\"\"\"" ] }, { @@ -1057,7 +1364,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 34, "id": "1387109d", "metadata": {}, "outputs": [], @@ -1085,7 +1392,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 35, "id": "2fb7d560", "metadata": {}, "outputs": [], @@ -1118,8 +1425,8 @@ }, { "cell_type": "code", - "execution_count": 21, - "id": "d86b2131", + "execution_count": null, + "id": "2217d987", "metadata": {}, "outputs": [ { @@ -1139,9 +1446,8 @@ "'Black body radiation is the electromagnetic radiation emitted by a perfect black body, which absorbs all incident radiation and reflects none. It is characterized by a continuous spectrum of radiated energy that is dependent on the temperature of the body, with higher temperatures leading to more intense and shorter wavelength radiation. This phenomenon is an important concept in thermal physics and has numerous applications, ranging from understanding stellar spectra to designing artificial light sources.'" ] }, - "execution_count": 21, "metadata": {}, - "output_type": "execute_result" + "output_type": "display_data" } ], "source": [ @@ -1151,7 +1457,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 36, "id": "4446724c", "metadata": {}, "outputs": [ @@ -1162,24 +1468,57 @@ "\n", "\n", "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", - "physics: {'input': 'What is a black hole made of?'}\n", + "physics: {'input': '什么是黑体辐射?'}\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'A black hole is not made of any material substance that we know of. It is formed by the collapse of a massive object, such as a star, into an infinitely small point called a singularity. This singularity has infinite density and gravity, and is surrounded by an event horizon, which is the point of no return for anything that gets too close to the black hole. So, in essence, a black hole is made of nothing but gravity.'" + "'黑体辐射是指一个理想化的物体,它能够完全吸收所有入射到它表面的辐射能量,并以热辐射的形式重新发射出来。黑体辐射的特点是其辐射能量的分布与温度有关,随着温度的升高,辐射能量的峰值会向更短的波长方向移动。这个现象被称为黑体辐射谱的位移定律,由普朗克在20世纪初提出。黑体辐射在研究热力学、量子力学和宇宙学等领域中具有重要的应用。'" ] }, - "execution_count": 24, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#中文\n", - "chain.run(\"黑洞是种什么物质?\")" + "chain.run(\"什么是黑体辐射?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "ef81eda3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "History: {'input': '你知道李白是谁嘛?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'李白是唐朝时期的一位著名诗人。他的诗歌以豪放、奔放、自由的风格著称,被誉为“诗仙”。他的作品涉及广泛,包括山水田园、历史传说、哲理思考等多个方面,对中国古典文学的发展产生了深远的影响。'" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 中文\n", + "chain.run(\"你知道李白是谁嘛?\")" ] }, { @@ -1224,6 +1563,53 @@ "chain.run(\"what is 2 + 2\")" ] }, + { + "cell_type": "code", + "execution_count": 37, + "id": "795bea17", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "math: {'input': '2 + 2 等于多少'}" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised ServiceUnavailableError: The server is overloaded or not ready yet..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'2 + 2 等于 4。'" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 中文\n", + "chain.run(\"2 + 2 等于多少\")" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -1270,8 +1656,8 @@ }, { "cell_type": "code", - "execution_count": 41, - "id": "ef81eda3", + "execution_count": 38, + "id": "a64d0759", "metadata": {}, "outputs": [ { @@ -1281,24 +1667,24 @@ "\n", "\n", "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", - "History: {'input': '你知道李白是谁嘛?'}\n", + "None: {'input': '为什么我们身体里的每个细胞都包含DNA?'}\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'李白是唐朝时期的一位著名诗人。他的诗歌以豪放、奔放、自由的风格著称,被誉为“诗仙”。他的作品涉及广泛,包括山水田园、历史传说、哲理思考等多个方面,对中国古典文学的发展产生了深远的影响。'" + "'我们身体里的每个细胞都包含DNA,是因为DNA是遗传信息的载体。DNA是由四种碱基(腺嘌呤、鸟嘌呤、胸腺嘧啶和鳞嘌呤)组成的长链状分子,它存储了生物体的遗传信息,包括个体的特征、生长发育、代谢功能等。每个细胞都需要这些遗传信息来执行其特定的功能和任务,因此每个细胞都需要包含DNA。此外,DNA还能通过复制和传递给下一代细胞和个体,以保证遗传信息的传承。'" ] }, - "execution_count": 41, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 中文\n", - "chain.run(\"你知道李白是谁嘛?\")" + "chain.run(\"为什么我们身体里的每个细胞都包含DNA?\")" ] } ], @@ -1318,7 +1704,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/content/LangChain for LLM Application Development/5.文档问答.ipynb b/content/LangChain for LLM Application Development/5.基于文档的问答.ipynb similarity index 94% rename from content/LangChain for LLM Application Development/5.文档问答.ipynb rename to content/LangChain for LLM Application Development/5.基于文档的问答.ipynb index 6993bc7..811e8df 100644 --- a/content/LangChain for LLM Application Development/5.文档问答.ipynb +++ b/content/LangChain for LLM Application Development/5.基于文档的问答.ipynb @@ -1,11 +1,20 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "f200ba9a", + "metadata": {}, + "source": [ + "# 5 基于文档的问答 \n", + "" + ] + }, { "cell_type": "markdown", "id": "52824b89-532a-4e54-87e9-1410813cd39e", "metadata": {}, "source": [ - "# 第四章 基于LangChain的文档问答\n", + "\n", "本章内容主要利用langchain构建向量数据库,可以在文档上方或关于文档回答问题,因此,给定从PDF文件、网页或某些公司的内部文档收集中提取的文本,使用llm回答有关这些文档内容的问题" ] }, @@ -16,18 +25,22 @@ "height": 30 }, "source": [ - "## 环境配置\n", + "\n", "\n", "安装langchain,设置chatGPT的OPENAI_API_KEY\n", + "\n", "* 安装langchain\n", + "\n", "```\n", "pip install langchain\n", "```\n", "* 安装docarray\n", + "\n", "```\n", "pip install docarray\n", "```\n", "* 设置API-KEY环境变量\n", + "\n", "```\n", "export OPENAI_API_KEY='api-key'\n", "\n", @@ -81,7 +94,7 @@ "height": 30 }, "source": [ - "### 导入embedding模型和向量存储组件\n", + "## 5.1 导入embedding模型和向量存储组件\n", "使用Dock Array内存搜索向量存储,作为一个内存向量存储,不需要连接外部数据库" ] }, @@ -264,7 +277,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "3bd6422c", "metadata": {}, @@ -273,12 +285,11 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "2963fc63", "metadata": {}, "source": [ - "#### 创建向量存储\n", + "### 5.1.2 创建向量存储\n", "将导入一个索引,即向量存储索引创建器" ] }, @@ -373,7 +384,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "eb74cc79", "metadata": {}, @@ -382,12 +392,11 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "dd34e50e", "metadata": {}, "source": [ - "#### 使用语言模型与文档结合使用\n", + "### 5.1.3 使用语言模型与文档结合使用\n", "想要使用语言模型并将其与我们的许多文档结合使用,但是语言模型一次只能检查几千个单词,如果我们有非常大的文档,如何让语言模型回答关于其中所有内容的问题呢?通过embedding和向量存储实现\n", "* embedding \n", "文本片段创建数值表示文本语义,相似内容的文本片段将具有相似的向量,这使我们可以在向量空间中比较文本片段\n", @@ -594,12 +603,11 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "fe41b36f", "metadata": {}, "source": [ - "### 如何回答我们文档的相关问题\n", + "## 5.2 如何回答我们文档的相关问题\n", "首先,我们需要从这个向量存储中创建一个检索器,检索器是一个通用接口,可以由任何接受查询并返回文档的方法支持。接下来,因为我们想要进行文本生成并返回自然语言响应\n" ] }, @@ -685,7 +693,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "12f042e7", "metadata": {}, @@ -779,7 +786,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "e28c5657", "metadata": {}, @@ -788,12 +794,11 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "44f1fa38", "metadata": {}, "source": [ - "#### 不同类型的chain链\n", + "### 5.2.1 不同类型的chain链\n", "想在许多不同类型的块上执行相同类型的问答,该怎么办?之前的实验中只返回了4个文档,如果有多个文档,那么我们可以使用几种不同的方法\n", "* Map Reduce \n", "将所有块与问题一起传递给语言模型,获取回复,使用另一个语言模型调用将所有单独的回复总结成最终答案,它可以在任意数量的文档上运行。可以并行处理单个问题,同时也需要更多的调用。它将所有文档视为独立的\n", @@ -804,12 +809,6 @@ "* Stuff \n", "将所有内容组合成一个文档" ] - }, - { - "cell_type": "markdown", - "id": "7988f412", - "metadata": {}, - "source": [] } ], "metadata": { @@ -828,7 +827,20 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.9.12" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true } }, "nbformat": 4, diff --git a/content/LangChain for LLM Application Development/6.评估.ipynb b/content/LangChain for LLM Application Development/6.评估.ipynb index 081a749..4601453 100644 --- a/content/LangChain for LLM Application Development/6.评估.ipynb +++ b/content/LangChain for LLM Application Development/6.评估.ipynb @@ -1,12 +1,12 @@ { "cells": [ { - "attachments": {}, "cell_type": "markdown", "id": "52824b89-532a-4e54-87e9-1410813cd39e", "metadata": {}, "source": [ - "# 第五章 如何评估基于LLM的应用程序\n", + "# 6.评估\n", + "\n", "当使用llm构建复杂应用程序时,评估应用程序的表现是一个重要但有时棘手的步骤,它是否满足某些准确性标准?\n", "通常更有用的是从许多不同的数据点中获得更全面的模型表现情况\n", "一种是使用语言模型本身和链本身来评估其他语言模型、其他链和其他应用程序" @@ -14,7 +14,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "b7ed03ed-1322-49e3-b2a2-33e94fb592ef", "metadata": { "height": 81, @@ -29,18 +29,18 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "28008949", "metadata": {}, "source": [ - "## 创建LLM应用\n", + "## 6.1 英文原版\n", + "### 6.1.1 创建LLM应用\n", "按照langchain链的方式进行构建" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", "metadata": { "height": 98 @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "id": "9ec1106d", "metadata": { "height": 64 @@ -71,7 +71,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "id": "06b1ffae", "metadata": {}, "outputs": [ @@ -174,8 +174,8 @@ "" ], "text/plain": [ - " 0 1 \n", - "0 NaN name \\\n", + " 0 1 \\\n", + "0 NaN name \n", "1 0.0 Women's Campside Oxfords \n", "2 1.0 Recycled Waterhog Dog Mat, Chevron Weave \n", "3 2.0 Infant and Toddler Girls' Coastal Chill Swimsu... \n", @@ -203,7 +203,7 @@ "[1001 rows x 3 columns]" ] }, - "execution_count": 6, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -217,12 +217,20 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 6, "id": "b31c218f", "metadata": { "height": 64 }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.embeddings.openai.embed_with_retry.._embed_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-embedding-ada-002 in organization org-2YlJyPMl62f07XPJCAlXfDxj on tokens per min. Limit: 150000 / min. Current: 127135 / min. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + } + ], "source": [ "'''\n", "将指定向量存储类,创建完成后,我们将从加载器中调用,通过文档记载器列表加载\n", @@ -234,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 7, "id": "a2006054", "metadata": { "height": 183 @@ -255,19 +263,19 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "791ebd73", "metadata": {}, "source": [ - "### 创建评估数据点\n", - "们需要做的第一件事是真正弄清楚我们想要评估它的一些数据点,我们将介绍几种不同的方法来完成这个任务\n", + "#### 6.1.1.1 创建评估数据点\n", + "我们需要做的第一件事是真正弄清楚我们想要评估它的一些数据点,我们将介绍几种不同的方法来完成这个任务\n", + "\n", "1、将自己想出好的数据点作为例子,查看一些数据,然后想出例子问题和答案,以便以后用于评估" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 8, "id": "fb04a0f9", "metadata": { "height": 30 @@ -276,10 +284,10 @@ { "data": { "text/plain": [ - "Document(page_content=\": 10\\nname: Cozy Comfort Pullover Set, Stripe\\ndescription: Perfect for lounging, this striped knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out.\\n\\nSize & Fit\\n- Pants are Favorite Fit: Sits lower on the waist.\\n- Relaxed Fit: Our most generous fit sits farthest from the body.\\n\\nFabric & Care\\n- In the softest blend of 63% polyester, 35% rayon and 2% spandex.\\n\\nAdditional Features\\n- Relaxed fit top with raglan sleeves and rounded hem.\\n- Pull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg.\\n\\nImported.\", metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 10})" + "Document(page_content=\": 10\\nname: Cozy Comfort Pullover Set, Stripe\\ndescription: Perfect for lounging, this striped knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out.\\r\\n\\r\\nSize & Fit\\r\\n- Pants are Favorite Fit: Sits lower on the waist.\\r\\n- Relaxed Fit: Our most generous fit sits farthest from the body.\\r\\n\\r\\nFabric & Care\\r\\n- In the softest blend of 63% polyester, 35% rayon and 2% spandex.\\r\\n\\r\\nAdditional Features\\r\\n- Relaxed fit top with raglan sleeves and rounded hem.\\r\\n- Pull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg.\\r\\n\\r\\nImported.\", metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 10})" ] }, - "execution_count": 14, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -290,7 +298,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 9, "id": "fe4a88c2", "metadata": { "height": 30 @@ -302,7 +310,7 @@ "Document(page_content=': 11\\nname: Ultra-Lofty 850 Stretch Down Hooded Jacket\\ndescription: This technical stretch down jacket from our DownTek collection is sure to keep you warm and comfortable with its full-stretch construction providing exceptional range of motion. With a slightly fitted style that falls at the hip and best with a midweight layer, this jacket is suitable for light activity up to 20° and moderate activity up to -30°. The soft and durable 100% polyester shell offers complete windproof protection and is insulated with warm, lofty goose down. Other features include welded baffles for a no-stitch construction and excellent stretch, an adjustable hood, an interior media port and mesh stash pocket and a hem drawcord. Machine wash and dry. Imported.', metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 11})" ] }, - "execution_count": 15, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -312,7 +320,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "b9c52116", "metadata": {}, @@ -321,17 +328,16 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "8d548aef", "metadata": {}, "source": [ - "### 创建测试用例数据\n" + "#### 6.1.1.2 创建测试用例数据\n" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 10, "id": "c2d59bf2", "metadata": { "height": 217 @@ -353,7 +359,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "b73ce510", "metadata": {}, @@ -363,17 +368,16 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "c7ce3e4f", "metadata": {}, "source": [ - "### 通过LLM生成测试用例" + "#### 6.1.1.3 通过LLM生成测试用例" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 11, "id": "d44f8376", "metadata": { "height": 47 @@ -385,7 +389,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 12, "id": "34e87816", "metadata": { "height": 30 @@ -397,12 +401,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, + "id": "b93e7a5d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[Document(page_content=\": 0\\nname: Women's Campside Oxfords\\ndescription: This ultracomfortable lace-to-toe Oxford boasts a super-soft canvas, thick cushioning, and quality construction for a broken-in feel from the first time you put them on. \\r\\n\\r\\nSize & Fit: Order regular shoe size. For half sizes not offered, order up to next whole size. \\r\\n\\r\\nSpecs: Approx. weight: 1 lb.1 oz. per pair. \\r\\n\\r\\nConstruction: Soft canvas material for a broken-in feel and look. Comfortable EVA innersole with Cleansport NXT® antimicrobial odor control. Vintage hunt, fish and camping motif on innersole. Moderate arch contour of innersole. EVA foam midsole for cushioning and support. Chain-tread-inspired molded rubber outsole with modified chain-tread pattern. Imported. \\r\\n\\r\\nQuestions? Please contact us for any inquiries.\", metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 0}),\n", + " Document(page_content=': 1\\nname: Recycled Waterhog Dog Mat, Chevron Weave\\ndescription: Protect your floors from spills and splashing with our ultradurable recycled Waterhog dog mat made right here in the USA. \\r\\n\\r\\nSpecs\\r\\nSmall - Dimensions: 18\" x 28\". \\r\\nMedium - Dimensions: 22.5\" x 34.5\".\\r\\n\\r\\nWhy We Love It\\r\\nMother nature, wet shoes and muddy paws have met their match with our Recycled Waterhog mats. Ruggedly constructed from recycled plastic materials, these ultratough mats help keep dirt and water off your floors and plastic out of landfills, trails and oceans. Now, that\\'s a win-win for everyone.\\r\\n\\r\\nFabric & Care\\r\\nVacuum or hose clean.\\r\\n\\r\\nConstruction\\r\\n24 oz. polyester fabric made from 94% recycled materials.\\r\\nRubber backing.\\r\\n\\r\\nAdditional Features\\r\\nFeatures an -exclusive design.\\r\\nFeatures thick and thin fibers for scraping dirt and absorbing water.\\r\\nDries quickly and resists fading, rotting, mildew and shedding.\\r\\nUse indoors or out.\\r\\nMade in the USA.\\r\\n\\r\\nHave questions? Reach out to our customer service team with any questions you may have.', metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 1}),\n", + " Document(page_content=\": 2\\nname: Infant and Toddler Girls' Coastal Chill Swimsuit, Two-Piece\\ndescription: She'll love the bright colors, ruffles and exclusive whimsical prints of this toddler's two-piece swimsuit! Our four-way-stretch and chlorine-resistant fabric keeps its shape and resists snags. The UPF 50+ rated fabric provides the highest rated sun protection possible, blocking 98% of the sun's harmful rays. The crossover no-slip straps and fully lined bottom ensure a secure fit and maximum coverage. Machine wash and line dry for best results. Imported.\", metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 2}),\n", + " Document(page_content=\": 3\\nname: Refresh Swimwear, V-Neck Tankini Contrasts\\ndescription: Whether you're going for a swim or heading out on an SUP, this watersport-ready tankini top is designed to move with you and stay comfortable. All while looking great in an eye-catching colorblock style. \\r\\n\\r\\nSize & Fit\\r\\nFitted: Sits close to the body.\\r\\n\\r\\nWhy We Love It\\r\\nNot only does this swimtop feel good to wear, its fabric is good for the earth too. In recycled nylon, with Lycra® spandex for the perfect amount of stretch. \\r\\n\\r\\nFabric & Care\\r\\nThe premium Italian-blend is breathable, quick drying and abrasion resistant. \\r\\nBody in 82% recycled nylon with 18% Lycra® spandex. \\r\\nLined in 90% recycled nylon with 10% Lycra® spandex. \\r\\nUPF 50+ rated – the highest rated sun protection possible. \\r\\nHandwash, line dry.\\r\\n\\r\\nAdditional Features\\r\\nLightweight racerback straps are easy to get on and off, and won't get in your way. \\r\\nFlattering V-neck silhouette. \\r\\nImported.\\r\\n\\r\\nSun Protection That Won't Wear Off\\r\\nOur high-performance fabric provides SPF\", metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 3}),\n", + " Document(page_content=\": 4\\nname: EcoFlex 3L Storm Pants\\ndescription: Our new TEK O2 technology makes our four-season waterproof pants even more breathable. It's guaranteed to keep you dry and comfortable – whatever the activity and whatever the weather. Size & Fit: Slightly Fitted through hip and thigh. \\r\\n\\r\\nWhy We Love It: Our state-of-the-art TEK O2 technology offers the most breathability we've ever tested. Great as ski pants, they're ideal for a variety of outdoor activities year-round. Plus, they're loaded with features outdoor enthusiasts appreciate, including weather-blocking gaiters and handy side zips. Air In. Water Out. See how our air-permeable TEK O2 technology keeps you dry and comfortable. \\r\\n\\r\\nFabric & Care: 100% nylon, exclusive of trim. Machine wash and dry. \\r\\n\\r\\nAdditional Features: Three-layer shell delivers waterproof protection. Brand new TEK O2 technology provides enhanced breathability. Interior gaiters keep out rain and snow. Full side zips for easy on/off over boots. Two zippered hand pockets. Thigh pocket. Imported.\\r\\n\\r\\n – Official Supplier to the U.S. Ski Team\\r\\nTHEIR WILL\", metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 4})]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, "id": "62abae09", "metadata": { "height": 64 }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + } + ], "source": [ "new_examples = example_gen_chain.apply_and_parse(\n", " [{\"doc\": t} for t in data[:5]]\n", @@ -411,26 +455,26 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 16, "id": "31c9f786", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[{'query': \"What is the weight of the Women's Campside Oxfords?\",\n", - " 'answer': \"The Women's Campside Oxfords weigh approximately 1 lb.1 oz. per pair.\"},\n", - " {'query': 'What are the dimensions of the medium Recycled Waterhog dog mat?',\n", - " 'answer': 'The dimensions of the medium Recycled Waterhog dog mat are 22.5\" x 34.5\".'},\n", - " {'query': \"What are some features of the Infant and Toddler Girls' Coastal Chill Swimsuit?\",\n", - " 'answer': \"The swimsuit has bright colors, ruffles, and exclusive whimsical prints. It is made of four-way-stretch and chlorine-resistant fabric that keeps its shape and resists snags. The swimsuit is also UPF 50+ rated, providing the highest rated sun protection possible, blocking 98% of the sun's harmful rays. The crossover no-slip straps and fully lined bottom ensure a secure fit and maximum coverage. It is machine washable and should be line dried for best results. The swimsuit is imported.\"},\n", + "[{'query': \"What is the description of the Women's Campside Oxfords?\",\n", + " 'answer': \"The Women's Campside Oxfords are described as an ultracomfortable lace-to-toe Oxford made of soft canvas material, featuring thick cushioning, quality construction, and a broken-in feel from the first time they are worn.\"},\n", + " {'query': 'What are the dimensions of the small and medium sizes of the Recycled Waterhog Dog Mat, Chevron Weave?',\n", + " 'answer': 'The small size of the Recycled Waterhog Dog Mat, Chevron Weave has dimensions of 18\" x 28\", while the medium size has dimensions of 22.5\" x 34.5\".'},\n", + " {'query': \"What are some features of the Infant and Toddler Girls' Coastal Chill Swimsuit, Two-Piece?\",\n", + " 'answer': \"The swimsuit features bright colors, ruffles, and exclusive whimsical prints. It is made of a four-way-stretch and chlorine-resistant fabric that maintains its shape and resists snags. The fabric is also UPF 50+ rated, providing the highest rated sun protection possible by blocking 98% of the sun's harmful rays. The swimsuit has crossover no-slip straps and a fully lined bottom for a secure fit and maximum coverage.\"},\n", " {'query': 'What is the fabric composition of the Refresh Swimwear, V-Neck Tankini Contrasts?',\n", - " 'answer': 'The Refresh Swimwear, V-Neck Tankini Contrasts is made of 82% recycled nylon with 18% Lycra® spandex for the body and 90% recycled nylon with 10% Lycra® spandex for the lining.'},\n", - " {'query': 'What is the name of the waterproof pants and what technology makes them more breathable?',\n", - " 'answer': 'The name of the pants is EcoFlex 3L Storm Pants and the TEK O2 technology makes them more breathable.'}]" + " 'answer': 'The Refresh Swimwear, V-Neck Tankini Contrasts is made of 82% recycled nylon with 18% Lycra® spandex for the body, and 90% recycled nylon with 10% Lycra® spandex for the lining.'},\n", + " {'query': 'What is the technology used in the EcoFlex 3L Storm Pants that makes them more breathable?',\n", + " 'answer': 'The EcoFlex 3L Storm Pants use TEK O2 technology to make them more breathable.'}]" ] }, - "execution_count": 21, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -441,7 +485,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 17, "id": "97ab28b5", "metadata": { "height": 30 @@ -450,11 +494,11 @@ { "data": { "text/plain": [ - "{'query': \"What is the weight of the Women's Campside Oxfords?\",\n", - " 'answer': \"The Women's Campside Oxfords weigh approximately 1 lb.1 oz. per pair.\"}" + "{'query': \"What is the description of the Women's Campside Oxfords?\",\n", + " 'answer': \"The Women's Campside Oxfords are described as an ultracomfortable lace-to-toe Oxford made of soft canvas material, featuring thick cushioning, quality construction, and a broken-in feel from the first time they are worn.\"}" ] }, - "execution_count": 22, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -465,7 +509,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 18, "id": "0ebe4228", "metadata": { "height": 30 @@ -474,10 +518,10 @@ { "data": { "text/plain": [ - "Document(page_content=\": 0\\nname: Women's Campside Oxfords\\ndescription: This ultracomfortable lace-to-toe Oxford boasts a super-soft canvas, thick cushioning, and quality construction for a broken-in feel from the first time you put them on. \\n\\nSize & Fit: Order regular shoe size. For half sizes not offered, order up to next whole size. \\n\\nSpecs: Approx. weight: 1 lb.1 oz. per pair. \\n\\nConstruction: Soft canvas material for a broken-in feel and look. Comfortable EVA innersole with Cleansport NXT® antimicrobial odor control. Vintage hunt, fish and camping motif on innersole. Moderate arch contour of innersole. EVA foam midsole for cushioning and support. Chain-tread-inspired molded rubber outsole with modified chain-tread pattern. Imported. \\n\\nQuestions? Please contact us for any inquiries.\", metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 0})" + "Document(page_content=\": 0\\nname: Women's Campside Oxfords\\ndescription: This ultracomfortable lace-to-toe Oxford boasts a super-soft canvas, thick cushioning, and quality construction for a broken-in feel from the first time you put them on. \\r\\n\\r\\nSize & Fit: Order regular shoe size. For half sizes not offered, order up to next whole size. \\r\\n\\r\\nSpecs: Approx. weight: 1 lb.1 oz. per pair. \\r\\n\\r\\nConstruction: Soft canvas material for a broken-in feel and look. Comfortable EVA innersole with Cleansport NXT® antimicrobial odor control. Vintage hunt, fish and camping motif on innersole. Moderate arch contour of innersole. EVA foam midsole for cushioning and support. Chain-tread-inspired molded rubber outsole with modified chain-tread pattern. Imported. \\r\\n\\r\\nQuestions? Please contact us for any inquiries.\", metadata={'source': 'OutdoorClothingCatalog_1000.csv', 'row': 0})" ] }, - "execution_count": 23, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -487,17 +531,16 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "faf25f2f", "metadata": {}, "source": [ - "### 组合用例数据" + "#### 6.1.1.4 组合用例数据" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 19, "id": "ada2a3fc", "metadata": { "height": 30 @@ -509,7 +552,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 20, "id": "9cdf5cf5", "metadata": { "height": 30 @@ -521,7 +564,7 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -529,10 +572,10 @@ { "data": { "text/plain": [ - "'The Cozy Comfort Pullover Set, Stripe has side pockets.'" + "'Yes, the Cozy Comfort Pullover Set does have side pockets.'" ] }, - "execution_count": 25, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -542,12 +585,11 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "63f3cb08", "metadata": {}, "source": [ - "## 人工评估\n", + "### 6.1.2 人工评估\n", "现在有了这些示例,但是我们如何评估正在发生的事情呢?\n", "通过运行一个示例通过链,并查看它产生的输出\n", "在这里我们传递一个查询,然后我们得到一个答案。实际上正在发生的事情,进入语言模型的实际提示是什么? \n", @@ -558,7 +600,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 21, "id": "fcaf622e", "metadata": { "height": 47 @@ -574,7 +616,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 22, "id": "1e1deab0", "metadata": {}, "outputs": [ @@ -591,23 +633,23 @@ "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain] Entering Chain run with input:\n", "\u001b[0m{\n", " \"question\": \"Do the Cozy Comfort Pullover Set have side pockets?\",\n", - " \"context\": \": 10\\nname: Cozy Comfort Pullover Set, Stripe\\ndescription: Perfect for lounging, this striped knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out.\\n\\nSize & Fit\\n- Pants are Favorite Fit: Sits lower on the waist.\\n- Relaxed Fit: Our most generous fit sits farthest from the body.\\n\\nFabric & Care\\n- In the softest blend of 63% polyester, 35% rayon and 2% spandex.\\n\\nAdditional Features\\n- Relaxed fit top with raglan sleeves and rounded hem.\\n- Pull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg.\\n\\nImported.<<<<>>>>>: 73\\nname: Cozy Cuddles Knit Pullover Set\\ndescription: Perfect for lounging, this knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out. \\n\\nSize & Fit \\nPants are Favorite Fit: Sits lower on the waist. \\nRelaxed Fit: Our most generous fit sits farthest from the body. \\n\\nFabric & Care \\nIn the softest blend of 63% polyester, 35% rayon and 2% spandex.\\n\\nAdditional Features \\nRelaxed fit top with raglan sleeves and rounded hem. \\nPull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg. \\nImported.<<<<>>>>>: 632\\nname: Cozy Comfort Fleece Pullover\\ndescription: The ultimate sweater fleece \\u2013 made from superior fabric and offered at an unbeatable price. \\n\\nSize & Fit\\nSlightly Fitted: Softly shapes the body. Falls at hip. \\n\\nWhy We Love It\\nOur customers (and employees) love the rugged construction and heritage-inspired styling of our popular Sweater Fleece Pullover and wear it for absolutely everything. From high-intensity activities to everyday tasks, you'll find yourself reaching for it every time.\\n\\nFabric & Care\\nRugged sweater-knit exterior and soft brushed interior for exceptional warmth and comfort. Made from soft, 100% polyester. Machine wash and dry.\\n\\nAdditional Features\\nFeatures our classic Mount Katahdin logo. Snap placket. Front princess seams create a feminine shape. Kangaroo handwarmer pockets. Cuffs and hem reinforced with jersey binding. Imported.\\n\\n \\u2013 Official Supplier to the U.S. Ski Team\\nTHEIR WILL TO WIN, WOVEN RIGHT IN. LEARN MORE<<<<>>>>>: 151\\nname: Cozy Quilted Sweatshirt\\ndescription: Our sweatshirt is an instant classic with its great quilted texture and versatile weight that easily transitions between seasons. With a traditional fit that is relaxed through the chest, sleeve, and waist, this pullover is lightweight enough to be worn most months of the year. The cotton blend fabric is super soft and comfortable, making it the perfect casual layer. To make dressing easy, this sweatshirt also features a snap placket and a heritage-inspired Mt. Katahdin logo patch. For care, machine wash and dry. Imported.\"\n", + " \"context\": \": 10\\nname: Cozy Comfort Pullover Set, Stripe\\ndescription: Perfect for lounging, this striped knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out.\\r\\n\\r\\nSize & Fit\\r\\n- Pants are Favorite Fit: Sits lower on the waist.\\r\\n- Relaxed Fit: Our most generous fit sits farthest from the body.\\r\\n\\r\\nFabric & Care\\r\\n- In the softest blend of 63% polyester, 35% rayon and 2% spandex.\\r\\n\\r\\nAdditional Features\\r\\n- Relaxed fit top with raglan sleeves and rounded hem.\\r\\n- Pull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg.\\r\\n\\r\\nImported.<<<<>>>>>: 73\\nname: Cozy Cuddles Knit Pullover Set\\ndescription: Perfect for lounging, this knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out. \\r\\n\\r\\nSize & Fit \\r\\nPants are Favorite Fit: Sits lower on the waist. \\r\\nRelaxed Fit: Our most generous fit sits farthest from the body. \\r\\n\\r\\nFabric & Care \\r\\nIn the softest blend of 63% polyester, 35% rayon and 2% spandex.\\r\\n\\r\\nAdditional Features \\r\\nRelaxed fit top with raglan sleeves and rounded hem. \\r\\nPull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg. \\r\\nImported.<<<<>>>>>: 632\\nname: Cozy Comfort Fleece Pullover\\ndescription: The ultimate sweater fleece – made from superior fabric and offered at an unbeatable price. \\r\\n\\r\\nSize & Fit\\r\\nSlightly Fitted: Softly shapes the body. Falls at hip. \\r\\n\\r\\nWhy We Love It\\r\\nOur customers (and employees) love the rugged construction and heritage-inspired styling of our popular Sweater Fleece Pullover and wear it for absolutely everything. From high-intensity activities to everyday tasks, you'll find yourself reaching for it every time.\\r\\n\\r\\nFabric & Care\\r\\nRugged sweater-knit exterior and soft brushed interior for exceptional warmth and comfort. Made from soft, 100% polyester. Machine wash and dry.\\r\\n\\r\\nAdditional Features\\r\\nFeatures our classic Mount Katahdin logo. Snap placket. Front princess seams create a feminine shape. Kangaroo handwarmer pockets. Cuffs and hem reinforced with jersey binding. Imported.\\r\\n\\r\\n – Official Supplier to the U.S. Ski Team\\r\\nTHEIR WILL TO WIN, WOVEN RIGHT IN. LEARN MORE<<<<>>>>>: 265\\nname: Cozy Workout Vest\\ndescription: For serious warmth that won't weigh you down, reach for this fleece-lined vest, which provides you with layering options whether you're inside or outdoors.\\r\\nSize & Fit\\r\\nRelaxed Fit. Falls at hip.\\r\\nFabric & Care\\r\\nSoft, textured fleece lining. Nylon shell. Machine wash and dry. \\r\\nAdditional Features \\r\\nTwo handwarmer pockets. Knit side panels stretch for a more flattering fit. Shell fabric is treated to resist water and stains. Imported.\"\n", "}\n", "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain > 4:llm:ChatOpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", - " \"System: Use the following pieces of context to answer the users question. \\nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\\n----------------\\n: 10\\nname: Cozy Comfort Pullover Set, Stripe\\ndescription: Perfect for lounging, this striped knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out.\\n\\nSize & Fit\\n- Pants are Favorite Fit: Sits lower on the waist.\\n- Relaxed Fit: Our most generous fit sits farthest from the body.\\n\\nFabric & Care\\n- In the softest blend of 63% polyester, 35% rayon and 2% spandex.\\n\\nAdditional Features\\n- Relaxed fit top with raglan sleeves and rounded hem.\\n- Pull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg.\\n\\nImported.<<<<>>>>>: 73\\nname: Cozy Cuddles Knit Pullover Set\\ndescription: Perfect for lounging, this knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out. \\n\\nSize & Fit \\nPants are Favorite Fit: Sits lower on the waist. \\nRelaxed Fit: Our most generous fit sits farthest from the body. \\n\\nFabric & Care \\nIn the softest blend of 63% polyester, 35% rayon and 2% spandex.\\n\\nAdditional Features \\nRelaxed fit top with raglan sleeves and rounded hem. \\nPull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg. \\nImported.<<<<>>>>>: 632\\nname: Cozy Comfort Fleece Pullover\\ndescription: The ultimate sweater fleece \\u2013 made from superior fabric and offered at an unbeatable price. \\n\\nSize & Fit\\nSlightly Fitted: Softly shapes the body. Falls at hip. \\n\\nWhy We Love It\\nOur customers (and employees) love the rugged construction and heritage-inspired styling of our popular Sweater Fleece Pullover and wear it for absolutely everything. From high-intensity activities to everyday tasks, you'll find yourself reaching for it every time.\\n\\nFabric & Care\\nRugged sweater-knit exterior and soft brushed interior for exceptional warmth and comfort. Made from soft, 100% polyester. Machine wash and dry.\\n\\nAdditional Features\\nFeatures our classic Mount Katahdin logo. Snap placket. Front princess seams create a feminine shape. Kangaroo handwarmer pockets. Cuffs and hem reinforced with jersey binding. Imported.\\n\\n \\u2013 Official Supplier to the U.S. Ski Team\\nTHEIR WILL TO WIN, WOVEN RIGHT IN. LEARN MORE<<<<>>>>>: 151\\nname: Cozy Quilted Sweatshirt\\ndescription: Our sweatshirt is an instant classic with its great quilted texture and versatile weight that easily transitions between seasons. With a traditional fit that is relaxed through the chest, sleeve, and waist, this pullover is lightweight enough to be worn most months of the year. The cotton blend fabric is super soft and comfortable, making it the perfect casual layer. To make dressing easy, this sweatshirt also features a snap placket and a heritage-inspired Mt. Katahdin logo patch. For care, machine wash and dry. Imported.\\nHuman: Do the Cozy Comfort Pullover Set have side pockets?\"\n", + " \"System: Use the following pieces of context to answer the users question. \\nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\\n----------------\\n: 10\\nname: Cozy Comfort Pullover Set, Stripe\\ndescription: Perfect for lounging, this striped knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out.\\r\\n\\r\\nSize & Fit\\r\\n- Pants are Favorite Fit: Sits lower on the waist.\\r\\n- Relaxed Fit: Our most generous fit sits farthest from the body.\\r\\n\\r\\nFabric & Care\\r\\n- In the softest blend of 63% polyester, 35% rayon and 2% spandex.\\r\\n\\r\\nAdditional Features\\r\\n- Relaxed fit top with raglan sleeves and rounded hem.\\r\\n- Pull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg.\\r\\n\\r\\nImported.<<<<>>>>>: 73\\nname: Cozy Cuddles Knit Pullover Set\\ndescription: Perfect for lounging, this knit set lives up to its name. We used ultrasoft fabric and an easy design that's as comfortable at bedtime as it is when we have to make a quick run out. \\r\\n\\r\\nSize & Fit \\r\\nPants are Favorite Fit: Sits lower on the waist. \\r\\nRelaxed Fit: Our most generous fit sits farthest from the body. \\r\\n\\r\\nFabric & Care \\r\\nIn the softest blend of 63% polyester, 35% rayon and 2% spandex.\\r\\n\\r\\nAdditional Features \\r\\nRelaxed fit top with raglan sleeves and rounded hem. \\r\\nPull-on pants have a wide elastic waistband and drawstring, side pockets and a modern slim leg. \\r\\nImported.<<<<>>>>>: 632\\nname: Cozy Comfort Fleece Pullover\\ndescription: The ultimate sweater fleece – made from superior fabric and offered at an unbeatable price. \\r\\n\\r\\nSize & Fit\\r\\nSlightly Fitted: Softly shapes the body. Falls at hip. \\r\\n\\r\\nWhy We Love It\\r\\nOur customers (and employees) love the rugged construction and heritage-inspired styling of our popular Sweater Fleece Pullover and wear it for absolutely everything. From high-intensity activities to everyday tasks, you'll find yourself reaching for it every time.\\r\\n\\r\\nFabric & Care\\r\\nRugged sweater-knit exterior and soft brushed interior for exceptional warmth and comfort. Made from soft, 100% polyester. Machine wash and dry.\\r\\n\\r\\nAdditional Features\\r\\nFeatures our classic Mount Katahdin logo. Snap placket. Front princess seams create a feminine shape. Kangaroo handwarmer pockets. Cuffs and hem reinforced with jersey binding. Imported.\\r\\n\\r\\n – Official Supplier to the U.S. Ski Team\\r\\nTHEIR WILL TO WIN, WOVEN RIGHT IN. LEARN MORE<<<<>>>>>: 265\\nname: Cozy Workout Vest\\ndescription: For serious warmth that won't weigh you down, reach for this fleece-lined vest, which provides you with layering options whether you're inside or outdoors.\\r\\nSize & Fit\\r\\nRelaxed Fit. Falls at hip.\\r\\nFabric & Care\\r\\nSoft, textured fleece lining. Nylon shell. Machine wash and dry. \\r\\nAdditional Features \\r\\nTwo handwarmer pockets. Knit side panels stretch for a more flattering fit. Shell fabric is treated to resist water and stains. Imported.\\nHuman: Do the Cozy Comfort Pullover Set have side pockets?\"\n", " ]\n", "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain > 4:llm:ChatOpenAI] [1.68s] Exiting LLM run with output:\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain > 4:llm:ChatOpenAI] [2.29s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", - " \"text\": \"The Cozy Comfort Pullover Set, Stripe has side pockets on the pull-on pants.\",\n", + " \"text\": \"Yes, the Cozy Comfort Pullover Set does have side pockets.\",\n", " \"generation_info\": null,\n", " \"message\": {\n", - " \"content\": \"The Cozy Comfort Pullover Set, Stripe has side pockets on the pull-on pants.\",\n", + " \"content\": \"Yes, the Cozy Comfort Pullover Set does have side pockets.\",\n", " \"additional_kwargs\": {},\n", " \"example\": false\n", " }\n", @@ -616,34 +658,35 @@ " ],\n", " \"llm_output\": {\n", " \"token_usage\": {\n", - " \"prompt_tokens\": 734,\n", - " \"completion_tokens\": 18,\n", - " \"total_tokens\": 752\n", + " \"prompt_tokens\": 717,\n", + " \"completion_tokens\": 14,\n", + " \"total_tokens\": 731\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:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain] [1.68s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain] [2.29s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"text\": \"The Cozy Comfort Pullover Set, Stripe has side pockets on the pull-on pants.\"\n", + " \"text\": \"Yes, the Cozy Comfort Pullover Set does have side pockets.\"\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain] [1.71s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain] [2.29s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"output_text\": \"The Cozy Comfort Pullover Set, Stripe has side pockets on the pull-on pants.\"\n", + " \"output_text\": \"Yes, the Cozy Comfort Pullover Set does have side pockets.\"\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA] [2.86s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA] [2.93s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"result\": \"The Cozy Comfort Pullover Set, Stripe has side pockets on the pull-on pants.\"\n", + " \"result\": \"Yes, the Cozy Comfort Pullover Set does have side pockets.\"\n", "}\n" ] }, { "data": { "text/plain": [ - "'The Cozy Comfort Pullover Set, Stripe has side pockets on the pull-on pants.'" + "'Yes, the Cozy Comfort Pullover Set does have side pockets.'" ] }, - "execution_count": 27, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -653,7 +696,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "8dee0f24", "metadata": {}, @@ -669,18 +711,17 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "7b37c7bc", "metadata": {}, "source": [ - "##### 如何评估新创建的实例\n", + "#### 6.1.2.1 如何评估新创建的实例\n", "与创建它们类似,可以运行链条来处理所有示例,然后查看输出并尝试弄清楚,发生了什么,它是否正确" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 23, "id": "b3d6bef0", "metadata": { "height": 47 @@ -692,17 +733,16 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "d5bdbdce", "metadata": {}, "source": [ - "## 通过LLM进行评估实例" + "### 6.1.3 通过LLM进行评估实例" ] }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 24, "id": "a4dca05a", "metadata": { "height": 30 @@ -714,19 +754,21 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", - "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n" + "\u001b[1m> Entering new chain...\u001b[0m\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" ] }, { @@ -737,18 +779,17 @@ "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", - "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n" + "\u001b[1m> Entering new chain...\u001b[0m\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 16.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" ] }, { @@ -759,14 +800,17 @@ "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", - "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n" + "\u001b[1m> Entering new chain...\u001b[0m\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" ] }, { @@ -777,18 +821,17 @@ "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", - "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n" + "\u001b[1m> Entering new chain...\u001b[0m\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 16.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" ] }, { @@ -799,22 +842,38 @@ "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", - "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\n", - "\n", - "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n" + "\u001b[1m> Entering new chain...\u001b[0m\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", - "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-Nul9WsPYdsnjttqS3f0hDSWd on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" ] }, { @@ -832,7 +891,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 25, "id": "6012a3e0", "metadata": { "height": 30 @@ -847,7 +906,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 26, "id": "724b1c0b", "metadata": { "height": 47 @@ -861,29 +920,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "id": "8b46ae55", "metadata": { "height": 30 }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 16.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 16.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + } + ], "source": [ "graded_outputs = eval_chain.evaluate(examples, predictions)#在此链上调用evaluate,进行评估" ] }, { - "attachments": {}, "cell_type": "markdown", "id": "9ad64f72", "metadata": {}, "source": [ - "##### 评估思路\n", + "#### 6.1.3.1 评估思路\n", "当它面前有整个文档时,它可以生成一个真实的答案,我们将打印出预测的答,当它进行QA链时,使用embedding和向量数据库进行检索时,将其传递到语言模型中,然后尝试猜测预测的答案,我们还将打印出成绩,这也是语言模型生成的。当它要求评估链评估正在发生的事情时,以及它是否正确或不正确。因此,当我们循环遍历所有这些示例并将它们打印出来时,可以详细了解每个示例" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 28, "id": "3437cfbe", "metadata": { "height": 132 @@ -894,45 +986,1343 @@ "output_type": "stream", "text": [ "Example 0:\n", - "Question: Do the Cozy Comfort Pullover Set have side pockets?\n", + "Question: Do the Cozy Comfort Pullover Set have side pockets?\n", "Real Answer: Yes\n", - "Predicted Answer: The Cozy Comfort Pullover Set, Stripe does have side pockets.\n", + "Predicted Answer: Yes, the Cozy Comfort Pullover Set does have side pockets.\n", "Predicted Grade: CORRECT\n", "\n", "Example 1:\n", - "Question: What collection is the Ultra-Lofty 850 Stretch Down Hooded Jacket from?\n", + "Question: What collection is the Ultra-Lofty 850 Stretch Down Hooded Jacket from?\n", "Real Answer: The DownTek collection\n", "Predicted Answer: The Ultra-Lofty 850 Stretch Down Hooded Jacket is from the DownTek collection.\n", "Predicted Grade: CORRECT\n", "\n", "Example 2:\n", - "Question: What is the weight of each pair of Women's Campside Oxfords?\n", - "Real Answer: The approximate weight of each pair of Women's Campside Oxfords is 1 lb. 1 oz.\n", - "Predicted Answer: The weight of each pair of Women's Campside Oxfords is approximately 1 lb. 1 oz.\n", + "Question: What is the description of the Women's Campside Oxfords?\n", + "Real Answer: The Women's Campside Oxfords are described as an ultracomfortable lace-to-toe Oxford made of soft canvas material, featuring thick cushioning, quality construction, and a broken-in feel from the first time they are worn.\n", + "Predicted Answer: The description of the Women's Campside Oxfords is: \"This ultracomfortable lace-to-toe Oxford boasts a super-soft canvas, thick cushioning, and quality construction for a broken-in feel from the first time you put them on.\"\n", "Predicted Grade: CORRECT\n", "\n", "Example 3:\n", - "Question: What are the dimensions of the small and medium Recycled Waterhog Dog Mat?\n", - "Real Answer: The dimensions of the small Recycled Waterhog Dog Mat are 18\" x 28\" and the dimensions of the medium Recycled Waterhog Dog Mat are 22.5\" x 34.5\".\n", - "Predicted Answer: The small Recycled Waterhog Dog Mat has dimensions of 18\" x 28\" and the medium size has dimensions of 22.5\" x 34.5\".\n", + "Question: What are the dimensions of the small and medium sizes of the Recycled Waterhog Dog Mat, Chevron Weave?\n", + "Real Answer: The small size of the Recycled Waterhog Dog Mat, Chevron Weave has dimensions of 18\" x 28\", while the medium size has dimensions of 22.5\" x 34.5\".\n", + "Predicted Answer: The dimensions of the small size of the Recycled Waterhog Dog Mat, Chevron Weave are 18\" x 28\". The dimensions of the medium size are 22.5\" x 34.5\".\n", "Predicted Grade: CORRECT\n", "\n", "Example 4:\n", - "Question: What are some features of the Infant and Toddler Girls' Coastal Chill Swimsuit?\n", - "Real Answer: The swimsuit features bright colors, ruffles, and exclusive whimsical prints. It is made of four-way-stretch and chlorine-resistant fabric, ensuring that it keeps its shape and resists snags. The swimsuit is also UPF 50+ rated, providing the highest rated sun protection possible by blocking 98% of the sun's harmful rays. The crossover no-slip straps and fully lined bottom ensure a secure fit and maximum coverage. Finally, it can be machine washed and line dried for best results.\n", - "Predicted Answer: The Infant and Toddler Girls' Coastal Chill Swimsuit is a two-piece swimsuit with bright colors, ruffles, and exclusive whimsical prints. It is made of four-way-stretch and chlorine-resistant fabric that keeps its shape and resists snags. The swimsuit has UPF 50+ rated fabric that provides the highest rated sun protection possible, blocking 98% of the sun's harmful rays. The crossover no-slip straps and fully lined bottom ensure a secure fit and maximum coverage. It is machine washable and should be line dried for best results.\n", + "Question: What are some features of the Infant and Toddler Girls' Coastal Chill Swimsuit, Two-Piece?\n", + "Real Answer: The swimsuit features bright colors, ruffles, and exclusive whimsical prints. It is made of a four-way-stretch and chlorine-resistant fabric that maintains its shape and resists snags. The fabric is also UPF 50+ rated, providing the highest rated sun protection possible by blocking 98% of the sun's harmful rays. The swimsuit has crossover no-slip straps and a fully lined bottom for a secure fit and maximum coverage.\n", + "Predicted Answer: Some features of the Infant and Toddler Girls' Coastal Chill Swimsuit, Two-Piece are:\n", + "- Bright colors and ruffles\n", + "- Exclusive whimsical prints\n", + "- Four-way-stretch and chlorine-resistant fabric\n", + "- UPF 50+ rated fabric for sun protection\n", + "- Crossover no-slip straps\n", + "- Fully lined bottom for secure fit and maximum coverage\n", + "- Machine washable and line dry for best results\n", + "- Imported\n", "Predicted Grade: CORRECT\n", "\n", "Example 5:\n", - "Question: What is the fabric composition of the Refresh Swimwear V-Neck Tankini Contrasts?\n", - "Real Answer: The body of the Refresh Swimwear V-Neck Tankini Contrasts is made of 82% recycled nylon and 18% Lycra® spandex, while the lining is made of 90% recycled nylon and 10% Lycra® spandex.\n", - "Predicted Answer: The Refresh Swimwear V-Neck Tankini Contrasts is made of 82% recycled nylon with 18% Lycra® spandex for the body and 90% recycled nylon with 10% Lycra® spandex for the lining.\n", + "Question: What is the fabric composition of the Refresh Swimwear, V-Neck Tankini Contrasts?\n", + "Real Answer: The Refresh Swimwear, V-Neck Tankini Contrasts is made of 82% recycled nylon with 18% Lycra® spandex for the body, and 90% recycled nylon with 10% Lycra® spandex for the lining.\n", + "Predicted Answer: The fabric composition of the Refresh Swimwear, V-Neck Tankini Contrasts is 82% recycled nylon with 18% Lycra® spandex for the body, and 90% recycled nylon with 10% Lycra® spandex for the lining.\n", "Predicted Grade: CORRECT\n", "\n", "Example 6:\n", - "Question: What is the fabric composition of the EcoFlex 3L Storm Pants?\n", - "Real Answer: The EcoFlex 3L Storm Pants are made of 100% nylon, exclusive of trim.\n", - "Predicted Answer: The fabric composition of the EcoFlex 3L Storm Pants is 100% nylon, exclusive of trim.\n", + "Question: What is the technology used in the EcoFlex 3L Storm Pants that makes them more breathable?\n", + "Real Answer: The EcoFlex 3L Storm Pants use TEK O2 technology to make them more breathable.\n", + "Predicted Answer: The technology used in the EcoFlex 3L Storm Pants that makes them more breathable is called TEK O2 technology.\n", + "Predicted Grade: CORRECT\n", + "\n" + ] + } + ], + "source": [ + "#我们将传入示例和预测,得到一堆分级输出,循环遍历它们打印答案\n", + "for i, eg in enumerate(examples):\n", + " print(f\"Example {i}:\")\n", + " print(\"Question: \" + predictions[i]['query'])\n", + " print(\"Real Answer: \" + predictions[i]['answer'])\n", + " print(\"Predicted Answer: \" + predictions[i]['result'])\n", + " print(\"Predicted Grade: \" + graded_outputs[i]['text'])\n", + " print()" + ] + }, + { + "cell_type": "markdown", + "id": "87ecb476", + "metadata": {}, + "source": [ + "#### 6.1.3.2 结果分析\n", + "对于每个示例,它看起来都是正确的,让我们看看第一个例子。\n", + "这里的问题是,舒适的套头衫套装,有侧口袋吗?真正的答案,我们创建了这个,是肯定的。模型预测的答案是舒适的套头衫套装条纹,确实有侧口袋。因此,我们可以理解这是一个正确的答案。它将其评为正确。 \n", + "#### 6.1.3.3 使用模型评估的优势\n", + "\n", + "你有这些答案,它们是任意的字符串。没有单一的真实字符串是最好的可能答案,有许多不同的变体,只要它们具有相同的语义,它们应该被评为相似。如果使用正则进行精准匹配就会丢失语义信息,到目前为止存在的许多评估指标都不够好。目前最有趣和最受欢迎的之一就是使用语言模型进行评估。" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fe07d2ef", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "146806a9", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "44a464d5", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "0b6680d2", + "metadata": {}, + "source": [ + "## 6.2 中文版\n", + "### 6.2.1 创建LLM应用\n", + "按照langchain链的方式进行构建" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c045628e", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.chains import RetrievalQA #检索QA链,在文档上进行检索\n", + "from langchain.chat_models import ChatOpenAI #openai模型\n", + "from langchain.document_loaders import CSVLoader #文档加载器,采用csv格式存储\n", + "from langchain.indexes import VectorstoreIndexCreator #导入向量存储索引创建器\n", + "from langchain.vectorstores import DocArrayInMemorySearch #向量存储\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bfd04e93", + "metadata": {}, + "outputs": [], + "source": [ + "#加载中文数据\n", + "file = 'product_data.csv'\n", + "loader = CSVLoader(file_path=file)\n", + "data = loader.load()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da8eb973", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
01
0product_namedescription
1全自动咖啡机规格:\\n大型 - 尺寸:13.8'' x 17.3''。\\n中型 - 尺寸:11.5'' ...
2电动牙刷规格:\\n一般大小 - 高度:9.5'',宽度:1''。\\n\\n为什么我们热爱它:\\n我们的...
3橙味维生素C泡腾片规格:\\n每盒含有20片。\\n\\n为什么我们热爱它:\\n我们的橙味维生素C泡腾片是快速补充维...
4无线蓝牙耳机规格:\\n单个耳机尺寸:1.5'' x 1.3''。\\n\\n为什么我们热爱它:\\n这款无线蓝...
5瑜伽垫规格:\\n尺寸:24'' x 68''。\\n\\n为什么我们热爱它:\\n我们的瑜伽垫拥有出色的...
6防水运动手表规格:\\n表盘直径:40mm。\\n\\n为什么我们热爱它:\\n这款防水运动手表配备了心率监测和...
7书籍:《机器学习基础》规格:\\n页数:580页。\\n\\n为什么我们热爱它:\\n《机器学习基础》以易懂的语言讲解了机...
8空气净化器规格:\\n尺寸:15'' x 15'' x 20''。\\n\\n为什么我们热爱它:\\n我们的空...
9陶瓷保温杯规格:\\n容量:350ml。\\n\\n为什么我们热爱它:\\n我们的陶瓷保温杯设计优雅,保温效果...
10宠物自动喂食器规格:\\n尺寸:14'' x 9'' x 15''。\\n\\n为什么我们热爱它:\\n我们的宠物...
11高清电视机规格:\\n尺寸:50''。\\n\\n为什么我们热爱它:\\n我们的高清电视机拥有出色的画质和强大...
12旅行背包规格:\\n尺寸:18'' x 12'' x 6''。\\n\\n为什么我们热爱它:\\n我们的旅行...
13太阳能庭院灯规格:\\n高度:18''。\\n\\n为什么我们热爱它:\\n我们的太阳能庭院灯无需电源,只需将其...
14厨房刀具套装规格:\\n一套包括8把刀。\\n\\n为什么我们热爱它:\\n我们的厨房刀具套装由专业级不锈钢制成...
15迷你无线蓝牙音箱规格:\\n直径:3'',高度:2''。\\n\\n为什么我们热爱它:\\n我们的迷你无线蓝牙音箱体...
16抗菌洗手液规格:\\n容量:500ml。\\n\\n为什么我们热爱它:\\n我们的抗菌洗手液含有天然植物精华,...
17纯棉T恤规格:\\n尺码:S, M, L, XL, XXL。\\n\\n为什么我们热爱它:\\n我们的纯棉T...
18自动咖啡机规格:\\n尺寸:12'' x 8'' x 14''。\\n\\n为什么我们热爱它:\\n我们的自动...
19摄像头保护套规格:\\n适用于各种品牌和型号的摄像头。\\n\\n为什么我们热爱它:\\n我们的摄像头保护套可以...
20玻璃保护膜规格:\\n适用于各种尺寸的手机屏幕。\\n\\n为什么我们热爱它:\\n我们的玻璃保护膜可以有效防...
21儿童益智玩具规格:\\n适合3岁以上的儿童。\\n\\n为什么我们热爱它:\\n我们的儿童益智玩具设计独特,色彩...
22迷你书架规格:\\n尺寸:20'' x 8'' x 24''。\\n\\n为什么我们热爱它:\\n我们的迷你...
23防滑瑜伽垫规格:\\n尺寸:72'' x 24''。\\n\\n为什么我们热爱它:\\n我们的防滑瑜伽垫采用高...
24LED台灯规格:\\n尺寸:6'' x 6'' x 18''。\\n\\n为什么我们热爱它:\\n我们的LED...
25水晶酒杯规格:\\n容量:250ml。\\n\\n为什么我们热爱它:\\n我们的水晶酒杯采用高品质水晶玻璃制...
\n", + "
" + ], + "text/plain": [ + " 0 1\n", + "0 product_name description\n", + "1 全自动咖啡机 规格:\\n大型 - 尺寸:13.8'' x 17.3''。\\n中型 - 尺寸:11.5'' ...\n", + "2 电动牙刷 规格:\\n一般大小 - 高度:9.5'',宽度:1''。\\n\\n为什么我们热爱它:\\n我们的...\n", + "3 橙味维生素C泡腾片 规格:\\n每盒含有20片。\\n\\n为什么我们热爱它:\\n我们的橙味维生素C泡腾片是快速补充维...\n", + "4 无线蓝牙耳机 规格:\\n单个耳机尺寸:1.5'' x 1.3''。\\n\\n为什么我们热爱它:\\n这款无线蓝...\n", + "5 瑜伽垫 规格:\\n尺寸:24'' x 68''。\\n\\n为什么我们热爱它:\\n我们的瑜伽垫拥有出色的...\n", + "6 防水运动手表 规格:\\n表盘直径:40mm。\\n\\n为什么我们热爱它:\\n这款防水运动手表配备了心率监测和...\n", + "7 书籍:《机器学习基础》 规格:\\n页数:580页。\\n\\n为什么我们热爱它:\\n《机器学习基础》以易懂的语言讲解了机...\n", + "8 空气净化器 规格:\\n尺寸:15'' x 15'' x 20''。\\n\\n为什么我们热爱它:\\n我们的空...\n", + "9 陶瓷保温杯 规格:\\n容量:350ml。\\n\\n为什么我们热爱它:\\n我们的陶瓷保温杯设计优雅,保温效果...\n", + "10 宠物自动喂食器 规格:\\n尺寸:14'' x 9'' x 15''。\\n\\n为什么我们热爱它:\\n我们的宠物...\n", + "11 高清电视机 规格:\\n尺寸:50''。\\n\\n为什么我们热爱它:\\n我们的高清电视机拥有出色的画质和强大...\n", + "12 旅行背包 规格:\\n尺寸:18'' x 12'' x 6''。\\n\\n为什么我们热爱它:\\n我们的旅行...\n", + "13 太阳能庭院灯 规格:\\n高度:18''。\\n\\n为什么我们热爱它:\\n我们的太阳能庭院灯无需电源,只需将其...\n", + "14 厨房刀具套装 规格:\\n一套包括8把刀。\\n\\n为什么我们热爱它:\\n我们的厨房刀具套装由专业级不锈钢制成...\n", + "15 迷你无线蓝牙音箱 规格:\\n直径:3'',高度:2''。\\n\\n为什么我们热爱它:\\n我们的迷你无线蓝牙音箱体...\n", + "16 抗菌洗手液 规格:\\n容量:500ml。\\n\\n为什么我们热爱它:\\n我们的抗菌洗手液含有天然植物精华,...\n", + "17 纯棉T恤 规格:\\n尺码:S, M, L, XL, XXL。\\n\\n为什么我们热爱它:\\n我们的纯棉T...\n", + "18 自动咖啡机 规格:\\n尺寸:12'' x 8'' x 14''。\\n\\n为什么我们热爱它:\\n我们的自动...\n", + "19 摄像头保护套 规格:\\n适用于各种品牌和型号的摄像头。\\n\\n为什么我们热爱它:\\n我们的摄像头保护套可以...\n", + "20 玻璃保护膜 规格:\\n适用于各种尺寸的手机屏幕。\\n\\n为什么我们热爱它:\\n我们的玻璃保护膜可以有效防...\n", + "21 儿童益智玩具 规格:\\n适合3岁以上的儿童。\\n\\n为什么我们热爱它:\\n我们的儿童益智玩具设计独特,色彩...\n", + "22 迷你书架 规格:\\n尺寸:20'' x 8'' x 24''。\\n\\n为什么我们热爱它:\\n我们的迷你...\n", + "23 防滑瑜伽垫 规格:\\n尺寸:72'' x 24''。\\n\\n为什么我们热爱它:\\n我们的防滑瑜伽垫采用高...\n", + "24 LED台灯 规格:\\n尺寸:6'' x 6'' x 18''。\\n\\n为什么我们热爱它:\\n我们的LED...\n", + "25 水晶酒杯 规格:\\n容量:250ml。\\n\\n为什么我们热爱它:\\n我们的水晶酒杯采用高品质水晶玻璃制..." + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "#查看数据\n", + "import pandas as pd\n", + "test_data = pd.read_csv(file,header=None)\n", + "test_data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95890a3b", + "metadata": {}, + "outputs": [], + "source": [ + "'''\n", + "将指定向量存储类,创建完成后,我们将从加载器中调用,通过文档记载器列表加载\n", + "'''\n", + "index = VectorstoreIndexCreator(\n", + " vectorstore_cls=DocArrayInMemorySearch\n", + ").from_loaders([loader])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f5230594", + "metadata": {}, + "outputs": [], + "source": [ + "#通过指定语言模型、链类型、检索器和我们要打印的详细程度来创建检索QA链\n", + "llm = ChatOpenAI(temperature = 0.0)\n", + "qa = RetrievalQA.from_chain_type(\n", + " llm=llm, \n", + " chain_type=\"stuff\", \n", + " retriever=index.vectorstore.as_retriever(), \n", + " verbose=True,\n", + " chain_type_kwargs = {\n", + " \"document_separator\": \"<<<<>>>>>\"\n", + " }\n", + ")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "952fc528", + "metadata": {}, + "source": [ + "#### 6.2.1.1 创建评估数据点\n", + "我们需要做的第一件事是真正弄清楚我们想要评估它的一些数据点,我们将介绍几种不同的方法来完成这个任务\n", + "\n", + "1、将自己想出好的数据点作为例子,查看一些数据,然后想出例子问题和答案,以便以后用于评估" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eae9bd65", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Document(page_content=\"product_name: 高清电视机\\ndescription: 规格:\\n尺寸:50''。\\n\\n为什么我们热爱它:\\n我们的高清电视机拥有出色的画质和强大的音效,带来沉浸式的观看体验。\\n\\n材质与护理:\\n使用干布清洁。\\n\\n构造:\\n由塑料、金属和电子元件制成。\\n\\n其他特性:\\n支持网络连接,可以在线观看视频。\\n配备遥控器。\\n在韩国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。\", metadata={'source': 'product_data.csv', 'row': 10})" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "data[10]#查看这里的一些文档,我们可以对其中发生的事情有所了解" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5ef28a34", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Document(page_content=\"product_name: 旅行背包\\ndescription: 规格:\\n尺寸:18'' x 12'' x 6''。\\n\\n为什么我们热爱它:\\n我们的旅行背包拥有多个实用的内外袋,轻松装下您的必需品,是短途旅行的理想选择。\\n\\n材质与护理:\\n可以手洗,自然晾干。\\n\\n构造:\\n由防水尼龙制成。\\n\\n其他特性:\\n附带可调节背带和安全锁。\\n在中国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。\", metadata={'source': 'product_data.csv', 'row': 11})" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "data[11]" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "b5b994b0", + "metadata": {}, + "source": [ + "看上面的第一个文档中有高清电视机,第二个文档中有旅行背包,从这些细节中,我们可以创建一些例子查询和答案" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "0214fe01", + "metadata": {}, + "source": [ + "#### 6.2.1.2 创建测试用例数据\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8936f72d", + "metadata": {}, + "outputs": [], + "source": [ + "examples = [\n", + " {\n", + " \"query\": \"高清电视机怎么进行护理?\",\n", + " \"answer\": \"使用干布清洁。\"\n", + " },\n", + " {\n", + " \"query\": \"旅行背包有内外袋吗?\",\n", + " \"answer\": \"有。\"\n", + " }\n", + "]" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "27f3bc68", + "metadata": {}, + "source": [ + "#### 6.2.1.3 通过LLM生成测试用例" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d9c7342c", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.evaluation.qa import QAGenerateChain #导入QA生成链,它将接收文档,并从每个文档中创建一个问题答案对\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "d2117da8", + "metadata": {}, + "source": [ + "由于`QAGenerateChain`类中使用的`PROMPT`是英文,故我们继承`QAGenerateChain`类,将`PROMPT`加上“请使用中文输出”。" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "9e76a2ae", + "metadata": {}, + "source": [ + "下面是`generate_chain.py`文件中的`QAGenerateChain`类的源码" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "0401f602", + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"LLM Chain specifically for generating examples for question answering.\"\"\"\n", + "from __future__ import annotations\n", + "\n", + "from typing import Any\n", + "\n", + "from langchain.base_language import BaseLanguageModel\n", + "from langchain.chains.llm import LLMChain\n", + "from langchain.evaluation.qa.generate_prompt import PROMPT\n", + "\n", + "class QAGenerateChain(LLMChain):\n", + " \"\"\"LLM Chain specifically for generating examples for question answering.\"\"\"\n", + "\n", + " @classmethod\n", + " def from_llm(cls, llm: BaseLanguageModel, **kwargs: Any) -> QAGenerateChain:\n", + " \"\"\"Load QA Generate Chain from LLM.\"\"\"\n", + " return cls(llm=llm, prompt=PROMPT, **kwargs)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "9fb1e63e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "PromptTemplate(input_variables=['doc'], output_parser=RegexParser(regex='QUESTION: (.*?)\\\\nANSWER: (.*)', output_keys=['query', 'answer'], default_output_key=None), partial_variables={}, template='You are a teacher coming up with questions to ask on a quiz. \\nGiven the following document, please generate a question and answer based on that document.\\n\\nExample Format:\\n\\n...\\n\\nQUESTION: question here\\nANSWER: answer here\\n\\nThese questions should be detailed and be based explicitly on information in the document. Begin!\\n\\n\\n{doc}\\n\\n请用中文。', template_format='f-string', validate_template=True)" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PROMPT" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "13e72db4", + "metadata": {}, + "source": [ + "我们可以看到`PROMPT`为英文,下面我们将`PROMPT`添加上“请使用中文输出”" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "a33dc3af", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "PromptTemplate(input_variables=['doc'], output_parser=RegexParser(regex='QUESTION: (.*?)\\\\nANSWER: (.*)', output_keys=['query', 'answer'], default_output_key=None), partial_variables={}, template='You are a teacher coming up with questions to ask on a quiz. \\nGiven the following document, please generate a question and answer based on that document.\\n\\nExample Format:\\n\\n...\\n\\nQUESTION: question here\\nANSWER: answer here\\n\\nThese questions should be detailed and be based explicitly on information in the document. Begin!\\n\\n\\n{doc}\\n\\n请使用中文输出。\\n', template_format='f-string', validate_template=True)" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 下面是langchain.evaluation.qa.generate_prompt中的源码,我们在template的最后加上“请使用中文输出”\n", + "# flake8: noqa\n", + "from langchain.output_parsers.regex import RegexParser\n", + "from langchain.prompts import PromptTemplate\n", + "\n", + "template = \"\"\"You are a teacher coming up with questions to ask on a quiz. \n", + "Given the following document, please generate a question and answer based on that document.\n", + "\n", + "Example Format:\n", + "\n", + "...\n", + "\n", + "QUESTION: question here\n", + "ANSWER: answer here\n", + "\n", + "These questions should be detailed and be based explicitly on information in the document. Begin!\n", + "\n", + "\n", + "{doc}\n", + "\n", + "请使用中文输出。\n", + "\"\"\"\n", + "output_parser = RegexParser(\n", + " regex=r\"QUESTION: (.*?)\\nANSWER: (.*)\", output_keys=[\"query\", \"answer\"]\n", + ")\n", + "PROMPT = PromptTemplate(\n", + " input_variables=[\"doc\"], template=template, output_parser=output_parser\n", + ")\n", + "\n", + "PROMPT\n" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "# 继承QAGenerateChain\n", + "class MyQAGenerateChain(QAGenerateChain):\n", + " \"\"\"LLM Chain specifically for generating examples for question answering.\"\"\"\n", + "\n", + " @classmethod\n", + " def from_llm(cls, llm: BaseLanguageModel, **kwargs: Any) -> QAGenerateChain:\n", + " \"\"\"Load QA Generate Chain from LLM.\"\"\"\n", + " return cls(llm=llm, prompt=PROMPT, **kwargs)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "8dfc4372", + "metadata": {}, + "outputs": [], + "source": [ + "example_gen_chain = MyQAGenerateChain.from_llm(ChatOpenAI())#通过传递chat open AI语言模型来创建这个链" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "d25c6a0e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[Document(page_content=\"product_name: 全自动咖啡机\\ndescription: 规格:\\n大型 - 尺寸:13.8'' x 17.3''。\\n中型 - 尺寸:11.5'' x 15.2''。\\n\\n为什么我们热爱它:\\n这款全自动咖啡机是爱好者的理想选择。 一键操作,即可研磨豆子并沏制出您喜爱的咖啡。它的耐用性和一致性使它成为家庭和办公室的理想选择。\\n\\n材质与护理:\\n清洁时只需轻擦。\\n\\n构造:\\n由高品质不锈钢制成。\\n\\n其他特性:\\n内置研磨器和滤网。\\n预设多种咖啡模式。\\n在中国制造。\\n\\n有问题? 请随时联系我们的客户服务团队,他们会解答您的所有问题。\", metadata={'source': 'product_data.csv', 'row': 0}),\n", + " Document(page_content=\"product_name: 电动牙刷\\ndescription: 规格:\\n一般大小 - 高度:9.5'',宽度:1''。\\n\\n为什么我们热爱它:\\n我们的电动牙刷采用先进的刷头设计和强大的电机,为您提供超凡的清洁力和舒适的刷牙体验。\\n\\n材质与护理:\\n不可水洗,只需用湿布清洁。\\n\\n构造:\\n由食品级塑料和尼龙刷毛制成。\\n\\n其他特性:\\n具有多种清洁模式和定时功能。\\nUSB充电。\\n在日本制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。\", metadata={'source': 'product_data.csv', 'row': 1}),\n", + " Document(page_content='product_name: 橙味维生素C泡腾片\\ndescription: 规格:\\n每盒含有20片。\\n\\n为什么我们热爱它:\\n我们的橙味维生素C泡腾片是快速补充维生素C的理想方式。每片含有500mg的维生素C,可以帮助提升免疫力,保护您的健康。\\n\\n材质与护理:\\n请存放在阴凉干燥的地方,避免阳光直射。\\n\\n构造:\\n主要成分为维生素C和柠檬酸钠。\\n\\n其他特性:\\n含有天然橙味。\\n易于携带。\\n在美国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。', metadata={'source': 'product_data.csv', 'row': 2}),\n", + " Document(page_content=\"product_name: 无线蓝牙耳机\\ndescription: 规格:\\n单个耳机尺寸:1.5'' x 1.3''。\\n\\n为什么我们热爱它:\\n这款无线蓝牙耳机配备了降噪技术和长达8小时的电池续航力,让您无论在哪里都可以享受无障碍的音乐体验。\\n\\n材质与护理:\\n只需用湿布清洁。\\n\\n构造:\\n由耐用的塑料和金属构成,配备有软质耳塞。\\n\\n其他特性:\\n快速充电功能。\\n内置麦克风,支持接听电话。\\n在韩国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。\", metadata={'source': 'product_data.csv', 'row': 3}),\n", + " Document(page_content=\"product_name: 瑜伽垫\\ndescription: 规格:\\n尺寸:24'' x 68''。\\n\\n为什么我们热爱它:\\n我们的瑜伽垫拥有出色的抓地力和舒适度,无论是做瑜伽还是健身,都是理想的选择。\\n\\n材质与护理:\\n可用清水清洁,自然晾干。\\n\\n构造:\\n由环保PVC材料制成。\\n\\n其他特性:\\n附带便携包和绑带。\\n在印度制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。\", metadata={'source': 'product_data.csv', 'row': 4})]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "ef0f5cad", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "d:\\anaconda3\\envs\\gpt_flask\\lib\\site-packages\\langchain\\chains\\llm.py:303: UserWarning: The apply_and_parse method is deprecated, instead pass an output parser directly to LLMChain.\n", + " warnings.warn(\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + } + ], + "source": [ + "new_examples = example_gen_chain.apply_and_parse(\n", + " [{\"doc\": t} for t in data[:5]]\n", + ") #我们可以创建许多例子" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "7bc64a85", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'query': '这款全自动咖啡机的规格是什么?',\n", + " 'answer': \"大型尺寸为13.8'' x 17.3'',中型尺寸为11.5'' x 15.2''。\"},\n", + " {'query': '这款电动牙刷的高度和宽度分别是多少?', 'answer': '这款电动牙刷的高度是9.5英寸,宽度是1英寸。'},\n", + " {'query': '这款橙味维生素C泡腾片的规格是什么?', 'answer': '每盒含有20片。'},\n", + " {'query': '这款无线蓝牙耳机的尺寸是多少?', 'answer': \"这款无线蓝牙耳机的尺寸是1.5'' x 1.3''。\"},\n", + " {'query': '这款瑜伽垫的尺寸是多少?', 'answer': \"这款瑜伽垫的尺寸是24'' x 68''。\"}]" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_examples #查看用例数据" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "1e6b2fe4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'query': '这款全自动咖啡机的规格是什么?',\n", + " 'answer': \"大型尺寸为13.8'' x 17.3'',中型尺寸为11.5'' x 15.2''。\"}" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_examples[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "7ec72577", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Document(page_content=\"product_name: 全自动咖啡机\\ndescription: 规格:\\n大型 - 尺寸:13.8'' x 17.3''。\\n中型 - 尺寸:11.5'' x 15.2''。\\n\\n为什么我们热爱它:\\n这款全自动咖啡机是爱好者的理想选择。 一键操作,即可研磨豆子并沏制出您喜爱的咖啡。它的耐用性和一致性使它成为家庭和办公室的理想选择。\\n\\n材质与护理:\\n清洁时只需轻擦。\\n\\n构造:\\n由高品质不锈钢制成。\\n\\n其他特性:\\n内置研磨器和滤网。\\n预设多种咖啡模式。\\n在中国制造。\\n\\n有问题? 请随时联系我们的客户服务团队,他们会解答您的所有问题。\", metadata={'source': 'product_data.csv', 'row': 0})" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[0]" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "0f076f4c", + "metadata": {}, + "source": [ + "#### 6.2.1.4 组合用例数据" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "c0636823", + "metadata": {}, + "outputs": [], + "source": [ + "examples += new_examples" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "8d33b5de", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'高清电视机的护理非常简单。您只需要使用干布清洁即可。避免使用湿布或化学清洁剂,以免损坏电视机的表面。'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "qa.run(examples[0][\"query\"])" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "9737c0cc", + "metadata": {}, + "source": [ + "### 6.2.2 人工评估\n", + "现在有了这些示例,但是我们如何评估正在发生的事情呢?\n", + "通过运行一个示例通过链,并查看它产生的输出\n", + "在这里我们传递一个查询,然后我们得到一个答案。实际上正在发生的事情,进入语言模型的实际提示是什么? \n", + "它检索的文档是什么? \n", + "中间结果是什么? \n", + "仅仅查看最终答案通常不足以了解链中出现了什么问题或可能出现了什么问题" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "45f60c6e", + "metadata": {}, + "outputs": [], + "source": [ + "''' \n", + "LingChainDebug工具可以了解运行一个实例通过链中间所经历的步骤\n", + "'''\n", + "import langchain\n", + "langchain.debug = True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f216f9a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:RetrievalQA] Entering Chain run with input:\n", + "\u001b[0m{\n", + " \"query\": \"高清电视机怎么进行护理?\"\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain] Entering Chain run with input:\n", + "\u001b[0m[inputs]\n", + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain] Entering Chain run with input:\n", + "\u001b[0m{\n", + " \"question\": \"高清电视机怎么进行护理?\",\n", + " \"context\": \"product_name: 高清电视机\\ndescription: 规格:\\n尺寸:50''。\\n\\n为什么我们热爱它:\\n我们的高清电视机拥有出色的画质和强大的音效,带来沉浸式的观看体验。\\n\\n材质与护理:\\n使用干布清洁。\\n\\n构造:\\n由塑料、金属和电子元件制成。\\n\\n其他特性:\\n支持网络连接,可以在线观看视频。\\n配备遥控器。\\n在韩国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。<<<<>>>>>product_name: 空气净化器\\ndescription: 规格:\\n尺寸:15'' x 15'' x 20''。\\n\\n为什么我们热爱它:\\n我们的空气净化器采用了先进的HEPA过滤技术,能有效去除空气中的微粒和异味,为您提供清新的室内环境。\\n\\n材质与护理:\\n清洁时使用干布擦拭。\\n\\n构造:\\n由塑料和电子元件制成。\\n\\n其他特性:\\n三档风速,附带定时功能。\\n在德国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。<<<<>>>>>product_name: 宠物自动喂食器\\ndescription: 规格:\\n尺寸:14'' x 9'' x 15''。\\n\\n为什么我们热爱它:\\n我们的宠物自动喂食器可以定时定量投放食物,让您无论在家或外出都能确保宠物的饮食。\\n\\n材质与护理:\\n可用湿布清洁。\\n\\n构造:\\n由塑料和电子元件制成。\\n\\n其他特性:\\n配备LCD屏幕,操作简单。\\n可以设置多次投食。\\n在美国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。<<<<>>>>>product_name: 玻璃保护膜\\ndescription: 规格:\\n适用于各种尺寸的手机屏幕。\\n\\n为什么我们热爱它:\\n我们的玻璃保护膜可以有效防止手机屏幕刮伤和破裂,而且不影响触控的灵敏度。\\n\\n材质与护理:\\n使用干布擦拭。\\n\\n构造:\\n由高强度的玻璃材料制成。\\n\\n其他特性:\\n安装简单,适合自行安装。\\n在日本制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。\"\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain > 4:llm:ChatOpenAI] Entering LLM run with input:\n", + "\u001b[0m{\n", + " \"prompts\": [\n", + " \"System: Use the following pieces of context to answer the users question. \\nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\\n----------------\\nproduct_name: 高清电视机\\ndescription: 规格:\\n尺寸:50''。\\n\\n为什么我们热爱它:\\n我们的高清电视机拥有出色的画质和强大的音效,带来沉浸式的观看体验。\\n\\n材质与护理:\\n使用干布清洁。\\n\\n构造:\\n由塑料、金属和电子元件制成。\\n\\n其他特性:\\n支持网络连接,可以在线观看视频。\\n配备遥控器。\\n在韩国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。<<<<>>>>>product_name: 空气净化器\\ndescription: 规格:\\n尺寸:15'' x 15'' x 20''。\\n\\n为什么我们热爱它:\\n我们的空气净化器采用了先进的HEPA过滤技术,能有效去除空气中的微粒和异味,为您提供清新的室内环境。\\n\\n材质与护理:\\n清洁时使用干布擦拭。\\n\\n构造:\\n由塑料和电子元件制成。\\n\\n其他特性:\\n三档风速,附带定时功能。\\n在德国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。<<<<>>>>>product_name: 宠物自动喂食器\\ndescription: 规格:\\n尺寸:14'' x 9'' x 15''。\\n\\n为什么我们热爱它:\\n我们的宠物自动喂食器可以定时定量投放食物,让您无论在家或外出都能确保宠物的饮食。\\n\\n材质与护理:\\n可用湿布清洁。\\n\\n构造:\\n由塑料和电子元件制成。\\n\\n其他特性:\\n配备LCD屏幕,操作简单。\\n可以设置多次投食。\\n在美国制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。<<<<>>>>>product_name: 玻璃保护膜\\ndescription: 规格:\\n适用于各种尺寸的手机屏幕。\\n\\n为什么我们热爱它:\\n我们的玻璃保护膜可以有效防止手机屏幕刮伤和破裂,而且不影响触控的灵敏度。\\n\\n材质与护理:\\n使用干布擦拭。\\n\\n构造:\\n由高强度的玻璃材料制成。\\n\\n其他特性:\\n安装简单,适合自行安装。\\n在日本制造。\\n\\n有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。\\nHuman: 高清电视机怎么进行护理?\"\n", + " ]\n", + "}\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain > 4:llm:ChatOpenAI] [21.02s] Exiting LLM run with output:\n", + "\u001b[0m{\n", + " \"generations\": [\n", + " [\n", + " {\n", + " \"text\": \"高清电视机的护理非常简单。您只需要使用干布清洁即可。避免使用湿布或化学清洁剂,以免损坏电视机的表面。\",\n", + " \"generation_info\": null,\n", + " \"message\": {\n", + " \"content\": \"高清电视机的护理非常简单。您只需要使用干布清洁即可。避免使用湿布或化学清洁剂,以免损坏电视机的表面。\",\n", + " \"additional_kwargs\": {},\n", + " \"example\": false\n", + " }\n", + " }\n", + " ]\n", + " ],\n", + " \"llm_output\": {\n", + " \"token_usage\": {\n", + " \"prompt_tokens\": 823,\n", + " \"completion_tokens\": 58,\n", + " \"total_tokens\": 881\n", + " },\n", + " \"model_name\": \"gpt-3.5-turbo\"\n", + " },\n", + " \"run\": null\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain > 3:chain:LLMChain] [21.02s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"text\": \"高清电视机的护理非常简单。您只需要使用干布清洁即可。避免使用湿布或化学清洁剂,以免损坏电视机的表面。\"\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA > 2:chain:StuffDocumentsChain] [21.02s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"output_text\": \"高清电视机的护理非常简单。您只需要使用干布清洁即可。避免使用湿布或化学清洁剂,以免损坏电视机的表面。\"\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQA] [21.81s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"result\": \"高清电视机的护理非常简单。您只需要使用干布清洁即可。避免使用湿布或化学清洁剂,以免损坏电视机的表面。\"\n", + "}\n" + ] + }, + { + "data": { + "text/plain": [ + "'高清电视机的护理非常简单。您只需要使用干布清洁即可。避免使用湿布或化学清洁剂,以免损坏电视机的表面。'" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "qa.run(examples[0][\"query\"])#重新运行与上面相同的示例,可以看到它开始打印出更多的信息" + ] + }, + { + "cell_type": "markdown", + "id": "24536d9b", + "metadata": {}, + "source": [ + "我们可以看到它首先深入到检索QA链中,然后它进入了一些文档链。如上所述,我们正在使用stuff方法,现在我们正在传递这个上下文,可以看到,这个上下文是由我们检索到的不同文档创建的。因此,在进行问答时,当返回错误结果时,通常不是语言模型本身出错了,实际上是检索步骤出错了,仔细查看问题的确切内容和上下文可以帮助调试出错的原因。 \n", + "然后,我们可以再向下一级,看看进入语言模型的确切内容,以及 OpenAI 自身,在这里,我们可以看到传递的完整提示,我们有一个系统消息,有所使用的提示的描述,这是问题回答链使用的提示,我们可以看到提示打印出来,使用以下上下文片段回答用户的问题。\n", + "如果您不知道答案,只需说您不知道即可,不要试图编造答案。然后我们看到一堆之前插入的上下文,我们还可以看到有关实际返回类型的更多信息。我们不仅仅返回一个答案,还有token的使用情况,可以了解到token数的使用情况\n", + "\n", + "\n", + "由于这是一个相对简单的链,我们现在可以看到最终的响应,舒适的毛衣套装,条纹款,有侧袋,正在起泡,通过链返回给用户,我们刚刚讲解了如何查看和调试单个输入到该链的情况。\n", + "\n", + "\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "9f3ebc93", + "metadata": {}, + "source": [ + "#### 6.2.2.1 如何评估新创建的实例\n", + "与创建它们类似,可以运行链条来处理所有示例,然后查看输出并尝试弄清楚,发生了什么,它是否正确" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a32fbb61", + "metadata": {}, + "outputs": [], + "source": [ + "# 我们需要为所有示例创建预测,关闭调试模式,以便不将所有内容打印到屏幕上\n", + "langchain.debug = False" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "dd57860a", + "metadata": {}, + "source": [ + "### 6.2.3 通过LLM进行评估实例" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "40d908c9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 16.0 seconds as it raised APIError: Bad gateway. {\"error\":{\"code\":502,\"message\":\"Bad gateway.\",\"param\":null,\"type\":\"cf_bad_gateway\"}} 502 {'error': {'code': 502, 'message': 'Bad gateway.', 'param': None, 'type': 'cf_bad_gateway'}} {'Date': 'Thu, 29 Jun 2023 02:20:57 GMT', 'Content-Type': 'application/json', 'Content-Length': '84', 'Connection': 'keep-alive', 'X-Frame-Options': 'SAMEORIGIN', 'Referrer-Policy': 'same-origin', 'Cache-Control': 'private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Expires': 'Thu, 01 Jan 1970 00:00:01 GMT', 'Server': 'cloudflare', 'CF-RAY': '7deaa9d4cd2640da-SIN', 'alt-svc': 'h3=\":443\"; ma=86400'}.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + } + ], + "source": [ + "predictions = qa.apply(examples) #为所有不同的示例创建预测" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "c71d3d2f", + "metadata": {}, + "outputs": [], + "source": [ + "''' \n", + "对预测的结果进行评估,导入QA问题回答,评估链,通过语言模型创建此链\n", + "'''\n", + "from langchain.evaluation.qa import QAEvalChain #导入QA问题回答,评估链" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "ac1c848f", + "metadata": {}, + "outputs": [], + "source": [ + "#通过调用chatGPT进行评估\n", + "llm = ChatOpenAI(temperature=0)\n", + "eval_chain = QAEvalChain.from_llm(llm)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "50c1250a", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 16.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 16.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 16.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 16.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + } + ], + "source": [ + "graded_outputs = eval_chain.evaluate(examples, predictions)#在此链上调用evaluate,进行评估" + ] + }, + { + "cell_type": "markdown", + "id": "72dc8595", + "metadata": {}, + "source": [ + "##### 6.2.3.1 评估思路\n", + "当它面前有整个文档时,它可以生成一个真实的答案,我们将打印出预测的答,当它进行QA链时,使用embedding和向量数据库进行检索时,将其传递到语言模型中,然后尝试猜测预测的答案,我们还将打印出成绩,这也是语言模型生成的。当它要求评估链评估正在发生的事情时,以及它是否正确或不正确。因此,当我们循环遍历所有这些示例并将它们打印出来时,可以详细了解每个示例" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "bf21e40a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Example 0:\n", + "Question: 高清电视机怎么进行护理?\n", + "Real Answer: 使用干布清洁。\n", + "Predicted Answer: 高清电视机的护理非常简单。您只需要使用干布清洁即可。避免使用湿布或化学清洁剂,以免损坏电视机的表面。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 1:\n", + "Question: 旅行背包有内外袋吗?\n", + "Real Answer: 有。\n", + "Predicted Answer: 是的,旅行背包有多个实用的内外袋,可以轻松装下您的必需品。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 2:\n", + "Question: 这款全自动咖啡机有什么特点和优势?\n", + "Real Answer: 这款全自动咖啡机的特点和优势包括一键操作、内置研磨器和滤网、预设多种咖啡模式、耐用性和一致性,以及由高品质不锈钢制成的构造。\n", + "Predicted Answer: 这款全自动咖啡机有以下特点和优势:\n", + "1. 一键操作:只需按下按钮,即可研磨咖啡豆并沏制出您喜爱的咖啡,非常方便。\n", + "2. 耐用性和一致性:这款咖啡机具有耐用性和一致性,使其成为家庭和办公室的理想选择。\n", + "3. 内置研磨器和滤网:咖啡机内置研磨器和滤网,可以确保咖啡的新鲜和口感。\n", + "4. 多种咖啡模式:咖啡机预设了多种咖啡模式,可以根据个人口味选择不同的咖啡。\n", + "5. 高品质材料:咖啡机由高品质不锈钢制成,具有优良的耐用性和质感。\n", + "6. 中国制造:这款咖啡机是在中国制造的,具有可靠的品质保证。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 3:\n", + "Question: 这款电动牙刷的规格是什么?\n", + "Real Answer: 这款电动牙刷的规格是一般大小,高度为9.5英寸,宽度为1英寸。\n", + "Predicted Answer: 这款电动牙刷的规格是:高度为9.5英寸,宽度为1英寸。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 4:\n", + "Question: 这款橙味维生素C泡腾片的规格是什么?\n", + "Real Answer: 这款橙味维生素C泡腾片每盒含有20片。\n", + "Predicted Answer: 这款橙味维生素C泡腾片的规格是每盒含有20片。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 5:\n", + "Question: 这款无线蓝牙耳机的规格是什么?\n", + "Real Answer: 单个耳机尺寸为1.5'' x 1.3''。\n", + "Predicted Answer: 这款无线蓝牙耳机的规格是单个耳机尺寸为1.5'' x 1.3''。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 6:\n", + "Question: 这个产品的名称是什么?\n", + "Real Answer: 瑜伽垫\n", + "Predicted Answer: 这个产品的名称是儿童益智玩具。\n", + "Predicted Grade: INCORRECT\n", + "\n", + "Example 7:\n", + "Question: 这款全自动咖啡机的规格是什么?\n", + "Real Answer: 大型尺寸为13.8'' x 17.3'',中型尺寸为11.5'' x 15.2''。\n", + "Predicted Answer: 这款全自动咖啡机有两种规格:\n", + "- 大型尺寸为13.8'' x 17.3''。\n", + "- 中型尺寸为11.5'' x 15.2''。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 8:\n", + "Question: 这款电动牙刷的高度和宽度分别是多少?\n", + "Real Answer: 这款电动牙刷的高度是9.5英寸,宽度是1英寸。\n", + "Predicted Answer: 这款电动牙刷的高度是9.5英寸,宽度是1英寸。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 9:\n", + "Question: 这款橙味维生素C泡腾片的规格是什么?\n", + "Real Answer: 每盒含有20片。\n", + "Predicted Answer: 这款橙味维生素C泡腾片的规格是每盒含有20片。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 10:\n", + "Question: 这款无线蓝牙耳机的尺寸是多少?\n", + "Real Answer: 这款无线蓝牙耳机的尺寸是1.5'' x 1.3''。\n", + "Predicted Answer: 这款无线蓝牙耳机的尺寸是1.5'' x 1.3''。\n", + "Predicted Grade: CORRECT\n", + "\n", + "Example 11:\n", + "Question: 这款瑜伽垫的尺寸是多少?\n", + "Real Answer: 这款瑜伽垫的尺寸是24'' x 68''。\n", + "Predicted Answer: 这款瑜伽垫的尺寸是24'' x 68''。\n", "Predicted Grade: CORRECT\n", "\n" ] @@ -952,16 +2342,40 @@ { "attachments": {}, "cell_type": "markdown", - "id": "87ecb476", + "id": "ece7c7b4", "metadata": {}, "source": [ - "#### 结果分析\n", + "#### 6.2.3.2 结果分析\n", "对于每个示例,它看起来都是正确的,让我们看看第一个例子。\n", - "这里的问题是,舒适的套头衫套装,有侧口袋吗?真正的答案,我们创建了这个,是肯定的。模型预测的答案是舒适的套头衫套装条纹,确实有侧口袋。因此,我们可以理解这是一个正确的答案。它将其评为正确。 \n", - "#### 使用模型评估的优势\n", + "这里的问题是,旅行背包有内外袋吗?真正的答案,我们创建了这个,是肯定的。模型预测的答案是是的,旅行背包有多个实用的内外袋,可以轻松装下您的必需品。因此,我们可以理解这是一个正确的答案。它将其评为正确。 \n", + "#### 6.2.3.3 使用模型评估的优势\n", "\n", "你有这些答案,它们是任意的字符串。没有单一的真实字符串是最好的可能答案,有许多不同的变体,只要它们具有相同的语义,它们应该被评为相似。如果使用正则进行精准匹配就会丢失语义信息,到目前为止存在的许多评估指标都不够好。目前最有趣和最受欢迎的之一就是使用语言模型进行评估。" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "314095a6", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cafb368e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a9e546e4", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -980,7 +2394,25 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.9.12" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": { + "height": "calc(100% - 180px)", + "left": "10px", + "top": "150px", + "width": "261.818px" + }, + "toc_section_display": true, + "toc_window_display": true } }, "nbformat": 4, diff --git a/content/LangChain for LLM Application Development/7.代理.ipynb b/content/LangChain for LLM Application Development/7.代理.ipynb index b7fe19b..d746699 100644 --- a/content/LangChain for LLM Application Development/7.代理.ipynb +++ b/content/LangChain for LLM Application Development/7.代理.ipynb @@ -1,24 +1,34 @@ { "cells": [ { + "attachments": {}, "cell_type": "markdown", "id": "2caa79ba-45e3-437c-9cb6-e433f443f0bf", "metadata": {}, "source": [ - "# 代理\n", + "# 7. 代理\n", + "\n", + "\n", + "\n", "\n", "大语言模型学习并记住许多的网络公开信息,大语言模型最常见的应用场景是,将它当作知识库,让它对给定的问题做出回答。\n", "\n", "另一种思路是将大语言模型当作推理引擎,让它基于已有的知识库,并利用新的信息(新的大段文本或者其他信息)来帮助回答问题或者进行推理LongChain的内置代理工具便是适用该场景。\n", "\n", - "本节我们将会了解什么是代理,如何创建代理, 如何使用代理,以及如何与不同类型的工具集成,例如搜索引擎。\n", - "\n", - "**目录**\n", - "* 使用LangChain内置工具\n", - " * 使用llm-math和wikipedia工具\n", - " * 使用PythonREPLTool工具\n", - "* 定义自己的工具并在代理中使用\n", - " * 创建和使用自定义时间工具" + "本节我们将会了解什么是代理,如何创建代理, 如何使用代理,以及如何与不同类型的工具集成,例如搜索引擎。" ] }, { @@ -38,16 +48,17 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "631c764b-68fa-483a-80a5-9a322cd1117c", "metadata": {}, "source": [ - "## LangChain内置工具" + "## 7.1 LangChain内置工具" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "8ab03352-1554-474c-afda-ec8974250e31", "metadata": {}, "outputs": [], @@ -60,7 +71,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "bc80a8a7-a66e-4fbb-8d57-05004a3e77c7", "metadata": {}, "outputs": [], @@ -72,14 +83,16 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "d5a3655c-4d5e-4a86-8bf4-a11bd1525059", "metadata": {}, "source": [ - "### 📚 使用llm-math和wikipedia工具" + "### 7.1.1 使用llm-math和wikipedia工具" ] }, { + "attachments": {}, "cell_type": "markdown", "id": "e484d285-2110-4a06-a360-55313d6d9ffc", "metadata": {}, @@ -93,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 4, "id": "71e5894f-fc50-478b-a3d3-58891cc46ffc", "metadata": {}, "outputs": [], @@ -103,6 +116,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "d7f75023-1825-4d74-bc8e-2c362f551fd1", "metadata": { @@ -116,7 +130,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 5, "id": "3541b866-8a35-4b6f-bfe6-66edb1d666cd", "metadata": {}, "outputs": [], @@ -128,6 +142,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "e5b4fcc8-8817-4a94-b154-3f328480e441", "metadata": {}, @@ -141,7 +156,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 6, "id": "e7cdc782-2b8c-4649-ae1e-cabacee1a757", "metadata": {}, "outputs": [], @@ -156,6 +171,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "b980137a-a1d2-4c19-80c8-ec380f9c1efe", "metadata": { @@ -167,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "id": "1e2bcd3b-ae72-4fa1-b38e-d0ce650e1f31", "metadata": {}, "outputs": [ @@ -177,22 +193,26 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mThought: We need to calculate 25% of 300, which involves multiplication and division.\n", - "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mQuestion: 计算300的25%\n", + "Thought: 我可以使用计算器来计算这个百分比\n", "Action:\n", "```\n", "{\n", " \"action\": \"Calculator\",\n", - " \"action_input\": \"300*0.25\"\n", + " \"action_input\": \"300 * 25 / 100\"\n", "}\n", "```\n", - "\n", "\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mAnswer: 75.0\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3mWe have the answer to the question.\n", - "\n", - "Final Answer: 75.0\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m我现在知道最终答案了\n", + "Final Answer: 300的25%是75.0\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -200,19 +220,20 @@ { "data": { "text/plain": [ - "{'input': 'What is the 25% of 300?', 'output': '75.0'}" + "{'input': '计算300的25%,请用中文', 'output': '300的25%是75.0'}" ] }, - "execution_count": 9, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "agent(\"What is the 25% of 300?\")" + "agent(\"计算300的25%,思考过程请使用中文。\") " ] }, { + "attachments": {}, "cell_type": "markdown", "id": "05bb0811-d71e-4016-868b-efbb651d8e59", "metadata": {}, @@ -237,6 +258,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "bdd13d06-0f0e-4918-ac28-339f524f8c76", "metadata": {}, @@ -246,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 18, "id": "f43b5cbf-1011-491b-90e8-09cf903fb866", "metadata": {}, "outputs": [ @@ -256,28 +278,272 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mThought: I should use Wikipedia to find the answer to this question.\n", - "\n", - "Action:\n", - "```\n", - "{\n", - " \"action\": \"Wikipedia\",\n", - " \"action_input\": \"Tom M. Mitchell\"\n", - "}\n", - "```\n", - "\n", - "\u001b[0m\n", - "Observation: \u001b[33;1m\u001b[1;3mPage: Tom M. Mitchell\n", - "Summary: Tom Michael Mitchell (born August 9, 1951) is an American computer scientist and the Founders University Professor at Carnegie Mellon University (CMU). He is a founder and former Chair of the Machine Learning Department at CMU. Mitchell is known for his contributions to the advancement of machine learning, artificial intelligence, and cognitive neuroscience and is the author of the textbook Machine Learning. He is a member of the United States National Academy of Engineering since 2010. He is also a Fellow of the American Academy of Arts and Sciences, the American Association for the Advancement of Science and a Fellow and past President of the Association for the Advancement of Artificial Intelligence. In October 2018, Mitchell was appointed as the Interim Dean of the School of Computer Science at Carnegie Mellon.\n", - "\n", - "Page: Tom Mitchell (Australian footballer)\n", - "Summary: Thomas Mitchell (born 31 May 1993) is a professional Australian rules footballer playing for the Collingwood Football Club in the Australian Football League (AFL). He previously played for the Sydney Swans from 2012 to 2016, and the Hawthorn Football Club between 2017 and 2022. Mitchell won the Brownlow Medal as the league's best and fairest player in 2018 and set the record for the most disposals in a VFL/AFL match, accruing 54 in a game against Collingwood during that season.\n", - "\n", - "\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3mThe book that Tom M. Mitchell wrote is \"Machine Learning\".\n", - "\n", - "Final Answer: Machine Learning.\u001b[0m\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3mI need to find out what book Tom M. Mitchell wrote.\n", + "Action: Search for Tom M. Mitchell's books.\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for Tom M. Mitchell's books. is not a valid tool, try another one.\n", + "Thought:\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3mI can try searching for Tom M. Mitchell's books using a search engine.\n", + "Action: Search for \"Tom M. Mitchell books\"\n", + "Action Input: \"Tom M. Mitchell books\"\u001b[0m\n", + "Observation: Search for \"Tom M. Mitchell books\" is not a valid tool, try another one.\n", + "Thought:\u001b[32;1m\u001b[1;3m\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -286,10 +552,10 @@ "data": { "text/plain": [ "{'input': 'Tom M. Mitchell is an American computer scientist and the Founders University Professor at Carnegie Mellon University (CMU)what book did he write?',\n", - " 'output': 'Machine Learning.'}" + " 'output': 'Agent stopped due to iteration limit or time limit.'}" ] }, - "execution_count": 29, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -302,6 +568,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "39e2f6fb-f229-4235-ad58-98a5f505800c", "metadata": {}, @@ -328,14 +595,16 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "5901cab6-a7c9-4590-b35d-d41c29e39a39", "metadata": {}, "source": [ - "### 📚 使用PythonREPLTool工具" + "### 7.1.2 使用PythonREPLTool工具" ] }, { + "attachments": {}, "cell_type": "markdown", "id": "12b8d837-524c-46b8-9189-504808cf1f93", "metadata": {}, @@ -345,7 +614,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 8, "id": "6c60d933-168d-4834-b29c-b2b43ade80cc", "metadata": {}, "outputs": [], @@ -361,6 +630,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "d32ed40c-cbcd-4efd-b044-57e611f5fab5", "metadata": { @@ -372,24 +642,27 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 14, "id": "3d416f28-8fdb-4761-a4f9-d46c6d37fb3d", "metadata": {}, "outputs": [], "source": [ - "customer_list = [[\"Harrison\", \"Chase\"], \n", - " [\"Lang\", \"Chain\"],\n", - " [\"Dolly\", \"Too\"],\n", - " [\"Elle\", \"Elem\"], \n", - " [\"Geoff\",\"Fusion\"], \n", - " [\"Trance\",\"Former\"],\n", - " [\"Jen\",\"Ayai\"]\n", - " ]" + "customer_list = [\"小明\",\"小黄\",\"小红\",\"小蓝\",\"小橘\",\"小绿\",]" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 22, + "id": "d0fbfc4c", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install -q pinyin" + ] + }, + { + "cell_type": "code", + "execution_count": 23, "id": "ba7f9a50-acf6-4fba-a1b9-503baa80266b", "metadata": {}, "outputs": [ @@ -399,26 +672,44 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mI 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.\n", - "Action: Python REPL\n", - "Action Input:\n", - "```\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", - "```\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3m['Jen', 'Ayai']\n", - "['Lang', 'Chain']\n", - "['Harrison', 'Chase']\n", - "['Elle', 'Elem']\n", - "['Trance', 'Former']\n", - "['Geoff', 'Fusion']\n", - "['Dolly', 'Too']\n", - "\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3mThe customers are now sorted by last name and then first name. \n", - "Final Answer: [['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]\u001b[0m\n", + "\u001b[1m> Entering new chain...\u001b[0m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3m我需要使用拼音库来将客户名字转换为拼音。我可以使用Python的pinyin库来实现这个功能。\n", + "Action: Python_REPL\n", + "Action Input: import pinyin\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m我现在可以使用pinyin库来将客户名字转换为拼音了。\n", + "Action: Python_REPL\n", + "Action Input: names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m我现在可以使用pinyin库将names列表中的客户名字转换为拼音了。\n", + "Action: Python_REPL\n", + "Action Input: pinyin_names = [pinyin.get(i, format='strip', delimiter='') for i in names]\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n", + "Thought:" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3m我已经成功将客户名字转换为拼音并存储在pinyin_names列表中了。\n", + "Final Answer: pinyin_names = ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaojv', 'xiaolv']\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -426,66 +717,22 @@ { "data": { "text/plain": [ - "\"[['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]\"" + "\"pinyin_names = ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaojv', 'xiaolv']\"" ] }, - "execution_count": 33, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "agent.run(f\"\"\"Sort these customers by \\\n", - "last name and then first name \\\n", - "and print the output: {customer_list}\"\"\") " - ] - }, - { - "cell_type": "markdown", - "id": "c6c6fd4b-1e9f-4f37-9a0f-3f69cc121ab0", - "metadata": { - "tags": [] - }, - "source": [ - "✅ **总结**\n", - "\n", - "1. 模型对于接下来需要做什么,给出思考(Thought) \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", - "3. 模型得到观察(Observation)\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", - "4. 基于观察,模型对于接下来需要做什么,给出思考(Thought)\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 +744,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "951048dc-0414-410d-a0e9-6ef32bbdda89", "metadata": { @@ -527,7 +775,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 25, "id": "92b8961b-b990-4540-a7f9-e4701dfaa616", "metadata": {}, "outputs": [ @@ -537,11 +785,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 +799,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 +819,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 +847,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.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] [12.76s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", - " \"text\": \"The customers are now sorted by last name and then first name.\\nFinal Answer: [['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]\",\n", + " \"text\": \"我现在可以使用pinyin库来将客户名字转换为拼音。\\nAction: Python_REPL\\nAction Input: names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\\npinyin_names = [pinyin.get(i, format='strip', delimiter='') for i in names]\\nprint(pinyin_names)\",\n", " \"generation_info\": null,\n", " \"message\": {\n", - " \"content\": \"The customers are now sorted by last name and then first name.\\nFinal Answer: [['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]\",\n", + " \"content\": \"我现在可以使用pinyin库来将客户名字转换为拼音。\\nAction: Python_REPL\\nAction Input: names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\\npinyin_names = [pinyin.get(i, format='strip', delimiter='') for i in names]\\nprint(pinyin_names)\",\n", " \"additional_kwargs\": {},\n", " \"example\": false\n", " }\n", @@ -622,20 +882,87 @@ " ],\n", " \"llm_output\": {\n", " \"token_usage\": {\n", - " \"prompt_tokens\": 521,\n", - " \"completion_tokens\": 67,\n", - " \"total_tokens\": 588\n", + " \"prompt_tokens\": 379,\n", + " \"completion_tokens\": 89,\n", + " \"total_tokens\": 468\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 > 5:chain:LLMChain] [6.94s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 5:chain:LLMChain] [12.77s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"text\": \"The customers are now sorted by last name and then first name.\\nFinal Answer: [['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]\"\n", + " \"text\": \"我现在可以使用pinyin库来将客户名字转换为拼音。\\nAction: Python_REPL\\nAction Input: names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\\npinyin_names = [pinyin.get(i, format='strip', delimiter='') for i in names]\\nprint(pinyin_names)\"\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor] [19.20s] Exiting Chain run with output:\n", + "\u001b[32;1m\u001b[1;3m[tool/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 7:tool:Python_REPL] Entering Tool run with input:\n", + "\u001b[0m\"names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\n", + "pinyin_names = [pinyin.get(i, format='strip', delimiter='') for i in names]\n", + "print(pinyin_names)\"\n", + "\u001b[36;1m\u001b[1;3m[tool/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 7:tool:Python_REPL] [0.0ms] Exiting Tool run with output:\n", + "\u001b[0m\"['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']\"\n", + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:chain:LLMChain] Entering Chain run with input:\n", "\u001b[0m{\n", - " \"output\": \"[['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]\"\n", + " \"input\": \"在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\",\n", + " \"agent_scratchpad\": \"我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\\nObservation: \\nThought:我现在可以使用pinyin库来将客户名字转换为拼音。\\nAction: Python_REPL\\nAction Input: names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\\npinyin_names = [pinyin.get(i, format='strip', delimiter='') for i in names]\\nprint(pinyin_names)\\nObservation: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']\\n\\nThought:\",\n", + " \"stop\": [\n", + " \"\\nObservation:\",\n", + " \"\\n\\tObservation:\"\n", + " ]\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:chain:LLMChain > 9: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: 在这些客户名字转换为拼音并打印输出列表: ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']思考过程请使用中文。\\nThought:我需要使用拼音转换库来将这些客户名字转换为拼音。我可以使用Python的pinyin库来完成这个任务。\\nAction: Python_REPL\\nAction Input: import pinyin\\nObservation: \\nThought:我现在可以使用pinyin库来将客户名字转换为拼音。\\nAction: Python_REPL\\nAction Input: names = ['小明', '小黄', '小红', '小蓝', '小橘', '小绿']\\npinyin_names = [pinyin.get(i, format='strip', delimiter='') for i in names]\\nprint(pinyin_names)\\nObservation: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']\\n\\nThought:\"\n", + " ]\n", + "}\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-2YlJyPMl62f07XPJCAlXfDxj on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:chain:LLMChain > 9:llm:ChatOpenAI] [20.25s] Exiting LLM run with output:\n", + "\u001b[0m{\n", + " \"generations\": [\n", + " [\n", + " {\n", + " \"text\": \"我现在知道了最终答案。\\nFinal Answer: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']\",\n", + " \"generation_info\": null,\n", + " \"message\": {\n", + " \"content\": \"我现在知道了最终答案。\\nFinal Answer: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']\",\n", + " \"additional_kwargs\": {},\n", + " \"example\": false\n", + " }\n", + " }\n", + " ]\n", + " ],\n", + " \"llm_output\": {\n", + " \"token_usage\": {\n", + " \"prompt_tokens\": 500,\n", + " \"completion_tokens\": 43,\n", + " \"total_tokens\": 543\n", + " },\n", + " \"model_name\": \"gpt-3.5-turbo\"\n", + " },\n", + " \"run\": null\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:chain:LLMChain] [20.25s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"text\": \"我现在知道了最终答案。\\nFinal Answer: ['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']\"\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor] [35.93s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"output\": \"['xiaoming', 'xiaohuang', 'xiaohong', 'xiaolan', 'xiaoju', 'xiaolv']\"\n", "}\n" ] } @@ -643,25 +970,26 @@ "source": [ "import langchain\n", "langchain.debug=True\n", - "agent.run(f\"\"\"Sort these customers by \\\n", - "last name and then first name \\\n", - "and print the output: {customer_list}\"\"\") \n", + "agent.run(f\"\"\"在这些客户名字转换为拼音\\\n", + "并打印输出列表: {customer_list}\\\n", + "思考过程请使用中文。\"\"\") \n", "langchain.debug=False" ] }, { + "attachments": {}, "cell_type": "markdown", "id": "6b2ba50d-53c4-4eab-9467-98f562a072cc", "metadata": { "tags": [] }, "source": [ - "## 定义自己的工具并在代理中使用" + "## 7.2 定义自己的工具并在代理中使用" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 26, "id": "d8e88bdf-01d2-4adc-b2af-086ece6ba97d", "metadata": {}, "outputs": [], @@ -671,16 +999,17 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "fb92f6e4-21ab-494d-9c22-d0678050fd37", "metadata": {}, "source": [ - "### 📚 创建和使用自定义时间工具" + "### 7.2.1 创建和使用自定义时间工具" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 27, "id": "4b7e80e4-48d5-48f0-abeb-3dc33678ed9e", "metadata": {}, "outputs": [], @@ -691,6 +1020,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "8990b3c6-fe05-45b6-9567-06baec267a99", "metadata": {}, @@ -714,23 +1044,23 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 28, "id": "e22a823f-3749-4bda-bac3-1b892da77929", "metadata": {}, "outputs": [], "source": [ "@tool\n", "def time(text: str) -> str:\n", - " \"\"\"Returns todays date, use this for any \\\n", - " questions related to knowing todays date. \\\n", - " The input should always be an empty string, \\\n", - " and this function will always return todays \\\n", - " date - any date mathmatics should occur \\\n", - " outside this function.\"\"\"\n", + " \"\"\"\n", + " 返回今天的日期,用于任何需要知道今天日期的问题。\\\n", + " 输入应该总是一个空字符串,\\\n", + " 这个函数将总是返回今天的日期,任何日期计算应该在这个函数之外进行。\n", + " \"\"\"\n", " return str(date.today())" ] }, { + "attachments": {}, "cell_type": "markdown", "id": "440cb348-0ec6-4999-a6cd-81cb48b8c546", "metadata": {}, @@ -740,14 +1070,14 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 36, "id": "09c9f59e-c14c-41b9-b6bd-0e97e8be72ab", "metadata": {}, "outputs": [], "source": [ "agent= initialize_agent(\n", - " tools + [time], #将刚刚创建的时间工具加入到已有的工具中\n", - " llm, #初始化的模型\n", + " tools=[time], #将刚刚创建的时间工具加入代理\n", + " llm=llm, #初始化的模型\n", " agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, #代理类型\n", " handle_parsing_errors=True, #处理解析错误\n", " verbose = True #输出中间步骤\n", @@ -755,6 +1085,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "f1a10657-23f2-4f78-a247-73f272119eb4", "metadata": {}, @@ -765,7 +1096,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 37, "id": "f868e30b-ec67-4d76-9dc9-81ecd3332115", "metadata": {}, "outputs": [ @@ -775,8 +1106,11 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mThought: I need to use the `time` tool to get today's date.\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3m根据提供的工具,我们可以使用`time`函数来获取今天的日期。\n", + "\n", + "Thought: 使用`time`函数来获取今天的日期。\n", + "\n", "Action:\n", "```\n", "{\n", @@ -784,10 +1118,11 @@ " \"action_input\": \"\"\n", "}\n", "```\n", + "\n", "\u001b[0m\n", - "Observation: \u001b[38;5;200m\u001b[1;3m2023-06-03\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3mI have successfully retrieved today's date using the `time` tool.\n", - "Final Answer: Today's date is 2023-06-03.\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m2023-07-04\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m我现在知道了最终答案。\n", + "Final Answer: 今天的日期是2023-07-04。\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -795,19 +1130,20 @@ { "data": { "text/plain": [ - "{'input': 'whats the date today?', 'output': \"Today's date is 2023-06-03.\"}" + "{'input': '今天的日期是?', 'output': '今天的日期是2023-07-04。'}" ] }, - "execution_count": 39, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "agent(\"whats the date today?\") " + "agent(\"今天的日期是?\") " ] }, { + "attachments": {}, "cell_type": "markdown", "id": "923a199f-08e8-4e70-afb5-70cd3e123acf", "metadata": {}, @@ -822,7 +1158,7 @@ " \n", " \n", "3. 模型得到观察(Observation)\n", - "

观测: 2023-06-03

\n", + "

观测: 2023-07-04

\n", " \n", " \n", " \n", @@ -830,7 +1166,7 @@ "

思考: 我已成功使用 time 工具检索到了今天的日期

\n", "\n", "5. 给出最终答案(Final Answer)\n", - "

最终答案: 今天的日期是2023-06-03.

\n", + "

最终答案: 今天的日期是2023-07-04.

\n", "6. 返回最终答案。" ] } @@ -851,7 +1187,20 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.12" + "version": "3.9.16" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true } }, "nbformat": 4, diff --git a/content/LangChain for LLM Application Development/8.总结.ipynb b/content/LangChain for LLM Application Development/8.总结.ipynb new file mode 100644 index 0000000..1a3e885 --- /dev/null +++ b/content/LangChain for LLM Application Development/8.总结.ipynb @@ -0,0 +1,64 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "87f7cfaa", + "metadata": {}, + "source": [ + "# 8. 总结\n", + "\n", + "\n", + "本次简短课程涵盖了一系列LangChain的应用实践,包括处理顾客评论和基于文档回答问题,以及通过LLM判断何时求助外部工具 (如网站) 来回答复杂问题。\n", + "\n", + "**👍🏻 LangChain如此强大**\n", + "\n", + "构建这类应用曾经需要耗费数周时间,而现在只需要非常少的代码,就可以通过LangChain高效构建所需的应用程序。LangChain已成为开发大模型应用的有力范式,希望大家拥抱这个强大工具,积极探索更多更广泛的应用场景。\n", + "\n", + "**🌈 不同组合, 更多可能性**\n", + "\n", + "LangChain还可以协助我们做什么呢:基于CSV文件回答问题、查询sql数据库、与api交互,有很多例子通过Chain以及不同的提示(Prompts)和输出解析器(output parsers)组合得以实现。\n", + "\n", + "**💪🏻 出发 去探索新世界吧**\n", + "\n", + "因此非常感谢社区中做出贡献的每一个人,无论是协助文档的改进,还是让其他人更容易上手,还是构建新的Chain打开一个全新的世界。\n", + "\n", + "如果你还没有这样做,快去打开电脑,运行 pip install LangChain,然后去使用LangChain、搭建惊艳的应用吧~\n", + "\n" + ] + } + ], + "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.9.12" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/content/LangChain for LLM Application Development/8.课程总结.md b/content/LangChain for LLM Application Development/8.课程总结.md deleted file mode 100644 index ce57191..0000000 --- a/content/LangChain for LLM Application Development/8.课程总结.md +++ /dev/null @@ -1,20 +0,0 @@ -## 吴恩达 LangChain大模型应用开发 总结篇 - -## LangChain for LLM Application Development - -本次简短课程涵盖了一系列LangChain的应用实践,包括处理顾客评论和基于文档回答问题,以及通过LLM判断何时求助外部工具 (如网站) 来回答复杂问题。 - -### 👍🏻 LangChain如此强大 - -构建这类应用曾经需要耗费数周时间,而现在只需要非常少的代码,就可以通过LangChain高效构建所需的应用程序。LangChain已成为开发大模型应用的有力范式,希望大家拥抱这个强大工具,积极探索更多更广泛的应用场景。 - -### 🌈 不同组合->更多可能性 - -LangChain还可以协助我们做什么呢:基于CSV文件回答问题、查询sql数据库、与api交互,有很多例子通过Chain以及不同的提示(Prompts)和输出解析器(output parsers)组合得以实现。 - -### 💪🏻 出发~去探索新世界吧~ - -因此非常感谢社区中做出贡献的每一个人,无论是协助文档的改进,还是让其他人更容易上手,还是构建新的Chain打开一个全新的世界。 - -如果你还没有这样做,快去打开电脑,运行 pip install LangChain,然后去使用LangChain、搭建惊艳的应用吧~ - 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。 + +为什么我们热爱它: +我们的水晶酒杯采用高品质水晶玻璃制成,造型优雅,是享受美酒的理想选择。 + +材质与护理: +建议手洗。 + +构造: +由水晶玻璃制成。 + +其他特性: +适合各种场合使用。 +在法国制造。 + +有问题?请随时联系我们的客户服务团队,他们会解答您的所有问题。" diff --git a/content/LangChain for LLM Application Development/readme.md b/content/LangChain for LLM Application Development/readme.md index 2a99c32..ea3b506 100644 --- a/content/LangChain for LLM Application Development/readme.md +++ b/content/LangChain for LLM Application Development/readme.md @@ -4,7 +4,7 @@ ### 目录 1. 简介 Introduction @Sarai -2. 模型,提示和解析器 Models, Prompts and Output Parsers @Joye +2. 模型、提示和解析器 Models, Prompts and Output Parsers @Joye 3. 存储 Memory @徐虎 4. 模型链 Chains @徐虎 5. 基于文档的问答 Question and Answer @苟晓攀