mirror of
https://github.com/hwchase17/langchain
synced 2024-10-29 17:07:25 +00:00
207 lines
7.2 KiB
Plaintext
207 lines
7.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e734b314",
|
|
"metadata": {},
|
|
"source": [
|
|
"# OpenAPI calls with OpenAI functions\n",
|
|
"\n",
|
|
"In this notebook we'll show how to create a chain that automatically makes calls to an API based only on an OpenAPI spec. Under the hood, we're parsing the OpenAPI spec into a JSON schema that the OpenAI functions API can handle. This allows ChatGPT to automatically select and populate the relevant API call to make for any user input. Using the output of ChatGPT we then make the actual API call, and return the result."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a95f510a",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Query Klarna"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "08e19b64",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.chains.openai_functions.openapi import get_openapi_chain\n",
|
|
"\n",
|
|
"chain = get_openapi_chain(\"https://www.klarna.com/us/shopping/public/openai/v0/api-docs/\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "3959f866",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'products': [{'name': \"Tommy Hilfiger Men's Short Sleeve Button-Down Shirt\",\n",
|
|
" 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3204878580/Clothing/Tommy-Hilfiger-Men-s-Short-Sleeve-Button-Down-Shirt/?utm_source=openai&ref-site=openai_plugin',\n",
|
|
" 'price': '$26.78',\n",
|
|
" 'attributes': ['Material:Linen,Cotton',\n",
|
|
" 'Target Group:Man',\n",
|
|
" 'Color:Gray,Pink,White,Blue,Beige,Black,Turquoise',\n",
|
|
" 'Size:S,XL,M,XXL']},\n",
|
|
" {'name': \"Van Heusen Men's Long Sleeve Button-Down Shirt\",\n",
|
|
" 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3201809514/Clothing/Van-Heusen-Men-s-Long-Sleeve-Button-Down-Shirt/?utm_source=openai&ref-site=openai_plugin',\n",
|
|
" 'price': '$18.89',\n",
|
|
" 'attributes': ['Material:Cotton',\n",
|
|
" 'Target Group:Man',\n",
|
|
" 'Color:Red,Gray,White,Blue',\n",
|
|
" 'Size:XL,XXL']},\n",
|
|
" {'name': 'Brixton Bowery Flannel Shirt',\n",
|
|
" 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3202331096/Clothing/Brixton-Bowery-Flannel-Shirt/?utm_source=openai&ref-site=openai_plugin',\n",
|
|
" 'price': '$34.48',\n",
|
|
" 'attributes': ['Material:Cotton',\n",
|
|
" 'Target Group:Man',\n",
|
|
" 'Color:Gray,Blue,Black,Orange',\n",
|
|
" 'Size:XL,3XL,4XL,5XL,L,M,XXL']},\n",
|
|
" {'name': 'Cubavera Four Pocket Guayabera Shirt',\n",
|
|
" 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3202055522/Clothing/Cubavera-Four-Pocket-Guayabera-Shirt/?utm_source=openai&ref-site=openai_plugin',\n",
|
|
" 'price': '$23.22',\n",
|
|
" 'attributes': ['Material:Polyester,Cotton',\n",
|
|
" 'Target Group:Man',\n",
|
|
" 'Color:Red,White,Blue,Black',\n",
|
|
" 'Size:S,XL,L,M,XXL']},\n",
|
|
" {'name': 'Theory Sylvain Shirt - Eclipse',\n",
|
|
" 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3202028254/Clothing/Theory-Sylvain-Shirt-Eclipse/?utm_source=openai&ref-site=openai_plugin',\n",
|
|
" 'price': '$86.01',\n",
|
|
" 'attributes': ['Material:Polyester,Cotton',\n",
|
|
" 'Target Group:Man',\n",
|
|
" 'Color:Blue',\n",
|
|
" 'Size:S,XL,XS,L,M,XXL']}]}"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"chain.run(\"What are some options for a men's large blue button down shirt\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6f648c77",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Query a translation service"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "bf6cd695",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"chain = get_openapi_chain(\"https://api.speak.com/openapi.yaml\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "1ba51609",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'explanation': '<translation language=\"None\" context=\"None\">\\nNone\\n</translation>\\n\\n<alternatives context=\"None\">\\n1. \"N/A\" *(Formal - used in professional settings to indicate that the answer is not applicable)*\\n2. \"I don\\'t have an answer for that\" *(Neutral - commonly used when one does not know the answer to a question)*\\n3. \"I\\'m not sure\" *(Neutral - similar to the above alternative, used when one is unsure of the answer)*\\n</alternatives>\\n\\n<example-convo language=\"None\">\\n<context>None</context>\\n* Tom: \"Do you know what time the concert starts?\"\\n* Sarah: \"I\\'m sorry, I don\\'t have an answer for that.\"\\n</example-convo>\\n\\n*[Report an issue or leave feedback](https://speak.com/chatgpt?rid=p8i6p14duafpctg4ve7tm48z})*',\n",
|
|
" 'extra_response_instructions': 'Use all information in the API response and fully render all Markdown.\\nAlways end your response with a link to report an issue or leave feedback on the plugin.'}"
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"chain.run(\"How would you say no thanks in Russian\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "4923a291",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Query XKCD"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "a9198f62",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"chain = get_openapi_chain(\"https://gist.githubusercontent.com/roaldnefs/053e505b2b7a807290908fe9aa3e1f00/raw/0a212622ebfef501163f91e23803552411ed00e4/openapi.yaml\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "3110c398",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 1.0 seconds as it raised ServiceUnavailableError: The server is overloaded or not ready yet..\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'month': '6',\n",
|
|
" 'num': 2793,\n",
|
|
" 'link': '',\n",
|
|
" 'year': '2023',\n",
|
|
" 'news': '',\n",
|
|
" 'safe_title': 'Garden Path Sentence',\n",
|
|
" 'transcript': '',\n",
|
|
" 'alt': 'Arboretum Owner Denied Standing in Garden Path Suit on Grounds Grounds Appealing Appealing',\n",
|
|
" 'img': 'https://imgs.xkcd.com/comics/garden_path_sentence.png',\n",
|
|
" 'title': 'Garden Path Sentence',\n",
|
|
" 'day': '23'}"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"chain.run(\"What's today's comic?\")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "venv",
|
|
"language": "python",
|
|
"name": "venv"
|
|
},
|
|
"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.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|