forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Amadeus Flight and Travel Search Tool (langchain-ai#7890)
## Background With the addition on email and calendar tools, LangChain is continuing to complete its functionality to automate business processes. ## Challenge One of the pieces of business functionality that LangChain currently doesn't have is the ability to search for flights and travel in order to book business travel. ## Changes This PR implements an integration with the [Amadeus](https://developers.amadeus.com/) travel search API for LangChain, enabling seamless search for flights with a single authentication process. ## Who can review? @hinthornw ## Appendix @tsolakoua and @minjikarin, I utilized your [amadeus-python](https://github.com/amadeus4dev/amadeus-python) library extensively. Given the rising popularity of LangChain and similar AI frameworks, the convergence of libraries like amadeus-python and tools like this one is likely. So, I wanted to keep you updated on our progress. --------- Co-authored-by: Bagatur <[email protected]>
- Loading branch information
Showing
11 changed files
with
581 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Amadeus Toolkit\n", | ||
"\n", | ||
"This notebook walks you through connecting LangChain to the Amadeus travel information API\n", | ||
"\n", | ||
"To use this toolkit, you will need to set up your credentials explained in the [Amadeus for developers getting started overview](https://developers.amadeus.com/get-started/get-started-with-self-service-apis-335). Once you've received a AMADEUS_CLIENT_ID and AMADEUS_CLIENT_SECRET, you can input them as environmental variables below." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install --upgrade amadeus > /dev/null" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"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." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Set environmental variables here\n", | ||
"import os\n", | ||
"\n", | ||
"os.environ[\"AMADEUS_CLIENT_ID\"] = \"CLIENT_ID\"\n", | ||
"os.environ[\"AMADEUS_CLIENT_SECRET\"] = \"CLIENT_SECRET\"\n", | ||
"os.environ[\"OPENAI_API_KEY\"] = \"API_KEY\"" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Create the Amadeus Toolkit and Get Tools\n", | ||
"\n", | ||
"To start, you need to create the toolkit, so you can access its tools later." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain.agents.agent_toolkits.amadeus.toolkit import AmadeusToolkit\n", | ||
"\n", | ||
"toolkit = AmadeusToolkit()\n", | ||
"tools = toolkit.get_tools()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Use Amadeus Toolkit within an Agent" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain import OpenAI\n", | ||
"from langchain.agents import initialize_agent, AgentType" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"llm = OpenAI(temperature=0)\n", | ||
"agent = initialize_agent(\n", | ||
" tools=tools,\n", | ||
" llm=llm,\n", | ||
" verbose=False,\n", | ||
" agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'The closest airport to Cali, Colombia is Alfonso Bonilla Aragón International Airport (CLO).'" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"agent.run(\"What is the name of the airport in Cali, Colombia?\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"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" | ||
} | ||
], | ||
"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", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"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" | ||
} | ||
], | ||
"source": [ | ||
"agent.run(\n", | ||
" \"At what time does earliest flight on August 23, 2023 leaving Dallas, Texas to Lincoln, Nebraska land in Nebraska?\"\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"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" | ||
} | ||
], | ||
"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", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"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" | ||
} | ||
], | ||
"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", | ||
")" | ||
] | ||
} | ||
], | ||
"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.11.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, List | ||
|
||
from pydantic import Field | ||
|
||
from langchain.agents.agent_toolkits.base import BaseToolkit | ||
from langchain.tools import BaseTool | ||
from langchain.tools.amadeus.closest_airport import AmadeusClosestAirport | ||
from langchain.tools.amadeus.flight_search import AmadeusFlightSearch | ||
from langchain.tools.amadeus.utils import authenticate | ||
|
||
if TYPE_CHECKING: | ||
from amadeus import Client | ||
|
||
|
||
class AmadeusToolkit(BaseToolkit): | ||
"""Toolkit for interacting with Office365.""" | ||
|
||
client: Client = Field(default_factory=authenticate) | ||
|
||
class Config: | ||
"""Pydantic config.""" | ||
|
||
arbitrary_types_allowed = True | ||
|
||
def get_tools(self) -> List[BaseTool]: | ||
"""Get the tools in the toolkit.""" | ||
return [ | ||
AmadeusClosestAirport(), | ||
AmadeusFlightSearch(), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Amadeus tools.""" | ||
|
||
from langchain.tools.amadeus.closest_airport import AmadeusClosestAirport | ||
from langchain.tools.amadeus.flight_search import AmadeusFlightSearch | ||
|
||
__all__ = [ | ||
"AmadeusClosestAirport", | ||
"AmadeusFlightSearch", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Base class for Amadeus tools.""" | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from pydantic import Field | ||
|
||
from langchain.tools.amadeus.utils import authenticate | ||
from langchain.tools.base import BaseTool | ||
|
||
if TYPE_CHECKING: | ||
from amadeus import Client | ||
|
||
|
||
class AmadeusBaseTool(BaseTool): | ||
client: Client = Field(default_factory=authenticate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from typing import Optional, Type | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from langchain.callbacks.manager import ( | ||
AsyncCallbackManagerForToolRun, | ||
CallbackManagerForToolRun, | ||
) | ||
from langchain.chains import LLMChain | ||
from langchain.chat_models import ChatOpenAI | ||
from langchain.tools.amadeus.base import AmadeusBaseTool | ||
|
||
|
||
class ClosestAirportSchema(BaseModel): | ||
location: str = Field( | ||
description=( | ||
" The location for which you would like to find the nearest airport " | ||
" along with optional details such as country, state, region, or " | ||
" province, allowing for easy processing and identification of " | ||
" the closest airport. Examples of the format are the following:\n" | ||
" Cali, Colombia\n " | ||
" Lincoln, Nebraska, United States\n" | ||
" New York, United States\n" | ||
" Sydney, New South Wales, Australia\n" | ||
" Rome, Lazio, Italy\n" | ||
" Toronto, Ontario, Canada\n" | ||
) | ||
) | ||
|
||
|
||
class AmadeusClosestAirport(AmadeusBaseTool): | ||
name: str = "closest_airport" | ||
description: str = ( | ||
"Use this tool to find the closest airport to a particular location." | ||
) | ||
args_schema: Type[ClosestAirportSchema] = ClosestAirportSchema | ||
|
||
def _run( | ||
self, | ||
location: str, | ||
run_manager: Optional[CallbackManagerForToolRun] = None, | ||
) -> str: | ||
template = ( | ||
" What is the nearest airport to {location}? Please respond with the " | ||
" airport's International Air Transport Association (IATA) Location " | ||
' Identifier in the following JSON format. JSON: "iataCode": "IATA ' | ||
' Location Identifier" ' | ||
) | ||
|
||
llm = ChatOpenAI(temperature=0) | ||
|
||
llm_chain = LLMChain.from_string(llm=llm, template=template) | ||
|
||
output = llm_chain.run(location=location) | ||
|
||
return output | ||
|
||
async def _arun( | ||
self, | ||
location: str, | ||
run_manager: Optional[AsyncCallbackManagerForToolRun] = None, | ||
) -> str: | ||
raise NotImplementedError(f"The tool {self.name} does not support async yet.") |
Oops, something went wrong.