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.
197 lines
6.0 KiB
Plaintext
197 lines
6.0 KiB
Plaintext
{
|
|
"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 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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "c0a83623",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain import (\n",
|
|
" LLMMathChain,\n",
|
|
" OpenAI,\n",
|
|
" SerpAPIWrapper,\n",
|
|
" SQLDatabase,\n",
|
|
" SQLDatabaseChain,\n",
|
|
")\n",
|
|
"from langchain.agents import initialize_agent, Tool\n",
|
|
"from langchain.agents import AgentType\n",
|
|
"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,
|
|
"id": "6fefaba2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# 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 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 when you need to answer questions about math.\"\n",
|
|
" ),\n",
|
|
" Tool(\n",
|
|
" name=\"FooBar-DB\",\n",
|
|
" func=db_chain.run,\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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "9ff6cee9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mrkl = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "ba8e4cbe",
|
|
"metadata": {
|
|
"scrolled": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
|
"\u001b[32;1m\u001b[1;3m\n",
|
|
"Invoking: `Search` with `{'query': 'Leo DiCaprio girlfriend'}`\n",
|
|
"\n",
|
|
"\n",
|
|
"\u001b[0m\u001b[36;1m\u001b[1;3mAmidst his casual romance with Gigi, Leo allegedly entered a relationship with 19-year old model, Eden Polani, in February 2023.\u001b[0m\u001b[32;1m\u001b[1;3m\n",
|
|
"Invoking: `Calculator` with `{'expression': '19^0.43'}`\n",
|
|
"\n",
|
|
"\n",
|
|
"\u001b[0m\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
|
"19^0.43\u001b[32;1m\u001b[1;3m```text\n",
|
|
"19**0.43\n",
|
|
"```\n",
|
|
"...numexpr.evaluate(\"19**0.43\")...\n",
|
|
"\u001b[0m\n",
|
|
"Answer: \u001b[33;1m\u001b[1;3m3.547023357958959\u001b[0m\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n",
|
|
"\u001b[33;1m\u001b[1;3mAnswer: 3.547023357958959\u001b[0m\u001b[32;1m\u001b[1;3mLeo DiCaprio's girlfriend is reportedly Eden Polani. Her current age raised to the power of 0.43 is approximately 3.55.\u001b[0m\n",
|
|
"\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\"Leo DiCaprio's girlfriend is reportedly Eden Polani. Her current age raised to the power of 0.43 is approximately 3.55.\""
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"mrkl.run(\n",
|
|
" \"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?\"\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "9f5f6743",
|
|
"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
|
|
}
|