Files
prompt-engineering-for-deve…/content/LangChain for LLM Application Development/4.模型链.ipynb
xuhu0115 4bd4ca1a6b Add files via upload
对<2.模型、提示和解析器>、<4.模型链>部分补充中文prompt,部分内容修改
2023-07-06 19:17:25 +08:00

1713 lines
56 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "52824b89-532a-4e54-87e9-1410813cd39e",
"metadata": {},
"source": [
"# Chains in LangChainLangChain中的链\n",
"\n",
"## Outline\n",
"\n",
"* LLMChain大语言模型链\n",
"* Sequential Chains顺序链\n",
" * SimpleSequentialChain\n",
" * SequentialChain\n",
"* Router Chain路由链"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "54810ef7",
"metadata": {},
"source": [
"### 为什么我们需要Chains \n",
"链允许我们将多个组件组合在一起以创建一个单一的、连贯的应用程序。链Chains通常将一个LLM大语言模型与提示结合在一起使用这个构建块您还可以将一堆这些构建块组合在一起对您的文本或其他数据进行一系列操作。例如我们可以创建一个链该链接受用户输入使用提示模板对其进行格式化然后将格式化的响应传递给LLM。我们可以通过将多个链组合在一起或者通过将链与其他组件组合在一起来构建更复杂的链。"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "541eb2f1",
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "b7ed03ed-1322-49e3-b2a2-33e94fb592ef",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"import openai\n",
"from dotenv import load_dotenv, find_dotenv\n",
"\n",
"_ = load_dotenv(find_dotenv()) # 读取本地 .env 文件\n",
"\n",
"# 获取环境变量 OPENAI_API_KEY\n",
"openai.api_key = os.environ['OPENAI_API_KEY'] #\"填入你的专属的API key\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b84e441b",
"metadata": {},
"outputs": [],
"source": [
"#!pip install pandas"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "663fc885",
"metadata": {},
"source": [
"这些链的一部分的强大之处在于你可以一次运行它们在许多输入上因此我们将加载一个pandas数据框架"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "974acf8e-8f88-42de-88f8-40a82cb58e8b",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"df = pd.read_csv('Data.csv')"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "b7a09c35",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Product</th>\n",
" <th>Review</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Queen Size Sheet Set</td>\n",
" <td>I ordered a king size set. My only criticism w...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Waterproof Phone Pouch</td>\n",
" <td>I loved the waterproof sac, although the openi...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Luxury Air Mattress</td>\n",
" <td>This mattress had a small hole in the top of i...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Pillows Insert</td>\n",
" <td>This is the best throw pillow fillers on Amazo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Milk Frother Handheld\\n</td>\n",
" <td>I loved this product. But they only seem to l...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "b940ce7c",
"metadata": {},
"source": [
"## 1. LLMChain"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e000bd16",
"metadata": {},
"source": [
"LLMChain是一个简单但非常强大的链也是后面我们将要介绍的许多链的基础。"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "e92dff22",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chat_models import ChatOpenAI #导入OpenAI模型\n",
"from langchain.prompts import ChatPromptTemplate #导入聊天提示模板\n",
"from langchain.chains import LLMChain #导入LLM链。"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "94a32c6f",
"metadata": {},
"source": [
"初始化语言模型"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "943237a7",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0.0) #预测下一个token时概率越大的值就越平滑(平滑也就是让差异大的值之间的差异变得没那么大)temperature值越小则生成的内容越稳定"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "81887434",
"metadata": {},
"source": [
"初始化prompt这个prompt将接受一个名为product的变量。该prompt将要求LLM生成一个描述制造该产品的公司的最佳名称"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "cdcdb42d",
"metadata": {},
"outputs": [],
"source": [
"prompt = ChatPromptTemplate.from_template( \n",
" \"What is the best name to describe \\\n",
" a company that makes {product}?\"\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "5c22cb13",
"metadata": {},
"source": [
"将llm和prompt组合成链---这个LLM链非常简单他只是llm和prompt的结合但是现在这个链让我们可以以一种顺序的方式去通过prompt运行并且结合到LLM中"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d7abc20b",
"metadata": {},
"outputs": [],
"source": [
"chain = LLMChain(llm=llm, prompt=prompt)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "8d7d5ff6",
"metadata": {},
"source": [
"因此,如果我们有一个名为\"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)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "1e1ede1c",
"metadata": {},
"source": [
"您可以输入任何产品描述,然后查看链将输出什么结果"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "2181be10",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\"豪华床纺\"'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 中文\n",
"prompt = ChatPromptTemplate.from_template( \n",
" \"描述制造{product}的一个公司的最佳名称是什么?\"\n",
")\n",
"chain = LLMChain(llm=llm, prompt=prompt)\n",
"product = \"大号床单套装\"\n",
"chain.run(product)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "49158430",
"metadata": {},
"source": [
"## 2. Sequential Chains"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "69b03469",
"metadata": {},
"source": [
"### 2.1 SimpleSequentialChain\n",
"\n",
"顺序链Sequential Chains是按预定义顺序执行其链接的链。具体来说我们将使用简单顺序链SimpleSequentialChain这是顺序链的最简单类型其中每个步骤都有一个输入/输出,一个步骤的输出是下一个步骤的输入"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "febee243",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import SimpleSequentialChain"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "5d019d6f",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0.9)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "0e732589",
"metadata": {},
"source": [
"子链 1"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "2f31aa8a",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 提示模板 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)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "dcfca7bd",
"metadata": {},
"source": [
"子链 2"
]
},
{
"cell_type": "code",
"execution_count": 16,
"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)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "3a1991f4",
"metadata": {},
"source": [
"现在我们可以组合两个LLMChain以便我们可以在一个步骤中创建公司名称和描述"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "6c1eb2c4",
"metadata": {},
"outputs": [],
"source": [
"overall_simple_chain = SimpleSequentialChain(chains=[chain_one, chain_two],\n",
" verbose=True\n",
" )"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "5122f26a",
"metadata": {},
"source": [
"给一个输入,然后运行上面的链"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "78458efe",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n",
"\u001b[36;1m\u001b[1;3mRoyal Linens\u001b[0m\n",
"\u001b[33;1m\u001b[1;3mRoyal Linens is a high-quality bedding and linen company that offers luxurious and stylish products for comfortable living.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'Royal Linens is a high-quality bedding and linen company that offers luxurious and stylish products for comfortable living.'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"product = \"Queen Size Sheet Set\"\n",
"overall_simple_chain.run(product)"
]
},
{
"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],\n",
" verbose=True\n",
" )\n",
"product = \"大号床单套装\"\n",
"overall_simple_chain.run(product)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7b5ce18c",
"metadata": {},
"source": [
"### 2.2 SequentialChain"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "1e69f4c0",
"metadata": {},
"source": [
"当只有一个输入和一个输出时,简单的顺序链可以顺利完成。但是当有多个输入或多个输出时该如何实现呢?\n",
"\n",
"我们可以使用普通的顺序链来实现这一点"
]
},
{
"cell_type": "code",
"execution_count": 1,
"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链。"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "3d4be4e8",
"metadata": {},
"source": [
"初始化语言模型"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "03a8e203",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0.9)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "9811445c",
"metadata": {},
"source": [
"接下来我们将创建一系列的链,然后一个接一个使用他们"
]
},
{
"cell_type": "code",
"execution_count": 23,
"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, \n",
" output_key=\"English_Review\"\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"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, \n",
" output_key=\"summary\"\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"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,\n",
" output_key=\"language\"\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"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,\n",
" output_key=\"followup_message\"\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "89603117",
"metadata": {},
"outputs": [],
"source": [
"# 对四个子链进行组合\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",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "0509de01",
"metadata": {},
"source": [
"让我们选择一篇评论并通过整个链传递它可以发现原始review是法语可以把英文review看做是一种翻译接下来是根据英文review得到的总结最后输出的是用法语原文进行的续写信息。"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "51b04f45",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n",
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n",
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n",
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'Review': \"Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !?\",\n",
" 'English_Review': '\"I find the taste mediocre. The foam doesn\\'t hold, it\\'s weird. I buy the same ones in stores and the taste is much better... Old batch or counterfeit!?\"',\n",
" 'summary': \"The taste is mediocre, the foam doesn't hold and the reviewer suspects it's either an old batch or counterfeit.\",\n",
" 'followup_message': \"Réponse : La saveur est moyenne, la mousse ne tient pas et le critique soupçonne qu'il s'agit soit d'un lot périmé, soit d'une contrefaçon. Il est important de prendre en compte les commentaires des clients pour améliorer notre produit. Nous allons enquêter sur cette question plus en détail pour nous assurer que nos produits sont de la plus haute qualité possible. Nous espérons que vous nous donnerez une autre chance à l'avenir. Merci d'avoir pris le temps de nous donner votre avis sincère.\"}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"review = df.Review[5]\n",
"overall_chain(review)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "31624a7c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 1.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n",
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n",
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n",
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-kmMHlitaQynVMwTW3jDTCPga on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'Review': \"Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur...\\nVieux lot ou contrefaçon !?\",\n",
" 'English_Review': \"I find the taste mediocre. The foam doesn't last, it's strange. I buy the same ones in stores and the taste is much better...\\nOld batch or counterfeit?!\",\n",
" 'summary': \"The reviewer finds the taste mediocre and suspects that either the product is from an old batch or it might be counterfeit, as the foam doesn't last and the taste is not as good as the ones purchased in stores.\",\n",
" 'followup_message': \"后续回复: Bonjour! Je vous remercie de votre commentaire concernant notre produit. Nous sommes désolés d'apprendre que vous avez trouvé le goût moyen et que vous soupçonnez qu'il s'agit peut-être d'un ancien lot ou d'une contrefaçon, car la mousse ne dure pas et le goût n'est pas aussi bon que ceux achetés en magasin. Nous apprécions vos préoccupations et nous aimerions enquêter davantage sur cette situation. Pourriez-vous s'il vous plaît nous fournir plus de détails, tels que la date d'achat et le numéro de lot du produit? Nous sommes déterminés à offrir la meilleure qualité à nos clients et nous ferons tout notre possible pour résoudre ce problème. Merci encore pour votre commentaire et nous attendons votre réponse avec impatience. Cordialement.\"}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 中文\n",
"\n",
"#子链1\n",
"\n",
"# prompt模板 1: 翻译成英语把下面的review翻译成英语\n",
"first_prompt = ChatPromptTemplate.from_template(\n",
" \"把下面的评论review翻译成英文:\"\n",
" \"\\n\\n{Review}\"\n",
")\n",
"# chain 1: 输入Review 输出:英文的 Review\n",
"chain_one = LLMChain(llm=llm, prompt=first_prompt, \n",
" output_key=\"English_Review\"\n",
" )\n",
"\n",
"#子链2\n",
"\n",
"# prompt模板 2: 用一句话总结下面的 review\n",
"second_prompt = ChatPromptTemplate.from_template(\n",
" \"请你用一句话来总结下面的评论review:\"\n",
" \"\\n\\n{English_Review}\"\n",
")\n",
"# chain 2: 输入英文的Review 输出:总结\n",
"chain_two = LLMChain(llm=llm, prompt=second_prompt, \n",
" output_key=\"summary\"\n",
" )\n",
"\n",
"\n",
"#子链3\n",
"\n",
"# prompt模板 3: 下面review使用的什么语言\n",
"third_prompt = ChatPromptTemplate.from_template(\n",
" \"下面的评论review使用的什么语言:\\n\\n{Review}\"\n",
")\n",
"# chain 3: 输入Review 输出:语言\n",
"chain_three = LLMChain(llm=llm, prompt=third_prompt,\n",
" output_key=\"language\"\n",
" )\n",
"\n",
"\n",
"#子链4\n",
"\n",
"# prompt模板 4: 使用特定的语言对下面的总结写一个后续回复\n",
"fourth_prompt = ChatPromptTemplate.from_template(\n",
" \"使用特定的语言对下面的总结写一个后续回复:\"\n",
" \"\\n\\n总结: {summary}\\n\\n语言: {language}\"\n",
")\n",
"# chain 4: 输入: 总结, 语言 输出: 后续回复\n",
"chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n",
" output_key=\"followup_message\"\n",
" )\n",
"\n",
"\n",
"# 对四个子链进行组合\n",
"\n",
"#输入review 输出英文review总结后续回复 \n",
"overall_chain = SequentialChain(\n",
" chains=[chain_one, chain_two, chain_three, chain_four],\n",
" input_variables=[\"Review\"],\n",
" output_variables=[\"English_Review\", \"summary\",\"followup_message\"],\n",
" verbose=True\n",
")\n",
"\n",
"\n",
"review = df.Review[5]\n",
"overall_chain(review)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "3041ea4c",
"metadata": {},
"source": [
"## 3. Router Chain路由链"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "f0c32f97",
"metadata": {},
"source": [
"到目前为止我们已经学习了LLM链和顺序链。但是如果您想做一些更复杂的事情怎么办\n",
"\n",
"一个相当常见但基本的操作是根据输入将其路由到一条链,具体取决于该输入到底是什么。如果你有多个子链,每个子链都专门用于特定类型的输入,那么可以组成一个路由链,它首先决定将它传递给哪个子链,然后将它传递给那个链。\n",
"\n",
"路由器由两个组件组成:\n",
"\n",
"- 路由器链本身(负责选择要调用的下一个链)\n",
"- destination_chains路由器链可以路由到的链\n",
"\n",
"举一个具体的例子让我们看一下我们在不同类型的链之间路由的地方我们在这里有不同的prompt: "
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "cb1b4708",
"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": "code",
"execution_count": 51,
"id": "f7fade7a",
"metadata": {},
"outputs": [],
"source": [
"# 中文\n",
"#第一个提示适合回答物理问题\n",
"physics_template = \"\"\"你是一个非常聪明的物理专家。 \\\n",
"你擅长用一种简洁并且易于理解的方式去回答问题。\\\n",
"当你不知道问题的答案时,你承认\\\n",
"你不知道.\n",
"\n",
"这是一个问题:\n",
"{input}\"\"\"\n",
"\n",
"\n",
"#第二个提示适合回答数学问题\n",
"math_template = \"\"\"你是一个非常优秀的数学家。 \\\n",
"你擅长回答数学问题。 \\\n",
"你之所以如此优秀, \\\n",
"是因为你能够将棘手的问题分解为组成部分,\\\n",
"回答组成部分,然后将它们组合在一起,回答更广泛的问题。\n",
"\n",
"这是一个问题:\n",
"{input}\"\"\"\n",
"\n",
"\n",
"#第三个适合回答历史问题\n",
"history_template = \"\"\"你是以为非常优秀的历史学家。 \\\n",
"你对一系列历史时期的人物、事件和背景有着极好的学识和理解\\\n",
"你有能力思考、反思、辩证、讨论和评估过去。\\\n",
"你尊重历史证据,并有能力利用它来支持你的解释和判断。\n",
"\n",
"这是一个问题:\n",
"{input}\"\"\"\n",
"\n",
"\n",
"#第四个适合回答计算机问题\n",
"computerscience_template = \"\"\" 你是一个成功的计算机科学专家。\\\n",
"你有创造力、协作精神、\\\n",
"前瞻性思维、自信、解决问题的能力、\\\n",
"对理论和算法的理解以及出色的沟通技巧。\\\n",
"你非常擅长回答编程问题。\\\n",
"你之所以如此优秀,是因为你知道 \\\n",
"如何通过以机器可以轻松解释的命令式步骤描述解决方案来解决问题,\\\n",
"并且你知道如何选择在时间复杂性和空间复杂性之间取得良好平衡的解决方案。\n",
"\n",
"这还是一个输入:\n",
"{input}\"\"\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "6922b35e",
"metadata": {},
"source": [
"首先需要定义这些提示模板,在我们拥有了这些提示模板后,可以为每个模板命名,然后提供描述。例如,第一个物理学的描述适合回答关于物理学的问题,这些信息将传递给路由链,然后由路由链决定何时使用此子链。"
]
},
{
"cell_type": "code",
"execution_count": 27,
"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": "code",
"execution_count": 52,
"id": "deb8aafc",
"metadata": {},
"outputs": [],
"source": [
"# 中文\n",
"prompt_infos = [\n",
" {\n",
" \"名字\": \"物理学\", \n",
" \"描述\": \"擅长回答关于物理学的问题\", \n",
" \"提示模板\": physics_template\n",
" },\n",
" {\n",
" \"名字\": \"数学\", \n",
" \"描述\": \"擅长回答数学问题\", \n",
" \"提示模板\": math_template\n",
" },\n",
" {\n",
" \"名字\": \"历史\", \n",
" \"描述\": \"擅长回答历史问题\", \n",
" \"提示模板\": history_template\n",
" },\n",
" {\n",
" \"名字\": \"计算机科学\", \n",
" \"描述\": \"擅长回答计算机科学问题\", \n",
" \"提示模板\": computerscience_template\n",
" }\n",
"]\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "80eb1de8",
"metadata": {},
"source": [
"### 导入相关的包"
]
},
{
"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"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "50c16f01",
"metadata": {},
"source": [
"### 定义语言模型"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "f3f50bcc",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "8795cd42",
"metadata": {},
"source": [
"### LLMRouterChain此链使用 LLM 来确定如何路由事物)\n",
"\n",
"在这里,我们需要一个**多提示链**。这是一种特定类型的链,用于在多个不同的提示模板之间进行路由。\n",
"但是,这只是你可以路由的一种类型。你也可以在任何类型的链之间进行路由。\n",
"\n",
"这里我们要实现的几个类是LLM路由器链。这个类本身使用语言模型来在不同的子链之间进行路由。\n",
"这就是上面提供的描述和名称将被使用的地方。"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "46633b43",
"metadata": {},
"source": [
"#### 创建目标链 \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": "code",
"execution_count": null,
"id": "fd6eb641",
"metadata": {},
"outputs": [],
"source": [
"# 中文\n",
"destination_chains = {}\n",
"for p_info in prompt_infos:\n",
" name = p_info[\"名字\"]\n",
" prompt_template = p_info[\"提示模板\"]\n",
" prompt = ChatPromptTemplate.from_template(template=prompt_template)\n",
" chain = LLMChain(llm=llm, prompt=prompt)\n",
" destination_chains[name] = chain \n",
" \n",
"destinations = [f\"{p['名字']}: {p['描述']}\" for p in prompt_infos]\n",
"destinations_str = \"\\n\".join(destinations)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "eba115de",
"metadata": {},
"source": [
"#### 创建默认目标链\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)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "948700c4",
"metadata": {},
"source": [
"#### 创建LLM用于在不同链之间进行路由的模板\n",
"这包括要完成的任务的说明以及输出应该采用的特定格式。"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "24f30c2c",
"metadata": {},
"source": [
"注意:此处在原教程的基础上添加了一个示例,主要是因为\"gpt-3.5-turbo\"模型不能很好适应理解模板的意思,使用 \"text-davinci-003\" 或者\"gpt-4-0613\"可以很好的工作,因此在这里多加了示例提示让其更好的学习。\n",
"eg:\n",
"<< INPUT >>\n",
"\"What is black body radiation?\"\n",
"<< OUTPUT >>\n",
"```json\n",
"{{{{\n",
" \"destination\": string \\ name of the prompt to use or \"DEFAULT\"\n",
" \"next_inputs\": string \\ a potentially modified version of the original input\n",
"}}}}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 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": "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",
"\"\"\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "de5c46d0",
"metadata": {},
"source": [
"#### 构建路由链\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)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7e92355c",
"metadata": {},
"source": [
"#### 最后,将所有内容整合在一起,创建整体链路"
]
},
{
"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",
" )"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "086503f7",
"metadata": {},
"source": [
"#### 进行提问"
]
},
{
"attachments": {},
"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": "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(\"你知道李白是谁嘛?\")"
]
},
{
"attachments": {},
"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": "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.<locals>._completion_with_retry in 1.0 seconds as it raised ServiceUnavailableError: The server is overloaded or not ready yet..\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'2 + 2 等于 4。'"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 中文\n",
"chain.run(\"2 + 2 等于多少\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"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": "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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}