{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "70e7ed53-d278-4529-b4fe-c817ac7c2e27",
   "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": "7979c7f3-a3f6-45ff-b407-dd4574273106",
   "metadata": {},
   "source": [
    "## Models"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "450f8714-2de3-460c-8239-16dfa5539b4f",
   "metadata": {},
   "source": [
    "Langchain provides interfaces and integrations for two types of language models:\n",
    "1. LLMs: input a string of text, output a string of text.\n",
    "2. Chat Models: input a chat message, output a chat message."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ba8cf3a-2850-40c4-8a96-2c507cd87b94",
   "metadata": {},
   "source": [
    "**Example of LLM: OpenAI LLM model**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "60a1eb0e-2eb4-4c5a-a791-aec5a88aec32",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "48d40937-4f04-461d-88f9-2a974b6264f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_llm = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f305167d-41c6-4a16-970e-2704d4ad95ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"Name the most popular 4 U.S. presidents\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9e4c8210-94ff-499a-9600-8f702fe2c1aa",
   "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 `__call__` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    }
   ],
   "source": [
    "response = my_llm(question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cc5ff391-5cf6-44c3-ab78-a315b7c67635",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "1. George Washington\n",
      "2. Abraham Lincoln\n",
      "3. Franklin D. Roosevelt\n",
      "4. John F. Kennedy\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "78aa4af7-ea39-42fd-8f30-ee96e4c47620",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(type(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "df12c8c3-b182-4f6a-90b3-03a5e2690579",
   "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 `predict` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    }
   ],
   "source": [
    "response_variation_1 = my_llm.predict(question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "062822e2-2a12-45d2-8008-f2cdb08a9406",
   "metadata": {},
   "outputs": [],
   "source": [
    "response_variation_2 = my_llm.invoke(question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "544cc1dc-cbc2-4cf9-b635-492c9d672625",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "1. George Washington\n",
      "2. Abraham Lincoln\n",
      "3. Franklin D. Roosevelt\n",
      "4. John F. Kennedy\n"
     ]
    }
   ],
   "source": [
    "print(response_variation_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "74122383-c48f-4bf7-8163-41b5df0496b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "1. George Washington\n",
      "2. Abraham Lincoln\n",
      "3. Franklin D. Roosevelt\n",
      "4. John F. Kennedy\n"
     ]
    }
   ],
   "source": [
    "print(response_variation_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a4f16a3-594d-40e9-8a1e-1cf4a0e218f4",
   "metadata": {},
   "source": [
    "**Example of Chat Model: OpenAI Chat Model**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d94e725f-f380-4efc-8717-c27e095ad73b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.schema import HumanMessage, SystemMessage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9c88000e-df39-4ff4-a0ae-cb7178664126",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_chat = ChatOpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b1cbb627-1f41-4760-a9bf-fb6928f16c99",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat_question = [\n",
    "    SystemMessage(\n",
    "        content=\"You are a helpful and concise assistant\"\n",
    "    ),\n",
    "    HumanMessage(\n",
    "        content=\"Name the most popular 4 U.S. First Ladies\"\n",
    "    )\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7ae1e204-c459-4818-83e4-0c0d50c6acd3",
   "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 `__call__` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    }
   ],
   "source": [
    "chat_response = my_chat(chat_question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cafaa8c7-2efb-4761-bee2-33bfeb0771f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content=\"The most popular four U.S. First Ladies, based on public opinion and historical recognition, are:\\n\\n1. Jacqueline Kennedy: Known for her elegance, style, and preservation of cultural heritage. She played a significant role in the restoration of the White House and was admired for her grace during her time as First Lady from 1961 to 1963.\\n\\n2. Eleanor Roosevelt: A champion of social justice and human rights. She was highly influential and active during her tenure from 1933 to 1945, advocating for civil rights, women's rights, and international diplomacy.\\n\\n3. Michelle Obama: Known for her initiatives to promote healthy living and education. As First Lady from 2009 to 2017, she became a role model for many, working to combat childhood obesity and supporting military families.\\n\\n4. Barbara Bush: Respected for her wit, compassion, and dedication to literacy. She served as First Lady from 1989 to 1993 and focused on promoting literacy and education during her time in the White House.\"\n"
     ]
    }
   ],
   "source": [
    "print(chat_response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8aeb9092-69f0-4f3c-9c24-96a09c333934",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'langchain_core.messages.ai.AIMessage'>\n"
     ]
    }
   ],
   "source": [
    "print(type(chat_response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4602f560-374e-4468-8918-16a1e62b8506",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The most popular four U.S. First Ladies, based on public opinion and historical recognition, are:\n",
      "\n",
      "1. Jacqueline Kennedy: Known for her elegance, style, and preservation of cultural heritage. She played a significant role in the restoration of the White House and was admired for her grace during her time as First Lady from 1961 to 1963.\n",
      "\n",
      "2. Eleanor Roosevelt: A champion of social justice and human rights. She was highly influential and active during her tenure from 1933 to 1945, advocating for civil rights, women's rights, and international diplomacy.\n",
      "\n",
      "3. Michelle Obama: Known for her initiatives to promote healthy living and education. As First Lady from 2009 to 2017, she became a role model for many, working to combat childhood obesity and supporting military families.\n",
      "\n",
      "4. Barbara Bush: Respected for her wit, compassion, and dedication to literacy. She served as First Lady from 1989 to 1993 and focused on promoting literacy and education during her time in the White House.\n"
     ]
    }
   ],
   "source": [
    "print(chat_response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c1514048-9527-4ccd-a466-3765a611181e",
   "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 `predict_messages` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    }
   ],
   "source": [
    "chat_response_variation_1 = my_chat.predict_messages(chat_question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b56b3033-7e3a-4264-84e2-f7c06f3fed2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat_response_variation_2 = my_chat.invoke(chat_question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b2c268b5-1c89-4d9b-bd41-6617479ce568",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='The most popular four U.S. First Ladies are:\\n\\n1. Eleanor Roosevelt\\n2. Jacqueline Kennedy\\n3. Michelle Obama\\n4. Barbara Bush'\n"
     ]
    }
   ],
   "source": [
    "print(chat_response_variation_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ca73d7f5-43b0-44fe-bf3e-207e0d290f69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='The most popular four U.S. First Ladies are:\\n\\n1. Jacqueline Kennedy Onassis\\n2. Michelle Obama\\n3. Eleanor Roosevelt\\n4. Barbara Bush'\n"
     ]
    }
   ],
   "source": [
    "print(chat_response_variation_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06914e6b-bfaf-42d7-95c3-874cf9bc1ea1",
   "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
}
