forked from Archives/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
7.3 KiB
Plaintext
214 lines
7.3 KiB
Plaintext
2 years ago
|
{
|
||
|
"cells": [
|
||
2 years ago
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "f1390152",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# MRKL\n",
|
||
|
"\n",
|
||
2 years ago
|
"This notebook showcases using an agent to replicate the MRKL chain."
|
||
2 years ago
|
]
|
||
|
},
|
||
2 years ago
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "39ea3638",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"This uses the example Chinook database.\n",
|
||
|
"To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the `.db` file in a notebooks folder at the root of this repository."
|
||
|
]
|
||
|
},
|
||
2 years ago
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "ac561cc4",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
2 years ago
|
"from langchain import LLMMathChain, OpenAI, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain\n",
|
||
2 years ago
|
"from langchain.agents import initialize_agent, Tool"
|
||
2 years ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
1 year ago
|
"execution_count": 2,
|
||
2 years ago
|
"id": "07e96d99",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"llm = OpenAI(temperature=0)\n",
|
||
2 years ago
|
"search = SerpAPIWrapper()\n",
|
||
2 years ago
|
"llm_math_chain = LLMMathChain(llm=llm, verbose=True)\n",
|
||
1 year ago
|
"db = SQLDatabase.from_uri(\"sqlite:///../../../../notebooks/Chinook.db\")\n",
|
||
2 years ago
|
"db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)\n",
|
||
2 years ago
|
"tools = [\n",
|
||
|
" Tool(\n",
|
||
|
" name = \"Search\",\n",
|
||
|
" func=search.run,\n",
|
||
1 year ago
|
" description=\"useful for when you need to answer questions about current events. You should ask targeted questions\"\n",
|
||
2 years ago
|
" ),\n",
|
||
2 years ago
|
" Tool(\n",
|
||
|
" name=\"Calculator\",\n",
|
||
|
" func=llm_math_chain.run,\n",
|
||
|
" description=\"useful for when you need to answer questions about math\"\n",
|
||
2 years ago
|
" ),\n",
|
||
2 years ago
|
" Tool(\n",
|
||
|
" name=\"FooBar DB\",\n",
|
||
|
" func=db_chain.run,\n",
|
||
1 year ago
|
" description=\"useful for when you need to answer questions about FooBar. Input should be in the form of a question containing full context\"\n",
|
||
2 years ago
|
" )\n",
|
||
|
"]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
1 year ago
|
"execution_count": 3,
|
||
2 years ago
|
"id": "a069c4b6",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
2 years ago
|
"mrkl = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
||
2 years ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
1 year ago
|
"execution_count": 4,
|
||
2 years ago
|
"id": "e603cd7d",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
1 year ago
|
"\n",
|
||
|
"\n",
|
||
1 year ago
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
1 year ago
|
"\u001b[32;1m\u001b[1;3m I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.\n",
|
||
2 years ago
|
"Action: Search\n",
|
||
1 year ago
|
"Action Input: \"Who is Leo DiCaprio's girlfriend?\"\u001b[0m\n",
|
||
|
"Observation: \u001b[36;1m\u001b[1;3mCamila Morrone\u001b[0m\n",
|
||
|
"Thought:\u001b[32;1m\u001b[1;3m I need to find out Camila Morrone's age\n",
|
||
2 years ago
|
"Action: Search\n",
|
||
1 year ago
|
"Action Input: \"How old is Camila Morrone?\"\u001b[0m\n",
|
||
|
"Observation: \u001b[36;1m\u001b[1;3m25 years\u001b[0m\n",
|
||
|
"Thought:\u001b[32;1m\u001b[1;3m I need to calculate 25 raised to the 0.43 power\n",
|
||
2 years ago
|
"Action: Calculator\n",
|
||
1 year ago
|
"Action Input: 25^0.43\u001b[0m\n",
|
||
2 years ago
|
"\n",
|
||
1 year ago
|
"\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n",
|
||
1 year ago
|
"25^0.43\u001b[32;1m\u001b[1;3m\n",
|
||
2 years ago
|
"```python\n",
|
||
1 year ago
|
"import math\n",
|
||
1 year ago
|
"print(math.pow(25, 0.43))\n",
|
||
2 years ago
|
"```\n",
|
||
|
"\u001b[0m\n",
|
||
1 year ago
|
"Answer: \u001b[33;1m\u001b[1;3m3.991298452658078\n",
|
||
2 years ago
|
"\u001b[0m\n",
|
||
1 year ago
|
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||
2 years ago
|
"\n",
|
||
1 year ago
|
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 3.991298452658078\n",
|
||
2 years ago
|
"\u001b[0m\n",
|
||
1 year ago
|
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
||
1 year ago
|
"Final Answer: Camila Morrone is 25 years old and her age raised to the 0.43 power is 3.991298452658078.\u001b[0m\n",
|
||
|
"\n",
|
||
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
2 years ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
1 year ago
|
"'Camila Morrone is 25 years old and her age raised to the 0.43 power is 3.991298452658078.'"
|
||
2 years ago
|
]
|
||
|
},
|
||
1 year ago
|
"execution_count": 4,
|
||
2 years ago
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
1 year ago
|
"mrkl.run(\"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?\")"
|
||
2 years ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
1 year ago
|
"execution_count": 5,
|
||
2 years ago
|
"id": "a5c07010",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
2 years ago
|
"\n",
|
||
|
"\n",
|
||
1 year ago
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
1 year ago
|
"\u001b[32;1m\u001b[1;3m I need to find out the artist's full name and then search the FooBar database for their albums.\n",
|
||
|
"Action: Search\n",
|
||
|
"Action Input: \"The Storm Before the Calm\" artist\u001b[0m\n",
|
||
1 year ago
|
"Observation: \u001b[36;1m\u001b[1;3mThe Storm Before the Calm (stylized in all lowercase) is the tenth (and eighth international) studio album by Canadian-American singer-songwriter Alanis ...\u001b[0m\n",
|
||
|
"Thought:\u001b[32;1m\u001b[1;3m I now need to search the FooBar database for Alanis Morissette's albums\n",
|
||
2 years ago
|
"Action: FooBar DB\n",
|
||
1 year ago
|
"Action Input: What albums by Alanis Morissette are in the FooBar database?\u001b[0m\n",
|
||
2 years ago
|
"\n",
|
||
1 year ago
|
"\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
|
||
1 year ago
|
"What albums by Alanis Morissette are in the FooBar database? \n",
|
||
|
"SQLQuery:\u001b[32;1m\u001b[1;3m SELECT Title FROM Album INNER JOIN Artist ON Album.ArtistId = Artist.ArtistId WHERE Artist.Name = 'Alanis Morissette' LIMIT 5;\u001b[0m\n",
|
||
2 years ago
|
"SQLResult: \u001b[33;1m\u001b[1;3m[('Jagged Little Pill',)]\u001b[0m\n",
|
||
1 year ago
|
"Answer:\u001b[32;1m\u001b[1;3m The albums by Alanis Morissette in the FooBar database are Jagged Little Pill.\u001b[0m\n",
|
||
|
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||
2 years ago
|
"\n",
|
||
1 year ago
|
"Observation: \u001b[38;5;200m\u001b[1;3m The albums by Alanis Morissette in the FooBar database are Jagged Little Pill.\u001b[0m\n",
|
||
|
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
||
|
"Final Answer: The artist who released the album The Storm Before the Calm is Alanis Morissette and the albums of theirs in the FooBar database are Jagged Little Pill.\u001b[0m\n",
|
||
|
"\n",
|
||
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
2 years ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
1 year ago
|
"'The artist who released the album The Storm Before the Calm is Alanis Morissette and the albums of theirs in the FooBar database are Jagged Little Pill.'"
|
||
2 years ago
|
]
|
||
|
},
|
||
1 year ago
|
"execution_count": 5,
|
||
2 years ago
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
1 year ago
|
"mrkl.run(\"What is the full name of the artist who recently released an album called 'The Storm Before the Calm' and are they in the FooBar database? If so, what albums of theirs are in the FooBar database?\")"
|
||
2 years ago
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
1 year ago
|
"id": "af016a70",
|
||
2 years ago
|
"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",
|
||
1 year ago
|
"version": "3.9.1"
|
||
2 years ago
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|