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.
254 lines
8.4 KiB
Plaintext
254 lines
8.4 KiB
Plaintext
1 year ago
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "f1390152",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# MRKL Chat\n",
|
||
|
"\n",
|
||
|
"This notebook showcases using an agent to replicate the MRKL chain using an agent optimized for chat models."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"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."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "ac561cc4",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from langchain import OpenAI, LLMMathChain, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain\n",
|
||
|
"from langchain.agents import initialize_agent, Tool\n",
|
||
|
"from langchain.chat_models import ChatOpenAI"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"id": "07e96d99",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"llm = ChatOpenAI(temperature=0)\n",
|
||
|
"llm1 = OpenAI(temperature=0)\n",
|
||
|
"search = SerpAPIWrapper()\n",
|
||
|
"llm_math_chain = LLMMathChain(llm=llm1, verbose=True)\n",
|
||
|
"db = SQLDatabase.from_uri(\"sqlite:///../../../../notebooks/Chinook.db\")\n",
|
||
|
"db_chain = SQLDatabaseChain(llm=llm1, database=db, verbose=True)\n",
|
||
|
"tools = [\n",
|
||
|
" Tool(\n",
|
||
|
" name = \"Search\",\n",
|
||
|
" func=search.run,\n",
|
||
|
" description=\"useful for when you need to answer questions about current events. You should ask targeted questions\"\n",
|
||
|
" ),\n",
|
||
|
" Tool(\n",
|
||
|
" name=\"Calculator\",\n",
|
||
|
" func=llm_math_chain.run,\n",
|
||
|
" description=\"useful for when you need to answer questions about math\"\n",
|
||
|
" ),\n",
|
||
|
" 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 containing full context\"\n",
|
||
|
" )\n",
|
||
|
"]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "a069c4b6",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"mrkl = initialize_agent(tools, llm, agent=\"chat-zero-shot-react-description\", verbose=True)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"id": "e603cd7d",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
1 year ago
|
"\u001b[32;1m\u001b[1;3mThought: The first question requires a search, while the second question requires a calculator.\n",
|
||
1 year ago
|
"Action:\n",
|
||
|
"```\n",
|
||
|
"{\n",
|
||
|
" \"action\": \"Search\",\n",
|
||
|
" \"action_input\": \"Who is Leo DiCaprio's girlfriend?\"\n",
|
||
|
"}\n",
|
||
|
"```\n",
|
||
|
"\u001b[0m\n",
|
||
|
"Observation: \u001b[36;1m\u001b[1;3mCamila Morrone\u001b[0m\n",
|
||
1 year ago
|
"Thought:\u001b[32;1m\u001b[1;3mFor the second question, I need to use the calculator tool to raise her current age to the 0.43 power.\n",
|
||
1 year ago
|
"Action:\n",
|
||
|
"```\n",
|
||
|
"{\n",
|
||
|
" \"action\": \"Calculator\",\n",
|
||
1 year ago
|
" \"action_input\": \"22.0^(0.43)\"\n",
|
||
1 year ago
|
"}\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"\u001b[0m\n",
|
||
|
"\n",
|
||
|
"\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n",
|
||
1 year ago
|
"22.0^(0.43)\u001b[32;1m\u001b[1;3m\n",
|
||
1 year ago
|
"```python\n",
|
||
|
"import math\n",
|
||
1 year ago
|
"print(math.pow(22.0, 0.43))\n",
|
||
1 year ago
|
"```\n",
|
||
|
"\u001b[0m\n",
|
||
1 year ago
|
"Answer: \u001b[33;1m\u001b[1;3m3.777824273683966\n",
|
||
1 year ago
|
"\u001b[0m\n",
|
||
|
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||
|
"\n",
|
||
1 year ago
|
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 3.777824273683966\n",
|
||
1 year ago
|
"\u001b[0m\n",
|
||
1 year ago
|
"Thought:\u001b[32;1m\u001b[1;3mI now know the final answer.\n",
|
||
|
"Final Answer: Camila Morrone, 3.777824273683966.\u001b[0m\n",
|
||
1 year ago
|
"\n",
|
||
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
1 year ago
|
"'Camila Morrone, 3.777824273683966.'"
|
||
1 year ago
|
]
|
||
|
},
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"mrkl.run(\"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"id": "a5c07010",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
1 year ago
|
"\u001b[32;1m\u001b[1;3mQuestion: 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?\n",
|
||
|
"Thought: I should use the Search tool to find the answer to the first part of the question and then use the FooBar DB tool to find the answer to the second part of the question.\n",
|
||
|
"Action:\n",
|
||
1 year ago
|
"```\n",
|
||
|
"{\n",
|
||
|
" \"action\": \"Search\",\n",
|
||
1 year ago
|
" \"action_input\": \"Who recently released an album called 'The Storm Before the Calm'\"\n",
|
||
1 year ago
|
"}\n",
|
||
|
"```\n",
|
||
|
"\u001b[0m\n",
|
||
1 year ago
|
"Observation: \u001b[36;1m\u001b[1;3mAlanis Morissette\u001b[0m\n",
|
||
|
"Thought:\u001b[32;1m\u001b[1;3mNow that I have the name of the artist, I can use the FooBar DB tool to find their albums in the database.\n",
|
||
|
"Action:\n",
|
||
1 year ago
|
"```\n",
|
||
|
"{\n",
|
||
|
" \"action\": \"FooBar DB\",\n",
|
||
1 year ago
|
" \"action_input\": \"What albums does Alanis Morissette have in the database?\"\n",
|
||
1 year ago
|
"}\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"\u001b[0m\n",
|
||
|
"\n",
|
||
|
"\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
|
||
1 year ago
|
"What albums does Alanis Morissette have in the database? \n",
|
||
1 year ago
|
"SQLQuery:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"/Users/harrisonchase/workplace/langchain/langchain/sql_database.py:141: SAWarning: Dialect sqlite+pysqlite does *not* support Decimal objects natively, and SQLAlchemy must convert from floating point - rounding errors and other issues may occur. Please consider storing Decimal numbers as strings or integers on this platform for lossless storage.\n",
|
||
|
" sample_rows = connection.execute(command)\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"\u001b[32;1m\u001b[1;3m SELECT Title FROM Album WHERE ArtistId IN (SELECT ArtistId FROM Artist WHERE Name = 'Alanis Morissette') LIMIT 5;\u001b[0m\n",
|
||
|
"SQLResult: \u001b[33;1m\u001b[1;3m[('Jagged Little Pill',)]\u001b[0m\n",
|
||
1 year ago
|
"Answer:\u001b[32;1m\u001b[1;3m Alanis Morissette has the album 'Jagged Little Pill' in the database.\u001b[0m\n",
|
||
1 year ago
|
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||
|
"\n",
|
||
1 year ago
|
"Observation: \u001b[38;5;200m\u001b[1;3m Alanis Morissette has the album 'Jagged Little Pill' in the database.\u001b[0m\n",
|
||
|
"Thought:\u001b[32;1m\u001b[1;3mI have found the answer to both parts of the question.\n",
|
||
|
"Final Answer: The artist who recently released an album called 'The Storm Before the Calm' is Alanis Morissette. The album 'Jagged Little Pill' is in the FooBar database.\u001b[0m\n",
|
||
1 year ago
|
"\n",
|
||
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
1 year ago
|
"\"The artist who recently released an album called 'The Storm Before the Calm' is Alanis Morissette. The album 'Jagged Little Pill' is in the FooBar database.\""
|
||
1 year ago
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"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?\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "af016a70",
|
||
|
"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
|
||
|
}
|