{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9263bba4-f318-4ce3-b887-8d5abe94f935",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "_ = load_dotenv(find_dotenv())\n",
    "openai_api_key = os.environ[\"OPENAI_API_KEY\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f85e0e2-a83a-4627-970c-41ae335d80a4",
   "metadata": {},
   "source": [
    "## OpenAI Functions: Classic Way"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "07b8b36c-550e-41a1-afc2-8cc20cf50f22",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import ChatPromptTemplate\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.chains.openai_functions import create_openai_fn_chain\n",
    "from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c6d48f0f-8534-4ee2-8491-4fda461c4a14",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatOpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f9bf923b-e1ca-4f88-86d4-2bef5402cb8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = ChatPromptTemplate.from_template(\n",
    "    \"Tell me the name of the captain of the team who won the soccer world cup of {year}?\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "951e76e4-2554-4713-b3b4-59e9305f4248",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_parser = JsonOutputFunctionsParser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f328c185-4b21-4068-bbb0-ab8eddd98eb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "functions = [\n",
    "    {\n",
    "      \"name\": \"player_full_name\",\n",
    "      \"description\": \"Name and lastname of the player\",\n",
    "      \"parameters\": {\n",
    "        \"type\": \"object\",\n",
    "        \"properties\": {\n",
    "          \"name\": {\n",
    "            \"type\": \"string\",\n",
    "            \"description\": \"The first name of the player\"\n",
    "          },\n",
    "          \"lastname\": {\n",
    "            \"type\": \"string\",\n",
    "            \"description\": \"The lastname of the player\"\n",
    "          }\n",
    "        },\n",
    "        \"required\": [\"name\", \"lastname\"]\n",
    "      }\n",
    "    }\n",
    "  ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "770dea7d-6fab-4517-9e7c-35ecfb0455cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/juliocolomer/.pyenv/versions/3.11.4/envs/venv020124/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `create_openai_fn_chain` was deprecated in LangChain 0.1.1 and will be removed in 0.2.0. Use create_openai_fn_runnable instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'name': 'Iker', 'lastname': 'Casillas'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain = create_openai_fn_chain(\n",
    "    functions=functions,\n",
    "    llm=llm, \n",
    "    prompt=prompt,\n",
    "    output_parser=output_parser\n",
    ")\n",
    "chain.predict(year=\"2010\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0d3a0e7-5e9f-4698-92f9-981ff18f4972",
   "metadata": {},
   "source": [
    "## OpenAI Functions: New LCEL Way"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e97565a9-7301-47e8-9279-ea2cb0e9cd58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Iker', 'lastname': 'Casillas'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_chain = (\n",
    "    prompt \n",
    "    | llm.bind(function_call={\"name\": \"player_full_name\"}, functions=functions)\n",
    "    | JsonOutputFunctionsParser()\n",
    ")\n",
    "new_chain.invoke({\"year\": \"2010\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d311fa0-5000-4c8b-9f29-cede371a2910",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
