langchain-practice/L3-Chains.ipynb

784 lines
19 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "52824b89-532a-4e54-87e9-1410813cd39e",
"metadata": {},
"source": [
"# Chains in LangChain\n",
"\n",
"## Outline\n",
"\n",
"* LLMChain\n",
"* Sequential Chains\n",
" * SimpleSequentialChain\n",
" * SequentialChain\n",
"* Router Chain"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "541eb2f1",
"metadata": {
"height": 47,
"tags": []
},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7ed03ed-1322-49e3-b2a2-33e94fb592ef",
"metadata": {
"height": 98,
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv()) # read local .env file"
]
},
{
"cell_type": "markdown",
"id": "9f3b61a3-92eb-4891-90ee-1d10607b05ad",
"metadata": {},
"source": [
"Note: LLM's do not always produce the same results. When executing the code in your notebook, you may get slightly different answers that those in the video."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4336d784-65c2-4a11-8489-b445b1fad177",
"metadata": {
"height": 251
},
"outputs": [],
"source": [
"# account for deprecation of LLM model\n",
"import datetime\n",
"# Get the current date\n",
"current_date = datetime.datetime.now().date()\n",
"\n",
"# Define the date after which the model should be set to \"gpt-3.5-turbo\"\n",
"target_date = datetime.date(2024, 6, 12)\n",
"\n",
"# Set the model variable based on the current date\n",
"if current_date > target_date:\n",
" llm_model = \"gpt-3.5-turbo\"\n",
"else:\n",
" llm_model = \"gpt-3.5-turbo-0301\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b84e441b",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"#!pip install pandas"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "974acf8e-8f88-42de-88f8-40a82cb58e8b",
"metadata": {
"height": 48,
"tags": []
},
"outputs": [],
"source": [
"import pandas as pd\n",
"df = pd.read_csv('Data.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7a09c35",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"id": "b940ce7c",
"metadata": {},
"source": [
"## LLMChain"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e92dff22",
"metadata": {
"height": 65,
"tags": []
},
"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": null,
"id": "943237a7",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0.9, model=llm_model)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cdcdb42d",
"metadata": {
"height": 82,
"tags": []
},
"outputs": [],
"source": [
"prompt = ChatPromptTemplate.from_template(\n",
" \"What is the best name to describe \\\n",
" a company that makes {product}?\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d7abc20b",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"chain = LLMChain(llm=llm, prompt=prompt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad44d1fb",
"metadata": {
"height": 48,
"tags": []
},
"outputs": [],
"source": [
"product = \"Queen Size Sheet Set\"\n",
"chain.run(product)"
]
},
{
"cell_type": "markdown",
"id": "69b03469",
"metadata": {},
"source": [
"## SimpleSequentialChain"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "febee243",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"from langchain.chains import SimpleSequentialChain"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f31aa8a",
"metadata": {
"height": 184,
"tags": []
},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0.9, model=llm_model)\n",
"\n",
"# prompt template 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": null,
"id": "3f5d5b76",
"metadata": {
"height": 149,
"tags": []
},
"outputs": [],
"source": [
"# prompt template 2\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": "code",
"execution_count": null,
"id": "6c1eb2c4",
"metadata": {
"height": 81,
"tags": []
},
"outputs": [],
"source": [
"overall_simple_chain = SimpleSequentialChain(chains=[chain_one, chain_two],\n",
" verbose=True\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78458efe",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"overall_simple_chain.run(product)"
]
},
{
"cell_type": "markdown",
"id": "7b5ce18c",
"metadata": {},
"source": [
"## SequentialChain"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c129ef6",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"from langchain.chains import SequentialChain"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "016187ac",
"metadata": {
"height": 234,
"tags": []
},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0.9, model=llm_model)\n",
"\n",
"# prompt template 1: translate to english\n",
"first_prompt = ChatPromptTemplate.from_template(\n",
" \"Translate the following review to english:\"\n",
" \"\\n\\n{Review}\"\n",
")\n",
"# chain 1: input= Review and output= English_Review\n",
"chain_one = LLMChain(llm=llm, prompt=first_prompt, \n",
" output_key=\"English_Review\"\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0fb0730e",
"metadata": {
"height": 183,
"tags": []
},
"outputs": [],
"source": [
"second_prompt = ChatPromptTemplate.from_template(\n",
" \"Can you summarize the following review in 1 sentence:\"\n",
" \"\\n\\n{English_Review}\"\n",
")\n",
"# chain 2: input= English_Review and output= summary\n",
"chain_two = LLMChain(llm=llm, prompt=second_prompt, \n",
" output_key=\"summary\"\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6accf92d",
"metadata": {
"height": 183,
"tags": []
},
"outputs": [],
"source": [
"# prompt template 3: translate to english\n",
"third_prompt = ChatPromptTemplate.from_template(\n",
" \"What language is the following review:\\n\\n{Review}\"\n",
")\n",
"# chain 3: input= Review and output= language\n",
"chain_three = LLMChain(llm=llm, prompt=third_prompt,\n",
" output_key=\"language\"\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7a46121",
"metadata": {
"height": 234,
"tags": []
},
"outputs": [],
"source": [
"\n",
"# prompt template 4: follow up message\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: input= summary, language and output= followup_message\n",
"chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n",
" output_key=\"followup_message\"\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89603117",
"metadata": {
"height": 166,
"tags": []
},
"outputs": [],
"source": [
"# overall_chain: input= Review \n",
"# and output= English_Review,summary, followup_message\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": "code",
"execution_count": null,
"id": "51b04f45",
"metadata": {
"height": 48,
"tags": []
},
"outputs": [],
"source": [
"review = df.Review[5]\n",
"overall_chain(review)"
]
},
{
"cell_type": "markdown",
"id": "3041ea4c",
"metadata": {},
"source": [
"## Router Chain"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ade83f4f",
"metadata": {
"height": 795,
"tags": []
},
"outputs": [],
"source": [
"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",
"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",
"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",
"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": null,
"id": "5f590e9f",
"metadata": {
"height": 404,
"tags": []
},
"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": null,
"id": "31b06fc8",
"metadata": {
"height": 81,
"tags": []
},
"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": null,
"id": "f3f50bcc",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0, model=llm_model)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8eefec24",
"metadata": {
"height": 217,
"tags": []
},
"outputs": [],
"source": [
"\n",
"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": "9f98018a",
"metadata": {
"height": 64,
"tags": []
},
"outputs": [],
"source": [
"default_prompt = ChatPromptTemplate.from_template(\"{input}\")\n",
"default_chain = LLMChain(llm=llm, prompt=default_prompt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11b2e2ba",
"metadata": {
"height": 523,
"tags": []
},
"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)>>\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1387109d",
"metadata": {
"height": 200,
"tags": []
},
"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": "code",
"execution_count": null,
"id": "2fb7d560",
"metadata": {
"height": 98,
"tags": []
},
"outputs": [],
"source": [
"chain = MultiPromptChain(router_chain=router_chain, \n",
" destination_chains=destination_chains, \n",
" default_chain=default_chain, verbose=True\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d86b2131",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"What is black body radiation?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b717379",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"what is 2 + 2\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29e5be01",
"metadata": {
"height": 47,
"tags": []
},
"outputs": [],
"source": [
"chain.run(\"Why does every cell in our body contain DNA?\")"
]
},
{
"cell_type": "markdown",
"id": "069f0121-cf7b-464d-bb3d-6357719188ed",
"metadata": {},
"source": [
"Reminder: Download your notebook to you local computer to save your work."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "912633a1",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6378a95",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0cd9456d",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "77c46ddf",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}