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.
langchain/docs/modules/chains/examples/api.ipynb

209 lines
16 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# API Chains\n",
"This notebook showcases using LLMs to interact with APIs to retrieve relevant information."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains.api.prompt import API_RESPONSE_PROMPT"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import APIChain\n",
"from langchain.prompts.prompt import PromptTemplate\n",
"\n",
"\n",
"from langchain.llms import OpenAI\n",
"\n",
"llm = OpenAI(temperature=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## OpenMeteo Example"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains.api import open_meteo_docs\n",
"chain_new = APIChain.from_llm_and_api_docs(llm, open_meteo_docs.OPEN_METEO_DOCS, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new APIChain chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3mhttps://api.open-meteo.com/v1/forecast?latitude=48.1351&longitude=11.5820&temperature_unit=fahrenheit&current_weather=true\u001b[0m\n",
"\u001b[33;1m\u001b[1;3m{\"latitude\":48.14,\"longitude\":11.58,\"generationtime_ms\":0.33104419708251953,\"utc_offset_seconds\":0,\"timezone\":\"GMT\",\"timezone_abbreviation\":\"GMT\",\"elevation\":521.0,\"current_weather\":{\"temperature\":33.4,\"windspeed\":6.8,\"winddirection\":198.0,\"weathercode\":2,\"time\":\"2023-01-16T01:00\"}}\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"' The current temperature in Munich, Germany is 33.4 degrees Farenheit with a windspeed of 6.8 km/h and a wind direction of 198 degrees. The weathercode is 2.'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chain_new.run('What is the weather like right now in Munich, Germany in degrees Farenheit?')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## TMDB Example"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ['TMDB_BEARER_TOKEN'] = \"\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains.api import tmdb_docs\n",
"headers = {\"Authorization\": f\"Bearer {os.environ['TMDB_BEARER_TOKEN']}\"}\n",
"chain = APIChain.from_llm_and_api_docs(llm, tmdb_docs.TMDB_DOCS, headers=headers, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new APIChain chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m https://api.themoviedb.org/3/search/movie?query=Avatar&language=en-US\u001b[0m\n",
"\u001b[33;1m\u001b[1;3m{\"page\":1,\"results\":[{\"adult\":false,\"backdrop_path\":\"/o0s4XsEDfDlvit5pDRKjzXR4pp2.jpg\",\"genre_ids\":[28,12,14,878],\"id\":19995,\"original_language\":\"en\",\"original_title\":\"Avatar\",\"overview\":\"In the 22nd century, a paraplegic Marine is dispatched to the moon Pandora on a unique mission, but becomes torn between following orders and protecting an alien civilization.\",\"popularity\":2041.691,\"poster_path\":\"/jRXYjXNq0Cs2TcJjLkki24MLp7u.jpg\",\"release_date\":\"2009-12-15\",\"title\":\"Avatar\",\"video\":false,\"vote_average\":7.6,\"vote_count\":27777},{\"adult\":false,\"backdrop_path\":\"/s16H6tpK2utvwDtzZ8Qy4qm5Emw.jpg\",\"genre_ids\":[878,12,28],\"id\":76600,\"original_language\":\"en\",\"original_title\":\"Avatar: The Way of Water\",\"overview\":\"Set more than a decade after the events of the first film, learn the story of the Sully family (Jake, Neytiri, and their kids), the trouble that follows them, the lengths they go to keep each other safe, the battles they fight to stay alive, and the tragedies they endure.\",\"popularity\":3948.296,\"poster_path\":\"/t6HIqrRAclMCA60NsSmeqe9RmNV.jpg\",\"release_date\":\"2022-12-14\",\"title\":\"Avatar: The Way of Water\",\"video\":false,\"vote_average\":7.7,\"vote_count\":4219},{\"adult\":false,\"backdrop_path\":\"/uEwGFGtao9YG2JolmdvtHLLVbA9.jpg\",\"genre_ids\":[99],\"id\":111332,\"original_language\":\"en\",\"original_title\":\"Avatar: Creating the World of Pandora\",\"overview\":\"The Making-of James Cameron's Avatar. It shows interesting parts of the work on the set.\",\"popularity\":541.809,\"poster_path\":\"/sjf3xjuofCtDhZghJRzXlTiEjJe.jpg\",\"release_date\":\"2010-02-07\",\"title\":\"Avatar: Creating the World of Pandora\",\"video\":false,\"vote_average\":7.3,\"vote_count\":35},{\"adult\":false,\"backdrop_path\":null,\"genre_ids\":[99],\"id\":287003,\"original_language\":\"en\",\"original_title\":\"Avatar: Scene Deconstruction\",\"overview\":\"The deconstruction of the Avatar scenes and sets\",\"popularity\":394.941,\"poster_path\":\"/uCreCQFReeF0RiIXkQypRYHwikx.jpg\",\"release_date\":\"2009-12-18\",\"title\":\"Avatar: Scene Deconstruction\",\"video\":false,\"vote_average\":7.8,\"vote_count\":12},{\"adult\":false,\"backdrop_path\":null,\"genre_ids\":[28,18,878,12,14],\"id\":83533,\"original_language\":\"en\",\"original_title\":\"Avatar 3\",\"overview\":\"\",\"popularity\":172.488,\"poster_path\":\"/4rXqTMlkEaMiJjiG0Z2BX6F6Dkm.jpg\",\"release_date\":\"2024-12-18\",\"title\":\"Avatar 3\",\"video\":false,\"vote_average\":0,\"vote_count\":0},{\"adult\":false,\"backdrop_path\":null,\"genre_ids\":[28,878,12,14],\"id\":216527,\"original_language\":\"en\",\"original_title\":\"Avatar 4\",\"overview\":\"\",\"popularity\":162.536,\"poster_path\":\"/qzMYKnT4MG1d0gnhwytr4cKhUvS.jpg\",\"release_date\":\"2026-12-16\",\"title\":\"Avatar 4\",\"video\":false,\"vote_average\":0,\"vote_count\":0},{\"adult\":false,\"backdrop_path\":null,\"genre_ids\":[28,12,14,878],\"id\":393209,\"original_language\":\"en\",\"original_title\":\"Avatar 5\",\"overview\":\"\",\"popularity\":124.722,\"poster_path\":\"/rtmmvqkIC5zDMEd638Es2woxbz8.jpg\",\"release_date\":\"2028-12-20\",\"title\":\"Avatar 5\",\"video\":false,\"vote_average\":0,\"vote_count\":0},{\"adult\":false,\"backdrop_path\":\"/nNceJtrrovG1MUBHMAhId0ws9Gp.jpg\",\"genre_ids\":[99],\"id\":183392,\"original_language\":\"en\",\"original_title\":\"Capturing Avatar\",\"overview\":\"Capturing Avatar is a feature length behind-the-scenes documentary about the making of Avatar. It uses footage from the film's development, as well as stock footage from as far back as the production of Titanic in 1995. Also included are numerous interviews with cast, artists, and other crew members. The documentary was released as a bonus feature on the extended collector's edition of Avatar.\",\"popularity\":109.842,\"poster_path\":\"/26SMEXJl3978dn2svWBSqHbLl5U.jpg\",\"release_date\":\"2010-11-16\",\"title\":\"Capturing Avatar\",\"video\":false,\"vote_average\":7.8,\"vote_count\":39},{\"adult\":false,\"backdrop_path\"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"' This response contains 57 movies related to the search query \"Avatar\". The first movie in the list is the 2009 movie \"Avatar\" starring Sam Worthington. Other movies in the list include sequels to Avatar, documentaries, and live performances.'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chain.run(\"Search for 'Avatar'\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Listen API Example"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from langchain.llms import OpenAI\n",
"from langchain.chains.api import podcast_docs\n",
"from langchain.chains import APIChain\n",
"\n",
"# Get api key here: https://www.listennotes.com/api/pricing/\n",
"listen_api_key = 'xxx'\n",
"\n",
"llm = OpenAI(temperature=0)\n",
"headers = {\"X-ListenAPI-Key\": listen_api_key}\n",
"chain = APIChain.from_llm_and_api_docs(llm, podcast_docs.PODCAST_DOCS, headers=headers, verbose=True)\n",
"chain.run(\"Search for 'silicon valley bank' podcast episodes, audio length is more than 30 minutes, return only 1 results\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": 4
}