diff --git a/docs/docs/integrations/toolkits/amadeus.ipynb b/docs/docs/integrations/toolkits/amadeus.ipynb index 940b98f31e..e97067c0f6 100644 --- a/docs/docs/integrations/toolkits/amadeus.ipynb +++ b/docs/docs/integrations/toolkits/amadeus.ipynb @@ -17,7 +17,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -30,13 +30,18 @@ "source": [ "## Assign Environmental Variables\n", "\n", - "The toolkit will read the AMADEUS_CLIENT_ID and AMADEUS_CLIENT_SECRET environmental variables to authenticate the user so you need to set them here. You will also need to set your OPENAI_API_KEY to use the agent later." + "The toolkit will read the AMADEUS_CLIENT_ID and AMADEUS_CLIENT_SECRET environmental variables to authenticate the user, so you need to set them here. " ] }, { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2024-01-13T17:45:56.531388579Z", + "start_time": "2024-01-13T17:45:56.523533018Z" + } + }, "outputs": [], "source": [ "# Set environmental variables here\n", @@ -44,7 +49,6 @@ "\n", "os.environ[\"AMADEUS_CLIENT_ID\"] = \"CLIENT_ID\"\n", "os.environ[\"AMADEUS_CLIENT_SECRET\"] = \"CLIENT_SECRET\"\n", - "os.environ[\"OPENAI_API_KEY\"] = \"API_KEY\"\n", "# os.environ[\"AMADEUS_HOSTNAME\"] = \"production\" or \"test\"" ] }, @@ -57,11 +61,39 @@ "To start, you need to create the toolkit, so you can access its tools later." ] }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false + }, + "source": [ + "By default, `AmadeusToolkit` uses `ChatOpenAI` to identify airports closest to a given location. To use it, just set `OPENAI_API_KEY`.\n" + ] + }, { "cell_type": "code", "execution_count": 3, "metadata": { - "tags": [] + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-01-13T17:45:56.557041160Z", + "start_time": "2024-01-13T17:45:56.530682481Z" + } + }, + "outputs": [], + "source": [ + "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_KEY\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "tags": [], + "ExecuteTime": { + "end_time": "2024-01-13T17:45:58.431168124Z", + "start_time": "2024-01-13T17:45:56.536269739Z" + } }, "outputs": [], "source": [ @@ -71,6 +103,35 @@ "tools = toolkit.get_tools()" ] }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false + }, + "source": [ + "Alternatively, you can use any LLM supported by langchain, e.g. `HuggingFaceHub`. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from langchain_community.llms import HuggingFaceHub\n", + "\n", + "os.environ[\"HUGGINGFACEHUB_API_TOKEN\"] = \"YOUR_HF_API_TOKEN\"\n", + "\n", + "llm = HuggingFaceHub(\n", + " repo_id=\"tiiuae/falcon-7b-instruct\",\n", + " model_kwargs={\"temperature\": 0.5, \"max_length\": 64},\n", + ")\n", + "\n", + "toolkit_hf = AmadeusToolkit(llm=llm)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -80,142 +141,142 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": { - "tags": [] + "tags": [], + "ExecuteTime": { + "end_time": "2024-01-13T17:46:00.148691365Z", + "start_time": "2024-01-13T17:45:59.317173243Z" + } }, "outputs": [], "source": [ - "from langchain.agents import AgentType, initialize_agent\n", - "from langchain_openai import OpenAI" + "from langchain import hub\n", + "from langchain.agents import AgentExecutor, create_react_agent\n", + "from langchain_openai import ChatOpenAI" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": { - "tags": [] + "tags": [], + "ExecuteTime": { + "end_time": "2024-01-13T17:46:01.270044101Z", + "start_time": "2024-01-13T17:46:00.148988945Z" + } }, "outputs": [], "source": [ - "llm = OpenAI(temperature=0)\n", - "agent = initialize_agent(\n", + "llm = ChatOpenAI(temperature=0)\n", + "\n", + "prompt = hub.pull(\"hwchase17/react\")\n", + "agent = create_react_agent(llm, tools, prompt)\n", + "\n", + "agent_executor = AgentExecutor(\n", + " agent=agent,\n", " tools=tools,\n", - " llm=llm,\n", - " verbose=False,\n", - " agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n", + " verbose=True,\n", ")" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "metadata": { - "tags": [] + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-01-13T17:46:06.176227412Z", + "start_time": "2024-01-13T17:46:01.272468682Z" + } }, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n", + "\u001B[32;1m\u001B[1;3mI should use the closest_airport tool to find the airport in Cali, Colombia.\n", + "Action: closest_airport\n", + "Action Input: location= \"Cali, Colombia\"\u001B[0m\u001B[36;1m\u001B[1;3mcontent='{\\n \"iataCode\": \"CLO\"\\n}'\u001B[0m\u001B[32;1m\u001B[1;3mThe airport in Cali, Colombia is called CLO.\n", + "Final Answer: CLO\u001B[0m\n", + "\n", + "\u001B[1m> Finished chain.\u001B[0m\n" + ] + }, { "data": { - "text/plain": [ - "'The closest airport to Cali, Colombia is Alfonso Bonilla Aragón International Airport (CLO).'" - ] + "text/plain": "{'input': 'What is the name of the airport in Cali, Colombia?',\n 'output': 'CLO'}" }, - "execution_count": 6, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "agent.run(\"What is the name of the airport in Cali, Colombia?\")" + "agent_executor.invoke({\"input\": \"What is the name of the airport in Cali, Colombia?\"})" ] }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false + }, + "source": [] + }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'The cheapest flight on August 23, 2023 leaving Dallas, Texas before noon to Lincoln, Nebraska has a departure time of 16:42 and a total price of 276.08 EURO.'" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "agent.run(\n", - " \"What is the departure time of the cheapest flight on August 23, 2023 leaving Dallas, Texas before noon to Lincoln, Nebraska?\"\n", + "agent_executor.invoke(\n", + " {\n", + " \"input\": \"What is the departure time of the cheapest flight on August 23, 2023 leaving Dallas, Texas before noon to Lincoln, Nebraska?\"\n", + " }\n", ")" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'The earliest flight on August 23, 2023 leaving Dallas, Texas to Lincoln, Nebraska lands in Lincoln, Nebraska at 16:07.'" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "agent.run(\n", - " \"At what time does earliest flight on August 23, 2023 leaving Dallas, Texas to Lincoln, Nebraska land in Nebraska?\"\n", + "agent_executor.invoke(\n", + " {\n", + " \"input\": \"At what time does earliest flight on August 23, 2023 leaving Dallas, Texas to Lincoln, Nebraska land in Nebraska?\"\n", + " }\n", ")" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'The cheapest flight between Portland, Oregon to Dallas, TX on October 3, 2023 is a Spirit Airlines flight with a total price of 84.02 EURO and a total travel time of 8 hours and 43 minutes.'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "agent.run(\n", - " \"What is the full travel time for the cheapest flight between Portland, Oregon to Dallas, TX on October 3, 2023?\"\n", + "agent_executor.invoke(\n", + " {\n", + " \"input\": \"What is the full travel time for the cheapest flight between Portland, Oregon to Dallas, TX on October 3, 2023?\"\n", + " }\n", ")" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Dear Paul,\\n\\nI am writing to request that you book the earliest flight from DFW to DCA on Aug 28, 2023. The flight details are as follows:\\n\\nFlight 1: DFW to ATL, departing at 7:15 AM, arriving at 10:25 AM, flight number 983, carrier Delta Air Lines\\nFlight 2: ATL to DCA, departing at 12:15 PM, arriving at 2:02 PM, flight number 759, carrier Delta Air Lines\\n\\nThank you for your help.\\n\\nSincerely,\\nSantiago'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "agent.run(\n", - " \"Please draft a concise email from Santiago to Paul, Santiago's travel agent, asking him to book the earliest flight from DFW to DCA on Aug 28, 2023. Include all flight details in the email.\"\n", + "agent_executor.invoke(\n", + " {\n", + " \"input\": \"Please draft a concise email from Santiago to Paul, Santiago's travel agent, asking him to book the earliest flight from DFW to DCA on Aug 28, 2023. Include all flight details in the email.\"\n", + " }\n", ")" ] } diff --git a/libs/community/langchain_community/tools/amadeus/closest_airport.py b/libs/community/langchain_community/tools/amadeus/closest_airport.py index 4107cc500b..cf108f3b11 100644 --- a/libs/community/langchain_community/tools/amadeus/closest_airport.py +++ b/libs/community/langchain_community/tools/amadeus/closest_airport.py @@ -58,4 +58,4 @@ class AmadeusClosestAirport(AmadeusBaseTool): ' Location Identifier" ' ) - return self.llm.predict(content) + return self.llm.invoke(content)