Harrison/functions docs improvements (#6389)

Co-authored-by: Sumanth Donthula <46747610+sumanthdonthula@users.noreply.github.com>
This commit is contained in:
Harrison Chase 2023-06-18 16:57:33 -07:00 committed by GitHub
parent c7ca350cd3
commit 495128ba95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,13 +1,18 @@
{ {
"cells": [ "cells": [
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"id": "9502d5b0", "id": "9502d5b0",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# OpenAI Functions Agent\n", "# OpenAI Functions Agent\n",
"\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" "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", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
@ -36,28 +59,43 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"llm = ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-0613\")\n", "# Initialize the OpenAI language model\n",
"search = SerpAPIWrapper()\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", "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", "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", "db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)\n",
"\n",
"# Define a list of tools offered by the agent\n",
"tools = [\n", "tools = [\n",
" Tool(\n", " Tool(\n",
" name=\"Search\",\n", " name=\"Search\",\n",
" func=search.run,\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", " ),\n",
" Tool(\n", " Tool(\n",
" name=\"Calculator\",\n", " name=\"Calculator\",\n",
" func=llm_math_chain.run,\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", " ),\n",
" Tool(\n", " Tool(\n",
" name=\"FooBar-DB\",\n", " name=\"FooBar-DB\",\n",
" func=db_chain.run,\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", " 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",
"]" "]\n"
] ]
}, },
{ {