|
|
|
@ -1,13 +1,18 @@
|
|
|
|
|
{
|
|
|
|
|
"cells": [
|
|
|
|
|
{
|
|
|
|
|
"attachments": {},
|
|
|
|
|
"cell_type": "markdown",
|
|
|
|
|
"id": "9502d5b0",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
|
|
|
|
"# OpenAI Functions Agent\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"This notebook showcases using an agent that uses the OpenAI functions ability"
|
|
|
|
|
"This notebook showcases using an agent that uses the OpenAI functions ability to respond to the prompts of the user using a Large Language Model\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"Install openai,google-search-results packages which are required as the langchain packages call them internally\n",
|
|
|
|
|
"\n",
|
|
|
|
|
">pip install openai google-search-results\n"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
@ -29,6 +34,24 @@
|
|
|
|
|
"from langchain.chat_models import ChatOpenAI"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"attachments": {},
|
|
|
|
|
"cell_type": "markdown",
|
|
|
|
|
"id": "86198d9c",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
|
|
|
|
"The agent is given ability to perform 3 search functionalities with the respective tools\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"SerpAPIWrapper:\n",
|
|
|
|
|
">This initializes the SerpAPIWrapper for search functionality (search).\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"LLMMathChain Initialization:\n",
|
|
|
|
|
">This component provides math-related functionality.\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"SQL Database Initialization:\n",
|
|
|
|
|
">This component provides the agent to query in Custom Data Base.\n"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 2,
|
|
|
|
@ -36,28 +59,43 @@
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"llm = ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-0613\")\n",
|
|
|
|
|
"search = SerpAPIWrapper()\n",
|
|
|
|
|
"# Initialize the OpenAI language model\n",
|
|
|
|
|
"#Replace <your_api_key> in openai_api_key=\"<your_api_key>\" with your actual OpenAI key.\n",
|
|
|
|
|
"llm = ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-0613\",openai_api_key=\"<your_api_key>\")\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"# Initialize the SerpAPIWrapper for search functionality\n",
|
|
|
|
|
"#Replace <your_api_key> in openai_api_key=\"<your_api_key>\" with your actual SerpAPI key.\n",
|
|
|
|
|
"search = SerpAPIWrapper(serpapi_api_key=\"<your_api_key>\")\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"# Initialize the LLMMathChain\n",
|
|
|
|
|
"llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True)\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"# Initialize the SQL database using the Chinook database file\n",
|
|
|
|
|
"# Replace the file location to the custom Data Base\n",
|
|
|
|
|
"db = SQLDatabase.from_uri(\"sqlite:///../../../../../notebooks/Chinook.db\")\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"# Initialize the SQLDatabaseChain with the OpenAI language model and SQL database\n",
|
|
|
|
|
"db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"# Define a list of tools offered by the agent\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",
|
|
|
|
|
|
|
|
|
|
" description=\"Useful 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",
|
|
|
|
|
" description=\"Useful 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",
|
|
|
|
|
"]"
|
|
|
|
|
" description=\"Useful when you need to answer questions about FooBar. Input should be in the form of a question containing full context.\"\n",
|
|
|
|
|
" )\n",
|
|
|
|
|
"]\n"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|