langchain/cookbook/smart_llm.ipynb

282 lines
11 KiB
Plaintext
Raw Normal View History

Added SmartGPT workflow (issue #4463) (#4816) # Added SmartGPT workflow by providing SmartLLM wrapper around LLMs Edit: As @hwchase17 suggested, this should be a chain, not an LLM. I have adapted the PR. It is used like this: ``` from langchain.prompts import PromptTemplate from langchain.chains import SmartLLMChain from langchain.chat_models import ChatOpenAI hard_question = "I have a 12 liter jug and a 6 liter jug. I want to measure 6 liters. How do I do it?" hard_question_prompt = PromptTemplate.from_template(hard_question) llm = ChatOpenAI(model_name="gpt-4") prompt = PromptTemplate.from_template(hard_question) chain = SmartLLMChain(llm=llm, prompt=prompt, verbose=True) chain.run({}) ``` Original text: Added SmartLLM wrapper around LLMs to allow for SmartGPT workflow (as in https://youtu.be/wVzuvf9D9BU). SmartLLM can be used wherever LLM can be used. E.g: ``` smart_llm = SmartLLM(llm=OpenAI()) smart_llm("What would be a good company name for a company that makes colorful socks?") ``` or ``` smart_llm = SmartLLM(llm=OpenAI()) prompt = PromptTemplate( input_variables=["product"], template="What is a good name for a company that makes {product}?", ) chain = LLMChain(llm=smart_llm, prompt=prompt) chain.run("colorful socks") ``` SmartGPT consists of 3 steps: 1. Ideate - generate n possible solutions ("ideas") to user prompt 2. Critique - find flaws in every idea & select best one 3. Resolve - improve upon best idea & return it Fixes #4463 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: - @hwchase17 - @agola11 Twitter: [@UmerHAdil](https://twitter.com/@UmerHAdil) | Discord: RicChilligerDude#7589 --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-08-11 22:44:27 +00:00
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "9e9b7651",
"metadata": {},
"source": [
"# How to use a SmartLLMChain\n",
"\n",
"A SmartLLMChain is a form of self-critique chain that can help you if have particularly complex questions to answer. Instead of doing a single LLM pass, it instead performs these 3 steps:\n",
"1. Ideation: Pass the user prompt n times through the LLM to get n output proposals (called \"ideas\"), where n is a parameter you can set \n",
"2. Critique: The LLM critiques all ideas to find possible flaws and picks the best one \n",
"3. Resolve: The LLM tries to improve upon the best idea (as chosen in the critique step) and outputs it. This is then the final output.\n",
"\n",
"SmartLLMChains are based on the SmartGPT workflow proposed in https://youtu.be/wVzuvf9D9BU.\n",
"\n",
"Note that SmartLLMChains\n",
"- use more LLM passes (ie n+2 instead of just 1)\n",
2023-10-12 15:44:03 +00:00
"- only work then the underlying LLM has the capability for reflection, which smaller models often don't\n",
Added SmartGPT workflow (issue #4463) (#4816) # Added SmartGPT workflow by providing SmartLLM wrapper around LLMs Edit: As @hwchase17 suggested, this should be a chain, not an LLM. I have adapted the PR. It is used like this: ``` from langchain.prompts import PromptTemplate from langchain.chains import SmartLLMChain from langchain.chat_models import ChatOpenAI hard_question = "I have a 12 liter jug and a 6 liter jug. I want to measure 6 liters. How do I do it?" hard_question_prompt = PromptTemplate.from_template(hard_question) llm = ChatOpenAI(model_name="gpt-4") prompt = PromptTemplate.from_template(hard_question) chain = SmartLLMChain(llm=llm, prompt=prompt, verbose=True) chain.run({}) ``` Original text: Added SmartLLM wrapper around LLMs to allow for SmartGPT workflow (as in https://youtu.be/wVzuvf9D9BU). SmartLLM can be used wherever LLM can be used. E.g: ``` smart_llm = SmartLLM(llm=OpenAI()) smart_llm("What would be a good company name for a company that makes colorful socks?") ``` or ``` smart_llm = SmartLLM(llm=OpenAI()) prompt = PromptTemplate( input_variables=["product"], template="What is a good name for a company that makes {product}?", ) chain = LLMChain(llm=smart_llm, prompt=prompt) chain.run("colorful socks") ``` SmartGPT consists of 3 steps: 1. Ideate - generate n possible solutions ("ideas") to user prompt 2. Critique - find flaws in every idea & select best one 3. Resolve - improve upon best idea & return it Fixes #4463 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: - @hwchase17 - @agola11 Twitter: [@UmerHAdil](https://twitter.com/@UmerHAdil) | Discord: RicChilligerDude#7589 --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-08-11 22:44:27 +00:00
"- only work with underlying models that return exactly 1 output, not multiple\n",
"\n",
"This notebook demonstrates how to use a SmartLLMChain."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "714dede0",
"metadata": {},
"source": [
"##### Same LLM for all steps"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d3f7fb22",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"...\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "10e5ece6",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chat_models import ChatOpenAI\n",
2023-11-14 22:17:44 +00:00
"from langchain.prompts import PromptTemplate\n",
Added SmartGPT workflow (issue #4463) (#4816) # Added SmartGPT workflow by providing SmartLLM wrapper around LLMs Edit: As @hwchase17 suggested, this should be a chain, not an LLM. I have adapted the PR. It is used like this: ``` from langchain.prompts import PromptTemplate from langchain.chains import SmartLLMChain from langchain.chat_models import ChatOpenAI hard_question = "I have a 12 liter jug and a 6 liter jug. I want to measure 6 liters. How do I do it?" hard_question_prompt = PromptTemplate.from_template(hard_question) llm = ChatOpenAI(model_name="gpt-4") prompt = PromptTemplate.from_template(hard_question) chain = SmartLLMChain(llm=llm, prompt=prompt, verbose=True) chain.run({}) ``` Original text: Added SmartLLM wrapper around LLMs to allow for SmartGPT workflow (as in https://youtu.be/wVzuvf9D9BU). SmartLLM can be used wherever LLM can be used. E.g: ``` smart_llm = SmartLLM(llm=OpenAI()) smart_llm("What would be a good company name for a company that makes colorful socks?") ``` or ``` smart_llm = SmartLLM(llm=OpenAI()) prompt = PromptTemplate( input_variables=["product"], template="What is a good name for a company that makes {product}?", ) chain = LLMChain(llm=smart_llm, prompt=prompt) chain.run("colorful socks") ``` SmartGPT consists of 3 steps: 1. Ideate - generate n possible solutions ("ideas") to user prompt 2. Critique - find flaws in every idea & select best one 3. Resolve - improve upon best idea & return it Fixes #4463 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: - @hwchase17 - @agola11 Twitter: [@UmerHAdil](https://twitter.com/@UmerHAdil) | Discord: RicChilligerDude#7589 --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-08-11 22:44:27 +00:00
"from langchain_experimental.smart_llm import SmartLLMChain"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "1780da51",
"metadata": {},
"source": [
"As example question, we will use \"I have a 12 liter jug and a 6 liter jug. I want to measure 6 liters. How do I do it?\". This is an example from the original SmartGPT video (https://youtu.be/wVzuvf9D9BU?t=384). While this seems like a very easy question, LLMs struggle do these kinds of questions that involve numbers and physical reasoning.\n",
"\n",
"As we will see, all 3 initial ideas are completely wrong - even though we're using GPT4! Only when using self-reflection do we get a correct answer. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "054af6b1",
"metadata": {},
"outputs": [],
"source": [
"hard_question = \"I have a 12 liter jug and a 6 liter jug. I want to measure 6 liters. How do I do it?\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "8049cecd",
"metadata": {},
"source": [
"So, we first create an LLM and prompt template"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "811ea8e1",
"metadata": {},
"outputs": [],
"source": [
"prompt = PromptTemplate.from_template(hard_question)\n",
"llm = ChatOpenAI(temperature=0, model_name=\"gpt-4\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "50b602e4",
"metadata": {},
"source": [
"Now we can create a SmartLLMChain"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8cd49199",
"metadata": {},
"outputs": [],
"source": [
"chain = SmartLLMChain(llm=llm, prompt=prompt, n_ideas=3, verbose=True)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "6a72f276",
"metadata": {},
"source": [
"Now we can use the SmartLLM as a drop-in replacement for our LLM. E.g.:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "074e5e75",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new SmartLLMChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mI have a 12 liter jug and a 6 liter jug. I want to measure 6 liters. How do I do it?\u001b[0m\n",
"Idea 1:\n",
"\u001b[36;1m\u001b[1;3m1. Fill the 6-liter jug completely.\n",
"2. Pour the water from the 6-liter jug into the 12-liter jug.\n",
"3. Fill the 6-liter jug again.\n",
"4. Carefully pour the water from the 6-liter jug into the 12-liter jug until the 12-liter jug is full.\n",
"5. The amount of water left in the 6-liter jug will be exactly 6 liters.\u001b[0m\n",
"Idea 2:\n",
"\u001b[36;1m\u001b[1;3m1. Fill the 6-liter jug completely.\n",
"2. Pour the water from the 6-liter jug into the 12-liter jug.\n",
"3. Fill the 6-liter jug again.\n",
"4. Carefully pour the water from the 6-liter jug into the 12-liter jug until the 12-liter jug is full.\n",
"5. Since the 12-liter jug is now full, there will be 2 liters of water left in the 6-liter jug.\n",
"6. Empty the 12-liter jug.\n",
"7. Pour the 2 liters of water from the 6-liter jug into the 12-liter jug.\n",
"8. Fill the 6-liter jug completely again.\n",
"9. Pour the water from the 6-liter jug into the 12-liter jug, which already has 2 liters in it.\n",
"10. Now, the 12-liter jug will have exactly 6 liters of water (2 liters from before + 4 liters from the 6-liter jug).\u001b[0m\n",
"Idea 3:\n",
"\u001b[36;1m\u001b[1;3m1. Fill the 6-liter jug completely.\n",
"2. Pour the water from the 6-liter jug into the 12-liter jug.\n",
"3. Fill the 6-liter jug again.\n",
"4. Carefully pour the water from the 6-liter jug into the 12-liter jug until the 12-liter jug is full.\n",
"5. The amount of water left in the 6-liter jug will be exactly 6 liters.\u001b[0m\n",
"Critique:\n",
"\u001b[33;1m\u001b[1;3mIdea 1:\n",
"1. Fill the 6-liter jug completely. (No flaw)\n",
"2. Pour the water from the 6-liter jug into the 12-liter jug. (No flaw)\n",
"3. Fill the 6-liter jug again. (No flaw)\n",
"4. Carefully pour the water from the 6-liter jug into the 12-liter jug until the 12-liter jug is full. (Flaw: The 12-liter jug will never be full in this step, as it can hold 12 liters and we are only pouring 6 liters into it.)\n",
"5. The amount of water left in the 6-liter jug will be exactly 6 liters. (Flaw: This statement is incorrect, as there will be no water left in the 6-liter jug after pouring it into the 12-liter jug.)\n",
"\n",
"Idea 2:\n",
"1. Fill the 6-liter jug completely. (No flaw)\n",
"2. Pour the water from the 6-liter jug into the 12-liter jug. (No flaw)\n",
"3. Fill the 6-liter jug again. (No flaw)\n",
"4. Carefully pour the water from the 6-liter jug into the 12-liter jug until the 12-liter jug is full. (Flaw: The 12-liter jug will never be full in this step, as it can hold 12 liters and we are only pouring 6 liters into it.)\n",
"5. Since the 12-liter jug is now full, there will be 2 liters of water left in the 6-liter jug. (Flaw: This statement is incorrect, as the 12-liter jug will not be full and there will be no water left in the 6-liter jug.)\n",
"6. Empty the 12-liter jug. (No flaw)\n",
"7. Pour the 2 liters of water from the 6-liter jug into the 12-liter jug. (Flaw: This step is based on the incorrect assumption that there are 2 liters of water left in the 6-liter jug.)\n",
"8. Fill the 6-liter jug completely again. (No flaw)\n",
"9. Pour the water from the 6-liter jug into the 12-liter jug, which already has 2 liters in it. (Flaw: This step is based on the incorrect assumption that there are 2 liters of water in the 12-liter jug.)\n",
"10. Now, the 12-liter jug will have exactly 6 liters of water (2 liters from before + 4 liters from the 6-liter jug). (Flaw: This conclusion is based on the incorrect assumptions made in the previous steps.)\n",
"\n",
"Idea 3:\n",
"1. Fill the 6-liter jug completely. (No flaw)\n",
"2. Pour the water from the 6-liter jug into the 12-liter jug. (No flaw)\n",
"3. Fill the 6-liter jug again. (No flaw)\n",
"4. Carefully pour the water from the 6-liter jug into the 12-liter jug until the 12-liter jug is full. (Flaw: The 12-liter jug will never be full in this step, as it can hold 12 liters and we are only pouring 6 liters into it.)\n",
"5. The amount of water left in the 6-liter jug will be exactly 6 liters. (Flaw: This statement is incorrect, as there will be no water left in the 6-liter jug after pouring it into the 12-liter jug.)\u001b[0m\n",
"Resolution:\n",
"\u001b[32;1m\u001b[1;3m1. Fill the 12-liter jug completely.\n",
"2. Pour the water from the 12-liter jug into the 6-liter jug until the 6-liter jug is full.\n",
"3. The amount of water left in the 12-liter jug will be exactly 6 liters.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'1. Fill the 12-liter jug completely.\\n2. Pour the water from the 12-liter jug into the 6-liter jug until the 6-liter jug is full.\\n3. The amount of water left in the 12-liter jug will be exactly 6 liters.'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chain.run({})"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "bbfebea1",
"metadata": {},
"source": [
"##### Different LLM for different steps"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "5be6ec08",
"metadata": {},
"source": [
"You can also use different LLMs for the different steps by passing `ideation_llm`, `critique_llm` and `resolve_llm`. You might want to do this to use a more creative (i.e., high-temperature) model for ideation and a more strict (i.e., low-temperature) model for critique and resolution."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "9c33fa19",
"metadata": {},
"outputs": [],
"source": [
"chain = SmartLLMChain(\n",
" ideation_llm=ChatOpenAI(temperature=0.9, model_name=\"gpt-4\"),\n",
" llm=ChatOpenAI(\n",
" temperature=0, model_name=\"gpt-4\"\n",
2023-10-12 15:44:03 +00:00
" ), # will be used for critique and resolution as no specific llms are given\n",
Added SmartGPT workflow (issue #4463) (#4816) # Added SmartGPT workflow by providing SmartLLM wrapper around LLMs Edit: As @hwchase17 suggested, this should be a chain, not an LLM. I have adapted the PR. It is used like this: ``` from langchain.prompts import PromptTemplate from langchain.chains import SmartLLMChain from langchain.chat_models import ChatOpenAI hard_question = "I have a 12 liter jug and a 6 liter jug. I want to measure 6 liters. How do I do it?" hard_question_prompt = PromptTemplate.from_template(hard_question) llm = ChatOpenAI(model_name="gpt-4") prompt = PromptTemplate.from_template(hard_question) chain = SmartLLMChain(llm=llm, prompt=prompt, verbose=True) chain.run({}) ``` Original text: Added SmartLLM wrapper around LLMs to allow for SmartGPT workflow (as in https://youtu.be/wVzuvf9D9BU). SmartLLM can be used wherever LLM can be used. E.g: ``` smart_llm = SmartLLM(llm=OpenAI()) smart_llm("What would be a good company name for a company that makes colorful socks?") ``` or ``` smart_llm = SmartLLM(llm=OpenAI()) prompt = PromptTemplate( input_variables=["product"], template="What is a good name for a company that makes {product}?", ) chain = LLMChain(llm=smart_llm, prompt=prompt) chain.run("colorful socks") ``` SmartGPT consists of 3 steps: 1. Ideate - generate n possible solutions ("ideas") to user prompt 2. Critique - find flaws in every idea & select best one 3. Resolve - improve upon best idea & return it Fixes #4463 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: - @hwchase17 - @agola11 Twitter: [@UmerHAdil](https://twitter.com/@UmerHAdil) | Discord: RicChilligerDude#7589 --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-08-11 22:44:27 +00:00
" prompt=prompt,\n",
" n_ideas=3,\n",
" verbose=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "886c1cc1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}