diff --git a/content/LangChain for LLM Application Development/4.模型链 Chains.ipynb b/content/LangChain for LLM Application Development/4.模型链 Chains.ipynb index 85d6620..f4f60b1 100644 --- a/content/LangChain for LLM Application Development/4.模型链 Chains.ipynb +++ b/content/LangChain for LLM Application Development/4.模型链 Chains.ipynb @@ -1,1731 +1 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "52824b89-532a-4e54-87e9-1410813cd39e", - "metadata": {}, - "source": [ - "# 第四章 模型链\n", - "\n", - " - [一、设置OpenAI API Key](#一、设置OpenAI-API-Key)\n", - " - [二、LLMChain](#二、LLMChain)\n", - " - [三、Sequential Chains](#三、Sequential-Chains)\n", - " - [3.1 SimpleSequentialChain](#3.1-SimpleSequentialChain)\n", - " - [3.2 SequentialChain](#3.2-SequentialChain)\n", - " - [四、 Router Chain(路由链)](#四、-Router-Chain(路由链))\n", - " - [4.1 定义提示模板](#4.1-定义提示模板)\n", - " - [4.2 导入相关的包](#4.2-导入相关的包)\n", - " - [4.3 定义语言模型](#4.3-定义语言模型)\n", - " - [4.4 LLMRouterChain(此链使用 LLM 来确定如何路由事物)](#4.4-LLMRouterChain(此链使用-LLM-来确定如何路由事物))\n" - ] - }, - { - "cell_type": "markdown", - "id": "54810ef7", - "metadata": {}, - "source": [ - "链(Chains)通常将大语言模型(LLM)与提示(Prompt)结合在一起,基于此,我们可以对文本或数据进行一系列操作。\n", - "\n", - "链(Chains)可以一次性接受多个输入\n", - "\n", - "例如,我们可以创建一个链,该链接受用户输入,使用提示模板对其进行格式化,然后将格式化的响应传递给LLM。我们可以通过将多个链组合在一起,或者通过将链与其他组件组合在一起来构建更复杂的链。" - ] - }, - { - "cell_type": "markdown", - "id": "21009bf6-49bd-466e-8177-74c23533d938", - "metadata": { - "tags": [] - }, - "source": [ - "## 一、设置OpenAI API Key" - ] - }, - { - "cell_type": "markdown", - "id": "a2f5993b-f28d-4be4-984f-2d05be9f4579", - "metadata": {}, - "source": [ - "登陆 [OpenAI 账户](https://platform.openai.com/account/api-keys) 获取API Key,然后将其设置为环境变量。\n", - "\n", - "- 如果你想要设置为全局环境变量,可以参考[知乎文章](https://zhuanlan.zhihu.com/p/627665725)。\n", - "- 如果你想要设置为本地/项目环境变量,在本文件目录下创建`.env`文件, 打开文件输入以下内容。\n", - "\n", - "

\n", - " OPENAI_API_KEY=\"your_api_key\" \n", - "

\n", - " \n", - " 替换\"your_api_key\"为你自己的 API Key" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "adc3519c-4d12-4011-9223-2f3cb3c42b93", - "metadata": {}, - "outputs": [], - "source": [ - "# 下载需要的包python-dotenv和openai\n", - "# 如果你需要查看安装过程日志,可删除 -q \n", - "!pip install -q python-dotenv\n", - "!pip install -q openai" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "1ad53241-bef6-42b8-894b-bcbbc8c64df7", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "import os\n", - "import openai\n", - "from dotenv import load_dotenv, find_dotenv\n", - "\n", - "# 读取本地/项目的环境变量。\n", - "\n", - "# find_dotenv()寻找并定位.env文件的路径\n", - "# load_dotenv()读取该.env文件,并将其中的环境变量加载到当前的运行环境中 \n", - "# 如果你设置的是全局的环境变量,这行代码则没有任何作用。\n", - "_ = load_dotenv(find_dotenv())\n", - "\n", - "# 获取环境变量 OPENAI_API_KEY\n", - "openai.api_key = os.environ['OPENAI_API_KEY'] " - ] - }, - { - "cell_type": "markdown", - "id": "b940ce7c", - "metadata": { - "tags": [] - }, - "source": [ - "## 二、大语言模型链" - ] - }, - { - "cell_type": "markdown", - "id": "e000bd16", - "metadata": {}, - "source": [ - "大语言模型链(LLMChain)是一个简单但非常强大的链,也是后面我们将要介绍的许多链的基础。" - ] - }, - { - "cell_type": "markdown", - "id": "d57e93b9-52ba-4f86-a953-e4661b895a3d", - "metadata": {}, - "source": [ - "### 2.1 导入数据" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "b84e441b", - "metadata": {}, - "outputs": [], - "source": [ - "!pip install -q pandas" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", - "metadata": {}, - "outputs": [], - "source": [ - "import pandas as pd\n", - "df = pd.read_csv('data/Data.csv')" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "b7a09c35", - "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", - "
ProductReview
0Queen Size Sheet SetI ordered a king size set. My only criticism w...
1Waterproof Phone PouchI loved the waterproof sac, although the openi...
2Luxury Air MattressThis mattress had a small hole in the top of i...
3Pillows InsertThis is the best throw pillow fillers on Amazo...
4Milk Frother Handheld\\nI loved this product. But they only seem to l...
\n", - "
" - ], - "text/plain": [ - " Product Review\n", - "0 Queen Size Sheet Set I ordered a king size set. My only criticism w...\n", - "1 Waterproof Phone Pouch I loved the waterproof sac, although the openi...\n", - "2 Luxury Air Mattress This mattress had a small hole in the top of i...\n", - "3 Pillows Insert This is the best throw pillow fillers on Amazo...\n", - "4 Milk Frother Handheld\\n  I loved this product. But they only seem to l..." - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "df.head()" - ] - }, - { - "cell_type": "markdown", - "id": "daa83d10-04ee-4355-abe3-ab699c9eaca9", - "metadata": {}, - "source": [ - "### 2.2 初始化语言模型" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "e92dff22", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.chat_models import ChatOpenAI \n", - "from langchain.prompts import ChatPromptTemplate \n", - "from langchain.chains import LLMChain " - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "943237a7", - "metadata": {}, - "outputs": [], - "source": [ - "# 这里我们将参数temperature设置为0.0,从而减少生成答案的随机性。\n", - "# 如果你想要每次得到不一样的有新意的答案,可以尝试调整该参数。\n", - "llm = ChatOpenAI(temperature=0.0) " - ] - }, - { - "cell_type": "markdown", - "id": "81887434", - "metadata": {}, - "source": [ - "### 2.3 初始化提示模版\n", - "初始化提示,这个提示将接受一个名为product的变量。该prompt将要求LLM生成一个描述制造该产品的公司的最佳名称" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "cdcdb42d", - "metadata": {}, - "outputs": [], - "source": [ - "prompt = ChatPromptTemplate.from_template( \n", - " \"What is the best name to describe \\\n", - " a company that makes {product}?\"\n", - ")" - ] - }, - { - "cell_type": "markdown", - "id": "5c22cb13", - "metadata": {}, - "source": [ - "### 2.4 构建大语言模型链\n", - "\n", - "将大语言模型(LLM)和提示(Prompt)组合成链。这个大语言模型链非常简单,可以让我们以一种顺序的方式去通过运行提示并且结合到大语言模型中。" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "d7abc20b", - "metadata": {}, - "outputs": [], - "source": [ - "chain = LLMChain(llm=llm, prompt=prompt)" - ] - }, - { - "cell_type": "markdown", - "id": "8d7d5ff6", - "metadata": {}, - "source": [ - "### 2.5 运行大语言模型链\n", - "因此,如果我们有一个名为\"Queen Size Sheet Set\"的产品,我们可以通过使用chain.run将其通过这个链运行" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "ad44d1fb", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Royal Linens.'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "product = \"Queen Size Sheet Set\"\n", - "chain.run(product)" - ] - }, - { - "cell_type": "markdown", - "id": "1e1ede1c", - "metadata": {}, - "source": [ - "您可以输入任何产品描述,然后查看链将输出什么结果" - ] - }, - { - "cell_type": "markdown", - "id": "8ab31583-5cdf-4885-94d7-59c0d3b90b2e", - "metadata": {}, - "source": [ - "### 2.6 中文提示" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "id": "2181be10", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'\"豪华床纺\"'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "prompt = ChatPromptTemplate.from_template( \n", - " \"描述制造{product}的一个公司的最佳名称是什么?\"\n", - ")\n", - "chain = LLMChain(llm=llm, prompt=prompt)\n", - "product = \"大号床单套装\"\n", - "chain.run(product)" - ] - }, - { - "cell_type": "markdown", - "id": "49158430", - "metadata": {}, - "source": [ - "## 三、顺序链" - ] - }, - { - "cell_type": "markdown", - "id": "69b03469", - "metadata": {}, - "source": [ - "### 3.1 简单顺序链\n", - "\n", - "顺序链(SequentialChains)是按预定义顺序执行其链接的链。具体来说,我们将使用简单顺序链(SimpleSequentialChain),这是顺序链的最简单类型,其中每个步骤都有一个输入/输出,一个步骤的输出是下一个步骤的输入。" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "id": "febee243", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.chains import SimpleSequentialChain" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "5d019d6f", - "metadata": {}, - "outputs": [], - "source": [ - "llm = ChatOpenAI(temperature=0.9)" - ] - }, - { - "cell_type": "markdown", - "id": "0e732589", - "metadata": {}, - "source": [ - "#### 3.1.1 创建两个子链" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "id": "2f31aa8a", - "metadata": {}, - "outputs": [], - "source": [ - "# 提示模板 1 :这个提示将接受产品并返回最佳名称来描述该公司\n", - "first_prompt = ChatPromptTemplate.from_template(\n", - " \"What is the best name to describe \\\n", - " a company that makes {product}?\"\n", - ")\n", - "\n", - "# Chain 1\n", - "chain_one = LLMChain(llm=llm, prompt=first_prompt)" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "id": "3f5d5b76", - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "# 提示模板 2 :接受公司名称,然后输出该公司的长为20个单词的描述\n", - "second_prompt = ChatPromptTemplate.from_template(\n", - " \"Write a 20 words description for the following \\\n", - " company:{company_name}\"\n", - ")\n", - "# chain 2\n", - "chain_two = LLMChain(llm=llm, prompt=second_prompt)" - ] - }, - { - "cell_type": "markdown", - "id": "3a1991f4", - "metadata": {}, - "source": [ - "#### 3.1.2 构建简单顺序链\n", - "现在我们可以组合两个LLMChain,以便我们可以在一个步骤中创建公司名称和描述" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "id": "6c1eb2c4", - "metadata": {}, - "outputs": [], - "source": [ - "overall_simple_chain = SimpleSequentialChain(chains=[chain_one, chain_two], verbose=True)" - ] - }, - { - "cell_type": "markdown", - "id": "5122f26a", - "metadata": {}, - "source": [ - "给一个输入,然后运行上面的链" - ] - }, - { - "cell_type": "markdown", - "id": "9a80d102-da6b-49f4-8abd-7bff97211232", - "metadata": {}, - "source": [ - "#### 3.1.3 运行简单顺序链" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "id": "78458efe", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", - "\u001b[36;1m\u001b[1;3mRoyal Rest Linens\u001b[0m\n", - "\u001b[33;1m\u001b[1;3mRoyal Rest Linens provides high-quality and luxurious linens for a comfortable and restful night's sleep.\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "\"Royal Rest Linens provides high-quality and luxurious linens for a comfortable and restful night's sleep.\"" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "product = \"Queen Size Sheet Set\"\n", - "overall_simple_chain.run(product)" - ] - }, - { - "cell_type": "markdown", - "id": "3983eb68-adf0-4c18-beed-ff6fc09cd78b", - "metadata": {}, - "source": [ - "#### 3.1.4 中文提示" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "id": "c7c32997", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\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", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "'尺寸王床品有限公司是一家专注于床上用品生产的公司。'" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# 中文\n", - "\n", - "first_prompt = ChatPromptTemplate.from_template( \n", - " \"描述制造{product}的一个公司的最好的名称是什么\"\n", - ")\n", - "chain_one = LLMChain(llm=llm, prompt=first_prompt)\n", - "\n", - "second_prompt = ChatPromptTemplate.from_template( \n", - " \"写一个20字的描述对于下面这个\\\n", - " 公司:{company_name}的\"\n", - ")\n", - "chain_two = LLMChain(llm=llm, prompt=second_prompt)\n", - "\n", - "\n", - "overall_simple_chain = SimpleSequentialChain(chains=[chain_one, chain_two],verbose=True)\n", - "product = \"大号床单套装\"\n", - "overall_simple_chain.run(product)" - ] - }, - { - "cell_type": "markdown", - "id": "7b5ce18c", - "metadata": {}, - "source": [ - "### 3.2 顺序链" - ] - }, - { - "cell_type": "markdown", - "id": "1e69f4c0", - "metadata": {}, - "source": [ - "当只有一个输入和一个输出时,简单顺序链(SimpleSequentialChain)即可实现。当有多个输入或多个输出时,我们则需要使用顺序链(SequentialChain)来实现。" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "id": "4c129ef6", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.chains import SequentialChain\n", - "from langchain.chat_models import ChatOpenAI #导入OpenAI模型\n", - "from langchain.prompts import ChatPromptTemplate #导入聊天提示模板\n", - "from langchain.chains import LLMChain #导入LLM链。" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "id": "03a8e203", - "metadata": {}, - "outputs": [], - "source": [ - "llm = ChatOpenAI(temperature=0.9)" - ] - }, - { - "cell_type": "markdown", - "id": "9811445c", - "metadata": {}, - "source": [ - "接下来我们将创建一系列的链,然后一个接一个使用他们" - ] - }, - { - "cell_type": "markdown", - "id": "df9e87ec-b99a-4130-a5b5-cdce064dc6ca", - "metadata": {}, - "source": [ - "#### 3.2.1 创建四个子链" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "id": "016187ac", - "metadata": {}, - "outputs": [], - "source": [ - "#子链1\n", - "\n", - "# prompt模板 1: 翻译成英语(把下面的review翻译成英语)\n", - "first_prompt = ChatPromptTemplate.from_template(\n", - " \"Translate the following review to english:\"\n", - " \"\\n\\n{Review}\"\n", - ")\n", - "# chain 1: 输入:Review 输出: 英文的 Review\n", - "chain_one = LLMChain(llm=llm, prompt=first_prompt, output_key=\"English_Review\")" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "id": "0fb0730e", - "metadata": {}, - "outputs": [], - "source": [ - "#子链2\n", - "\n", - "# prompt模板 2: 用一句话总结下面的 review\n", - "second_prompt = ChatPromptTemplate.from_template(\n", - " \"Can you summarize the following review in 1 sentence:\"\n", - " \"\\n\\n{English_Review}\"\n", - ")\n", - "# chain 2: 输入:英文的Review 输出:总结\n", - "chain_two = LLMChain(llm=llm, prompt=second_prompt, output_key=\"summary\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "id": "6accf92d", - "metadata": {}, - "outputs": [], - "source": [ - "#子链3\n", - "\n", - "# prompt模板 3: 下面review使用的什么语言\n", - "third_prompt = ChatPromptTemplate.from_template(\n", - " \"What language is the following review:\\n\\n{Review}\"\n", - ")\n", - "# chain 3: 输入:Review 输出:语言\n", - "chain_three = LLMChain(llm=llm, prompt=third_prompt, output_key=\"language\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "id": "c7a46121", - "metadata": {}, - "outputs": [], - "source": [ - "#子链4\n", - "\n", - "# prompt模板 4: 使用特定的语言对下面的总结写一个后续回复\n", - "fourth_prompt = ChatPromptTemplate.from_template(\n", - " \"Write a follow up response to the following \"\n", - " \"summary in the specified language:\"\n", - " \"\\n\\nSummary: {summary}\\n\\nLanguage: {language}\"\n", - ")\n", - "# chain 4: 输入: 总结, 语言 输出: 后续回复\n", - "chain_four = LLMChain(llm=llm, prompt=fourth_prompt, output_key=\"followup_message\")\n" - ] - }, - { - "cell_type": "markdown", - "id": "ff435291-5d20-4c3e-9ed7-76a1140f96d2", - "metadata": {}, - "source": [ - "#### 3.2.2 对四个子链进行组合" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "id": "89603117", - "metadata": {}, - "outputs": [], - "source": [ - "#输入:review \n", - "#输出:英文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", - ")" - ] - }, - { - "cell_type": "markdown", - "id": "0509de01", - "metadata": {}, - "source": [ - "让我们选择一篇评论并通过整个链传递它,可以发现,原始review是法语,可以把英文review看做是一种翻译,接下来是根据英文review得到的总结,最后输出的是用法语原文进行的续写信息。" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "id": "51b04f45", - "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": [ - "{'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 hold, it's weird. I buy the same ones in the store and the taste is much better...\\nOld batch or counterfeit!?\",\n", - " 'summary': 'The reviewer is disappointed with the taste and consistency of the product, suspecting that either an old batch or counterfeit version is the cause.',\n", - " 'followup_message': \"Réponse de suivi :\\n\\nCher(e) critique,\\n\\nNous sommes désolés d'apprendre que notre produit ne répond pas à vos attentes. Nous prenons très au sérieux les commentaires de nos clients et nous tenons à vous rassurer que nous attachons une grande importance à la qualité de nos produits.\\n\\nNous vérifions constamment nos processus de production afin de nous assurer que nos produits sont toujours frais et authentiques. Si vous pensez avoir reçu un produit contrefait ou un lot périmé, nous vous prions de nous contacter directement afin de résoudre ce problème au plus vite. Nous avons un service client dédié qui sera ravi de vous aider avec votre préoccupation.\\n\\nNous tenons à nous excuser pour toute déception ou désagrément que cela aurait pu causer. Votre satisfaction est notre priorité absolue et nous ferons tout notre possible pour remédier à la situation et regagner votre confiance.\\n\\nNous vous remercions de votre compréhension et de votre patience. Votre avis est important pour nous et nous espérons avoir l'occasion de nous rattraper à l'avenir.\\n\\nCordialement,\\nL'équipe de [Nom de l'entreprise]\"}" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "review = df.Review[5]\n", - "overall_chain(review)" - ] - }, - { - "cell_type": "markdown", - "id": "18d72f1d-8a1e-4eb6-b00f-1963b3cf3adb", - "metadata": {}, - "source": [ - "#### 3.2.3 中文提示" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "id": "31624a7c", - "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": [ - "{'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 weird. I buy the same ones from the store and the taste is much better...\\nOld batch or counterfeit!?\",\n", - " 'summary': \"The taste is mediocre, the foam doesn't last, and there is suspicion of old batch or counterfeit.\",\n", - " 'followup_message': \"回复: Je suis désolé de vous entendre dire que le goût est moyen et que la mousse ne dure pas longtemps. Nous prenons ces problèmes très au sérieux. Nous allons enquêter pour vérifier s'il s'agit d'un ancien lot ou d'une contrefaçon. Votre satisfaction est notre priorité et nous ferons de notre mieux pour résoudre ce problème. Merci de nous avoir informés.\"}" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# 中文\n", - "\n", - "#子链1\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, output_key=\"English_Review\")\n", - "\n", - "#子链2\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, output_key=\"summary\")\n", - "\n", - "\n", - "#子链3\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, output_key=\"language\")\n", - "\n", - "\n", - "#子链4\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", - "#输入: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)" - ] - }, - { - "cell_type": "markdown", - "id": "3041ea4c", - "metadata": {}, - "source": [ - "## 四、 路由链" - ] - }, - { - "cell_type": "markdown", - "id": "f0c32f97", - "metadata": {}, - "source": [ - "到目前为止,我们已经学习了大语言模型链和顺序链。但是,如果我们想做一些更复杂的事情怎么办?\n", - "\n", - "一个相当常见但基本的操作是根据输入将其路由到一条链,具体取决于该输入到底是什么。如果你有多个子链,每个子链都专门用于特定类型的输入,那么可以组成一个路由链,它首先决定将它传递给哪个子链,然后将它传递给那个链。\n", - "\n", - "路由器由两个组件组成:\n", - "\n", - "- 路由链(Router Chain):路由器链本身,负责选择要调用的下一个链\n", - "- destination_chains:路由器链可以路由到的链\n", - "\n", - "举一个具体的例子,让我们看一下我们在不同类型的链之间路由的地方,我们在这里有不同的prompt: " - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "id": "31b06fc8", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.chains.router import MultiPromptChain #导入多提示链\n", - "from langchain.chains.router.llm_router import LLMRouterChain,RouterOutputParser\n", - "from langchain.prompts import PromptTemplate" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "id": "f3f50bcc", - "metadata": {}, - "outputs": [], - "source": [ - "llm = ChatOpenAI(temperature=0)" - ] - }, - { - "cell_type": "markdown", - "id": "cb1b4708", - "metadata": {}, - "source": [ - "### 4.1 定义提示模板" - ] - }, - { - "cell_type": "markdown", - "id": "c3b85285-c736-4a5d-bd14-d9b5025ca29b", - "metadata": {}, - "source": [ - "首先,我们定义提示适用于不同场景下的提示模板。" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "id": "ade83f4f", - "metadata": {}, - "outputs": [], - "source": [ - "#第一个提示适合回答物理问题\n", - "physics_template = \"\"\"You are a very smart physics professor. \\\n", - "You are great at answering questions about physics in a concise\\\n", - "and easy to understand manner. \\\n", - "When you don't know the answer to a question you admit\\\n", - "that you don't know.\n", - "\n", - "Here is a question:\n", - "{input}\"\"\"\n", - "\n", - "\n", - "#第二个提示适合回答数学问题\n", - "math_template = \"\"\"You are a very good mathematician. \\\n", - "You are great at answering math questions. \\\n", - "You are so good because you are able to break down \\\n", - "hard problems into their component parts, \n", - "answer the component parts, and then put them together\\\n", - "to answer the broader question.\n", - "\n", - "Here is a question:\n", - "{input}\"\"\"\n", - "\n", - "\n", - "#第三个适合回答历史问题\n", - "history_template = \"\"\"You are a very good historian. \\\n", - "You have an excellent knowledge of and understanding of people,\\\n", - "events and contexts from a range of historical periods. \\\n", - "You have the ability to think, reflect, debate, discuss and \\\n", - "evaluate the past. You have a respect for historical evidence\\\n", - "and the ability to make use of it to support your explanations \\\n", - "and judgements.\n", - "\n", - "Here is a question:\n", - "{input}\"\"\"\n", - "\n", - "\n", - "#第四个适合回答计算机问题\n", - "computerscience_template = \"\"\" You are a successful computer scientist.\\\n", - "You have a passion for creativity, collaboration,\\\n", - "forward-thinking, confidence, strong problem-solving capabilities,\\\n", - "understanding of theories and algorithms, and excellent communication \\\n", - "skills. You are great at answering coding questions. \\\n", - "You are so good because you know how to solve a problem by \\\n", - "describing the solution in imperative steps \\\n", - "that a machine can easily interpret and you know how to \\\n", - "choose a solution that has a good balance between \\\n", - "time complexity and space complexity. \n", - "\n", - "Here is a question:\n", - "{input}\"\"\"" - ] - }, - { - "cell_type": "markdown", - "id": "cb3b749b-0d3c-46f2-afab-b1213f208b13", - "metadata": {}, - "source": [ - "### 4.2 对提示模版进行命名和描述" - ] - }, - { - "cell_type": "markdown", - "id": "6922b35e", - "metadata": {}, - "source": [ - "在定义了这些提示模板后,我们可以为每个模板命名,并给出具体描述。例如,第一个物理学的描述适合回答关于物理学的问题,这些信息将传递给路由链,然后由路由链决定何时使用此子链。" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "id": "141a3d32", - "metadata": {}, - "outputs": [], - "source": [ - "prompt_infos = [\n", - " {\n", - " \"name\": \"physics\", \n", - " \"description\": \"Good for answering questions about physics\", \n", - " \"prompt_template\": physics_template\n", - " },\n", - " {\n", - " \"name\": \"math\", \n", - " \"description\": \"Good for answering math questions\", \n", - " \"prompt_template\": math_template\n", - " },\n", - " {\n", - " \"name\": \"History\", \n", - " \"description\": \"Good for answering history questions\", \n", - " \"prompt_template\": history_template\n", - " },\n", - " {\n", - " \"name\": \"computer science\", \n", - " \"description\": \"Good for answering computer science questions\", \n", - " \"prompt_template\": computerscience_template\n", - " }\n", - "]" - ] - }, - { - "cell_type": "markdown", - "id": "8795cd42", - "metadata": {}, - "source": [ - "LLMRouterChain(此链使用 LLM 来确定如何路由事物)\n", - "\n", - "在这里,我们需要一个**多提示链**。这是一种特定类型的链,用于在多个不同的提示模板之间进行路由。 但是这只是路由的一种类型,我们也可以在任何类型的链之间进行路由。\n", - "\n", - "这里我们要实现的几个类是大模型路由器链。这个类本身使用语言模型来在不同的子链之间进行路由。这就是上面提供的描述和名称将被使用的地方。" - ] - }, - { - "cell_type": "markdown", - "id": "46633b43", - "metadata": {}, - "source": [ - "### 4.3 基于提示模版信息创建相应目标链 \n", - "目标链是由路由链调用的链,每个目标链都是一个语言模型链" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "id": "8eefec24", - "metadata": {}, - "outputs": [], - "source": [ - "destination_chains = {}\n", - "for p_info in prompt_infos:\n", - " name = p_info[\"name\"]\n", - " prompt_template = p_info[\"prompt_template\"]\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['name']}: {p['description']}\" for p in prompt_infos]\n", - "destinations_str = \"\\n\".join(destinations)" - ] - }, - { - "cell_type": "markdown", - "id": "eba115de", - "metadata": { - "tags": [] - }, - "source": [ - "### 4.4 创建默认目标链\n", - "除了目标链之外,我们还需要一个默认目标链。这是一个当路由器无法决定使用哪个子链时调用的链。在上面的示例中,当输入问题与物理、数学、历史或计算机科学无关时,可能会调用它。" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "id": "9f98018a", - "metadata": {}, - "outputs": [], - "source": [ - "default_prompt = ChatPromptTemplate.from_template(\"{input}\")\n", - "default_chain = LLMChain(llm=llm, prompt=default_prompt)" - ] - }, - { - "cell_type": "markdown", - "id": "948700c4", - "metadata": {}, - "source": [ - "### 4.5 定义不同链之间的路由模板\n", - "\n", - "这包括要完成的任务的说明以及输出应该采用的特定格式。" - ] - }, - { - "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": 32, - "id": "11b2e2ba", - "metadata": {}, - "outputs": [], - "source": [ - "MULTI_PROMPT_ROUTER_TEMPLATE = \"\"\"Given a raw text input to a \\\n", - "language model select the model prompt best suited for the input. \\\n", - "You will be given the names of the available prompts and a \\\n", - "description of what the prompt is best suited for. \\\n", - "You may also revise the original input if you think that revising\\\n", - "it will ultimately lead to a better response from the language model.\n", - "\n", - "<< FORMATTING >>\n", - "Return a markdown code snippet with a JSON object formatted to look like:\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", - "REMEMBER: \"destination\" MUST be one of the candidate prompt \\\n", - "names specified below OR it can be \"DEFAULT\" if the input is not\\\n", - "well suited for any of the candidate prompts.\n", - "REMEMBER: \"next_inputs\" can just be the original input \\\n", - "if you don't think any modifications are needed.\n", - "\n", - "<< CANDIDATE PROMPTS >>\n", - "{destinations}\n", - "\n", - "<< INPUT >>\n", - "{{input}}\n", - "\n", - "<< 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": "markdown", - "id": "de5c46d0", - "metadata": {}, - "source": [ - "### 4.6 构建路由链\n", - "首先,我们通过格式化上面定义的目标创建完整的路由器模板。这个模板可以适用许多不同类型的目标。\n", - "因此,在这里,您可以添加一个不同的学科,如英语或拉丁语,而不仅仅是物理、数学、历史和计算机科学。\n", - "\n", - "接下来,我们从这个模板创建提示模板\n", - "\n", - "最后,通过传入llm和整个路由提示来创建路由链。需要注意的是这里有路由输出解析,这很重要,因为它将帮助这个链路决定在哪些子链路之间进行路由。" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "id": "1387109d", - "metadata": {}, - "outputs": [], - "source": [ - "router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(\n", - " destinations=destinations_str\n", - ")\n", - "router_prompt = PromptTemplate(\n", - " template=router_template,\n", - " input_variables=[\"input\"],\n", - " output_parser=RouterOutputParser(),\n", - ")\n", - "\n", - "router_chain = LLMRouterChain.from_llm(llm, router_prompt)" - ] - }, - { - "cell_type": "markdown", - "id": "7e92355c", - "metadata": {}, - "source": [ - "### 4.7 创建整体链路" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "id": "2fb7d560", - "metadata": {}, - "outputs": [], - "source": [ - "#多提示链\n", - "chain = MultiPromptChain(router_chain=router_chain, #l路由链路\n", - " destination_chains=destination_chains, #目标链路\n", - " default_chain=default_chain, #默认链路\n", - " verbose=True \n", - " )" - ] - }, - { - "cell_type": "markdown", - "id": "086503f7", - "metadata": {}, - "source": [ - "### 4.8 进行提问" - ] - }, - { - "cell_type": "markdown", - "id": "969cd878", - "metadata": {}, - "source": [ - "如果我们问一个物理问题,我们希望看到他被路由到物理链路" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2217d987", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", - "physics: {'input': 'What is black body radiation?'}\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "'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.'" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# 问题:什么是黑体辐射?\n", - "chain.run(\"What is black body radiation?\")" - ] - }, - { - "cell_type": "markdown", - "id": "289c5ca9", - "metadata": {}, - "source": [ - "如果我们问一个数学问题,我们希望看到他被路由到数学链路" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "3b717379", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", - "math: {'input': 'what is 2 + 2'}\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "'As an AI language model, I can answer this question. The answer to 2 + 2 is 4.'" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# 问题:2+2等于多少?\n", - "chain.run(\"what is 2 + 2\")" - ] - }, - { - "cell_type": "markdown", - "id": "4186a2b9", - "metadata": {}, - "source": [ - "如果我们传递一个与任何子链路都无关的问题时,会发生什么呢?\n", - "\n", - "这里,我们问了一个关于生物学的问题,我们可以看到它选择的链路是无。这意味着它将被**传递到默认链路,它本身只是对语言模型的通用调用**。语言模型幸运地对生物学知道很多,所以它可以帮助我们。" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "id": "29e5be01", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", - "None: {'input': 'Why does every cell in our body contain DNA?'}\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "'Every cell in our body contains DNA because DNA carries the genetic information that determines the characteristics and functions of each cell. DNA contains the instructions for the synthesis of proteins, which are essential for the structure and function of cells. Additionally, DNA is responsible for the transmission of genetic information from one generation to the next. Therefore, every cell in our body needs DNA to carry out its specific functions and to maintain the integrity of the organism as a whole.'" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# 问题:为什么我们身体里的每个细胞都包含DNA?\n", - "chain.run(\"Why does every cell in our body contain DNA?\")" - ] - }, - { - "cell_type": "markdown", - "id": "753b5f1d-b010-4f33-a928-d3b9e85bf289", - "metadata": {}, - "source": [ - "### 4.9 中文提示" - ] - }, - { - "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}\"\"\"" - ] - }, - { - "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" - ] - }, - { - "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)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "af088d6b-a70b-4cd7-bc6e-4ad7b32e6ae1", - "metadata": {}, - "outputs": [], - "source": [ - "default_prompt = ChatPromptTemplate.from_template(\"{input}\")\n", - "default_chain = LLMChain(llm=llm, prompt=default_prompt)" - ] - }, - { - "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", - "\"\"\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7470b25c-ef82-496c-a6e0-e04b99b08e70", - "metadata": {}, - "outputs": [], - "source": [ - "#多提示链\n", - "chain = MultiPromptChain(router_chain=router_chain, #l路由链路\n", - " destination_chains=destination_chains, #目标链路\n", - " default_chain=default_chain, #默认链路\n", - " verbose=True \n", - " )" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "id": "4446724c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", - "physics: {'input': '什么是黑体辐射?'}\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "'黑体辐射是指一个理想化的物体,它能够完全吸收所有入射到它表面的辐射能量,并以热辐射的形式重新发射出来。黑体辐射的特点是其辐射能量的分布与温度有关,随着温度的升高,辐射能量的峰值会向更短的波长方向移动。这个现象被称为黑体辐射谱的位移定律,由普朗克在20世纪初提出。黑体辐射在研究热力学、量子力学和宇宙学等领域中具有重要的应用。'" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "#中文\n", - "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(\"你知道李白是谁嘛?\")" - ] - }, - { - "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 等于多少\")" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "id": "a64d0759", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", - "None: {'input': '为什么我们身体里的每个细胞都包含DNA?'}\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "'我们身体里的每个细胞都包含DNA,是因为DNA是遗传信息的载体。DNA是由四种碱基(腺嘌呤、鸟嘌呤、胸腺嘧啶和鳞嘌呤)组成的长链状分子,它存储了生物体的遗传信息,包括个体的特征、生长发育、代谢功能等。每个细胞都需要这些遗传信息来执行其特定的功能和任务,因此每个细胞都需要包含DNA。此外,DNA还能通过复制和传递给下一代细胞和个体,以保证遗传信息的传承。'" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# 中文\n", - "chain.run(\"为什么我们身体里的每个细胞都包含DNA?\")" - ] - } - ], - "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": 5 -} +{"cells": [{"cell_type": "markdown", "id": "52824b89-532a-4e54-87e9-1410813cd39e", "metadata": {}, "source": ["# \u7b2c\u56db\u7ae0 \u6a21\u578b\u94fe\n", "\n", " - [\u4e00\u3001\u8bbe\u7f6eOpenAI API Key](#\u4e00\u3001\u8bbe\u7f6eOpenAI-API-Key)\n", " - [\u4e8c\u3001\u5927\u8bed\u8a00\u6a21\u578b\u94fe](#\u4e8c\u3001\u5927\u8bed\u8a00\u6a21\u578b\u94fe)\n", " - [2.1 \u5bfc\u5165\u6570\u636e](#2.1-\u5bfc\u5165\u6570\u636e)\n", " - [2.2 \u521d\u59cb\u5316\u8bed\u8a00\u6a21\u578b](#2.2-\u521d\u59cb\u5316\u8bed\u8a00\u6a21\u578b)\n", " - [2.3 \u521d\u59cb\u5316\u63d0\u793a\u6a21\u7248](#2.3-\u521d\u59cb\u5316\u63d0\u793a\u6a21\u7248)\n", " - [2.4 \u6784\u5efa\u5927\u8bed\u8a00\u6a21\u578b\u94fe](#2.4-\u6784\u5efa\u5927\u8bed\u8a00\u6a21\u578b\u94fe)\n", " - [2.5 \u8fd0\u884c\u5927\u8bed\u8a00\u6a21\u578b\u94fe](#2.5-\u8fd0\u884c\u5927\u8bed\u8a00\u6a21\u578b\u94fe)\n", " - [2.6 \u4e2d\u6587\u63d0\u793a](#2.6-\u4e2d\u6587\u63d0\u793a)\n", " - [\u4e09\u3001\u987a\u5e8f\u94fe](#\u4e09\u3001\u987a\u5e8f\u94fe)\n", " - [3.1 \u7b80\u5355\u987a\u5e8f\u94fe](#3.1-\u7b80\u5355\u987a\u5e8f\u94fe)\n", " - [3.1.1 \u521b\u5efa\u4e24\u4e2a\u5b50\u94fe](#3.1.1-\u521b\u5efa\u4e24\u4e2a\u5b50\u94fe)\n", " - [3.1.2 \u6784\u5efa\u7b80\u5355\u987a\u5e8f\u94fe](#3.1.2-\u6784\u5efa\u7b80\u5355\u987a\u5e8f\u94fe)\n", " - [3.1.3 \u8fd0\u884c\u7b80\u5355\u987a\u5e8f\u94fe](#3.1.3-\u8fd0\u884c\u7b80\u5355\u987a\u5e8f\u94fe)\n", " - [3.1.4 \u4e2d\u6587\u63d0\u793a](#3.1.4-\u4e2d\u6587\u63d0\u793a)\n", " - [3.2 \u987a\u5e8f\u94fe](#3.2-\u987a\u5e8f\u94fe)\n", " - [3.2.1 \u521b\u5efa\u56db\u4e2a\u5b50\u94fe](#3.2.1-\u521b\u5efa\u56db\u4e2a\u5b50\u94fe)\n", " - [3.2.2 \u5bf9\u56db\u4e2a\u5b50\u94fe\u8fdb\u884c\u7ec4\u5408](#3.2.2-\u5bf9\u56db\u4e2a\u5b50\u94fe\u8fdb\u884c\u7ec4\u5408)\n", " - [3.2.3 \u4e2d\u6587\u63d0\u793a](#3.2.3-\u4e2d\u6587\u63d0\u793a)\n", " - [\u56db\u3001 \u8def\u7531\u94fe](#\u56db\u3001-\u8def\u7531\u94fe)\n", " - [4.1 \u5b9a\u4e49\u63d0\u793a\u6a21\u677f](#4.1-\u5b9a\u4e49\u63d0\u793a\u6a21\u677f)\n", " - [4.2 \u5bf9\u63d0\u793a\u6a21\u7248\u8fdb\u884c\u547d\u540d\u548c\u63cf\u8ff0](#4.2-\u5bf9\u63d0\u793a\u6a21\u7248\u8fdb\u884c\u547d\u540d\u548c\u63cf\u8ff0)\n", " - [4.3 \u57fa\u4e8e\u63d0\u793a\u6a21\u7248\u4fe1\u606f\u521b\u5efa\u76f8\u5e94\u76ee\u6807\u94fe ](#4.3-\u57fa\u4e8e\u63d0\u793a\u6a21\u7248\u4fe1\u606f\u521b\u5efa\u76f8\u5e94\u76ee\u6807\u94fe--)\n", " - [4.4 \u521b\u5efa\u9ed8\u8ba4\u76ee\u6807\u94fe](#4.4-\u521b\u5efa\u9ed8\u8ba4\u76ee\u6807\u94fe)\n", " - [4.5 \u5b9a\u4e49\u4e0d\u540c\u94fe\u4e4b\u95f4\u7684\u8def\u7531\u6a21\u677f](#4.5-\u5b9a\u4e49\u4e0d\u540c\u94fe\u4e4b\u95f4\u7684\u8def\u7531\u6a21\u677f)\n", " - [4.6 \u6784\u5efa\u8def\u7531\u94fe](#4.6-\u6784\u5efa\u8def\u7531\u94fe)\n", " - [4.7 \u521b\u5efa\u6574\u4f53\u94fe\u8def](#4.7-\u521b\u5efa\u6574\u4f53\u94fe\u8def)\n", " - [4.8 \u8fdb\u884c\u63d0\u95ee](#4.8-\u8fdb\u884c\u63d0\u95ee)\n", " - [4.9 \u4e2d\u6587\u63d0\u793a](#4.9-\u4e2d\u6587\u63d0\u793a)\n"]}, {"cell_type": "markdown", "id": "54810ef7", "metadata": {}, "source": ["\u94fe\uff08Chains\uff09\u901a\u5e38\u5c06\u5927\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u4e0e\u63d0\u793a(Prompt)\u7ed3\u5408\u5728\u4e00\u8d77\uff0c\u57fa\u4e8e\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6587\u672c\u6216\u6570\u636e\u8fdb\u884c\u4e00\u7cfb\u5217\u64cd\u4f5c\u3002\n", "\n", "\u94fe\uff08Chains\uff09\u53ef\u4ee5\u4e00\u6b21\u6027\u63a5\u53d7\u591a\u4e2a\u8f93\u5165\n", "\n", "\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u94fe\uff0c\u8be5\u94fe\u63a5\u53d7\u7528\u6237\u8f93\u5165\uff0c\u4f7f\u7528\u63d0\u793a\u6a21\u677f\u5bf9\u5176\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u7136\u540e\u5c06\u683c\u5f0f\u5316\u7684\u54cd\u5e94\u4f20\u9012\u7ed9LLM\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u591a\u4e2a\u94fe\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u6216\u8005\u901a\u8fc7\u5c06\u94fe\u4e0e\u5176\u4ed6\u7ec4\u4ef6\u7ec4\u5408\u5728\u4e00\u8d77\u6765\u6784\u5efa\u66f4\u590d\u6742\u7684\u94fe\u3002"]}, {"cell_type": "markdown", "id": "21009bf6-49bd-466e-8177-74c23533d938", "metadata": {"tags": []}, "source": ["## \u4e00\u3001\u8bbe\u7f6eOpenAI API Key"]}, {"cell_type": "markdown", "id": "a2f5993b-f28d-4be4-984f-2d05be9f4579", "metadata": {}, "source": ["\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", "

\n", " OPENAI_API_KEY=\"your_api_key\" \n", "

\n", " \n", " \u66ff\u6362\"your_api_key\"\u4e3a\u4f60\u81ea\u5df1\u7684 API Key"]}, {"cell_type": "code", "execution_count": 10, "id": "adc3519c-4d12-4011-9223-2f3cb3c42b93", "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": 11, "id": "1ad53241-bef6-42b8-894b-bcbbc8c64df7", "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", "id": "b940ce7c", "metadata": {"tags": []}, "source": ["## \u4e8c\u3001\u5927\u8bed\u8a00\u6a21\u578b\u94fe"]}, {"cell_type": "markdown", "id": "e000bd16", "metadata": {}, "source": ["\u5927\u8bed\u8a00\u6a21\u578b\u94fe(LLMChain)\u662f\u4e00\u4e2a\u7b80\u5355\u4f46\u975e\u5e38\u5f3a\u5927\u7684\u94fe\uff0c\u4e5f\u662f\u540e\u9762\u6211\u4eec\u5c06\u8981\u4ecb\u7ecd\u7684\u8bb8\u591a\u94fe\u7684\u57fa\u7840\u3002"]}, {"cell_type": "markdown", "id": "d57e93b9-52ba-4f86-a953-e4661b895a3d", "metadata": {}, "source": ["### 2.1 \u5bfc\u5165\u6570\u636e"]}, {"cell_type": "code", "execution_count": 1, "id": "b84e441b", "metadata": {}, "outputs": [], "source": ["!pip install -q pandas"]}, {"cell_type": "code", "execution_count": 4, "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", "metadata": {}, "outputs": [], "source": ["import pandas as pd\n", "df = pd.read_csv('data/Data.csv')"]}, {"cell_type": "code", "execution_count": 5, "id": "b7a09c35", "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", "
ProductReview
0Queen Size Sheet SetI ordered a king size set. My only criticism w...
1Waterproof Phone PouchI loved the waterproof sac, although the openi...
2Luxury Air MattressThis mattress had a small hole in the top of i...
3Pillows InsertThis is the best throw pillow fillers on Amazo...
4Milk Frother Handheld\\nI loved this product. But they only seem to l...
\n", "
"], "text/plain": [" Product Review\n", "0 Queen Size Sheet Set I ordered a king size set. My only criticism w...\n", "1 Waterproof Phone Pouch I loved the waterproof sac, although the openi...\n", "2 Luxury Air Mattress This mattress had a small hole in the top of i...\n", "3 Pillows Insert This is the best throw pillow fillers on Amazo...\n", "4 Milk Frother Handheld\\n \u00a0I loved this product. But they only seem to l..."]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["df.head()"]}, {"cell_type": "markdown", "id": "daa83d10-04ee-4355-abe3-ab699c9eaca9", "metadata": {}, "source": ["### 2.2 \u521d\u59cb\u5316\u8bed\u8a00\u6a21\u578b"]}, {"cell_type": "code", "execution_count": 8, "id": "e92dff22", "metadata": {}, "outputs": [], "source": ["from langchain.chat_models import ChatOpenAI \n", "from langchain.prompts import ChatPromptTemplate \n", "from langchain.chains import LLMChain "]}, {"cell_type": "code", "execution_count": 13, "id": "943237a7", "metadata": {}, "outputs": [], "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", "llm = ChatOpenAI(temperature=0.0) "]}, {"cell_type": "markdown", "id": "81887434", "metadata": {}, "source": ["### 2.3 \u521d\u59cb\u5316\u63d0\u793a\u6a21\u7248\n", "\u521d\u59cb\u5316\u63d0\u793a\uff0c\u8fd9\u4e2a\u63d0\u793a\u5c06\u63a5\u53d7\u4e00\u4e2a\u540d\u4e3aproduct\u7684\u53d8\u91cf\u3002\u8be5prompt\u5c06\u8981\u6c42LLM\u751f\u6210\u4e00\u4e2a\u63cf\u8ff0\u5236\u9020\u8be5\u4ea7\u54c1\u7684\u516c\u53f8\u7684\u6700\u4f73\u540d\u79f0"]}, {"cell_type": "code", "execution_count": 14, "id": "cdcdb42d", "metadata": {}, "outputs": [], "source": ["prompt = ChatPromptTemplate.from_template( \n", " \"What is the best name to describe \\\n", " a company that makes {product}?\"\n", ")"]}, {"cell_type": "markdown", "id": "5c22cb13", "metadata": {}, "source": ["### 2.4 \u6784\u5efa\u5927\u8bed\u8a00\u6a21\u578b\u94fe\n", "\n", "\u5c06\u5927\u8bed\u8a00\u6a21\u578b(LLM)\u548c\u63d0\u793a\uff08Prompt\uff09\u7ec4\u5408\u6210\u94fe\u3002\u8fd9\u4e2a\u5927\u8bed\u8a00\u6a21\u578b\u94fe\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u4ee5\u4e00\u79cd\u987a\u5e8f\u7684\u65b9\u5f0f\u53bb\u901a\u8fc7\u8fd0\u884c\u63d0\u793a\u5e76\u4e14\u7ed3\u5408\u5230\u5927\u8bed\u8a00\u6a21\u578b\u4e2d\u3002"]}, {"cell_type": "code", "execution_count": 15, "id": "d7abc20b", "metadata": {}, "outputs": [], "source": ["chain = LLMChain(llm=llm, prompt=prompt)"]}, {"cell_type": "markdown", "id": "8d7d5ff6", "metadata": {}, "source": ["### 2.5 \u8fd0\u884c\u5927\u8bed\u8a00\u6a21\u578b\u94fe\n", "\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a\"Queen Size Sheet Set\"\u7684\u4ea7\u54c1\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528chain.run\u5c06\u5176\u901a\u8fc7\u8fd9\u4e2a\u94fe\u8fd0\u884c"]}, {"cell_type": "code", "execution_count": 9, "id": "ad44d1fb", "metadata": {}, "outputs": [{"data": {"text/plain": ["'Royal Linens.'"]}, "execution_count": 9, "metadata": {}, "output_type": "execute_result"}], "source": ["product = \"Queen Size Sheet Set\"\n", "chain.run(product)"]}, {"cell_type": "markdown", "id": "1e1ede1c", "metadata": {}, "source": ["\u60a8\u53ef\u4ee5\u8f93\u5165\u4efb\u4f55\u4ea7\u54c1\u63cf\u8ff0\uff0c\u7136\u540e\u67e5\u770b\u94fe\u5c06\u8f93\u51fa\u4ec0\u4e48\u7ed3\u679c"]}, {"cell_type": "markdown", "id": "8ab31583-5cdf-4885-94d7-59c0d3b90b2e", "metadata": {}, "source": ["### 2.6 \u4e2d\u6587\u63d0\u793a"]}, {"cell_type": "code", "execution_count": 16, "id": "2181be10", "metadata": {}, "outputs": [{"data": {"text/plain": ["'\"\u8c6a\u534e\u5e8a\u7eba\"'"]}, "execution_count": 16, "metadata": {}, "output_type": "execute_result"}], "source": ["prompt = ChatPromptTemplate.from_template( \n", " \"\u63cf\u8ff0\u5236\u9020{product}\u7684\u4e00\u4e2a\u516c\u53f8\u7684\u6700\u4f73\u540d\u79f0\u662f\u4ec0\u4e48?\"\n", ")\n", "chain = LLMChain(llm=llm, prompt=prompt)\n", "product = \"\u5927\u53f7\u5e8a\u5355\u5957\u88c5\"\n", "chain.run(product)"]}, {"cell_type": "markdown", "id": "49158430", "metadata": {}, "source": ["## \u4e09\u3001\u987a\u5e8f\u94fe"]}, {"cell_type": "markdown", "id": "69b03469", "metadata": {}, "source": ["### 3.1 \u7b80\u5355\u987a\u5e8f\u94fe\n", "\n", "\u987a\u5e8f\u94fe\uff08SequentialChains\uff09\u662f\u6309\u9884\u5b9a\u4e49\u987a\u5e8f\u6267\u884c\u5176\u94fe\u63a5\u7684\u94fe\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u7b80\u5355\u987a\u5e8f\u94fe\uff08SimpleSequentialChain\uff09\uff0c\u8fd9\u662f\u987a\u5e8f\u94fe\u7684\u6700\u7b80\u5355\u7c7b\u578b\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u6709\u4e00\u4e2a\u8f93\u5165/\u8f93\u51fa\uff0c\u4e00\u4e2a\u6b65\u9aa4\u7684\u8f93\u51fa\u662f\u4e0b\u4e00\u4e2a\u6b65\u9aa4\u7684\u8f93\u5165\u3002"]}, {"cell_type": "code", "execution_count": 28, "id": "febee243", "metadata": {}, "outputs": [], "source": ["from langchain.chains import SimpleSequentialChain"]}, {"cell_type": "code", "execution_count": 22, "id": "5d019d6f", "metadata": {}, "outputs": [], "source": ["llm = ChatOpenAI(temperature=0.9)"]}, {"cell_type": "markdown", "id": "0e732589", "metadata": {}, "source": ["#### 3.1.1 \u521b\u5efa\u4e24\u4e2a\u5b50\u94fe"]}, {"cell_type": "code", "execution_count": 23, "id": "2f31aa8a", "metadata": {}, "outputs": [], "source": ["# \u63d0\u793a\u6a21\u677f 1 \uff1a\u8fd9\u4e2a\u63d0\u793a\u5c06\u63a5\u53d7\u4ea7\u54c1\u5e76\u8fd4\u56de\u6700\u4f73\u540d\u79f0\u6765\u63cf\u8ff0\u8be5\u516c\u53f8\n", "first_prompt = ChatPromptTemplate.from_template(\n", " \"What is the best name to describe \\\n", " a company that makes {product}?\"\n", ")\n", "\n", "# Chain 1\n", "chain_one = LLMChain(llm=llm, prompt=first_prompt)"]}, {"cell_type": "code", "execution_count": 24, "id": "3f5d5b76", "metadata": {}, "outputs": [], "source": ["\n", "# \u63d0\u793a\u6a21\u677f 2 \uff1a\u63a5\u53d7\u516c\u53f8\u540d\u79f0\uff0c\u7136\u540e\u8f93\u51fa\u8be5\u516c\u53f8\u7684\u957f\u4e3a20\u4e2a\u5355\u8bcd\u7684\u63cf\u8ff0\n", "second_prompt = ChatPromptTemplate.from_template(\n", " \"Write a 20 words description for the following \\\n", " company:{company_name}\"\n", ")\n", "# chain 2\n", "chain_two = LLMChain(llm=llm, prompt=second_prompt)"]}, {"cell_type": "markdown", "id": "3a1991f4", "metadata": {}, "source": ["#### 3.1.2 \u6784\u5efa\u7b80\u5355\u987a\u5e8f\u94fe\n", "\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u7ec4\u5408\u4e24\u4e2aLLMChain\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u6b65\u9aa4\u4e2d\u521b\u5efa\u516c\u53f8\u540d\u79f0\u548c\u63cf\u8ff0"]}, {"cell_type": "code", "execution_count": 25, "id": "6c1eb2c4", "metadata": {}, "outputs": [], "source": ["overall_simple_chain = SimpleSequentialChain(chains=[chain_one, chain_two], verbose=True)"]}, {"cell_type": "markdown", "id": "5122f26a", "metadata": {}, "source": ["\u7ed9\u4e00\u4e2a\u8f93\u5165\uff0c\u7136\u540e\u8fd0\u884c\u4e0a\u9762\u7684\u94fe"]}, {"cell_type": "markdown", "id": "9a80d102-da6b-49f4-8abd-7bff97211232", "metadata": {}, "source": ["#### 3.1.3 \u8fd0\u884c\u7b80\u5355\u987a\u5e8f\u94fe"]}, {"cell_type": "code", "execution_count": 27, "id": "78458efe", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "\u001b[36;1m\u001b[1;3mRoyal Rest Linens\u001b[0m\n", "\u001b[33;1m\u001b[1;3mRoyal Rest Linens provides high-quality and luxurious linens for a comfortable and restful night's sleep.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n"]}, {"data": {"text/plain": ["\"Royal Rest Linens provides high-quality and luxurious linens for a comfortable and restful night's sleep.\""]}, "execution_count": 27, "metadata": {}, "output_type": "execute_result"}], "source": ["product = \"Queen Size Sheet Set\"\n", "overall_simple_chain.run(product)"]}, {"cell_type": "markdown", "id": "3983eb68-adf0-4c18-beed-ff6fc09cd78b", "metadata": {}, "source": ["#### 3.1.4 \u4e2d\u6587\u63d0\u793a"]}, {"cell_type": "code", "execution_count": 19, "id": "c7c32997", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "\n", "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", "\u001b[36;1m\u001b[1;3m\"\u5c3a\u5bf8\u738b\u5e8a\u54c1\u6709\u9650\u516c\u53f8\"\u001b[0m\n", "\u001b[33;1m\u001b[1;3m\u5c3a\u5bf8\u738b\u5e8a\u54c1\u6709\u9650\u516c\u53f8\u662f\u4e00\u5bb6\u4e13\u6ce8\u4e8e\u5e8a\u4e0a\u7528\u54c1\u751f\u4ea7\u7684\u516c\u53f8\u3002\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n"]}, {"data": {"text/plain": ["'\u5c3a\u5bf8\u738b\u5e8a\u54c1\u6709\u9650\u516c\u53f8\u662f\u4e00\u5bb6\u4e13\u6ce8\u4e8e\u5e8a\u4e0a\u7528\u54c1\u751f\u4ea7\u7684\u516c\u53f8\u3002'"]}, "execution_count": 19, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u4e2d\u6587\n", "\n", "first_prompt = ChatPromptTemplate.from_template( \n", " \"\u63cf\u8ff0\u5236\u9020{product}\u7684\u4e00\u4e2a\u516c\u53f8\u7684\u6700\u597d\u7684\u540d\u79f0\u662f\u4ec0\u4e48\"\n", ")\n", "chain_one = LLMChain(llm=llm, prompt=first_prompt)\n", "\n", "second_prompt = ChatPromptTemplate.from_template( \n", " \"\u5199\u4e00\u4e2a20\u5b57\u7684\u63cf\u8ff0\u5bf9\u4e8e\u4e0b\u9762\u8fd9\u4e2a\\\n", " \u516c\u53f8\uff1a{company_name}\u7684\"\n", ")\n", "chain_two = LLMChain(llm=llm, prompt=second_prompt)\n", "\n", "\n", "overall_simple_chain = SimpleSequentialChain(chains=[chain_one, chain_two],verbose=True)\n", "product = \"\u5927\u53f7\u5e8a\u5355\u5957\u88c5\"\n", "overall_simple_chain.run(product)"]}, {"cell_type": "markdown", "id": "7b5ce18c", "metadata": {}, "source": ["### 3.2 \u987a\u5e8f\u94fe"]}, {"cell_type": "markdown", "id": "1e69f4c0", "metadata": {}, "source": ["\u5f53\u53ea\u6709\u4e00\u4e2a\u8f93\u5165\u548c\u4e00\u4e2a\u8f93\u51fa\u65f6\uff0c\u7b80\u5355\u987a\u5e8f\u94fe\uff08SimpleSequentialChain\uff09\u5373\u53ef\u5b9e\u73b0\u3002\u5f53\u6709\u591a\u4e2a\u8f93\u5165\u6216\u591a\u4e2a\u8f93\u51fa\u65f6\uff0c\u6211\u4eec\u5219\u9700\u8981\u4f7f\u7528\u987a\u5e8f\u94fe\uff08SequentialChain\uff09\u6765\u5b9e\u73b0\u3002"]}, {"cell_type": "code", "execution_count": 29, "id": "4c129ef6", "metadata": {}, "outputs": [], "source": ["from langchain.chains import SequentialChain\n", "from langchain.chat_models import ChatOpenAI #\u5bfc\u5165OpenAI\u6a21\u578b\n", "from langchain.prompts import ChatPromptTemplate #\u5bfc\u5165\u804a\u5929\u63d0\u793a\u6a21\u677f\n", "from langchain.chains import LLMChain #\u5bfc\u5165LLM\u94fe\u3002"]}, {"cell_type": "code", "execution_count": 30, "id": "03a8e203", "metadata": {}, "outputs": [], "source": ["llm = ChatOpenAI(temperature=0.9)"]}, {"cell_type": "markdown", "id": "9811445c", "metadata": {}, "source": ["\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u7cfb\u5217\u7684\u94fe\uff0c\u7136\u540e\u4e00\u4e2a\u63a5\u4e00\u4e2a\u4f7f\u7528\u4ed6\u4eec"]}, {"cell_type": "markdown", "id": "df9e87ec-b99a-4130-a5b5-cdce064dc6ca", "metadata": {}, "source": ["#### 3.2.1 \u521b\u5efa\u56db\u4e2a\u5b50\u94fe"]}, {"cell_type": "code", "execution_count": 38, "id": "016187ac", "metadata": {}, "outputs": [], "source": ["#\u5b50\u94fe1\n", "\n", "# prompt\u6a21\u677f 1: \u7ffb\u8bd1\u6210\u82f1\u8bed\uff08\u628a\u4e0b\u9762\u7684review\u7ffb\u8bd1\u6210\u82f1\u8bed\uff09\n", "first_prompt = ChatPromptTemplate.from_template(\n", " \"Translate the following review to english:\"\n", " \"\\n\\n{Review}\"\n", ")\n", "# chain 1: \u8f93\u5165\uff1aReview \u8f93\u51fa\uff1a \u82f1\u6587\u7684 Review\n", "chain_one = LLMChain(llm=llm, prompt=first_prompt, output_key=\"English_Review\")"]}, {"cell_type": "code", "execution_count": 39, "id": "0fb0730e", "metadata": {}, "outputs": [], "source": ["#\u5b50\u94fe2\n", "\n", "# prompt\u6a21\u677f 2: \u7528\u4e00\u53e5\u8bdd\u603b\u7ed3\u4e0b\u9762\u7684 review\n", "second_prompt = ChatPromptTemplate.from_template(\n", " \"Can you summarize the following review in 1 sentence:\"\n", " \"\\n\\n{English_Review}\"\n", ")\n", "# chain 2: \u8f93\u5165\uff1a\u82f1\u6587\u7684Review \u8f93\u51fa\uff1a\u603b\u7ed3\n", "chain_two = LLMChain(llm=llm, prompt=second_prompt, output_key=\"summary\")\n"]}, {"cell_type": "code", "execution_count": 40, "id": "6accf92d", "metadata": {}, "outputs": [], "source": ["#\u5b50\u94fe3\n", "\n", "# prompt\u6a21\u677f 3: \u4e0b\u9762review\u4f7f\u7528\u7684\u4ec0\u4e48\u8bed\u8a00\n", "third_prompt = ChatPromptTemplate.from_template(\n", " \"What language is the following review:\\n\\n{Review}\"\n", ")\n", "# chain 3: \u8f93\u5165\uff1aReview \u8f93\u51fa\uff1a\u8bed\u8a00\n", "chain_three = LLMChain(llm=llm, prompt=third_prompt, output_key=\"language\")\n"]}, {"cell_type": "code", "execution_count": 41, "id": "c7a46121", "metadata": {}, "outputs": [], "source": ["#\u5b50\u94fe4\n", "\n", "# prompt\u6a21\u677f 4: \u4f7f\u7528\u7279\u5b9a\u7684\u8bed\u8a00\u5bf9\u4e0b\u9762\u7684\u603b\u7ed3\u5199\u4e00\u4e2a\u540e\u7eed\u56de\u590d\n", "fourth_prompt = ChatPromptTemplate.from_template(\n", " \"Write a follow up response to the following \"\n", " \"summary in the specified language:\"\n", " \"\\n\\nSummary: {summary}\\n\\nLanguage: {language}\"\n", ")\n", "# chain 4: \u8f93\u5165\uff1a \u603b\u7ed3, \u8bed\u8a00 \u8f93\u51fa\uff1a \u540e\u7eed\u56de\u590d\n", "chain_four = LLMChain(llm=llm, prompt=fourth_prompt, output_key=\"followup_message\")\n"]}, {"cell_type": "markdown", "id": "ff435291-5d20-4c3e-9ed7-76a1140f96d2", "metadata": {}, "source": ["#### 3.2.2 \u5bf9\u56db\u4e2a\u5b50\u94fe\u8fdb\u884c\u7ec4\u5408"]}, {"cell_type": "code", "execution_count": 42, "id": "89603117", "metadata": {}, "outputs": [], "source": ["#\u8f93\u5165\uff1areview \n", "#\u8f93\u51fa\uff1a\u82f1\u6587review\uff0c\u603b\u7ed3\uff0c\u540e\u7eed\u56de\u590d \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", ")"]}, {"cell_type": "markdown", "id": "0509de01", "metadata": {}, "source": ["\u8ba9\u6211\u4eec\u9009\u62e9\u4e00\u7bc7\u8bc4\u8bba\u5e76\u901a\u8fc7\u6574\u4e2a\u94fe\u4f20\u9012\u5b83\uff0c\u53ef\u4ee5\u53d1\u73b0\uff0c\u539f\u59cbreview\u662f\u6cd5\u8bed\uff0c\u53ef\u4ee5\u628a\u82f1\u6587review\u770b\u505a\u662f\u4e00\u79cd\u7ffb\u8bd1\uff0c\u63a5\u4e0b\u6765\u662f\u6839\u636e\u82f1\u6587review\u5f97\u5230\u7684\u603b\u7ed3\uff0c\u6700\u540e\u8f93\u51fa\u7684\u662f\u7528\u6cd5\u8bed\u539f\u6587\u8fdb\u884c\u7684\u7eed\u5199\u4fe1\u606f\u3002"]}, {"cell_type": "code", "execution_count": 43, "id": "51b04f45", "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": ["{'Review': \"Je trouve le go\u00fbt m\u00e9diocre. La mousse ne tient pas, c'est bizarre. J'ach\u00e8te les m\u00eames dans le commerce et le go\u00fbt est bien meilleur...\\nVieux lot ou contrefa\u00e7on !?\",\n", " 'English_Review': \"I find the taste mediocre. The foam doesn't hold, it's weird. I buy the same ones in the store and the taste is much better...\\nOld batch or counterfeit!?\",\n", " 'summary': 'The reviewer is disappointed with the taste and consistency of the product, suspecting that either an old batch or counterfeit version is the cause.',\n", " 'followup_message': \"R\u00e9ponse de suivi :\\n\\nCher(e) critique,\\n\\nNous sommes d\u00e9sol\u00e9s d'apprendre que notre produit ne r\u00e9pond pas \u00e0 vos attentes. Nous prenons tr\u00e8s au s\u00e9rieux les commentaires de nos clients et nous tenons \u00e0 vous rassurer que nous attachons une grande importance \u00e0 la qualit\u00e9 de nos produits.\\n\\nNous v\u00e9rifions constamment nos processus de production afin de nous assurer que nos produits sont toujours frais et authentiques. Si vous pensez avoir re\u00e7u un produit contrefait ou un lot p\u00e9rim\u00e9, nous vous prions de nous contacter directement afin de r\u00e9soudre ce probl\u00e8me au plus vite. Nous avons un service client d\u00e9di\u00e9 qui sera ravi de vous aider avec votre pr\u00e9occupation.\\n\\nNous tenons \u00e0 nous excuser pour toute d\u00e9ception ou d\u00e9sagr\u00e9ment que cela aurait pu causer. Votre satisfaction est notre priorit\u00e9 absolue et nous ferons tout notre possible pour rem\u00e9dier \u00e0 la situation et regagner votre confiance.\\n\\nNous vous remercions de votre compr\u00e9hension et de votre patience. Votre avis est important pour nous et nous esp\u00e9rons avoir l'occasion de nous rattraper \u00e0 l'avenir.\\n\\nCordialement,\\nL'\u00e9quipe de [Nom de l'entreprise]\"}"]}, "execution_count": 43, "metadata": {}, "output_type": "execute_result"}], "source": ["review = df.Review[5]\n", "overall_chain(review)"]}, {"cell_type": "markdown", "id": "18d72f1d-8a1e-4eb6-b00f-1963b3cf3adb", "metadata": {}, "source": ["#### 3.2.3 \u4e2d\u6587\u63d0\u793a"]}, {"cell_type": "code", "execution_count": 37, "id": "31624a7c", "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": ["{'Review': \"Je trouve le go\u00fbt m\u00e9diocre. La mousse ne tient pas, c'est bizarre. J'ach\u00e8te les m\u00eames dans le commerce et le go\u00fbt est bien meilleur...\\nVieux lot ou contrefa\u00e7on !?\",\n", " 'English_Review': \"I find the taste mediocre. The foam doesn't last, it's weird. I buy the same ones from the store and the taste is much better...\\nOld batch or counterfeit!?\",\n", " 'summary': \"The taste is mediocre, the foam doesn't last, and there is suspicion of old batch or counterfeit.\",\n", " 'followup_message': \"\u56de\u590d: Je suis d\u00e9sol\u00e9 de vous entendre dire que le go\u00fbt est moyen et que la mousse ne dure pas longtemps. Nous prenons ces probl\u00e8mes tr\u00e8s au s\u00e9rieux. Nous allons enqu\u00eater pour v\u00e9rifier s'il s'agit d'un ancien lot ou d'une contrefa\u00e7on. Votre satisfaction est notre priorit\u00e9 et nous ferons de notre mieux pour r\u00e9soudre ce probl\u00e8me. Merci de nous avoir inform\u00e9s.\"}"]}, "execution_count": 37, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u4e2d\u6587\n", "\n", "#\u5b50\u94fe1\n", "# prompt\u6a21\u677f 1: \u7ffb\u8bd1\u6210\u82f1\u8bed\uff08\u628a\u4e0b\u9762\u7684review\u7ffb\u8bd1\u6210\u82f1\u8bed\uff09\n", "first_prompt = ChatPromptTemplate.from_template(\n", " \"\u628a\u4e0b\u9762\u7684\u8bc4\u8bbareview\u7ffb\u8bd1\u6210\u82f1\u6587:\"\n", " \"\\n\\n{Review}\"\n", ")\n", "# chain 1: \u8f93\u5165\uff1aReview \u8f93\u51fa\uff1a\u82f1\u6587\u7684 Review\n", "chain_one = LLMChain(llm=llm, prompt=first_prompt, output_key=\"English_Review\")\n", "\n", "#\u5b50\u94fe2\n", "# prompt\u6a21\u677f 2: \u7528\u4e00\u53e5\u8bdd\u603b\u7ed3\u4e0b\u9762\u7684 review\n", "second_prompt = ChatPromptTemplate.from_template(\n", " \"\u8bf7\u4f60\u7528\u4e00\u53e5\u8bdd\u6765\u603b\u7ed3\u4e0b\u9762\u7684\u8bc4\u8bbareview:\"\n", " \"\\n\\n{English_Review}\"\n", ")\n", "# chain 2: \u8f93\u5165\uff1a\u82f1\u6587\u7684Review \u8f93\u51fa\uff1a\u603b\u7ed3\n", "chain_two = LLMChain(llm=llm, prompt=second_prompt, output_key=\"summary\")\n", "\n", "\n", "#\u5b50\u94fe3\n", "# prompt\u6a21\u677f 3: \u4e0b\u9762review\u4f7f\u7528\u7684\u4ec0\u4e48\u8bed\u8a00\n", "third_prompt = ChatPromptTemplate.from_template(\n", " \"\u4e0b\u9762\u7684\u8bc4\u8bbareview\u4f7f\u7528\u7684\u4ec0\u4e48\u8bed\u8a00:\\n\\n{Review}\"\n", ")\n", "# chain 3: \u8f93\u5165\uff1aReview \u8f93\u51fa\uff1a\u8bed\u8a00\n", "chain_three = LLMChain(llm=llm, prompt=third_prompt, output_key=\"language\")\n", "\n", "\n", "#\u5b50\u94fe4\n", "# prompt\u6a21\u677f 4: \u4f7f\u7528\u7279\u5b9a\u7684\u8bed\u8a00\u5bf9\u4e0b\u9762\u7684\u603b\u7ed3\u5199\u4e00\u4e2a\u540e\u7eed\u56de\u590d\n", "fourth_prompt = ChatPromptTemplate.from_template(\n", " \"\u4f7f\u7528\u7279\u5b9a\u7684\u8bed\u8a00\u5bf9\u4e0b\u9762\u7684\u603b\u7ed3\u5199\u4e00\u4e2a\u540e\u7eed\u56de\u590d:\"\n", " \"\\n\\n\u603b\u7ed3: {summary}\\n\\n\u8bed\u8a00: {language}\"\n", ")\n", "# chain 4: \u8f93\u5165\uff1a \u603b\u7ed3, \u8bed\u8a00 \u8f93\u51fa\uff1a \u540e\u7eed\u56de\u590d\n", "chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n", " output_key=\"followup_message\"\n", " )\n", "\n", "\n", "# \u5bf9\u56db\u4e2a\u5b50\u94fe\u8fdb\u884c\u7ec4\u5408\n", "#\u8f93\u5165\uff1areview \u8f93\u51fa\uff1a\u82f1\u6587review\uff0c\u603b\u7ed3\uff0c\u540e\u7eed\u56de\u590d \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)"]}, {"cell_type": "markdown", "id": "3041ea4c", "metadata": {}, "source": ["## \u56db\u3001 \u8def\u7531\u94fe"]}, {"cell_type": "markdown", "id": "f0c32f97", "metadata": {}, "source": ["\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u5927\u8bed\u8a00\u6a21\u578b\u94fe\u548c\u987a\u5e8f\u94fe\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u60f3\u505a\u4e00\u4e9b\u66f4\u590d\u6742\u7684\u4e8b\u60c5\u600e\u4e48\u529e\uff1f\n", "\n", "\u4e00\u4e2a\u76f8\u5f53\u5e38\u89c1\u4f46\u57fa\u672c\u7684\u64cd\u4f5c\u662f\u6839\u636e\u8f93\u5165\u5c06\u5176\u8def\u7531\u5230\u4e00\u6761\u94fe\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u8be5\u8f93\u5165\u5230\u5e95\u662f\u4ec0\u4e48\u3002\u5982\u679c\u4f60\u6709\u591a\u4e2a\u5b50\u94fe\uff0c\u6bcf\u4e2a\u5b50\u94fe\u90fd\u4e13\u95e8\u7528\u4e8e\u7279\u5b9a\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u90a3\u4e48\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2a\u8def\u7531\u94fe\uff0c\u5b83\u9996\u5148\u51b3\u5b9a\u5c06\u5b83\u4f20\u9012\u7ed9\u54ea\u4e2a\u5b50\u94fe\uff0c\u7136\u540e\u5c06\u5b83\u4f20\u9012\u7ed9\u90a3\u4e2a\u94fe\u3002\n", "\n", "\u8def\u7531\u5668\u7531\u4e24\u4e2a\u7ec4\u4ef6\u7ec4\u6210\uff1a\n", "\n", "- \u8def\u7531\u94fe\uff08Router Chain\uff09\uff1a\u8def\u7531\u5668\u94fe\u672c\u8eab\uff0c\u8d1f\u8d23\u9009\u62e9\u8981\u8c03\u7528\u7684\u4e0b\u4e00\u4e2a\u94fe\n", "- destination_chains\uff1a\u8def\u7531\u5668\u94fe\u53ef\u4ee5\u8def\u7531\u5230\u7684\u94fe\n", "\n", "\u4e3e\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u6211\u4eec\u5728\u4e0d\u540c\u7c7b\u578b\u7684\u94fe\u4e4b\u95f4\u8def\u7531\u7684\u5730\u65b9\uff0c\u6211\u4eec\u5728\u8fd9\u91cc\u6709\u4e0d\u540c\u7684prompt: "]}, {"cell_type": "code", "execution_count": 28, "id": "31b06fc8", "metadata": {}, "outputs": [], "source": ["from langchain.chains.router import MultiPromptChain #\u5bfc\u5165\u591a\u63d0\u793a\u94fe\n", "from langchain.chains.router.llm_router import LLMRouterChain,RouterOutputParser\n", "from langchain.prompts import PromptTemplate"]}, {"cell_type": "code", "execution_count": 29, "id": "f3f50bcc", "metadata": {}, "outputs": [], "source": ["llm = ChatOpenAI(temperature=0)"]}, {"cell_type": "markdown", "id": "cb1b4708", "metadata": {}, "source": ["### 4.1 \u5b9a\u4e49\u63d0\u793a\u6a21\u677f"]}, {"cell_type": "markdown", "id": "c3b85285-c736-4a5d-bd14-d9b5025ca29b", "metadata": {}, "source": ["\u9996\u5148\uff0c\u6211\u4eec\u5b9a\u4e49\u63d0\u793a\u9002\u7528\u4e8e\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u63d0\u793a\u6a21\u677f\u3002"]}, {"cell_type": "code", "execution_count": 49, "id": "ade83f4f", "metadata": {}, "outputs": [], "source": ["#\u7b2c\u4e00\u4e2a\u63d0\u793a\u9002\u5408\u56de\u7b54\u7269\u7406\u95ee\u9898\n", "physics_template = \"\"\"You are a very smart physics professor. \\\n", "You are great at answering questions about physics in a concise\\\n", "and easy to understand manner. \\\n", "When you don't know the answer to a question you admit\\\n", "that you don't know.\n", "\n", "Here is a question:\n", "{input}\"\"\"\n", "\n", "\n", "#\u7b2c\u4e8c\u4e2a\u63d0\u793a\u9002\u5408\u56de\u7b54\u6570\u5b66\u95ee\u9898\n", "math_template = \"\"\"You are a very good mathematician. \\\n", "You are great at answering math questions. \\\n", "You are so good because you are able to break down \\\n", "hard problems into their component parts, \n", "answer the component parts, and then put them together\\\n", "to answer the broader question.\n", "\n", "Here is a question:\n", "{input}\"\"\"\n", "\n", "\n", "#\u7b2c\u4e09\u4e2a\u9002\u5408\u56de\u7b54\u5386\u53f2\u95ee\u9898\n", "history_template = \"\"\"You are a very good historian. \\\n", "You have an excellent knowledge of and understanding of people,\\\n", "events and contexts from a range of historical periods. \\\n", "You have the ability to think, reflect, debate, discuss and \\\n", "evaluate the past. You have a respect for historical evidence\\\n", "and the ability to make use of it to support your explanations \\\n", "and judgements.\n", "\n", "Here is a question:\n", "{input}\"\"\"\n", "\n", "\n", "#\u7b2c\u56db\u4e2a\u9002\u5408\u56de\u7b54\u8ba1\u7b97\u673a\u95ee\u9898\n", "computerscience_template = \"\"\" You are a successful computer scientist.\\\n", "You have a passion for creativity, collaboration,\\\n", "forward-thinking, confidence, strong problem-solving capabilities,\\\n", "understanding of theories and algorithms, and excellent communication \\\n", "skills. You are great at answering coding questions. \\\n", "You are so good because you know how to solve a problem by \\\n", "describing the solution in imperative steps \\\n", "that a machine can easily interpret and you know how to \\\n", "choose a solution that has a good balance between \\\n", "time complexity and space complexity. \n", "\n", "Here is a question:\n", "{input}\"\"\""]}, {"cell_type": "markdown", "id": "cb3b749b-0d3c-46f2-afab-b1213f208b13", "metadata": {}, "source": ["### 4.2 \u5bf9\u63d0\u793a\u6a21\u7248\u8fdb\u884c\u547d\u540d\u548c\u63cf\u8ff0"]}, {"cell_type": "markdown", "id": "6922b35e", "metadata": {}, "source": ["\u5728\u5b9a\u4e49\u4e86\u8fd9\u4e9b\u63d0\u793a\u6a21\u677f\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6a21\u677f\u547d\u540d\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u63cf\u8ff0\u3002\u4f8b\u5982\uff0c\u7b2c\u4e00\u4e2a\u7269\u7406\u5b66\u7684\u63cf\u8ff0\u9002\u5408\u56de\u7b54\u5173\u4e8e\u7269\u7406\u5b66\u7684\u95ee\u9898\uff0c\u8fd9\u4e9b\u4fe1\u606f\u5c06\u4f20\u9012\u7ed9\u8def\u7531\u94fe\uff0c\u7136\u540e\u7531\u8def\u7531\u94fe\u51b3\u5b9a\u4f55\u65f6\u4f7f\u7528\u6b64\u5b50\u94fe\u3002"]}, {"cell_type": "code", "execution_count": 27, "id": "141a3d32", "metadata": {}, "outputs": [], "source": ["prompt_infos = [\n", " {\n", " \"name\": \"physics\", \n", " \"description\": \"Good for answering questions about physics\", \n", " \"prompt_template\": physics_template\n", " },\n", " {\n", " \"name\": \"math\", \n", " \"description\": \"Good for answering math questions\", \n", " \"prompt_template\": math_template\n", " },\n", " {\n", " \"name\": \"History\", \n", " \"description\": \"Good for answering history questions\", \n", " \"prompt_template\": history_template\n", " },\n", " {\n", " \"name\": \"computer science\", \n", " \"description\": \"Good for answering computer science questions\", \n", " \"prompt_template\": computerscience_template\n", " }\n", "]"]}, {"cell_type": "markdown", "id": "8795cd42", "metadata": {}, "source": ["LLMRouterChain\uff08\u6b64\u94fe\u4f7f\u7528 LLM \u6765\u786e\u5b9a\u5982\u4f55\u8def\u7531\u4e8b\u7269\uff09\n", "\n", "\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a**\u591a\u63d0\u793a\u94fe**\u3002\u8fd9\u662f\u4e00\u79cd\u7279\u5b9a\u7c7b\u578b\u7684\u94fe\uff0c\u7528\u4e8e\u5728\u591a\u4e2a\u4e0d\u540c\u7684\u63d0\u793a\u6a21\u677f\u4e4b\u95f4\u8fdb\u884c\u8def\u7531\u3002 \u4f46\u662f\u8fd9\u53ea\u662f\u8def\u7531\u7684\u4e00\u79cd\u7c7b\u578b\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u4efb\u4f55\u7c7b\u578b\u7684\u94fe\u4e4b\u95f4\u8fdb\u884c\u8def\u7531\u3002\n", "\n", "\u8fd9\u91cc\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u51e0\u4e2a\u7c7b\u662f\u5927\u6a21\u578b\u8def\u7531\u5668\u94fe\u3002\u8fd9\u4e2a\u7c7b\u672c\u8eab\u4f7f\u7528\u8bed\u8a00\u6a21\u578b\u6765\u5728\u4e0d\u540c\u7684\u5b50\u94fe\u4e4b\u95f4\u8fdb\u884c\u8def\u7531\u3002\u8fd9\u5c31\u662f\u4e0a\u9762\u63d0\u4f9b\u7684\u63cf\u8ff0\u548c\u540d\u79f0\u5c06\u88ab\u4f7f\u7528\u7684\u5730\u65b9\u3002"]}, {"cell_type": "markdown", "id": "46633b43", "metadata": {}, "source": ["### 4.3 \u57fa\u4e8e\u63d0\u793a\u6a21\u7248\u4fe1\u606f\u521b\u5efa\u76f8\u5e94\u76ee\u6807\u94fe \n", "\u76ee\u6807\u94fe\u662f\u7531\u8def\u7531\u94fe\u8c03\u7528\u7684\u94fe\uff0c\u6bcf\u4e2a\u76ee\u6807\u94fe\u90fd\u662f\u4e00\u4e2a\u8bed\u8a00\u6a21\u578b\u94fe"]}, {"cell_type": "code", "execution_count": 30, "id": "8eefec24", "metadata": {}, "outputs": [], "source": ["destination_chains = {}\n", "for p_info in prompt_infos:\n", " name = p_info[\"name\"]\n", " prompt_template = p_info[\"prompt_template\"]\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['name']}: {p['description']}\" for p in prompt_infos]\n", "destinations_str = \"\\n\".join(destinations)"]}, {"cell_type": "markdown", "id": "eba115de", "metadata": {"tags": []}, "source": ["### 4.4 \u521b\u5efa\u9ed8\u8ba4\u76ee\u6807\u94fe\n", "\u9664\u4e86\u76ee\u6807\u94fe\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4e00\u4e2a\u9ed8\u8ba4\u76ee\u6807\u94fe\u3002\u8fd9\u662f\u4e00\u4e2a\u5f53\u8def\u7531\u5668\u65e0\u6cd5\u51b3\u5b9a\u4f7f\u7528\u54ea\u4e2a\u5b50\u94fe\u65f6\u8c03\u7528\u7684\u94fe\u3002\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5f53\u8f93\u5165\u95ee\u9898\u4e0e\u7269\u7406\u3001\u6570\u5b66\u3001\u5386\u53f2\u6216\u8ba1\u7b97\u673a\u79d1\u5b66\u65e0\u5173\u65f6\uff0c\u53ef\u80fd\u4f1a\u8c03\u7528\u5b83\u3002"]}, {"cell_type": "code", "execution_count": 31, "id": "9f98018a", "metadata": {}, "outputs": [], "source": ["default_prompt = ChatPromptTemplate.from_template(\"{input}\")\n", "default_chain = LLMChain(llm=llm, prompt=default_prompt)"]}, {"cell_type": "markdown", "id": "948700c4", "metadata": {}, "source": ["### 4.5 \u5b9a\u4e49\u4e0d\u540c\u94fe\u4e4b\u95f4\u7684\u8def\u7531\u6a21\u677f\n", "\n", "\u8fd9\u5305\u62ec\u8981\u5b8c\u6210\u7684\u4efb\u52a1\u7684\u8bf4\u660e\u4ee5\u53ca\u8f93\u51fa\u5e94\u8be5\u91c7\u7528\u7684\u7279\u5b9a\u683c\u5f0f\u3002"]}, {"cell_type": "markdown", "id": "24f30c2c", "metadata": {}, "source": ["\u6ce8\u610f\uff1a\u6b64\u5904\u5728\u539f\u6559\u7a0b\u7684\u57fa\u7840\u4e0a\u6dfb\u52a0\u4e86\u4e00\u4e2a\u793a\u4f8b\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\"gpt-3.5-turbo\"\u6a21\u578b\u4e0d\u80fd\u5f88\u597d\u9002\u5e94\u7406\u89e3\u6a21\u677f\u7684\u610f\u601d\uff0c\u4f7f\u7528 \"text-davinci-003\" \u6216\u8005\"gpt-4-0613\"\u53ef\u4ee5\u5f88\u597d\u7684\u5de5\u4f5c\uff0c\u56e0\u6b64\u5728\u8fd9\u91cc\u591a\u52a0\u4e86\u793a\u4f8b\u63d0\u793a\u8ba9\u5176\u66f4\u597d\u7684\u5b66\u4e60\u3002\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": 32, "id": "11b2e2ba", "metadata": {}, "outputs": [], "source": ["MULTI_PROMPT_ROUTER_TEMPLATE = \"\"\"Given a raw text input to a \\\n", "language model select the model prompt best suited for the input. \\\n", "You will be given the names of the available prompts and a \\\n", "description of what the prompt is best suited for. \\\n", "You may also revise the original input if you think that revising\\\n", "it will ultimately lead to a better response from the language model.\n", "\n", "<< FORMATTING >>\n", "Return a markdown code snippet with a JSON object formatted to look like:\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", "REMEMBER: \"destination\" MUST be one of the candidate prompt \\\n", "names specified below OR it can be \"DEFAULT\" if the input is not\\\n", "well suited for any of the candidate prompts.\n", "REMEMBER: \"next_inputs\" can just be the original input \\\n", "if you don't think any modifications are needed.\n", "\n", "<< CANDIDATE PROMPTS >>\n", "{destinations}\n", "\n", "<< INPUT >>\n", "{{input}}\n", "\n", "<< 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": "markdown", "id": "de5c46d0", "metadata": {}, "source": ["### 4.6 \u6784\u5efa\u8def\u7531\u94fe\n", "\u9996\u5148\uff0c\u6211\u4eec\u901a\u8fc7\u683c\u5f0f\u5316\u4e0a\u9762\u5b9a\u4e49\u7684\u76ee\u6807\u521b\u5efa\u5b8c\u6574\u7684\u8def\u7531\u5668\u6a21\u677f\u3002\u8fd9\u4e2a\u6a21\u677f\u53ef\u4ee5\u9002\u7528\u8bb8\u591a\u4e0d\u540c\u7c7b\u578b\u7684\u76ee\u6807\u3002\n", "\u56e0\u6b64\uff0c\u5728\u8fd9\u91cc\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u4e0d\u540c\u7684\u5b66\u79d1\uff0c\u5982\u82f1\u8bed\u6216\u62c9\u4e01\u8bed\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u7269\u7406\u3001\u6570\u5b66\u3001\u5386\u53f2\u548c\u8ba1\u7b97\u673a\u79d1\u5b66\u3002\n", "\n", "\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4ece\u8fd9\u4e2a\u6a21\u677f\u521b\u5efa\u63d0\u793a\u6a21\u677f\n", "\n", "\u6700\u540e\uff0c\u901a\u8fc7\u4f20\u5165llm\u548c\u6574\u4e2a\u8def\u7531\u63d0\u793a\u6765\u521b\u5efa\u8def\u7531\u94fe\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u8fd9\u91cc\u6709\u8def\u7531\u8f93\u51fa\u89e3\u6790\uff0c\u8fd9\u5f88\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u5c06\u5e2e\u52a9\u8fd9\u4e2a\u94fe\u8def\u51b3\u5b9a\u5728\u54ea\u4e9b\u5b50\u94fe\u8def\u4e4b\u95f4\u8fdb\u884c\u8def\u7531\u3002"]}, {"cell_type": "code", "execution_count": 34, "id": "1387109d", "metadata": {}, "outputs": [], "source": ["router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(\n", " destinations=destinations_str\n", ")\n", "router_prompt = PromptTemplate(\n", " template=router_template,\n", " input_variables=[\"input\"],\n", " output_parser=RouterOutputParser(),\n", ")\n", "\n", "router_chain = LLMRouterChain.from_llm(llm, router_prompt)"]}, {"cell_type": "markdown", "id": "7e92355c", "metadata": {}, "source": ["### 4.7 \u521b\u5efa\u6574\u4f53\u94fe\u8def"]}, {"cell_type": "code", "execution_count": 35, "id": "2fb7d560", "metadata": {}, "outputs": [], "source": ["#\u591a\u63d0\u793a\u94fe\n", "chain = MultiPromptChain(router_chain=router_chain, #l\u8def\u7531\u94fe\u8def\n", " destination_chains=destination_chains, #\u76ee\u6807\u94fe\u8def\n", " default_chain=default_chain, #\u9ed8\u8ba4\u94fe\u8def\n", " verbose=True \n", " )"]}, {"cell_type": "markdown", "id": "086503f7", "metadata": {}, "source": ["### 4.8 \u8fdb\u884c\u63d0\u95ee"]}, {"cell_type": "markdown", "id": "969cd878", "metadata": {}, "source": ["\u5982\u679c\u6211\u4eec\u95ee\u4e00\u4e2a\u7269\u7406\u95ee\u9898\uff0c\u6211\u4eec\u5e0c\u671b\u770b\u5230\u4ed6\u88ab\u8def\u7531\u5230\u7269\u7406\u94fe\u8def"]}, {"cell_type": "code", "execution_count": null, "id": "2217d987", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "\n", "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", "physics: {'input': 'What is black body radiation?'}\n", "\u001b[1m> Finished chain.\u001b[0m\n"]}, {"data": {"text/plain": ["'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.'"]}, "metadata": {}, "output_type": "display_data"}], "source": ["# \u95ee\u9898\uff1a\u4ec0\u4e48\u662f\u9ed1\u4f53\u8f90\u5c04\uff1f\n", "chain.run(\"What is black body radiation?\")"]}, {"cell_type": "markdown", "id": "289c5ca9", "metadata": {}, "source": ["\u5982\u679c\u6211\u4eec\u95ee\u4e00\u4e2a\u6570\u5b66\u95ee\u9898\uff0c\u6211\u4eec\u5e0c\u671b\u770b\u5230\u4ed6\u88ab\u8def\u7531\u5230\u6570\u5b66\u94fe\u8def"]}, {"cell_type": "code", "execution_count": 22, "id": "3b717379", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "\n", "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", "math: {'input': 'what is 2 + 2'}\n", "\u001b[1m> Finished chain.\u001b[0m\n"]}, {"data": {"text/plain": ["'As an AI language model, I can answer this question. The answer to 2 + 2 is 4.'"]}, "execution_count": 22, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u95ee\u9898\uff1a2+2\u7b49\u4e8e\u591a\u5c11\uff1f\n", "chain.run(\"what is 2 + 2\")"]}, {"cell_type": "markdown", "id": "4186a2b9", "metadata": {}, "source": ["\u5982\u679c\u6211\u4eec\u4f20\u9012\u4e00\u4e2a\u4e0e\u4efb\u4f55\u5b50\u94fe\u8def\u90fd\u65e0\u5173\u7684\u95ee\u9898\u65f6\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\n", "\n", "\u8fd9\u91cc\uff0c\u6211\u4eec\u95ee\u4e86\u4e00\u4e2a\u5173\u4e8e\u751f\u7269\u5b66\u7684\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83\u9009\u62e9\u7684\u94fe\u8def\u662f\u65e0\u3002\u8fd9\u610f\u5473\u7740\u5b83\u5c06\u88ab**\u4f20\u9012\u5230\u9ed8\u8ba4\u94fe\u8def\uff0c\u5b83\u672c\u8eab\u53ea\u662f\u5bf9\u8bed\u8a00\u6a21\u578b\u7684\u901a\u7528\u8c03\u7528**\u3002\u8bed\u8a00\u6a21\u578b\u5e78\u8fd0\u5730\u5bf9\u751f\u7269\u5b66\u77e5\u9053\u5f88\u591a\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u3002"]}, {"cell_type": "code", "execution_count": 40, "id": "29e5be01", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "\n", "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", "None: {'input': 'Why does every cell in our body contain DNA?'}\n", "\u001b[1m> Finished chain.\u001b[0m\n"]}, {"data": {"text/plain": ["'Every cell in our body contains DNA because DNA carries the genetic information that determines the characteristics and functions of each cell. DNA contains the instructions for the synthesis of proteins, which are essential for the structure and function of cells. Additionally, DNA is responsible for the transmission of genetic information from one generation to the next. Therefore, every cell in our body needs DNA to carry out its specific functions and to maintain the integrity of the organism as a whole.'"]}, "execution_count": 40, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6211\u4eec\u8eab\u4f53\u91cc\u7684\u6bcf\u4e2a\u7ec6\u80de\u90fd\u5305\u542bDNA\uff1f\n", "chain.run(\"Why does every cell in our body contain DNA?\")"]}, {"cell_type": "markdown", "id": "753b5f1d-b010-4f33-a928-d3b9e85bf289", "metadata": {}, "source": ["### 4.9 \u4e2d\u6587\u63d0\u793a"]}, {"cell_type": "code", "execution_count": 51, "id": "f7fade7a", "metadata": {}, "outputs": [], "source": ["# \u4e2d\u6587\n", "#\u7b2c\u4e00\u4e2a\u63d0\u793a\u9002\u5408\u56de\u7b54\u7269\u7406\u95ee\u9898\n", "physics_template = \"\"\"\u4f60\u662f\u4e00\u4e2a\u975e\u5e38\u806a\u660e\u7684\u7269\u7406\u4e13\u5bb6\u3002 \\\n", "\u4f60\u64c5\u957f\u7528\u4e00\u79cd\u7b80\u6d01\u5e76\u4e14\u6613\u4e8e\u7406\u89e3\u7684\u65b9\u5f0f\u53bb\u56de\u7b54\u95ee\u9898\u3002\\\n", "\u5f53\u4f60\u4e0d\u77e5\u9053\u95ee\u9898\u7684\u7b54\u6848\u65f6\uff0c\u4f60\u627f\u8ba4\\\n", "\u4f60\u4e0d\u77e5\u9053.\n", "\n", "\u8fd9\u662f\u4e00\u4e2a\u95ee\u9898:\n", "{input}\"\"\"\n", "\n", "\n", "#\u7b2c\u4e8c\u4e2a\u63d0\u793a\u9002\u5408\u56de\u7b54\u6570\u5b66\u95ee\u9898\n", "math_template = \"\"\"\u4f60\u662f\u4e00\u4e2a\u975e\u5e38\u4f18\u79c0\u7684\u6570\u5b66\u5bb6\u3002 \\\n", "\u4f60\u64c5\u957f\u56de\u7b54\u6570\u5b66\u95ee\u9898\u3002 \\\n", "\u4f60\u4e4b\u6240\u4ee5\u5982\u6b64\u4f18\u79c0\uff0c \\\n", "\u662f\u56e0\u4e3a\u4f60\u80fd\u591f\u5c06\u68d8\u624b\u7684\u95ee\u9898\u5206\u89e3\u4e3a\u7ec4\u6210\u90e8\u5206\uff0c\\\n", "\u56de\u7b54\u7ec4\u6210\u90e8\u5206\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u56de\u7b54\u66f4\u5e7f\u6cdb\u7684\u95ee\u9898\u3002\n", "\n", "\u8fd9\u662f\u4e00\u4e2a\u95ee\u9898\uff1a\n", "{input}\"\"\"\n", "\n", "\n", "#\u7b2c\u4e09\u4e2a\u9002\u5408\u56de\u7b54\u5386\u53f2\u95ee\u9898\n", "history_template = \"\"\"\u4f60\u662f\u4ee5\u4e3a\u975e\u5e38\u4f18\u79c0\u7684\u5386\u53f2\u5b66\u5bb6\u3002 \\\n", "\u4f60\u5bf9\u4e00\u7cfb\u5217\u5386\u53f2\u65f6\u671f\u7684\u4eba\u7269\u3001\u4e8b\u4ef6\u548c\u80cc\u666f\u6709\u7740\u6781\u597d\u7684\u5b66\u8bc6\u548c\u7406\u89e3\\\n", "\u4f60\u6709\u80fd\u529b\u601d\u8003\u3001\u53cd\u601d\u3001\u8fa9\u8bc1\u3001\u8ba8\u8bba\u548c\u8bc4\u4f30\u8fc7\u53bb\u3002\\\n", "\u4f60\u5c0a\u91cd\u5386\u53f2\u8bc1\u636e\uff0c\u5e76\u6709\u80fd\u529b\u5229\u7528\u5b83\u6765\u652f\u6301\u4f60\u7684\u89e3\u91ca\u548c\u5224\u65ad\u3002\n", "\n", "\u8fd9\u662f\u4e00\u4e2a\u95ee\u9898:\n", "{input}\"\"\"\n", "\n", "\n", "#\u7b2c\u56db\u4e2a\u9002\u5408\u56de\u7b54\u8ba1\u7b97\u673a\u95ee\u9898\n", "computerscience_template = \"\"\" \u4f60\u662f\u4e00\u4e2a\u6210\u529f\u7684\u8ba1\u7b97\u673a\u79d1\u5b66\u4e13\u5bb6\u3002\\\n", "\u4f60\u6709\u521b\u9020\u529b\u3001\u534f\u4f5c\u7cbe\u795e\u3001\\\n", "\u524d\u77bb\u6027\u601d\u7ef4\u3001\u81ea\u4fe1\u3001\u89e3\u51b3\u95ee\u9898\u7684\u80fd\u529b\u3001\\\n", "\u5bf9\u7406\u8bba\u548c\u7b97\u6cd5\u7684\u7406\u89e3\u4ee5\u53ca\u51fa\u8272\u7684\u6c9f\u901a\u6280\u5de7\u3002\\\n", "\u4f60\u975e\u5e38\u64c5\u957f\u56de\u7b54\u7f16\u7a0b\u95ee\u9898\u3002\\\n", "\u4f60\u4e4b\u6240\u4ee5\u5982\u6b64\u4f18\u79c0\uff0c\u662f\u56e0\u4e3a\u4f60\u77e5\u9053 \\\n", "\u5982\u4f55\u901a\u8fc7\u4ee5\u673a\u5668\u53ef\u4ee5\u8f7b\u677e\u89e3\u91ca\u7684\u547d\u4ee4\u5f0f\u6b65\u9aa4\u63cf\u8ff0\u89e3\u51b3\u65b9\u6848\u6765\u89e3\u51b3\u95ee\u9898\uff0c\\\n", "\u5e76\u4e14\u4f60\u77e5\u9053\u5982\u4f55\u9009\u62e9\u5728\u65f6\u95f4\u590d\u6742\u6027\u548c\u7a7a\u95f4\u590d\u6742\u6027\u4e4b\u95f4\u53d6\u5f97\u826f\u597d\u5e73\u8861\u7684\u89e3\u51b3\u65b9\u6848\u3002\n", "\n", "\u8fd9\u8fd8\u662f\u4e00\u4e2a\u8f93\u5165\uff1a\n", "{input}\"\"\""]}, {"cell_type": "code", "execution_count": 52, "id": "deb8aafc", "metadata": {}, "outputs": [], "source": ["# \u4e2d\u6587\n", "prompt_infos = [\n", " {\n", " \"\u540d\u5b57\": \"\u7269\u7406\u5b66\", \n", " \"\u63cf\u8ff0\": \"\u64c5\u957f\u56de\u7b54\u5173\u4e8e\u7269\u7406\u5b66\u7684\u95ee\u9898\", \n", " \"\u63d0\u793a\u6a21\u677f\": physics_template\n", " },\n", " {\n", " \"\u540d\u5b57\": \"\u6570\u5b66\", \n", " \"\u63cf\u8ff0\": \"\u64c5\u957f\u56de\u7b54\u6570\u5b66\u95ee\u9898\", \n", " \"\u63d0\u793a\u6a21\u677f\": math_template\n", " },\n", " {\n", " \"\u540d\u5b57\": \"\u5386\u53f2\", \n", " \"\u63cf\u8ff0\": \"\u64c5\u957f\u56de\u7b54\u5386\u53f2\u95ee\u9898\", \n", " \"\u63d0\u793a\u6a21\u677f\": history_template\n", " },\n", " {\n", " \"\u540d\u5b57\": \"\u8ba1\u7b97\u673a\u79d1\u5b66\", \n", " \"\u63cf\u8ff0\": \"\u64c5\u957f\u56de\u7b54\u8ba1\u7b97\u673a\u79d1\u5b66\u95ee\u9898\", \n", " \"\u63d0\u793a\u6a21\u677f\": computerscience_template\n", " }\n", "]\n"]}, {"cell_type": "code", "execution_count": null, "id": "fd6eb641", "metadata": {}, "outputs": [], "source": ["# \u4e2d\u6587\n", "destination_chains = {}\n", "for p_info in prompt_infos:\n", " name = p_info[\"\u540d\u5b57\"]\n", " prompt_template = p_info[\"\u63d0\u793a\u6a21\u677f\"]\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['\u540d\u5b57']}: {p['\u63cf\u8ff0']}\" for p in prompt_infos]\n", "destinations_str = \"\\n\".join(destinations)"]}, {"cell_type": "code", "execution_count": null, "id": "af088d6b-a70b-4cd7-bc6e-4ad7b32e6ae1", "metadata": {}, "outputs": [], "source": ["default_prompt = ChatPromptTemplate.from_template(\"{input}\")\n", "default_chain = LLMChain(llm=llm, prompt=default_prompt)"]}, {"cell_type": "code", "execution_count": null, "id": "a7aae035", "metadata": {}, "outputs": [], "source": ["# \u4e2d\u6587\n", "\n", "# \u591a\u63d0\u793a\u8def\u7531\u6a21\u677f\n", "MULTI_PROMPT_ROUTER_TEMPLATE = \"\"\"\u7ed9\u8bed\u8a00\u6a21\u578b\u4e00\u4e2a\u539f\u59cb\u6587\u672c\u8f93\u5165\uff0c\\\n", "\u8ba9\u5176\u9009\u62e9\u6700\u9002\u5408\u8f93\u5165\u7684\u6a21\u578b\u63d0\u793a\u3002\\\n", "\u7cfb\u7edf\u5c06\u4e3a\u60a8\u63d0\u4f9b\u53ef\u7528\u63d0\u793a\u7684\u540d\u79f0\u4ee5\u53ca\u6700\u9002\u5408\u6539\u63d0\u793a\u7684\u63cf\u8ff0\u3002\\\n", "\u5982\u679c\u4f60\u8ba4\u4e3a\u4fee\u6539\u539f\u59cb\u8f93\u5165\u6700\u7ec8\u4f1a\u5bfc\u81f4\u8bed\u8a00\u6a21\u578b\u505a\u51fa\u66f4\u597d\u7684\u54cd\u5e94\uff0c\\\n", "\u4f60\u4e5f\u53ef\u4ee5\u4fee\u6539\u539f\u59cb\u8f93\u5165\u3002\n", "\n", "\n", "<< \u683c\u5f0f >>\n", "\u8fd4\u56de\u4e00\u4e2a\u5e26\u6709JSON\u5bf9\u8c61\u7684markdown\u4ee3\u7801\u7247\u6bb5\uff0c\u8be5JSON\u5bf9\u8c61\u7684\u683c\u5f0f\u5982\u4e0b\uff1a\n", "```json\n", "{{{{\n", " \"destination\": \u5b57\u7b26\u4e32 \\ \u4f7f\u7528\u7684\u63d0\u793a\u540d\u5b57\u6216\u8005\u4f7f\u7528 \"DEFAULT\"\n", " \"next_inputs\": \u5b57\u7b26\u4e32 \\ \u539f\u59cb\u8f93\u5165\u7684\u6539\u8fdb\u7248\u672c\n", "}}}}\n", "```\n", "\n", "\n", "\u8bb0\u4f4f\uff1a\u201cdestination\u201d\u5fc5\u987b\u662f\u4e0b\u9762\u6307\u5b9a\u7684\u5019\u9009\u63d0\u793a\u540d\u79f0\u4e4b\u4e00\uff0c\\\n", "\u6216\u8005\u5982\u679c\u8f93\u5165\u4e0d\u592a\u9002\u5408\u4efb\u4f55\u5019\u9009\u63d0\u793a\uff0c\\\n", "\u5219\u53ef\u4ee5\u662f \u201cDEFAULT\u201d \u3002\n", "\u8bb0\u4f4f\uff1a\u5982\u679c\u60a8\u8ba4\u4e3a\u4e0d\u9700\u8981\u4efb\u4f55\u4fee\u6539\uff0c\\\n", "\u5219 \u201cnext_inputs\u201d \u53ef\u4ee5\u53ea\u662f\u539f\u59cb\u8f93\u5165\u3002\n", "\n", "<< \u5019\u9009\u63d0\u793a >>\n", "{destinations}\n", "\n", "<< \u8f93\u5165 >>\n", "{{input}}\n", "\n", "<< \u8f93\u51fa (\u8bb0\u5f97\u8981\u5305\u542b ```json)>>\n", "\n", "\u6837\u4f8b:\n", "<< \u8f93\u5165 >>\n", "\"\u4ec0\u4e48\u662f\u9ed1\u4f53\u8f90\u5c04?\"\n", "<< \u8f93\u51fa >>\n", "```json\n", "{{{{\n", " \"destination\": \u5b57\u7b26\u4e32 \\ \u4f7f\u7528\u7684\u63d0\u793a\u540d\u5b57\u6216\u8005\u4f7f\u7528 \"DEFAULT\"\n", " \"next_inputs\": \u5b57\u7b26\u4e32 \\ \u539f\u59cb\u8f93\u5165\u7684\u6539\u8fdb\u7248\u672c\n", "}}}}\n", "```\n", "\n", "\"\"\""]}, {"cell_type": "code", "execution_count": null, "id": "7470b25c-ef82-496c-a6e0-e04b99b08e70", "metadata": {}, "outputs": [], "source": ["#\u591a\u63d0\u793a\u94fe\n", "chain = MultiPromptChain(router_chain=router_chain, #l\u8def\u7531\u94fe\u8def\n", " destination_chains=destination_chains, #\u76ee\u6807\u94fe\u8def\n", " default_chain=default_chain, #\u9ed8\u8ba4\u94fe\u8def\n", " verbose=True \n", " )"]}, {"cell_type": "code", "execution_count": 36, "id": "4446724c", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "\n", "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", "physics: {'input': '\u4ec0\u4e48\u662f\u9ed1\u4f53\u8f90\u5c04\uff1f'}\n", "\u001b[1m> Finished chain.\u001b[0m\n"]}, {"data": {"text/plain": ["'\u9ed1\u4f53\u8f90\u5c04\u662f\u6307\u4e00\u4e2a\u7406\u60f3\u5316\u7684\u7269\u4f53\uff0c\u5b83\u80fd\u591f\u5b8c\u5168\u5438\u6536\u6240\u6709\u5165\u5c04\u5230\u5b83\u8868\u9762\u7684\u8f90\u5c04\u80fd\u91cf\uff0c\u5e76\u4ee5\u70ed\u8f90\u5c04\u7684\u5f62\u5f0f\u91cd\u65b0\u53d1\u5c04\u51fa\u6765\u3002\u9ed1\u4f53\u8f90\u5c04\u7684\u7279\u70b9\u662f\u5176\u8f90\u5c04\u80fd\u91cf\u7684\u5206\u5e03\u4e0e\u6e29\u5ea6\u6709\u5173\uff0c\u968f\u7740\u6e29\u5ea6\u7684\u5347\u9ad8\uff0c\u8f90\u5c04\u80fd\u91cf\u7684\u5cf0\u503c\u4f1a\u5411\u66f4\u77ed\u7684\u6ce2\u957f\u65b9\u5411\u79fb\u52a8\u3002\u8fd9\u4e2a\u73b0\u8c61\u88ab\u79f0\u4e3a\u9ed1\u4f53\u8f90\u5c04\u8c31\u7684\u4f4d\u79fb\u5b9a\u5f8b\uff0c\u7531\u666e\u6717\u514b\u572820\u4e16\u7eaa\u521d\u63d0\u51fa\u3002\u9ed1\u4f53\u8f90\u5c04\u5728\u7814\u7a76\u70ed\u529b\u5b66\u3001\u91cf\u5b50\u529b\u5b66\u548c\u5b87\u5b99\u5b66\u7b49\u9886\u57df\u4e2d\u5177\u6709\u91cd\u8981\u7684\u5e94\u7528\u3002'"]}, "execution_count": 36, "metadata": {}, "output_type": "execute_result"}], "source": ["#\u4e2d\u6587\n", "chain.run(\"\u4ec0\u4e48\u662f\u9ed1\u4f53\u8f90\u5c04\uff1f\")"]}, {"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': '\u4f60\u77e5\u9053\u674e\u767d\u662f\u8c01\u561b?'}\n", "\u001b[1m> Finished chain.\u001b[0m\n"]}, {"data": {"text/plain": ["'\u674e\u767d\u662f\u5510\u671d\u65f6\u671f\u7684\u4e00\u4f4d\u8457\u540d\u8bd7\u4eba\u3002\u4ed6\u7684\u8bd7\u6b4c\u4ee5\u8c6a\u653e\u3001\u5954\u653e\u3001\u81ea\u7531\u7684\u98ce\u683c\u8457\u79f0\uff0c\u88ab\u8a89\u4e3a\u201c\u8bd7\u4ed9\u201d\u3002\u4ed6\u7684\u4f5c\u54c1\u6d89\u53ca\u5e7f\u6cdb\uff0c\u5305\u62ec\u5c71\u6c34\u7530\u56ed\u3001\u5386\u53f2\u4f20\u8bf4\u3001\u54f2\u7406\u601d\u8003\u7b49\u591a\u4e2a\u65b9\u9762\uff0c\u5bf9\u4e2d\u56fd\u53e4\u5178\u6587\u5b66\u7684\u53d1\u5c55\u4ea7\u751f\u4e86\u6df1\u8fdc\u7684\u5f71\u54cd\u3002'"]}, "execution_count": 41, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u4e2d\u6587\n", "chain.run(\"\u4f60\u77e5\u9053\u674e\u767d\u662f\u8c01\u561b?\")"]}, {"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 \u7b49\u4e8e\u591a\u5c11'}"]}, {"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 \u7b49\u4e8e 4\u3002'"]}, "execution_count": 37, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u4e2d\u6587\n", "chain.run(\"2 + 2 \u7b49\u4e8e\u591a\u5c11\")"]}, {"cell_type": "code", "execution_count": 38, "id": "a64d0759", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "\n", "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", "None: {'input': '\u4e3a\u4ec0\u4e48\u6211\u4eec\u8eab\u4f53\u91cc\u7684\u6bcf\u4e2a\u7ec6\u80de\u90fd\u5305\u542bDNA\uff1f'}\n", "\u001b[1m> Finished chain.\u001b[0m\n"]}, {"data": {"text/plain": ["'\u6211\u4eec\u8eab\u4f53\u91cc\u7684\u6bcf\u4e2a\u7ec6\u80de\u90fd\u5305\u542bDNA\uff0c\u662f\u56e0\u4e3aDNA\u662f\u9057\u4f20\u4fe1\u606f\u7684\u8f7d\u4f53\u3002DNA\u662f\u7531\u56db\u79cd\u78b1\u57fa\uff08\u817a\u560c\u5464\u3001\u9e1f\u560c\u5464\u3001\u80f8\u817a\u5627\u5576\u548c\u9cde\u560c\u5464\uff09\u7ec4\u6210\u7684\u957f\u94fe\u72b6\u5206\u5b50\uff0c\u5b83\u5b58\u50a8\u4e86\u751f\u7269\u4f53\u7684\u9057\u4f20\u4fe1\u606f\uff0c\u5305\u62ec\u4e2a\u4f53\u7684\u7279\u5f81\u3001\u751f\u957f\u53d1\u80b2\u3001\u4ee3\u8c22\u529f\u80fd\u7b49\u3002\u6bcf\u4e2a\u7ec6\u80de\u90fd\u9700\u8981\u8fd9\u4e9b\u9057\u4f20\u4fe1\u606f\u6765\u6267\u884c\u5176\u7279\u5b9a\u7684\u529f\u80fd\u548c\u4efb\u52a1\uff0c\u56e0\u6b64\u6bcf\u4e2a\u7ec6\u80de\u90fd\u9700\u8981\u5305\u542bDNA\u3002\u6b64\u5916\uff0cDNA\u8fd8\u80fd\u901a\u8fc7\u590d\u5236\u548c\u4f20\u9012\u7ed9\u4e0b\u4e00\u4ee3\u7ec6\u80de\u548c\u4e2a\u4f53\uff0c\u4ee5\u4fdd\u8bc1\u9057\u4f20\u4fe1\u606f\u7684\u4f20\u627f\u3002'"]}, "execution_count": 38, "metadata": {}, "output_type": "execute_result"}], "source": ["# \u4e2d\u6587\n", "chain.run(\"\u4e3a\u4ec0\u4e48\u6211\u4eec\u8eab\u4f53\u91cc\u7684\u6bcf\u4e2a\u7ec6\u80de\u90fd\u5305\u542bDNA\uff1f\")"]}], "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": 5} \ No newline at end of file