Files
prompt-engineering-for-deve…/content/LangChain for LLM Application Development/2.模型、提示和解析器 Models, Prompts and Output Parsers.ipynb
2023-07-13 22:55:45 +08:00

1 line
70 KiB
Plaintext

{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# \u7b2c\u4e8c\u7ae0 \u6a21\u578b\uff0c\u63d0\u793a\u548c\u8f93\u51fa\u89e3\u91ca\u5668\n", "\n", " - [\u4e00\u3001\u8bbe\u7f6eOpenAI API Key](#\u4e00\u3001\u8bbe\u7f6eOpenAI-API-Key)\n", " - [\u4e8c\u3001\u76f4\u63a5\u4f7f\u7528OpenAI](#\u4e8c\u3001\u76f4\u63a5\u4f7f\u7528OpenAI)\n", " - [2.1 \u8ba1\u7b971+1](#2.1-\u8ba1\u7b971+1)\n", " - [2.2 \u7528\u7f8e\u5f0f\u82f1\u8bed\u8868\u8fbe\u6d77\u76d7\u90ae\u4ef6](#2.2-\u7528\u7f8e\u5f0f\u82f1\u8bed\u8868\u8fbe\u6d77\u76d7\u90ae\u4ef6)\n", " - [2.3 \u4e2d\u6587\u7248](#2.3-\u4e2d\u6587\u7248)\n", " - [\u4e09\u3001\u901a\u8fc7LangChain\u4f7f\u7528OpenAI](#\u4e09\u3001\u901a\u8fc7LangChain\u4f7f\u7528OpenAI)\n", " - [3.1 \u6a21\u578b](#3.1-\u6a21\u578b)\n", " - [3.2 \u63d0\u793a\u6a21\u677f](#3.2-\u63d0\u793a\u6a21\u677f)\n", " - [3.3 \u8f93\u51fa\u89e3\u6790\u5668](#3.3-\u8f93\u51fa\u89e3\u6790\u5668)\n", " - [\u56db\u3001\u8865\u5145\u6750\u6599](#\u56db\u3001\u8865\u5145\u6750\u6599)\n", " - [4.1 \u94fe\u5f0f\u601d\u8003\u63a8\u7406(ReAct)](#4.1-\u94fe\u5f0f\u601d\u8003\u63a8\u7406(ReAct))\n"]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["## \u4e00\u3001\u8bbe\u7f6eOpenAI API Key\n", "\n", "\u767b\u9646 [OpenAI \u8d26\u6237](https://platform.openai.com/account/api-keys) \u83b7\u53d6API Key\uff0c\u7136\u540e\u5c06\u5176\u8bbe\u7f6e\u4e3a\u73af\u5883\u53d8\u91cf\u3002\n", "\n", "- \u5982\u679c\u4f60\u60f3\u8981\u8bbe\u7f6e\u4e3a\u5168\u5c40\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u53c2\u8003[\u77e5\u4e4e\u6587\u7ae0](https://zhuanlan.zhihu.com/p/627665725)\u3002\n", "- \u5982\u679c\u4f60\u60f3\u8981\u8bbe\u7f6e\u4e3a\u672c\u5730/\u9879\u76ee\u73af\u5883\u53d8\u91cf\uff0c\u5728\u672c\u6587\u4ef6\u76ee\u5f55\u4e0b\u521b\u5efa`.env`\u6587\u4ef6, \u6253\u5f00\u6587\u4ef6\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\u3002\n", "\n", " <p style=\"font-family:verdana; font-size:12px;color:green\">\n", " OPENAI_API_KEY=\"your_api_key\" \n", " </p>\n", " \n", " \u66ff\u6362\"your_api_key\"\u4e3a\u4f60\u81ea\u5df1\u7684 API Key"]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": ["# \u4e0b\u8f7d\u9700\u8981\u7684\u5305python-dotenv\u548copenai\n", "# \u5982\u679c\u4f60\u9700\u8981\u67e5\u770b\u5b89\u88c5\u8fc7\u7a0b\u65e5\u5fd7\uff0c\u53ef\u5220\u9664 -q \n", "!pip install -q python-dotenv\n", "!pip install -q openai"]}, {"cell_type": "code", "execution_count": 2, "metadata": {"tags": []}, "outputs": [], "source": ["import os\n", "import openai\n", "from dotenv import load_dotenv, find_dotenv\n", "\n", "# \u8bfb\u53d6\u672c\u5730/\u9879\u76ee\u7684\u73af\u5883\u53d8\u91cf\u3002\n", "\n", "# find_dotenv()\u5bfb\u627e\u5e76\u5b9a\u4f4d.env\u6587\u4ef6\u7684\u8def\u5f84\n", "# load_dotenv()\u8bfb\u53d6\u8be5.env\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u4e2d\u7684\u73af\u5883\u53d8\u91cf\u52a0\u8f7d\u5230\u5f53\u524d\u7684\u8fd0\u884c\u73af\u5883\u4e2d \n", "# \u5982\u679c\u4f60\u8bbe\u7f6e\u7684\u662f\u5168\u5c40\u7684\u73af\u5883\u53d8\u91cf\uff0c\u8fd9\u884c\u4ee3\u7801\u5219\u6ca1\u6709\u4efb\u4f55\u4f5c\u7528\u3002\n", "_ = load_dotenv(find_dotenv())\n", "\n", "# \u83b7\u53d6\u73af\u5883\u53d8\u91cf OPENAI_API_KEY\n", "openai.api_key = os.environ['OPENAI_API_KEY'] "]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["## \u4e8c\u3001\u76f4\u63a5\u4f7f\u7528OpenAI\n", "\n", "\u6211\u4eec\u5148\u4ece\u76f4\u63a5\u8c03\u7528OpenAI\u7684API\u5f00\u59cb\u3002\n", "\n", "`get_completion`\u51fd\u6570\u662f\u57fa\u4e8e`openai`\u7684\u5c01\u88c5\u51fd\u6570\uff0c\u5bf9\u4e8e\u7ed9\u5b9a\u63d0\u793a\uff08prompt\uff09\u8f93\u51fa\u76f8\u5e94\u7684\u56de\u7b54\u3002\u5176\u5305\u542b\u4e24\u4e2a\u53c2\u6570\n", " \n", " - `prompt` \u5fc5\u9700\u8f93\u5165\u53c2\u6570\u3002 \u4f60\u7ed9\u6a21\u578b\u7684**\u63d0\u793a\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u662f\u4f60\u9700\u8981\u6a21\u578b\u5e2e\u52a9\u4f60\u505a\u7684\u4e8b**\uff08\u6539\u53d8\u6587\u672c\u5199\u4f5c\u98ce\u683c\uff0c\u7ffb\u8bd1\uff0c\u56de\u590d\u6d88\u606f\u7b49\u7b49\uff09\u3002\n", " - `model` \u975e\u5fc5\u9700\u8f93\u5165\u53c2\u6570\u3002\u9ed8\u8ba4\u4f7f\u7528gpt-3.5-turbo\u3002\u4f60\u4e5f\u53ef\u4ee5\u9009\u62e9\u5176\u4ed6\u6a21\u578b\u3002\n", " \n", "\u8fd9\u91cc\u7684\u63d0\u793a\u5bf9\u5e94\u6211\u4eec\u7ed9chatgpt\u7684\u95ee\u9898\uff0c\u51fd\u6570\u7ed9\u51fa\u7684\u8f93\u51fa\u5219\u5bf9\u5e94chatpgt\u7ed9\u6211\u4eec\u7684\u7b54\u6848\u3002"]}, {"cell_type": "code", "execution_count": 3, "metadata": {"tags": []}, "outputs": [], "source": ["def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n", " \n", " messages = [{\"role\": \"user\", \"content\": prompt}]\n", " \n", " response = openai.ChatCompletion.create(\n", " model=model,\n", " messages=messages,\n", " temperature=0, \n", " )\n", " return response.choices[0].message[\"content\"]"]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["### 2.1 \u8ba1\u7b971+1\n", "\n", "\u6211\u4eec\u6765\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50 - \u5206\u522b\u7528\u4e2d\u82f1\u6587\u95ee\u95ee\u6a21\u578b\n", "\n", "- \u4e2d\u6587\u63d0\u793a(Prompt in Chinese)\uff1a `1+1\u662f\u4ec0\u4e48\uff1f`\n", "- \u82f1\u6587\u63d0\u793a(Prompt in English)\uff1a `What is 1+1?`"]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [{"data": {"text/plain": ["'1+1\u7b49\u4e8e2\u3002'"]}, "execution_count": 4, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u4e2d\u6587\n", "get_completion(\"1+1\u662f\u4ec0\u4e48\uff1f\")"]}, {"cell_type": "code", "execution_count": 5, "metadata": {"tags": []}, "outputs": [{"data": {"text/plain": ["'1+1 equals 2.'"]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u82f1\u6587\n", "get_completion(\"What is 1+1?\")"]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["### 2.2 \u7528\u7f8e\u5f0f\u82f1\u8bed\u8868\u8fbe\u6d77\u76d7\u90ae\u4ef6\n", "\n", "\u4e0a\u9762\u7684\u7b80\u5355\u4f8b\u5b50\uff0c\u6a21\u578b`gpt-3.5-turbo`\u5bf9\u6211\u4eec\u7684\u5173\u4e8e1+1\u662f\u4ec0\u4e48\u7684\u63d0\u95ee\u7ed9\u51fa\u4e86\u56de\u7b54\u3002\n", "\n", "\u73b0\u5728\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\uff1a \n", "\n", "\u5047\u8bbe\u6211\u4eec\u662f\u7535\u5546\u516c\u53f8\u5458\u5de5\uff0c\u6211\u4eec\u7684\u987e\u5ba2\u662f\u4e00\u540d\u6d77\u76d7A\uff0c\u4ed6\u5728\u6211\u4eec\u7684\u7f51\u7ad9\u4e0a\u4e70\u4e86\u4e00\u4e2a\u69a8\u6c41\u673a\u7528\u6765\u505a\u5976\u6614\uff0c\u5728\u5236\u4f5c\u5976\u6614\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5976\u6614\u7684\u76d6\u5b50\u98de\u4e86\u51fa\u53bb\uff0c\u5f04\u5f97\u53a8\u623f\u5899\u4e0a\u5230\u5904\u90fd\u662f\u3002\u4e8e\u662f\u6d77\u76d7A\u7ed9\u6211\u4eec\u7684\u5ba2\u670d\u4e2d\u5fc3\u5199\u6765\u4ee5\u4e0b\u90ae\u4ef6\uff1a`customer_email`"]}, {"cell_type": "code", "execution_count": 6, "metadata": {"tags": []}, "outputs": [], "source": ["customer_email = \"\"\"\n", "Arrr, I be fuming that me blender lid \\\n", "flew off and splattered me kitchen walls \\\n", "with smoothie! And to make matters worse,\\\n", "the warranty don't cover the cost of \\\n", "cleaning up me kitchen. I need yer help \\\n", "right now, matey!\n", "\"\"\""]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u6211\u4eec\u7684\u5ba2\u670d\u4eba\u5458\u5bf9\u4e8e\u6d77\u76d7\u7684\u63aa\u8f9e\u8868\u8fbe\u89c9\u5f97\u6709\u70b9\u96be\u4ee5\u7406\u89e3\u3002 \u73b0\u5728\u6211\u4eec\u60f3\u8981\u5b9e\u73b0\u4e24\u4e2a\u5c0f\u76ee\u6807\uff1a\n", "\n", "- \u8ba9\u6a21\u578b\u7528\u7f8e\u5f0f\u82f1\u8bed\u7684\u8868\u8fbe\u65b9\u5f0f\u5c06\u6d77\u76d7\u7684\u90ae\u4ef6\u8fdb\u884c\u7ffb\u8bd1\uff0c\u5ba2\u670d\u4eba\u5458\u53ef\u4ee5\u66f4\u597d\u7406\u89e3\u3002*\u8fd9\u91cc\u6d77\u76d7\u7684\u82f1\u6587\u8868\u8fbe\u53ef\u4ee5\u7406\u89e3\u4e3a\u82f1\u6587\u7684\u65b9\u8a00\uff0c\u5176\u4e0e\u7f8e\u5f0f\u82f1\u8bed\u7684\u5173\u7cfb\uff0c\u5c31\u5982\u56db\u5ddd\u8bdd\u4e0e\u666e\u901a\u8bdd\u7684\u5173\u7cfb\u3002\n", "- \u8ba9\u6a21\u578b\u5728\u7ffb\u8bd1\u662f\u7528\u5e73\u548c\u5c0a\u91cd\u7684\u8bed\u6c14\u8fdb\u884c\u8868\u8fbe\uff0c\u5ba2\u670d\u4eba\u5458\u7684\u5fc3\u60c5\u4e5f\u4f1a\u66f4\u597d\u3002\n", "\n", "\u6839\u636e\u8fd9\u4e24\u4e2a\u5c0f\u76ee\u6807\uff0c\u5b9a\u4e49\u4e00\u4e0b\u6587\u672c\u8868\u8fbe\u98ce\u683c\uff1a`style`"]}, {"cell_type": "code", "execution_count": 7, "metadata": {"tags": []}, "outputs": [], "source": ["# \u7f8e\u5f0f\u82f1\u8bed + \u5e73\u9759\u3001\u5c0a\u656c\u7684\u8bed\u8c03\n", "style = \"\"\"American English \\\n", "in a calm and respectful tone\n", "\"\"\""]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u4e0b\u4e00\u6b65\u9700\u8981\u505a\u7684\u662f\u5c06`customer_email`\u548c`style`\u7ed3\u5408\u8d77\u6765\u6784\u9020\u6211\u4eec\u7684\u63d0\u793a:`prompt`"]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": ["# \u975e\u6b63\u5f0f\u7528\u8bed\n", "customer_email = \"\"\" \n", "\u963f\uff0c\u6211\u5f88\u751f\u6c14\uff0c\\\n", "\u56e0\u4e3a\u6211\u7684\u6405\u62cc\u673a\u76d6\u6389\u4e86\uff0c\\\n", "\u628a\u5976\u6614\u6e85\u5230\u4e86\u53a8\u623f\u7684\u5899\u4e0a\uff01\\\n", "\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4fdd\u4fee\u4e0d\u5305\u62ec\u6253\u626b\u53a8\u623f\u7684\u8d39\u7528\u3002\\\n", "\u6211\u73b0\u5728\u9700\u8981\u4f60\u7684\u5e2e\u52a9\uff0c\u4f19\u8ba1\uff01\n", "\"\"\""]}, {"cell_type": "code", "execution_count": 9, "metadata": {"tags": []}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Translate the text that is delimited by triple backticks \n", "into a style that is American English in a calm and respectful tone\n", ".\n", "text: ``` \n", "\u963f\uff0c\u6211\u5f88\u751f\u6c14\uff0c\u56e0\u4e3a\u6211\u7684\u6405\u62cc\u673a\u76d6\u6389\u4e86\uff0c\u628a\u5976\u6614\u6e85\u5230\u4e86\u53a8\u623f\u7684\u5899\u4e0a\uff01\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4fdd\u4fee\u4e0d\u5305\u62ec\u6253\u626b\u53a8\u623f\u7684\u8d39\u7528\u3002\u6211\u73b0\u5728\u9700\u8981\u4f60\u7684\u5e2e\u52a9\uff0c\u4f19\u8ba1\uff01\n", "```\n", "\n"]}], "source": ["# \u8981\u6c42\u6a21\u578b\u6839\u636e\u7ed9\u51fa\u7684\u8bed\u8c03\u8fdb\u884c\u8f6c\u5316\n", "prompt = f\"\"\"Translate the text \\\n", "that is delimited by triple backticks \n", "into a style that is {style}.\n", "text: ```{customer_email}```\n", "\"\"\"\n", "\n", "print(prompt)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["`prompt` \u6784\u9020\u597d\u4e86\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528`get_completion`\u5f97\u5230\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c - \u7528\u5e73\u548c\u5c0a\u91cd\u7684\u8bed\u6c14\uff0c\u7f8e\u5f0f\u82f1\u8bed\u8868\u8fbe\u7684\u6d77\u76d7\u8bed\u8a00\u90ae\u4ef6"]}, {"cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": ["response = get_completion(prompt)"]}, {"cell_type": "code", "execution_count": 11, "metadata": {"tags": []}, "outputs": [{"data": {"text/plain": ["\"Oh, I'm really frustrated because the lid of my blender fell off and splattered the milkshake all over the kitchen wall! To make matters worse, the warranty doesn't cover the cost of cleaning the kitchen. I could really use your help right now, buddy!\""]}, "execution_count": 11, "metadata": {}, "output_type": "execute_result"}], "source": ["response"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5bf9\u6bd4\u8bed\u8a00\u98ce\u683c\u8f6c\u6362\u524d\u540e\uff0c\u7528\u8bcd\u66f4\u4e3a\u6b63\u5f0f\uff0c\u66ff\u6362\u4e86\u6781\u7aef\u60c5\u7eea\u7684\u8868\u8fbe\uff0c\u5e76\u8868\u8fbe\u4e86\u611f\u8c22\u3002\n", "\n", "\u2728 \u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4fee\u6539\u63d0\u793a\uff0c\u770b\u53ef\u4ee5\u5f97\u5230\u4ec0\u4e48\u4e0d\u4e00\u6837\u7684\u7ed3\u679c\ud83d\ude09"]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["### 2.3 \u4e2d\u6587\u7248"]}, {"cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": ["# \u666e\u901a\u8bdd + \u5e73\u9759\u3001\u5c0a\u656c\u7684\u8bed\u8c03\n", "style = \"\"\"\u6b63\u5f0f\u666e\u901a\u8bdd \\\n", "\u7528\u4e00\u4e2a\u5e73\u9759\u3001\u5c0a\u656c\u7684\u8bed\u8c03\n", "\"\"\""]}, {"cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u628a\u7531\u4e09\u4e2a\u53cd\u5f15\u53f7\u5206\u9694\u7684\u6587\u672c\u7ffb\u8bd1\u6210\u4e00\u79cd\u6b63\u5f0f\u666e\u901a\u8bdd \u7528\u4e00\u4e2a\u5e73\u9759\u3001\u5c0a\u656c\u7684\u8bed\u8c03\n", "\u98ce\u683c\u3002\n", "\u6587\u672c: ``` \n", "\u963f\uff0c\u6211\u5f88\u751f\u6c14\uff0c\u56e0\u4e3a\u6211\u7684\u6405\u62cc\u673a\u76d6\u6389\u4e86\uff0c\u628a\u5976\u6614\u6e85\u5230\u4e86\u53a8\u623f\u7684\u5899\u4e0a\uff01\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4fdd\u4fee\u4e0d\u5305\u62ec\u6253\u626b\u53a8\u623f\u7684\u8d39\u7528\u3002\u6211\u73b0\u5728\u9700\u8981\u4f60\u7684\u5e2e\u52a9\uff0c\u4f19\u8ba1\uff01\n", "```\n", "\n"]}], "source": ["# \u8981\u6c42\u6a21\u578b\u6839\u636e\u7ed9\u51fa\u7684\u8bed\u8c03\u8fdb\u884c\u8f6c\u5316\n", "prompt = f\"\"\"\u628a\u7531\u4e09\u4e2a\u53cd\u5f15\u53f7\u5206\u9694\u7684\u6587\u672c\\\n", "\u7ffb\u8bd1\u6210\u4e00\u79cd{style}\u98ce\u683c\u3002\n", "\u6587\u672c: ```{customer_email}```\n", "\"\"\"\n", "\n", "print(prompt)\n", "\n"]}, {"cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [{"data": {"text/plain": ["'\u5c0a\u656c\u7684\u670b\u53cb\u4eec\uff0c\u6211\u611f\u5230\u975e\u5e38\u4e0d\u5b89\uff0c\u56e0\u4e3a\u6211\u7684\u6405\u62cc\u673a\u76d6\u5b50\u4e0d\u614e\u6389\u843d\uff0c\u5bfc\u81f4\u5976\u6614\u6e85\u5230\u4e86\u53a8\u623f\u7684\u5899\u58c1\u4e0a\uff01\u66f4\u52a0\u4ee4\u4eba\u7cdf\u5fc3\u7684\u662f\uff0c\u4fdd\u4fee\u670d\u52a1\u5e76\u4e0d\u5305\u542b\u53a8\u623f\u6e05\u6d01\u7684\u8d39\u7528\u3002\u6b64\u523b\uff0c\u6211\u771f\u8bda\u5730\u8bf7\u6c42\u5404\u4f4d\u7684\u5e2e\u52a9\uff0c\u670b\u53cb\u4eec\uff01'"]}, "execution_count": 14, "metadata": {}, "output_type": "execute_result"}], "source": ["response = get_completion(prompt)\n", "\n", "response"]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["## \u4e09\u3001\u901a\u8fc7LangChain\u4f7f\u7528OpenAI\n", "\n", "\u5728\u524d\u9762\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u901a\u8fc7\u5c01\u88c5\u51fd\u6570`get_completion`\u76f4\u63a5\u8c03\u7528\u4e86OpenAI\u5b8c\u6210\u4e86\u5bf9\u65b9\u8a00\u90ae\u4ef6\u8fdb\u884c\u4e86\u7684\u7ffb\u8bd1\uff0c\u5f97\u5230\u7528\u5e73\u548c\u5c0a\u91cd\u7684\u8bed\u6c14\u3001\u6b63\u5f0f\u7684\u666e\u901a\u8bdd\u8868\u8fbe\u7684\u90ae\u4ef6\u3002\n", "\n", "\u8ba9\u6211\u4eec\u5c1d\u8bd5\u4f7f\u7528LangChain\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002"]}, {"cell_type": "code", "execution_count": 15, "metadata": {"tags": []}, "outputs": [], "source": ["# \u5982\u679c\u4f60\u9700\u8981\u67e5\u770b\u5b89\u88c5\u8fc7\u7a0b\u65e5\u5fd7\uff0c\u53ef\u5220\u9664 -q \n", "# --upgrade \u8ba9\u6211\u4eec\u53ef\u4ee5\u5b89\u88c5\u5230\u6700\u65b0\u7248\u672c\u7684 langchain\n", "!pip install -q --upgrade langchain"]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["### 3.1 \u6a21\u578b\n", "\n", "\u4ece`langchain.chat_models`\u5bfc\u5165`OpenAI`\u7684\u5bf9\u8bdd\u6a21\u578b`ChatOpenAI`\u3002 \u9664\u53bbOpenAI\u4ee5\u5916\uff0c`langchain.chat_models`\u8fd8\u96c6\u6210\u4e86\u5176\u4ed6\u5bf9\u8bdd\u6a21\u578b\uff0c\u66f4\u591a\u7ec6\u8282\u53ef\u4ee5\u67e5\u770b[Langchain\u5b98\u65b9\u6587\u6863](https://python.langchain.com/en/latest/modules/models/chat/integrations.html)\u3002"]}, {"cell_type": "code", "execution_count": 16, "metadata": {"tags": []}, "outputs": [], "source": ["from langchain.chat_models import ChatOpenAI"]}, {"cell_type": "code", "execution_count": 17, "metadata": {"tags": []}, "outputs": [{"data": {"text/plain": ["ChatOpenAI(cache=None, verbose=False, callbacks=None, callback_manager=None, tags=None, metadata=None, client=<class 'openai.api_resources.chat_completion.ChatCompletion'>, model_name='gpt-3.5-turbo', temperature=0.0, model_kwargs={}, openai_api_key='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', openai_api_base='', openai_organization='', openai_proxy='', request_timeout=None, max_retries=6, streaming=False, n=1, max_tokens=None, tiktoken_model_name=None)"]}, "execution_count": 17, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u8fd9\u91cc\u6211\u4eec\u5c06\u53c2\u6570temperature\u8bbe\u7f6e\u4e3a0.0\uff0c\u4ece\u800c\u51cf\u5c11\u751f\u6210\u7b54\u6848\u7684\u968f\u673a\u6027\u3002\n", "# \u5982\u679c\u4f60\u60f3\u8981\u6bcf\u6b21\u5f97\u5230\u4e0d\u4e00\u6837\u7684\u6709\u65b0\u610f\u7684\u7b54\u6848\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u8c03\u6574\u8be5\u53c2\u6570\u3002\n", "chat = ChatOpenAI(temperature=0.0)\n", "chat"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u4e0a\u9762\u7684\u8f93\u51fa\u663e\u793aChatOpenAI\u7684\u9ed8\u8ba4\u6a21\u578b\u4e3a`gpt-3.5-turbo`"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### 3.2 \u63d0\u793a\u6a21\u677f\n", "\n", "\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7[f\u5b57\u7b26\u4e32](https://docs.python.org/zh-cn/3/tutorial/inputoutput.html#tut-f-strings)\u628aPython\u8868\u8fbe\u5f0f\u7684\u503c`style`\u548c`customer_email`\u6dfb\u52a0\u5230`prompt`\u5b57\u7b26\u4e32\u5185\u3002\n", "\n", "```python\n", "prompt = f\"\"\"Translate the text \\\n", "that is delimited by triple backticks \n", "into a style that is {style}.\n", "text: ```{customer_email}```\n", "\"\"\"\n", "```\n", "`langchain`\u63d0\u4f9b\u4e86\u63a5\u53e3\u65b9\u4fbf\u5feb\u901f\u7684\u6784\u9020\u548c\u4f7f\u7528\u63d0\u793a\u3002\u73b0\u5728\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u4f7f\u7528`langchain`\u6765\u6784\u9020\u63d0\u793a\u3002"]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["#### 3.2.1 \u4f7f\u7528LangChain\u63d0\u793a\u6a21\u7248"]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["##### 1\ufe0f\u20e3 \u6784\u9020\u63d0\u793a\u6a21\u7248\u5b57\u7b26\u4e32\n", "\u6211\u4eec\u6784\u9020\u4e00\u4e2a\u63d0\u793a\u6a21\u7248\u5b57\u7b26\u4e32\uff1a`template_string`"]}, {"cell_type": "code", "execution_count": 18, "metadata": {"tags": []}, "outputs": [], "source": ["template_string = \"\"\"Translate the text \\\n", "that is delimited by triple backticks \\\n", "into a style that is {style}. \\\n", "text: ```{text}```\n", "\"\"\""]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["##### 2\ufe0f\u20e3 \u6784\u9020LangChain\u63d0\u793a\u6a21\u7248\n", "\u6211\u4eec\u8c03\u7528`ChatPromptTemplatee.from_template()`\u51fd\u6570\u5c06\u4e0a\u9762\u7684\u63d0\u793a\u6a21\u7248\u5b57\u7b26`template_string`\u8f6c\u6362\u4e3a\u63d0\u793a\u6a21\u7248`prompt_template`"]}, {"cell_type": "code", "execution_count": 19, "metadata": {"tags": []}, "outputs": [], "source": ["from langchain.prompts import ChatPromptTemplate\n", "prompt_template = ChatPromptTemplate.from_template(template_string)"]}, {"cell_type": "code", "execution_count": 20, "metadata": {"tags": []}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["input_variables=['style', 'text'] output_parser=None partial_variables={} template='Translate the text that is delimited by triple backticks into a style that is {style}. text: ```{text}```\\n' template_format='f-string' validate_template=True\n"]}], "source": ["print(prompt_template.messages[0].prompt)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u4ece\u4e0a\u9762\u7684\u8f93\u51fa\u53ef\u4ee5\u770b\u51fa\uff0c`prompt_template` \u6709\u4e24\u4e2a\u8f93\u5165\u53d8\u91cf\uff1a `style` \u548c `text`\u3002"]}, {"cell_type": "code", "execution_count": 21, "metadata": {"tags": []}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["['style', 'text']\n"]}], "source": ["print(prompt_template.messages[0].prompt.input_variables)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 3\ufe0f\u20e3 \u4f7f\u7528\u6a21\u7248\u5f97\u5230\u5ba2\u6237\u6d88\u606f\u63d0\u793a\n", "\n", "langchain\u63d0\u793a\u6a21\u7248`prompt_template`\u9700\u8981\u4e24\u4e2a\u8f93\u5165\u53d8\u91cf\uff1a `style` \u548c `text`\u3002 \u8fd9\u91cc\u5206\u522b\u5bf9\u5e94 \n", "- `customer_style`: \u6211\u4eec\u60f3\u8981\u7684\u987e\u5ba2\u90ae\u4ef6\u98ce\u683c\n", "- `customer_email`: \u987e\u5ba2\u7684\u539f\u59cb\u90ae\u4ef6\u6587\u672c\u3002"]}, {"cell_type": "code", "execution_count": 22, "metadata": {"tags": []}, "outputs": [], "source": ["customer_style = \"\"\"American English \\\n", "in a calm and respectful tone\n", "\"\"\""]}, {"cell_type": "code", "execution_count": 23, "metadata": {"tags": []}, "outputs": [], "source": ["customer_email = \"\"\"\n", "Arrr, I be fuming that me blender lid \\\n", "flew off and splattered me kitchen walls \\\n", "with smoothie! And to make matters worse, \\\n", "the warranty don't cover the cost of \\\n", "cleaning up me kitchen. I need yer help \\\n", "right now, matey!\n", "\"\"\""]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5bf9\u4e8e\u7ed9\u5b9a\u7684`customer_style`\u548c`customer_email`, \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u63d0\u793a\u6a21\u7248`prompt_template`\u7684`format_messages`\u65b9\u6cd5\u751f\u6210\u60f3\u8981\u7684\u5ba2\u6237\u6d88\u606f`customer_messages`\u3002"]}, {"cell_type": "code", "execution_count": 24, "metadata": {"tags": []}, "outputs": [], "source": ["customer_messages = prompt_template.format_messages(\n", " style=customer_style,\n", " text=customer_email)"]}, {"cell_type": "code", "execution_count": 25, "metadata": {"tags": []}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["<class 'list'>\n", "<class 'langchain.schema.messages.HumanMessage'>\n"]}], "source": ["print(type(customer_messages))\n", "print(type(customer_messages[0]))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u53ef\u4ee5\u770b\u51fa`customer_messages`\u53d8\u91cf\u7c7b\u578b\u4e3a\u5217\u8868(`list`)\uff0c\u800c\u5217\u8868\u91cc\u7684\u5143\u7d20\u53d8\u91cf\u7c7b\u578b\u4e3alangchain\u81ea\u5b9a\u4e49\u6d88\u606f(`langchain.schema.HumanMessage`)\u3002\n", "\n", "\u6253\u5370\u7b2c\u4e00\u4e2a\u5143\u7d20\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b:"]}, {"cell_type": "code", "execution_count": 26, "metadata": {"tags": []}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["content=\"Translate the text that is delimited by triple backticks into a style that is American English in a calm and respectful tone\\n. text: ```\\nArrr, I be fuming that me blender lid flew off and splattered me kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\\n```\\n\" additional_kwargs={} example=False\n"]}], "source": ["print(customer_messages[0])"]}, {"cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["content='\u628a\u7531\u4e09\u4e2a\u53cd\u5f15\u53f7\u5206\u9694\u7684\u6587\u672c\u7ffb\u8bd1\u6210\u4e00\u79cd\u6b63\u5f0f\u666e\u901a\u8bdd \u7528\u4e00\u4e2a\u5e73\u9759\u3001\u5c0a\u656c\u7684\u8bed\u6c14\\n\u98ce\u683c\u3002\u6587\u672c: ```\\n\u963f\uff0c\u6211\u5f88\u751f\u6c14\uff0c\u56e0\u4e3a\u6211\u7684\u6405\u62cc\u673a\u76d6\u6389\u4e86\uff0c\u628a\u5976\u6614\u6e85\u5230\u4e86\u53a8\u623f\u7684\u5899\u4e0a\uff01\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4fdd\u4fee\u4e0d\u5305\u62ec\u6253\u626b\u53a8\u623f\u7684\u8d39\u7528\u3002\u6211\u73b0\u5728\u9700\u8981\u4f60\u7684\u5e2e\u52a9\uff0c\u4f19\u8ba1\uff01\\n```\\n' additional_kwargs={} example=False\n"]}], "source": ["# \u4e2d\u6587\u63d0\u793a\n", "from langchain.prompts import ChatPromptTemplate\n", "\n", "template_string = \"\"\"\u628a\u7531\u4e09\u4e2a\u53cd\u5f15\u53f7\u5206\u9694\u7684\u6587\u672c\\\n", "\u7ffb\u8bd1\u6210\u4e00\u79cd{style}\u98ce\u683c\u3002\\\n", "\u6587\u672c: ```{text}```\n", "\"\"\"\n", "prompt_template = ChatPromptTemplate.from_template(template_string)\n", "\n", "customer_style = \"\"\"\u6b63\u5f0f\u666e\u901a\u8bdd \\\n", "\u7528\u4e00\u4e2a\u5e73\u9759\u3001\u5c0a\u656c\u7684\u8bed\u6c14\n", "\"\"\"\n", "\n", "customer_email = \"\"\"\n", "\u963f\uff0c\u6211\u5f88\u751f\u6c14\uff0c\\\n", "\u56e0\u4e3a\u6211\u7684\u6405\u62cc\u673a\u76d6\u6389\u4e86\uff0c\\\n", "\u628a\u5976\u6614\u6e85\u5230\u4e86\u53a8\u623f\u7684\u5899\u4e0a\uff01\\\n", "\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4fdd\u4fee\u4e0d\u5305\u62ec\u6253\u626b\u53a8\u623f\u7684\u8d39\u7528\u3002\\\n", "\u6211\u73b0\u5728\u9700\u8981\u4f60\u7684\u5e2e\u52a9\uff0c\u4f19\u8ba1\uff01\n", "\"\"\"\n", "\n", "customer_messages = prompt_template.format_messages(\n", " style=customer_style,\n", " text=customer_email)\n", "\n", "\n", "print(customer_messages[0])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 4\ufe0f\u20e3 \u8c03\u7528chat\u6a21\u578b\u8f6c\u6362\u5ba2\u6237\u6d88\u606f\u98ce\u683c\n", "\n", "\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u8c03\u7528[\u6a21\u578b](#model)\u90e8\u5206\u5b9a\u4e49\u7684chat\u6a21\u578b\u6765\u5b9e\u73b0\u8f6c\u6362\u5ba2\u6237\u6d88\u606f\u98ce\u683c\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u5b9e\u73b0\u4e86\u5728\u524d\u4e00\u90e8\u5206\u7684\u4efb\u52a1\u3002"]}, {"cell_type": "code", "execution_count": 28, "metadata": {"tags": []}, "outputs": [], "source": ["customer_response = chat(customer_messages)"]}, {"cell_type": "code", "execution_count": 29, "metadata": {"tags": []}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u5c0a\u656c\u7684\u4f19\u8ba1\uff0c\u6211\u611f\u5230\u975e\u5e38\u6124\u6012\uff0c\u56e0\u4e3a\u6211\u7684\u6405\u62cc\u673a\u76d6\u5b50\u4e0d\u614e\u6389\u843d\uff0c\u5bfc\u81f4\u5976\u6614\u6e85\u5230\u4e86\u53a8\u623f\u7684\u5899\u58c1\u4e0a\uff01\u66f4\u52a0\u4ee4\u4eba\u7cdf\u5fc3\u7684\u662f\uff0c\u4fdd\u4fee\u5e76\u4e0d\u5305\u62ec\u53a8\u623f\u6e05\u6d01\u7684\u8d39\u7528\u3002\u73b0\u5728\uff0c\u6211\u9700\u8981\u4f60\u7684\u5e2e\u52a9\uff0c\u8bf7\u4f60\u7ed9\u4e88\u63f4\u624b\uff01\n"]}], "source": ["print(customer_response.content)"]}, {"cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u5c0a\u656c\u7684\u4f19\u8ba1\uff0c\u6211\u611f\u5230\u975e\u5e38\u6124\u6012\uff0c\u56e0\u4e3a\u6211\u7684\u6405\u62cc\u673a\u76d6\u5b50\u4e0d\u614e\u6389\u843d\uff0c\u5bfc\u81f4\u5976\u6614\u6e85\u5230\u4e86\u53a8\u623f\u7684\u5899\u58c1\u4e0a\uff01\u66f4\u52a0\u4ee4\u4eba\u7cdf\u5fc3\u7684\u662f\uff0c\u4fdd\u4fee\u5e76\u4e0d\u5305\u62ec\u53a8\u623f\u6e05\u6d01\u7684\u8d39\u7528\u3002\u73b0\u5728\uff0c\u6211\u9700\u8981\u4f60\u7684\u5e2e\u52a9\uff0c\u8bf7\u4f60\u7ed9\u4e88\u63f4\u624b\uff01\n"]}], "source": ["print(customer_response.content)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 5\ufe0f\u20e3 \u4f7f\u7528\u6a21\u7248\u5f97\u5230\u56de\u590d\u6d88\u606f\u63d0\u793a\n", "\n", "\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u66f4\u8fdb\u4e00\u6b65\uff0c\u5c06\u5ba2\u670d\u4eba\u5458\u56de\u590d\u7684\u6d88\u606f\uff0c\u8f6c\u6362\u4e3a\u6d77\u76d7\u7684\u8bed\u8a00\u98ce\u683c\uff0c\u5e76\u786e\u4fdd\u6d88\u606f\u6bd4\u8f83\u6709\u793c\u8c8c\u3002 \n", "\n", "\u8fd9\u91cc\uff0c\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528\u7b2c2\ufe0f\u20e3\u6b65\u6784\u9020\u7684langchain\u63d0\u793a\u6a21\u7248\uff0c\u6765\u83b7\u5f97\u6211\u4eec\u56de\u590d\u6d88\u606f\u63d0\u793a\u3002"]}, {"cell_type": "code", "execution_count": 31, "metadata": {"tags": []}, "outputs": [], "source": ["service_reply = \"\"\"Hey there customer, \\\n", "the warranty does not cover \\\n", "cleaning expenses for your kitchen \\\n", "because it's your fault that \\\n", "you misused your blender \\\n", "by forgetting to put the lid on before \\\n", "starting the blender. \\\n", "Tough luck! See ya!\n", "\"\"\""]}, {"cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": ["service_style_pirate = \"\"\"\\\n", "a polite tone \\\n", "that speaks in English Pirate\\\n", "\"\"\""]}, {"cell_type": "code", "execution_count": 33, "metadata": {"tags": []}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u628a\u7531\u4e09\u4e2a\u53cd\u5f15\u53f7\u5206\u9694\u7684\u6587\u672c\u7ffb\u8bd1\u6210\u4e00\u79cda polite tone that speaks in English Pirate\u98ce\u683c\u3002\u6587\u672c: ```Hey there customer, the warranty does not cover cleaning expenses for your kitchen because it's your fault that you misused your blender by forgetting to put the lid on before starting the blender. Tough luck! See ya!\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)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 6\ufe0f\u20e3 \u8c03\u7528chat\u6a21\u578b\u8f6c\u6362\u56de\u590d\u6d88\u606f\u98ce\u683c\n", "\n", "\u8c03\u7528[\u6a21\u578b](#model)\u90e8\u5206\u5b9a\u4e49\u7684chat\u6a21\u578b\u6765\u8f6c\u6362\u56de\u590d\u6d88\u606f\u98ce\u683c"]}, {"cell_type": "code", "execution_count": 34, "metadata": {"tags": []}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Ahoy there, matey! I regret to inform ye, valued customer, that the warranty be not coverin' the expenses o' cleanin' yer galley due to yer own negligence. 'Tis yer own fault, ye see, fer ye be misusin' yer blender by forgettin' to secure the lid afore settin' it in motion. Aye, 'tis a tough break, indeed! Fare thee well, me heartie!\n"]}], "source": ["service_response = chat(service_messages)\n", "print(service_response.content)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### 3.2.2 \u4e2d\u6587\u7248"]}, {"cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u628a\u7531\u4e09\u4e2a\u53cd\u5f15\u53f7\u5206\u9694\u7684\u6587\u672c\u7ffb\u8bd1\u6210\u4e00\u79cd\u4e00\u4e2a\u6709\u793c\u8c8c\u7684\u8bed\u6c14 \u4f7f\u7528\u6b63\u5f0f\u7684\u666e\u901a\u8bdd \u98ce\u683c\u3002\u6587\u672c: ```\u563f\uff0c\u987e\u5ba2\uff0c \u4fdd\u4fee\u4e0d\u5305\u62ec\u53a8\u623f\u7684\u6e05\u6d01\u8d39\u7528\uff0c \u56e0\u4e3a\u60a8\u5728\u542f\u52a8\u6405\u62cc\u673a\u4e4b\u524d \u5fd8\u8bb0\u76d6\u4e0a\u76d6\u5b50\u800c\u8bef\u7528\u6405\u62cc\u673a, \u8fd9\u662f\u60a8\u7684\u9519\u3002 \u5012\u9709\uff01 \u518d\u89c1\uff01\n", "```\n", "\n"]}], "source": ["service_reply = \"\"\"\u563f\uff0c\u987e\u5ba2\uff0c \\\n", "\u4fdd\u4fee\u4e0d\u5305\u62ec\u53a8\u623f\u7684\u6e05\u6d01\u8d39\u7528\uff0c \\\n", "\u56e0\u4e3a\u60a8\u5728\u542f\u52a8\u6405\u62cc\u673a\u4e4b\u524d \\\n", "\u5fd8\u8bb0\u76d6\u4e0a\u76d6\u5b50\u800c\u8bef\u7528\u6405\u62cc\u673a, \\\n", "\u8fd9\u662f\u60a8\u7684\u9519\u3002 \\\n", "\u5012\u9709\uff01 \u518d\u89c1\uff01\n", "\"\"\"\n", "\n", "service_style_pirate = \"\"\"\\\n", "\u4e00\u4e2a\u6709\u793c\u8c8c\u7684\u8bed\u6c14 \\\n", "\u4f7f\u7528\u6b63\u5f0f\u7684\u666e\u901a\u8bdd \\\n", "\"\"\"\n", "service_messages = prompt_template.format_messages(\n", " style=service_style_pirate,\n", " text=service_reply)\n", "\n", "print(service_messages[0].content)"]}, {"cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u5c0a\u656c\u7684\u987e\u5ba2\uff0c\u5f88\u62b1\u6b49\u544a\u77e5\u60a8\uff0c\u4fdd\u4fee\u670d\u52a1\u4e0d\u5305\u542b\u53a8\u623f\u6e05\u6d01\u8d39\u7528\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u60a8\u4f7f\u7528\u6405\u62cc\u673a\u4e4b\u524d\uff0c\u4e0d\u614e\u5fd8\u8bb0\u76d6\u4e0a\u76d6\u5b50\u800c\u5bfc\u81f4\u8bef\u7528\u6405\u62cc\u673a\uff0c\u8fd9\u5c5e\u4e8e\u60a8\u7684\u758f\u5ffd\u3002\u975e\u5e38\u9057\u61be\uff01\u795d\u60a8\u4e00\u5207\u987a\u5229\uff01\u518d\u89c1\uff01\n"]}], "source": ["service_response = chat(service_messages)\n", "print(service_response.content)"]}, {"cell_type": "markdown", "metadata": {"tags": []}, "source": ["#### 3.2.2 \u4e3a\u4ec0\u4e48\u9700\u8981\u63d0\u793a\u6a21\u7248"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5728\u5e94\u7528\u4e8e\u6bd4\u8f83\u590d\u6742\u7684\u573a\u666f\u65f6\uff0c\u63d0\u793a\u53ef\u80fd\u4f1a\u975e\u5e38\u957f\u5e76\u4e14\u5305\u542b\u6d89\u53ca\u8bb8\u591a\u7ec6\u8282\u3002**\u4f7f\u7528\u63d0\u793a\u6a21\u7248\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u4e3a\u65b9\u4fbf\u5730\u91cd\u590d\u4f7f\u7528\u8bbe\u8ba1\u597d\u7684\u63d0\u793a**\u3002"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u4e0b\u9762\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6bd4\u8f83\u957f\u7684\u63d0\u793a\u6a21\u7248\u6848\u4f8b\u3002\u5b66\u751f\u4eec\u7ebf\u4e0a\u5b66\u4e60\u5e76\u63d0\u4ea4\u4f5c\u4e1a\uff0c\u901a\u8fc7\u4ee5\u4e0b\u7684\u63d0\u793a\u6765\u5b9e\u73b0\u5bf9\u5b66\u751f\u7684\u63d0\u4ea4\u7684\u4f5c\u4e1a\u7684\u8bc4\u5206\u3002"]}, {"cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": ["# \u82f1\u6587\u7248\n", "prompt = \"\"\" Your task is to determine if the student's solution is correct or not\n", "\n", " To solve the problem do the following:\n", " - First, workout your own solution to the problem\n", " - Then compare your solution to the student's solution \n", " and evaluate if the sudtent's solution is correct or not.\n", " ...\n", " Use the following format:\n", " Question:\n", " ```\n", " question here\n", " ```\n", " Student's solution:\n", " ```\n", " student's solution here\n", " ```\n", " Actual solution:\n", " ```\n", " ...\n", " steps to work out the solution and your solution here\n", " ```\n", " Is the student's solution the same as acutal solution \\\n", " just calculated:\n", " ```\n", " yes or no\n", " ```\n", " Student grade\n", " ```\n", " correct or incorrect\n", " ```\n", " \n", " Question:\n", " ```\n", " {question}\n", " ```\n", " Student's solution:\n", " ```\n", " {student's solution}\n", " ```\n", " Actual solution:\n", " \n", " \"\"\""]}, {"cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": ["# \u4e2d\u6587\u7248\n", "prompt = \"\"\" \u4f60\u7684\u4efb\u52a1\u662f\u5224\u65ad\u5b66\u751f\u7684\u89e3\u51b3\u65b9\u6848\u662f\u6b63\u786e\u7684\u8fd8\u662f\u4e0d\u6b63\u786e\u7684\n", "\n", "\u8981\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a\n", " - \u9996\u5148\uff0c\u5236\u5b9a\u81ea\u5df1\u7684\u95ee\u9898\u89e3\u51b3\u65b9\u6848\n", " - \u7136\u540e\u5c06\u60a8\u7684\u89e3\u51b3\u65b9\u6848\u4e0e\u5b66\u751f\u7684\u89e3\u51b3\u65b9\u6848\u8fdb\u884c\u6bd4\u8f83\n", " \u5e76\u8bc4\u4f30\u5b66\u751f\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5426\u6b63\u786e\u3002\n", "...\n", "\u4f7f\u7528\u4e0b\u9762\u7684\u683c\u5f0f:\n", "\n", "\u95ee\u9898:\n", "```\n", "\u95ee\u9898\u6587\u672c\n", "```\n", "\u5b66\u751f\u7684\u89e3\u51b3\u65b9\u6848:\n", "```\n", "\u5b66\u751f\u7684\u89e3\u51b3\u65b9\u6848\u6587\u672c\n", "```\n", "\u5b9e\u9645\u89e3\u51b3\u65b9\u6848:\n", "```\n", "...\n", "\u5236\u5b9a\u89e3\u51b3\u65b9\u6848\u7684\u6b65\u9aa4\u4ee5\u53ca\u60a8\u7684\u89e3\u51b3\u65b9\u6848\u8bf7\u53c2\u89c1\u6b64\u5904\n", "```\n", "\u5b66\u751f\u7684\u89e3\u51b3\u65b9\u6848\u548c\u5b9e\u9645\u89e3\u51b3\u65b9\u6848\u662f\u5426\u76f8\u540c \\\n", "\u53ea\u8ba1\u7b97\uff1a\n", "```\n", "\u662f\u6216\u8005\u4e0d\u662f\n", "```\n", "\u5b66\u751f\u7684\u6210\u7ee9\n", "```\n", "\u6b63\u786e\u6216\u8005\u4e0d\u6b63\u786e\n", "```\n", "\n", "\u95ee\u9898:\n", "```\n", "{question}\n", "```\n", "\u5b66\u751f\u7684\u89e3\u51b3\u65b9\u6848:\n", "```\n", "{student's solution}\n", "```\n", "\u5b9e\u9645\u89e3\u51b3\u65b9\u6848:\n", "\n", "\"\"\""]}, {"cell_type": "markdown", "metadata": {"jp-MarkdownHeadingCollapsed": true, "tags": []}, "source": ["\u6b64\u5916\uff0cLangChain\u8fd8\u63d0\u4f9b\u4e86\u63d0\u793a\u6a21\u7248\u7528\u4e8e\u4e00\u4e9b\u5e38\u7528\u573a\u666f\u3002\u6bd4\u5982\u81ea\u52a8\u6458\u8981\u3001\u95ee\u7b54\u3001\u8fde\u63a5\u5230SQL\u6570\u636e\u5e93\u3001\u8fde\u63a5\u5230\u4e0d\u540c\u7684API. \u901a\u8fc7\u4f7f\u7528LongChain\u5185\u7f6e\u7684\u63d0\u793a\u6a21\u7248\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u5efa\u7acb\u81ea\u5df1\u7684\u5927\u6a21\u578b\u5e94\u7528\uff0c\u800c\u4e0d\u9700\u8981\u82b1\u65f6\u95f4\u53bb\u8bbe\u8ba1\u548c\u6784\u9020\u63d0\u793a\u3002\n", "\n", "\u6700\u540e\uff0c\u6211\u4eec\u5728\u5efa\u7acb\u5927\u6a21\u578b\u5e94\u7528\u65f6\uff0c\u901a\u5e38\u5e0c\u671b\u6a21\u578b\u7684\u8f93\u51fa\u4e3a\u7ed9\u5b9a\u7684\u683c\u5f0f\uff0c\u6bd4\u5982\u5728\u8f93\u51fa\u4f7f\u7528\u7279\u5b9a\u7684\u5173\u952e\u8bcd\u6765\u8ba9\u8f93\u51fa\u7ed3\u6784\u5316\u3002 \u4e0b\u9762\u4e3a\u4e00\u4e2a\u4f7f\u7528\u5927\u6a21\u578b\u8fdb\u884c\u94fe\u5f0f\u601d\u8003\u63a8\u7406\u4f8b\u5b50\uff0c\u5bf9\u4e8e\u95ee\u9898\uff1a*What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?*\n", "\n", "\u901a\u8fc7\u4f7f\u7528LangChain\u5e93\u51fd\u6570\uff0c\u8f93\u51fa\u91c7\u7528\"Thought\"\uff08\u601d\u8003\uff09\u3001\"Action\"\uff08\u884c\u52a8\uff09\u3001\"Observation\"\uff08\u89c2\u5bdf\uff09\u4f5c\u4e3a\u94fe\u5f0f\u601d\u8003\u63a8\u7406\u7684\u5173\u952e\u8bcd\uff0c\u8ba9\u8f93\u51fa\u7ed3\u6784\u5316\u3002\n", "\n", "```\n", "Thought: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado orogeny extends into, then find the elevation range of the area.\n", "Action: Search[Colorado orogeny]\n", "Observation: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.\n", "\n", "Thought: It does not mention the eastern sector. So I need to look up eastern sector.\n", "Action: Lookup[eastern sector]\n", "Observation: (Result 1 / 1) The eastern sector extends into the High Plains and is called the Central Plains orogeny.\n", "\n", "Thought: The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range.\n", "Action: Search[High Plains]\n", "Observation: High Plains refers to one of two distinct land regions\n", "\n", "Thought: I need to instead search High Plains (United States).\n", "Action: Search[High Plains (United States)]\n", "Observation: The High Plains are a subregion of the Great Plains. From east to west, the High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130 m).[3]\n", "\n", "Thought: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.\n", "Action: Finish[1,800 to 7,000 ft]\n", "```"]}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5728\u8865\u5145\u6750\u6599\u4e2d\uff0c\u53ef\u4ee5\u67e5\u770b\u4f7f\u7528LangChain\u548cOpenAI\u8fdb\u884c\u94fe\u5f0f\u601d\u8003\u63a8\u7406\u7684\u53e6\u4e00\u4e2a\u4ee3\u7801\u5b9e\u4f8b\u3002"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### 3.3 \u8f93\u51fa\u89e3\u6790\u5668"]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### 3.3.1 \u5982\u679c\u6ca1\u6709\u8f93\u51fa\u89e3\u6790\u5668\n", "\n", "\u5bf9\u4e8e\u7ed9\u5b9a\u7684\u8bc4\u4ef7`customer_review`, \u6211\u4eec\u5e0c\u671b\u63d0\u53d6\u4fe1\u606f\uff0c\u5e76\u6309\u4ee5\u4e0b\u683c\u5f0f\u8f93\u51fa\uff1a\n", "\n", "```python\n", "{\n", " \"gift\": False,\n", " \"delivery_days\": 5,\n", " \"price_value\": \"pretty affordable!\"\n", "}\n", "```"]}, {"cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": ["customer_review = \"\"\"\\\n", "This leaf blower is pretty amazing. It has four settings:\\\n", "candle blower, gentle breeze, windy city, and tornado. \\\n", "It arrived in two days, just in time for my wife's \\\n", "anniversary present. \\\n", "I think my wife liked it so much she was speechless. \\\n", "So far I've been the only one using it, and I've been \\\n", "using it every other morning to clear the leaves on our lawn. \\\n", "It's slightly more expensive than the other leaf blowers \\\n", "out there, but I think it's worth it for the extra features.\n", "\"\"\""]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 1\ufe0f\u20e3 \u6784\u9020\u63d0\u793a\u6a21\u7248\u5b57\u7b26\u4e32"]}, {"cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": ["review_template = \"\"\"\\\n", "For the following text, extract the following information:\n", "\n", "gift: Was the item purchased as a gift for someone else? \\\n", "Answer True if yes, False if not or unknown.\n", "\n", "delivery_days: How many days did it take for the product \\\n", "to arrive? If this information is not found, output -1.\n", "\n", "price_value: Extract any sentences about the value or price,\\\n", "and output them as a comma separated Python list.\n", "\n", "Format the output as JSON with the following keys:\n", "gift\n", "delivery_days\n", "price_value\n", "\n", "text: {text}\n", "\"\"\""]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 2\ufe0f\u20e3 \u6784\u9020langchain\u63d0\u793a\u6a21\u7248"]}, {"cell_type": "code", "execution_count": 41, "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='For the following text, extract the following information:\\n\\ngift: Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown.\\n\\ndelivery_days: How many days did it take for the product to arrive? If this information is not found, output -1.\\n\\nprice_value: Extract any sentences about the value or price,and output them as a comma separated Python list.\\n\\nFormat the output as JSON with the following keys:\\ngift\\ndelivery_days\\nprice_value\\n\\ntext: {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)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 3\ufe0f\u20e3 \u4f7f\u7528\u6a21\u7248\u5f97\u5230\u63d0\u793a\u6d88\u606f"]}, {"cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": ["messages = prompt_template.format_messages(text=customer_review)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 4\ufe0f\u20e3 \u8c03\u7528chat\u6a21\u578b\u63d0\u53d6\u4fe1\u606f"]}, {"cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["{\n", " \"gift\": false,\n", " \"delivery_days\": 2,\n", " \"price_value\": [\"It's slightly more expensive than the other leaf blowers out there, but I think it's worth it for the extra features.\"]\n", "}\n"]}], "source": ["chat = ChatOpenAI(temperature=0.0)\n", "response = chat(messages)\n", "print(response.content)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### \ud83d\udcdd \u5206\u6790\u4e0e\u603b\u7ed3\n", "`response.content`\u7c7b\u578b\u4e3a\u5b57\u7b26\u4e32\uff08`str`\uff09\uff0c\u800c\u5e76\u975e\u5b57\u5178(`dict`), \u76f4\u63a5\u4f7f\u7528`get`\u65b9\u6cd5\u4f1a\u62a5\u9519\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u8f93\u51fa\u89e3\u91ca\u5668\u3002"]}, {"cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [{"data": {"text/plain": ["str"]}, "execution_count": 44, "metadata": {}, "output_type": "execute_result"}], "source": ["type(response.content)"]}, {"cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [{"ename": "AttributeError", "evalue": "'str' object has no attribute 'get'", "output_type": "error", "traceback": ["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [45]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcontent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgift\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: 'str' object has no attribute 'get'"]}], "source": ["response.content.get('gift')"]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### 3.3.2 \u4e2d\u6587\u7248"]}, {"cell_type": "code", "execution_count": 46, "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='\u5bf9\u4e8e\u4ee5\u4e0b\u6587\u672c\uff0c\u8bf7\u4ece\u4e2d\u63d0\u53d6\u4ee5\u4e0b\u4fe1\u606f\uff1a\\n\\n\u793c\u7269\uff1a\u8be5\u5546\u54c1\u662f\u4f5c\u4e3a\u793c\u7269\u9001\u7ed9\u522b\u4eba\u7684\u5417\uff1f \u5982\u679c\u662f\uff0c\u5219\u56de\u7b54 \u662f\u7684\uff1b\u5982\u679c\u5426\u6216\u672a\u77e5\uff0c\u5219\u56de\u7b54 \u4e0d\u662f\u3002\\n\\n\u4ea4\u8d27\u5929\u6570\uff1a\u4ea7\u54c1\u9700\u8981\u591a\u5c11\u5929\u5230\u8fbe\uff1f \u5982\u679c\u6ca1\u6709\u627e\u5230\u8be5\u4fe1\u606f\uff0c\u5219\u8f93\u51fa-1\u3002\\n\\n\u4ef7\u94b1\uff1a\u63d0\u53d6\u6709\u5173\u4ef7\u503c\u6216\u4ef7\u683c\u7684\u4efb\u4f55\u53e5\u5b50\uff0c\u5e76\u5c06\u5b83\u4eec\u8f93\u51fa\u4e3a\u9017\u53f7\u5206\u9694\u7684 Python \u5217\u8868\u3002\\n\\n\u4f7f\u7528\u4ee5\u4e0b\u952e\u5c06\u8f93\u51fa\u683c\u5f0f\u5316\u4e3a JSON\uff1a\\n\u793c\u7269\\n\u4ea4\u8d27\u5929\u6570\\n\u4ef7\u94b1\\n\\n\u6587\u672c: {text}\\n', template_format='f-string', validate_template=True), additional_kwargs={})]\n"]}], "source": ["from langchain.prompts import ChatPromptTemplate\n", "\n", "customer_review = \"\"\"\\\n", "\u8fd9\u6b3e\u5439\u53f6\u673a\u975e\u5e38\u795e\u5947\u3002 \u5b83\u6709\u56db\u4e2a\u8bbe\u7f6e\uff1a\\\n", "\u5439\u8721\u70db\u3001\u5fae\u98ce\u3001\u98ce\u57ce\u3001\u9f99\u5377\u98ce\u3002 \\\n", "\u4e24\u5929\u540e\u5c31\u5230\u4e86\uff0c\u6b63\u597d\u8d76\u4e0a\u6211\u59bb\u5b50\u7684\\\n", "\u5468\u5e74\u7eaa\u5ff5\u793c\u7269\u3002 \\\n", "\u6211\u60f3\u6211\u7684\u59bb\u5b50\u4f1a\u559c\u6b22\u5b83\u5230\u8bf4\u4e0d\u51fa\u8bdd\u6765\u3002 \\\n", "\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u662f\u552f\u4e00\u4e00\u4e2a\u4f7f\u7528\u5b83\u7684\u4eba\uff0c\u800c\u4e14\u6211\u4e00\u76f4\\\n", "\u6bcf\u9694\u4e00\u5929\u65e9\u4e0a\u7528\u5b83\u6765\u6e05\u7406\u8349\u576a\u4e0a\u7684\u53f6\u5b50\u3002 \\\n", "\u5b83\u6bd4\u5176\u4ed6\u5439\u53f6\u673a\u7a0d\u5fae\u8d35\u4e00\u70b9\uff0c\\\n", "\u4f46\u6211\u8ba4\u4e3a\u5b83\u7684\u989d\u5916\u529f\u80fd\u662f\u503c\u5f97\u7684\u3002\n", "\"\"\"\n", "\n", "review_template = \"\"\"\\\n", "\u5bf9\u4e8e\u4ee5\u4e0b\u6587\u672c\uff0c\u8bf7\u4ece\u4e2d\u63d0\u53d6\u4ee5\u4e0b\u4fe1\u606f\uff1a\n", "\n", "\u793c\u7269\uff1a\u8be5\u5546\u54c1\u662f\u4f5c\u4e3a\u793c\u7269\u9001\u7ed9\u522b\u4eba\u7684\u5417\uff1f \\\n", "\u5982\u679c\u662f\uff0c\u5219\u56de\u7b54 \u662f\u7684\uff1b\u5982\u679c\u5426\u6216\u672a\u77e5\uff0c\u5219\u56de\u7b54 \u4e0d\u662f\u3002\n", "\n", "\u4ea4\u8d27\u5929\u6570\uff1a\u4ea7\u54c1\u9700\u8981\u591a\u5c11\u5929\\\n", "\u5230\u8fbe\uff1f \u5982\u679c\u6ca1\u6709\u627e\u5230\u8be5\u4fe1\u606f\uff0c\u5219\u8f93\u51fa-1\u3002\n", "\n", "\u4ef7\u94b1\uff1a\u63d0\u53d6\u6709\u5173\u4ef7\u503c\u6216\u4ef7\u683c\u7684\u4efb\u4f55\u53e5\u5b50\uff0c\\\n", "\u5e76\u5c06\u5b83\u4eec\u8f93\u51fa\u4e3a\u9017\u53f7\u5206\u9694\u7684 Python \u5217\u8868\u3002\n", "\n", "\u4f7f\u7528\u4ee5\u4e0b\u952e\u5c06\u8f93\u51fa\u683c\u5f0f\u5316\u4e3a JSON\uff1a\n", "\u793c\u7269\n", "\u4ea4\u8d27\u5929\u6570\n", "\u4ef7\u94b1\n", "\n", "\u6587\u672c: {text}\n", "\"\"\"\n", "\n", "prompt_template = ChatPromptTemplate.from_template(review_template)\n", "print(prompt_template)\n"]}, {"cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["{\n", " \"\u793c\u7269\": \"\u662f\u7684\",\n", " \"\u4ea4\u8d27\u5929\u6570\": 2,\n", " \"\u4ef7\u94b1\": [\"\u5b83\u6bd4\u5176\u4ed6\u5439\u53f6\u673a\u7a0d\u5fae\u8d35\u4e00\u70b9\"]\n", "}\n"]}], "source": ["messages = prompt_template.format_messages(text=customer_review)\n", "\n", "chat = ChatOpenAI(temperature=0.0)\n", "response = chat(messages)\n", "print(response.content)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### 3.3.3 LangChain\u8f93\u51fa\u89e3\u6790\u5668"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 1\ufe0f\u20e3 \u6784\u9020\u63d0\u793a\u6a21\u7248\u5b57\u7b26\u4e32"]}, {"cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": ["review_template_2 = \"\"\"\\\n", "For the following text, extract the following information:\n", "\n", "gift: Was the item purchased as a gift for someone else? \\\n", "Answer True if yes, False if not or unknown.\n", "\n", "delivery_days: How many days did it take for the product\\\n", "to arrive? If this information is not found, output -1.\n", "\n", "price_value: Extract any sentences about the value or price,\\\n", "and output them as a comma separated Python list.\n", "\n", "text: {text}\n", "\n", "{format_instructions}\n", "\"\"\""]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 2\ufe0f\u20e3 \u6784\u9020langchain\u63d0\u793a\u6a21\u7248"]}, {"cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": ["prompt = ChatPromptTemplate.from_template(template=review_template_2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### \ud83d\udd25 \u6784\u9020\u8f93\u51fa\u89e3\u6790\u5668"]}, {"cell_type": "code", "execution_count": 50, "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\"gift\": string // Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown.\n", "\t\"delivery_days\": string // How many days did it take for the product to arrive? If this information is not found, output -1.\n", "\t\"price_value\": string // Extract any sentences about the value or price, and output them as a comma separated Python list.\n", "}\n", "```\n"]}], "source": ["from langchain.output_parsers import ResponseSchema\n", "from langchain.output_parsers import StructuredOutputParser\n", "\n", "gift_schema = ResponseSchema(name=\"gift\",\n", " description=\"Was the item purchased\\\n", " as a gift for someone else? \\\n", " Answer True if yes,\\\n", " False if not or unknown.\")\n", "\n", "delivery_days_schema = ResponseSchema(name=\"delivery_days\",\n", " description=\"How many days\\\n", " did it take for the product\\\n", " to arrive? If this \\\n", " information is not found,\\\n", " output -1.\")\n", "\n", "price_value_schema = ResponseSchema(name=\"price_value\",\n", " description=\"Extract any\\\n", " sentences about the value or \\\n", " 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)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 3\ufe0f\u20e3 \u4f7f\u7528\u6a21\u7248\u5f97\u5230\u63d0\u793a\u6d88\u606f"]}, {"cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": ["messages = prompt.format_messages(text=customer_review, format_instructions=format_instructions)"]}, {"cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["For the following text, extract the following information:\n", "\n", "gift: Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown.\n", "\n", "delivery_days: How many days did it take for the productto arrive? If this information is not found, output -1.\n", "\n", "price_value: Extract any sentences about the value or price,and output them as a comma separated Python list.\n", "\n", "text: \u8fd9\u6b3e\u5439\u53f6\u673a\u975e\u5e38\u795e\u5947\u3002 \u5b83\u6709\u56db\u4e2a\u8bbe\u7f6e\uff1a\u5439\u8721\u70db\u3001\u5fae\u98ce\u3001\u98ce\u57ce\u3001\u9f99\u5377\u98ce\u3002 \u4e24\u5929\u540e\u5c31\u5230\u4e86\uff0c\u6b63\u597d\u8d76\u4e0a\u6211\u59bb\u5b50\u7684\u5468\u5e74\u7eaa\u5ff5\u793c\u7269\u3002 \u6211\u60f3\u6211\u7684\u59bb\u5b50\u4f1a\u559c\u6b22\u5b83\u5230\u8bf4\u4e0d\u51fa\u8bdd\u6765\u3002 \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u662f\u552f\u4e00\u4e00\u4e2a\u4f7f\u7528\u5b83\u7684\u4eba\uff0c\u800c\u4e14\u6211\u4e00\u76f4\u6bcf\u9694\u4e00\u5929\u65e9\u4e0a\u7528\u5b83\u6765\u6e05\u7406\u8349\u576a\u4e0a\u7684\u53f6\u5b50\u3002 \u5b83\u6bd4\u5176\u4ed6\u5439\u53f6\u673a\u7a0d\u5fae\u8d35\u4e00\u70b9\uff0c\u4f46\u6211\u8ba4\u4e3a\u5b83\u7684\u989d\u5916\u529f\u80fd\u662f\u503c\u5f97\u7684\u3002\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\"gift\": string // Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown.\n", "\t\"delivery_days\": string // How many days did it take for the product to arrive? If this information is not found, output -1.\n", "\t\"price_value\": string // Extract any sentences about the value or price, and output them as a comma separated Python list.\n", "}\n", "```\n", "\n"]}], "source": ["print(messages[0].content)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 4\ufe0f\u20e3 \u8c03\u7528chat\u6a21\u578b\u63d0\u53d6\u4fe1\u606f"]}, {"cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["```json\n", "{\n", "\t\"gift\": false,\n", "\t\"delivery_days\": \"2\",\n", "\t\"price_value\": \"\u5b83\u6bd4\u5176\u4ed6\u5439\u53f6\u673a\u7a0d\u5fae\u8d35\u4e00\u70b9\"\n", "}\n", "```\n"]}], "source": ["response = chat(messages)\n", "print(response.content)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### 5\ufe0f\u20e3 \u4f7f\u7528\u8f93\u51fa\u89e3\u6790\u5668\u89e3\u6790\u8f93\u51fa"]}, {"cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [{"data": {"text/plain": ["{'gift': False, 'delivery_days': '2', 'price_value': '\u5b83\u6bd4\u5176\u4ed6\u5439\u53f6\u673a\u7a0d\u5fae\u8d35\u4e00\u70b9'}"]}, "execution_count": 54, "metadata": {}, "output_type": "execute_result"}], "source": ["output_dict = output_parser.parse(response.content)\n", "output_dict"]}, {"cell_type": "markdown", "metadata": {}, "source": ["##### \ud83d\udcdd \u5206\u6790\u4e0e\u603b\u7ed3\n", "`output_dict`\u7c7b\u578b\u4e3a\u5b57\u5178(`dict`), \u53ef\u76f4\u63a5\u4f7f\u7528`get`\u65b9\u6cd5\u3002\u8fd9\u6837\u7684\u8f93\u51fa\u66f4\u65b9\u4fbf\u4e0b\u6e38\u4efb\u52a1\u7684\u5904\u7406\u3002"]}, {"cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [{"data": {"text/plain": ["dict"]}, "execution_count": 55, "metadata": {}, "output_type": "execute_result"}], "source": ["type(output_dict)"]}, {"cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [{"data": {"text/plain": ["'2'"]}, "execution_count": 56, "metadata": {}, "output_type": "execute_result"}], "source": ["output_dict.get('delivery_days')"]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### 3.3.4 \u4e2d\u6587\u7248"]}, {"cell_type": "code", "execution_count": 57, "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\"\u793c\u7269\": string // \u8fd9\u4ef6\u7269\u54c1\u662f\u4f5c\u4e3a\u793c\u7269\u9001\u7ed9\u522b\u4eba\u7684\u5417\uff1f \u5982\u679c\u662f\uff0c\u5219\u56de\u7b54 \u662f\u7684\uff0c \u5982\u679c\u5426\u6216\u672a\u77e5\uff0c\u5219\u56de\u7b54 \u4e0d\u662f\u3002\n", "\t\"\u4ea4\u8d27\u5929\u6570\": string // \u4ea7\u54c1\u9700\u8981\u591a\u5c11\u5929\u624d\u80fd\u5230\u8fbe\uff1f \u5982\u679c\u6ca1\u6709\u627e\u5230\u8be5\u4fe1\u606f\uff0c\u5219\u8f93\u51fa-1\u3002\n", "\t\"\u4ef7\u94b1\": string // \u63d0\u53d6\u6709\u5173\u4ef7\u503c\u6216\u4ef7\u683c\u7684\u4efb\u4f55\u53e5\u5b50\uff0c \u5e76\u5c06\u5b83\u4eec\u8f93\u51fa\u4e3a\u9017\u53f7\u5206\u9694\u7684 Python \u5217\u8868\n", "}\n", "```\n"]}], "source": ["# \u4e2d\u6587\n", "review_template_2 = \"\"\"\\\n", "\u5bf9\u4e8e\u4ee5\u4e0b\u6587\u672c\uff0c\u8bf7\u4ece\u4e2d\u63d0\u53d6\u4ee5\u4e0b\u4fe1\u606f\uff1a\uff1a\n", "\n", "\u793c\u7269\uff1a\u8be5\u5546\u54c1\u662f\u4f5c\u4e3a\u793c\u7269\u9001\u7ed9\u522b\u4eba\u7684\u5417\uff1f\n", "\u5982\u679c\u662f\uff0c\u5219\u56de\u7b54 \u662f\u7684\uff1b\u5982\u679c\u5426\u6216\u672a\u77e5\uff0c\u5219\u56de\u7b54 \u4e0d\u662f\u3002\n", "\n", "\u4ea4\u8d27\u5929\u6570\uff1a\u4ea7\u54c1\u5230\u8fbe\u9700\u8981\u591a\u5c11\u5929\uff1f \u5982\u679c\u6ca1\u6709\u627e\u5230\u8be5\u4fe1\u606f\uff0c\u5219\u8f93\u51fa-1\u3002\n", "\n", "\u4ef7\u94b1\uff1a\u63d0\u53d6\u6709\u5173\u4ef7\u503c\u6216\u4ef7\u683c\u7684\u4efb\u4f55\u53e5\u5b50\uff0c\u5e76\u5c06\u5b83\u4eec\u8f93\u51fa\u4e3a\u9017\u53f7\u5206\u9694\u7684 Python \u5217\u8868\u3002\n", "\n", "\u6587\u672c: {text}\n", "\n", "{format_instructions}\n", "\"\"\"\n", "\n", "from langchain.output_parsers import ResponseSchema\n", "from langchain.output_parsers import StructuredOutputParser\n", "\n", "gift_schema = ResponseSchema(name=\"\u793c\u7269\",\n", " description=\"\u8fd9\u4ef6\u7269\u54c1\u662f\u4f5c\u4e3a\u793c\u7269\u9001\u7ed9\u522b\u4eba\u7684\u5417\uff1f\\\n", " \u5982\u679c\u662f\uff0c\u5219\u56de\u7b54 \u662f\u7684\uff0c\\\n", " \u5982\u679c\u5426\u6216\u672a\u77e5\uff0c\u5219\u56de\u7b54 \u4e0d\u662f\u3002\")\n", "\n", "delivery_days_schema = ResponseSchema(name=\"\u4ea4\u8d27\u5929\u6570\",\n", " description=\"\u4ea7\u54c1\u9700\u8981\u591a\u5c11\u5929\u624d\u80fd\u5230\u8fbe\uff1f\\\n", " \u5982\u679c\u6ca1\u6709\u627e\u5230\u8be5\u4fe1\u606f\uff0c\u5219\u8f93\u51fa-1\u3002\")\n", "\n", "price_value_schema = ResponseSchema(name=\"\u4ef7\u94b1\",\n", " description=\"\u63d0\u53d6\u6709\u5173\u4ef7\u503c\u6216\u4ef7\u683c\u7684\u4efb\u4f55\u53e5\u5b50\uff0c\\\n", " \u5e76\u5c06\u5b83\u4eec\u8f93\u51fa\u4e3a\u9017\u53f7\u5206\u9694\u7684 Python \u5217\u8868\")\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)"]}, {"cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["For the following text, extract the following information:\n", "\n", "gift: Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown.\n", "\n", "delivery_days: How many days did it take for the productto arrive? If this information is not found, output -1.\n", "\n", "price_value: Extract any sentences about the value or price,and output them as a comma separated Python list.\n", "\n", "text: \u8fd9\u6b3e\u5439\u53f6\u673a\u975e\u5e38\u795e\u5947\u3002 \u5b83\u6709\u56db\u4e2a\u8bbe\u7f6e\uff1a\u5439\u8721\u70db\u3001\u5fae\u98ce\u3001\u98ce\u57ce\u3001\u9f99\u5377\u98ce\u3002 \u4e24\u5929\u540e\u5c31\u5230\u4e86\uff0c\u6b63\u597d\u8d76\u4e0a\u6211\u59bb\u5b50\u7684\u5468\u5e74\u7eaa\u5ff5\u793c\u7269\u3002 \u6211\u60f3\u6211\u7684\u59bb\u5b50\u4f1a\u559c\u6b22\u5b83\u5230\u8bf4\u4e0d\u51fa\u8bdd\u6765\u3002 \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u662f\u552f\u4e00\u4e00\u4e2a\u4f7f\u7528\u5b83\u7684\u4eba\uff0c\u800c\u4e14\u6211\u4e00\u76f4\u6bcf\u9694\u4e00\u5929\u65e9\u4e0a\u7528\u5b83\u6765\u6e05\u7406\u8349\u576a\u4e0a\u7684\u53f6\u5b50\u3002 \u5b83\u6bd4\u5176\u4ed6\u5439\u53f6\u673a\u7a0d\u5fae\u8d35\u4e00\u70b9\uff0c\u4f46\u6211\u8ba4\u4e3a\u5b83\u7684\u989d\u5916\u529f\u80fd\u662f\u503c\u5f97\u7684\u3002\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\"\u793c\u7269\": string // \u8fd9\u4ef6\u7269\u54c1\u662f\u4f5c\u4e3a\u793c\u7269\u9001\u7ed9\u522b\u4eba\u7684\u5417\uff1f \u5982\u679c\u662f\uff0c\u5219\u56de\u7b54 \u662f\u7684\uff0c \u5982\u679c\u5426\u6216\u672a\u77e5\uff0c\u5219\u56de\u7b54 \u4e0d\u662f\u3002\n", "\t\"\u4ea4\u8d27\u5929\u6570\": string // \u4ea7\u54c1\u9700\u8981\u591a\u5c11\u5929\u624d\u80fd\u5230\u8fbe\uff1f \u5982\u679c\u6ca1\u6709\u627e\u5230\u8be5\u4fe1\u606f\uff0c\u5219\u8f93\u51fa-1\u3002\n", "\t\"\u4ef7\u94b1\": string // \u63d0\u53d6\u6709\u5173\u4ef7\u503c\u6216\u4ef7\u683c\u7684\u4efb\u4f55\u53e5\u5b50\uff0c \u5e76\u5c06\u5b83\u4eec\u8f93\u51fa\u4e3a\u9017\u53f7\u5206\u9694\u7684 Python \u5217\u8868\n", "}\n", "```\n", "\n"]}], "source": ["messages = prompt.format_messages(text=customer_review, format_instructions=format_instructions)\n", "print(messages[0].content)"]}, {"cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["```json\n", "{\n", "\t\"\u793c\u7269\": false,\n", "\t\"\u4ea4\u8d27\u5929\u6570\": \"\u4e24\u5929\u540e\",\n", "\t\"\u4ef7\u94b1\": \"\u5b83\u6bd4\u5176\u4ed6\u5439\u53f6\u673a\u7a0d\u5fae\u8d35\u4e00\u70b9\"\n", "}\n", "```\n"]}], "source": ["response = chat(messages)\n", "print(response.content)\n"]}, {"cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [{"data": {"text/plain": ["{'\u793c\u7269': False, '\u4ea4\u8d27\u5929\u6570': '\u4e24\u5929\u540e', '\u4ef7\u94b1': '\u5b83\u6bd4\u5176\u4ed6\u5439\u53f6\u673a\u7a0d\u5fae\u8d35\u4e00\u70b9'}"]}, "execution_count": 60, "metadata": {}, "output_type": "execute_result"}], "source": ["output_dict = output_parser.parse(response.content)\n", "output_dict"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## \u56db\u3001\u8865\u5145\u6750\u6599"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### 4.1 \u94fe\u5f0f\u601d\u8003\u63a8\u7406(ReAct)\n", "\u53c2\u8003\u8d44\u6599\uff1a[ReAct (Reason+Act) prompting in OpenAI GPT and LangChain](https://tsmatz.wordpress.com/2023/03/07/react-with-openai-gpt-and-langchain/)"]}, {"cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": ["!pip install -q wikipedia"]}, {"cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "\n", "\u001b[1m> Entering new 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, \u0110o\u00e0n V\u0103n To\u1ea1i, 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, \u0110o\u00e0n V\u0103n To\u1ea1i, 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, \u0110o\u00e0n V\u0103n To\u1ea1i.\n", "Action: Search[\u0110o\u00e0n V\u0103n To\u1ea1i]\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m\u0110o\u00e0n V\u0103n To\u1ea1i (1945 in Vietnam \u2013 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:\u001b[32;1m\u001b[1;3m\u0110o\u00e0n V\u0103n To\u1ea1i 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 \u2013 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:\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 \u2013 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:\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": 69, "metadata": {}, "output_type": "execute_result"}], "source": ["from langchain.docstore.wikipedia import Wikipedia\n", "from langchain.llms import OpenAI\n", "from langchain.agents import initialize_agent, Tool, AgentExecutor\n", "from langchain.agents.react.base import DocstoreExplorer\n", "\n", "docstore=DocstoreExplorer(Wikipedia())\n", "tools = [\n", " Tool(\n", " name=\"Search\",\n", " func=docstore.search,\n", " description=\"Search for a term in the docstore.\",\n", " ),\n", " Tool(\n", " name=\"Lookup\",\n", " func=docstore.lookup,\n", " description=\"Lookup a term in the docstore.\",\n", " )\n", "]\n", "\n", "# \u4f7f\u7528\u5927\u8bed\u8a00\u6a21\u578b\n", "llm = OpenAI(\n", " model_name=\"gpt-3.5-turbo\",\n", " temperature=0,\n", ")\n", "\n", "# \u521d\u59cb\u5316ReAct\u4ee3\u7406\n", "react = initialize_agent(tools, llm, agent=\"react-docstore\", verbose=True)\n", "agent_executor = AgentExecutor.from_agent_and_tools(\n", " agent=react.agent,\n", " tools=tools,\n", " verbose=True,\n", ")\n", "\n", "\n", "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)"]}], "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"}}, "nbformat": 4, "nbformat_minor": 4}