2022-11-05 21:41:53 +00:00
{
"cells": [
2022-11-14 04:13:23 +00:00
{
"cell_type": "markdown",
"id": "f1390152",
"metadata": {},
"source": [
"# MRKL\n",
"\n",
2022-11-22 14:16:26 +00:00
"This notebook showcases using an agent to replicate the MRKL chain."
2022-11-14 04:13:23 +00:00
]
},
2022-11-07 00:06:40 +00:00
{
"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."
]
},
2022-11-05 21:41:53 +00:00
{
"cell_type": "code",
"execution_count": 1,
"id": "ac561cc4",
"metadata": {},
"outputs": [],
"source": [
2022-11-22 14:16:26 +00:00
"from langchain import LLMMathChain, OpenAI, SerpAPIChain, SQLDatabase, SQLDatabaseChain\n",
"from langchain.agents import initialize_agent, Tool"
2022-11-05 21:41:53 +00:00
]
},
{
"cell_type": "code",
2022-11-22 14:16:26 +00:00
"execution_count": 2,
2022-11-05 21:41:53 +00:00
"id": "07e96d99",
"metadata": {},
"outputs": [],
"source": [
"llm = OpenAI(temperature=0)\n",
"search = SerpAPIChain()\n",
2022-11-05 22:13:12 +00:00
"llm_math_chain = LLMMathChain(llm=llm, verbose=True)\n",
2022-11-14 04:13:23 +00:00
"db = SQLDatabase.from_uri(\"sqlite:///../../../notebooks/Chinook.db\")\n",
2022-11-05 22:13:12 +00:00
"db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)\n",
2022-11-22 14:16:26 +00:00
"tools = [\n",
" Tool(\n",
" name = \"Search\",\n",
" func=search.run,\n",
" description=\"useful for when you need to answer questions about current events\"\n",
2022-11-05 21:41:53 +00:00
" ),\n",
2022-11-22 14:16:26 +00:00
" Tool(\n",
" name=\"Calculator\",\n",
" func=llm_math_chain.run,\n",
" description=\"useful for when you need to answer questions about math\"\n",
2022-11-05 21:41:53 +00:00
" ),\n",
2022-11-22 14:16:26 +00:00
" Tool(\n",
" name=\"FooBar DB\",\n",
" func=db_chain.run,\n",
" description=\"useful for when you need to answer questions about FooBar. Input should be in the form of a question\"\n",
2022-11-05 21:41:53 +00:00
" )\n",
"]"
]
},
{
"cell_type": "code",
2022-11-22 14:16:26 +00:00
"execution_count": 3,
2022-11-05 21:41:53 +00:00
"id": "a069c4b6",
"metadata": {},
"outputs": [],
"source": [
2022-11-23 02:02:20 +00:00
"mrkl = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
2022-11-05 21:41:53 +00:00
]
},
{
"cell_type": "code",
2022-11-22 14:16:26 +00:00
"execution_count": 4,
2022-11-05 21:41:53 +00:00
"id": "e603cd7d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What is the age of Olivia Wilde's boyfriend raised to the 0.23 power?\n",
2022-11-22 14:16:26 +00:00
"Thought:\u001b[32;1m\u001b[1;3m I need to find the age of Olivia Wilde's boyfriend\n",
2022-11-05 21:41:53 +00:00
"Action: Search\n",
"Action Input: \"Olivia Wilde's boyfriend\"\u001b[0m\n",
2022-11-22 14:16:26 +00:00
"Observation: \u001b[36;1m\u001b[1;3mOlivia Wilde started dating Harry Styles after ending her years-long engagement to Jason Sudeikis — see their relationship timeline.\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I need to find the age of Harry Styles\n",
2022-11-05 21:41:53 +00:00
"Action: Search\n",
"Action Input: \"Harry Styles age\"\u001b[0m\n",
2022-11-22 14:16:26 +00:00
"Observation: \u001b[36;1m\u001b[1;3m28 years\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I need to calculate 28 to the 0.23 power\n",
2022-11-05 21:41:53 +00:00
"Action: Calculator\n",
"Action Input: 28^0.23\u001b[0m\n",
2022-11-05 22:13:12 +00:00
"\n",
"\u001b[1m> Entering new chain...\u001b[0m\n",
2022-11-22 14:16:26 +00:00
"28^0.23\u001b[32;1m\u001b[1;3m\n",
2022-11-05 22:13:12 +00:00
"\n",
"```python\n",
"print(28**0.23)\n",
"```\n",
"\u001b[0m\n",
2022-11-22 14:16:26 +00:00
"Answer: \u001b[33;1m\u001b[1;3m2.1520202182226886\n",
2022-11-05 22:13:12 +00:00
"\u001b[0m\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
2022-11-22 14:16:26 +00:00
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.1520202182226886\n",
2022-11-05 21:41:53 +00:00
"\u001b[0m\n",
2022-11-22 14:16:26 +00:00
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: 2.1520202182226886\u001b[0m"
2022-11-05 21:41:53 +00:00
]
},
{
"data": {
"text/plain": [
"'2.1520202182226886'"
]
},
2022-11-22 14:16:26 +00:00
"execution_count": 4,
2022-11-05 21:41:53 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mrkl.run(\"What is the age of Olivia Wilde's boyfriend raised to the 0.23 power?\")"
]
},
{
"cell_type": "code",
2022-11-22 14:16:26 +00:00
"execution_count": 5,
2022-11-05 21:41:53 +00:00
"id": "a5c07010",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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?\n",
2022-11-22 14:16:26 +00:00
"Thought:\u001b[32;1m\u001b[1;3m I need to find an album called 'The Storm Before the Calm'\n",
2022-11-05 21:41:53 +00:00
"Action: Search\n",
"Action Input: \"The Storm Before the Calm album\"\u001b[0m\n",
2022-11-22 14:16:26 +00:00
"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 need to check if Alanis is in the FooBar database\n",
2022-11-05 21:41:53 +00:00
"Action: FooBar DB\n",
"Action Input: \"Does Alanis Morissette exist in the FooBar database?\"\u001b[0m\n",
2022-11-05 22:13:12 +00:00
"\n",
"\u001b[1m> Entering new chain...\u001b[0m\n",
"Does Alanis Morissette exist in the FooBar database?\n",
2022-11-22 14:16:26 +00:00
"SQLQuery:\u001b[32;1m\u001b[1;3m SELECT * FROM Artist WHERE Name = 'Alanis Morissette'\u001b[0m\n",
"SQLResult: \u001b[33;1m\u001b[1;3m[(4, 'Alanis Morissette')]\u001b[0m\n",
"Answer:\u001b[32;1m\u001b[1;3m Yes\u001b[0m\n",
2022-11-05 22:13:12 +00:00
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
2022-11-22 14:16:26 +00:00
"Observation: \u001b[38;5;200m\u001b[1;3m Yes\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I need to find out what albums of Alanis's are in the FooBar database\n",
2022-11-05 21:41:53 +00:00
"Action: FooBar DB\n",
"Action Input: \"What albums by Alanis Morissette are in the FooBar database?\"\u001b[0m\n",
2022-11-05 22:13:12 +00:00
"\n",
"\u001b[1m> Entering new chain...\u001b[0m\n",
"What albums by Alanis Morissette are in the FooBar database?\n",
2022-11-22 14:16:26 +00:00
"SQLQuery:\u001b[32;1m\u001b[1;3m SELECT Album.Title FROM Album JOIN Artist ON Album.ArtistId = Artist.ArtistId WHERE Artist.Name = 'Alanis Morissette'\u001b[0m\n",
"SQLResult: \u001b[33;1m\u001b[1;3m[('Jagged Little Pill',)]\u001b[0m\n",
"Answer:\u001b[32;1m\u001b[1;3m Jagged Little Pill\u001b[0m\n",
2022-11-05 22:13:12 +00:00
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
2022-11-22 14:16:26 +00:00
"Observation: \u001b[38;5;200m\u001b[1;3m Jagged Little Pill\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: The album is by Alanis Morissette and the albums in the FooBar database by her are Jagged Little Pill\u001b[0m"
2022-11-05 21:41:53 +00:00
]
},
{
"data": {
"text/plain": [
2022-11-14 04:13:23 +00:00
"'The album is by Alanis Morissette and the albums in the FooBar database by her are Jagged Little Pill'"
2022-11-05 21:41:53 +00:00
]
},
2022-11-22 14:16:26 +00:00
"execution_count": 5,
2022-11-05 21:41:53 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mrkl.run(\"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?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d7c2e6ac",
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}