{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c7ad998d",
   "metadata": {},
   "source": [
    "# Natural Language APIs\n",
    "\n",
    "`Natural Language API` Toolkits (`NLAToolkits`) permit LangChain Agents to efficiently plan and combine calls across endpoints. \n",
    "\n",
    "This notebook demonstrates a sample composition of the `Speak`, `Klarna`, and `Spoonacluar` APIs.\n",
    "\n",
    "For a detailed walkthrough of the OpenAPI chains wrapped within the NLAToolkit, see the [OpenAPI Operation Chain](/docs/use_cases/apis/openapi.html) notebook.\n",
    "\n",
    "### First, import dependencies and load the LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6593f793",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from typing import List, Optional\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.llms import OpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.requests import Requests\n",
    "from langchain.tools import APIOperation, OpenAPISpec\n",
    "from langchain.agents import AgentType, Tool, initialize_agent\n",
    "from langchain.agents.agent_toolkits import NLAToolkit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dd720860",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Select the LLM to use. Here, we use text-davinci-003\n",
    "llm = OpenAI(\n",
    "    temperature=0, max_tokens=700\n",
    ")  # You can swap between different core LLM's here."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cadac9d",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Next, load the Natural Language API Toolkits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6b208ab0",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Attempting to load an OpenAPI 3.0.1 spec.  This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support.\n",
      "Attempting to load an OpenAPI 3.0.1 spec.  This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support.\n",
      "Attempting to load an OpenAPI 3.0.1 spec.  This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support.\n"
     ]
    }
   ],
   "source": [
    "speak_toolkit = NLAToolkit.from_llm_and_url(llm, \"https://api.speak.com/openapi.yaml\")\n",
    "klarna_toolkit = NLAToolkit.from_llm_and_url(\n",
    "    llm, \"https://www.klarna.com/us/shopping/public/openai/v0/api-docs/\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16c7336f",
   "metadata": {},
   "source": [
    "### Create the Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "730a0dc2-b4d0-46d5-a1e9-583803220973",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Slightly tweak the instructions from the default agent\n",
    "openapi_format_instructions = \"\"\"Use the following format:\n",
    "\n",
    "Question: the input question you must answer\n",
    "Thought: you should always think about what to do\n",
    "Action: the action to take, should be one of [{tool_names}]\n",
    "Action Input: what to instruct the AI Action representative.\n",
    "Observation: The Agent's response\n",
    "... (this Thought/Action/Action Input/Observation can repeat N times)\n",
    "Thought: I now know the final answer. User can't see any of my observations, API responses, links, or tools.\n",
    "Final Answer: the final answer to the original input question with the right amount of detail\n",
    "\n",
    "When responding with your Final Answer, remember that the person you are responding to CANNOT see any of your Thought/Action/Action Input/Observations, so if there is any relevant information there you need to include it explicitly in your response.\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "40a979c3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "natural_language_tools = speak_toolkit.get_tools() + klarna_toolkit.get_tools()\n",
    "mrkl = initialize_agent(\n",
    "    natural_language_tools,\n",
    "    llm,\n",
    "    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
    "    verbose=True,\n",
    "    agent_kwargs={\"format_instructions\": openapi_format_instructions},\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "794380ba",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m I need to find out what kind of Italian clothes are available\n",
      "Action: Open_AI_Klarna_product_Api.productsUsingGET\n",
      "Action Input: Italian clothes\u001b[0m\n",
      "Observation: \u001b[31;1m\u001b[1;3mThe API response contains two products from the Alé brand in Italian Blue. The first is the Alé Colour Block Short Sleeve Jersey Men - Italian Blue, which costs $86.49, and the second is the Alé Dolid Flash Jersey Men - Italian Blue, which costs $40.00.\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m I now know what kind of Italian clothes are available and how much they cost.\n",
      "Final Answer: You can buy two products from the Alé brand in Italian Blue for your end of year party. The Alé Colour Block Short Sleeve Jersey Men - Italian Blue costs $86.49, and the Alé Dolid Flash Jersey Men - Italian Blue costs $40.00.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'You can buy two products from the Alé brand in Italian Blue for your end of year party. The Alé Colour Block Short Sleeve Jersey Men - Italian Blue costs $86.49, and the Alé Dolid Flash Jersey Men - Italian Blue costs $40.00.'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mrkl.run(\n",
    "    \"I have an end of year party for my Italian class and have to buy some Italian clothes for it\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c61d92a8",
   "metadata": {},
   "source": [
    "### Use Auth and add more Endpoints\n",
    "\n",
    "Some endpoints may require user authentication via things like access tokens. Here we show how to pass in the authentication information via the `Requests` wrapper object.\n",
    "\n",
    "Since each NLATool exposes a concisee natural language interface to its wrapped API, the top level conversational agent has an easier job incorporating each endpoint to satisfy a user's request."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0d132cc",
   "metadata": {},
   "source": [
    "**Adding the Spoonacular endpoints.**\n",
    "\n",
    "1. Go to the [Spoonacular API Console](https://spoonacular.com/food-api/console#Profile) and make a free account.\n",
    "2. Click on `Profile` and copy your API key below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c2368b9c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "spoonacular_api_key = \"\"  # Copy from the API Console"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fbd97c28-fef6-41b5-9600-a9611a32bfb3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Attempting to load an OpenAPI 3.0.0 spec.  This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support.\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Accept. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Accept. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Accept. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Accept. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Accept. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Accept. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Accept. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Accept. Valid values are ['path', 'query'] Ignoring optional parameter\n",
      "Unsupported APIPropertyLocation \"header\" for parameter Content-Type. Valid values are ['path', 'query'] Ignoring optional parameter\n"
     ]
    }
   ],
   "source": [
    "requests = Requests(headers={\"x-api-key\": spoonacular_api_key})\n",
    "spoonacular_toolkit = NLAToolkit.from_llm_and_url(\n",
    "    llm,\n",
    "    \"https://spoonacular.com/application/frontend/downloads/spoonacular-openapi-3.json\",\n",
    "    requests=requests,\n",
    "    max_text_length=1800,  # If you want to truncate the response text\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "81a6edac",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "34 tools loaded.\n"
     ]
    }
   ],
   "source": [
    "natural_language_api_tools = (\n",
    "    speak_toolkit.get_tools()\n",
    "    + klarna_toolkit.get_tools()\n",
    "    + spoonacular_toolkit.get_tools()[:30]\n",
    ")\n",
    "print(f\"{len(natural_language_api_tools)} tools loaded.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "831f772d-5cd1-4467-b494-a3172af2ff48",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Create an agent with the new tools\n",
    "mrkl = initialize_agent(\n",
    "    natural_language_api_tools,\n",
    "    llm,\n",
    "    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
    "    verbose=True,\n",
    "    agent_kwargs={\"format_instructions\": openapi_format_instructions},\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0385e04b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Make the query more complex!\n",
    "user_input = (\n",
    "    \"I'm learning Italian, and my language class is having an end of year party... \"\n",
    "    \" Could you help me find an Italian outfit to wear and\"\n",
    "    \" an appropriate recipe to prepare so I can present for the class in Italian?\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6ebd3f55",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m I need to find a recipe and an outfit that is Italian-themed.\n",
      "Action: spoonacular_API.searchRecipes\n",
      "Action Input: Italian\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3mThe API response contains 10 Italian recipes, including Turkey Tomato Cheese Pizza, Broccolini Quinoa Pilaf, Bruschetta Style Pork & Pasta, Salmon Quinoa Risotto, Italian Tuna Pasta, Roasted Brussels Sprouts With Garlic, Asparagus Lemon Risotto, Italian Steamed Artichokes, Crispy Italian Cauliflower Poppers Appetizer, and Pappa Al Pomodoro.\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m I need to find an Italian-themed outfit.\n",
      "Action: Open_AI_Klarna_product_Api.productsUsingGET\n",
      "Action Input: Italian\u001b[0m\n",
      "Observation: \u001b[31;1m\u001b[1;3mI found 10 products related to 'Italian' in the API response. These products include Italian Gold Sparkle Perfectina Necklace - Gold, Italian Design Miami Cuban Link Chain Necklace - Gold, Italian Gold Miami Cuban Link Chain Necklace - Gold, Italian Gold Herringbone Necklace - Gold, Italian Gold Claddagh Ring - Gold, Italian Gold Herringbone Chain Necklace - Gold, Garmin QuickFit 22mm Italian Vacchetta Leather Band, Macy's Italian Horn Charm - Gold, Dolce & Gabbana Light Blue Italian Love Pour Homme EdT 1.7 fl oz.\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
      "Final Answer: To present for your Italian language class, you could wear an Italian Gold Sparkle Perfectina Necklace - Gold, an Italian Design Miami Cuban Link Chain Necklace - Gold, or an Italian Gold Miami Cuban Link Chain Necklace - Gold. For a recipe, you could make Turkey Tomato Cheese Pizza, Broccolini Quinoa Pilaf, Bruschetta Style Pork & Pasta, Salmon Quinoa Risotto, Italian Tuna Pasta, Roasted Brussels Sprouts With Garlic, Asparagus Lemon Risotto, Italian Steamed Artichokes, Crispy Italian Cauliflower Poppers Appetizer, or Pappa Al Pomodoro.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'To present for your Italian language class, you could wear an Italian Gold Sparkle Perfectina Necklace - Gold, an Italian Design Miami Cuban Link Chain Necklace - Gold, or an Italian Gold Miami Cuban Link Chain Necklace - Gold. For a recipe, you could make Turkey Tomato Cheese Pizza, Broccolini Quinoa Pilaf, Bruschetta Style Pork & Pasta, Salmon Quinoa Risotto, Italian Tuna Pasta, Roasted Brussels Sprouts With Garlic, Asparagus Lemon Risotto, Italian Steamed Artichokes, Crispy Italian Cauliflower Poppers Appetizer, or Pappa Al Pomodoro.'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mrkl.run(user_input)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2959462",
   "metadata": {},
   "source": [
    "## Thank you!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6fcda5f0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"In Italian, you can say 'Buon appetito' to someone to wish them to enjoy their meal. This phrase is commonly used in Italy when someone is about to eat, often at the beginning of a meal. It's similar to saying 'Bon appétit' in French or 'Guten Appetit' in German.\""
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "natural_language_api_tools[1].run(\n",
    "    \"Tell the LangChain audience to 'enjoy the meal' in Italian, please!\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab366dc0",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
