{
 "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": [
    "## Chains: 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 import LLMChain"
   ]
  },
  {
   "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",
    "    \"Who won the soccer world cup of {year}?\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "770dea7d-6fab-4517-9e7c-35ecfb0455cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The soccer World Cup of 2010 was won by Spain.'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain = LLMChain(llm=llm, prompt=prompt)\n",
    "chain.predict(year=\"2010\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0d3a0e7-5e9f-4698-92f9-981ff18f4972",
   "metadata": {},
   "source": [
    "## Chains: New LCEL Way"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e97565a9-7301-47e8-9279-ea2cb0e9cd58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='The soccer world cup of 2010 was won by Spain.')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_chain = prompt | llm\n",
    "new_chain.invoke({\"year\": \"2010\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa6c8436-7d64-4033-86a1-033d3f6bdf4c",
   "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
}
