{ "cells": [ { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# !pip install -q langchain\n", "# !pip install -q openai" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "notebookRunGroups": { "groupValue": "" } }, "outputs": [], "source": [ "from keys import openapi_key\n", "import os\n", "\n", "os.environ['OPENAI_API_KEY'] = openapi_key\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "Golden Dragon Palace \n" ] } ], "source": [ "from langchain_community.llms import OpenAI\n", "llm = OpenAI(temperature=0.6)\n", "name = llm.invoke(\"I want to open a restaurant for Chinese food. Suggest a fancy name for this.\")\n", "print(name)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'I want to open a restaurant for Mexican food. Suggest a fancy name for this.'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.prompts import PromptTemplate\n", "\n", "prompt_template_name = PromptTemplate(\n", " input_variables = [\"cuisine\"],\n", " template=\"I want to open a restaurant for {cuisine} food. Suggest a fancy name for this.\",\n", ")\n", "\n", "prompt_template_name.format(cuisine=\"Mexican\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/chong-u/mambaforge/envs/codebasics-langchain-crash-course/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n", " warn_deprecated(\n" ] }, { "data": { "text/plain": [ "'\\n\\n\"La Cantina de Sabores\" (The Flavor Cantina)'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.chains import LLMChain\n", "\n", "name_chain = LLMChain(llm=llm, prompt=prompt_template_name)\n", "name_chain.run(cuisine=\"Mexican\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sequential Chains" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "prompt_template_items = PromptTemplate(\n", " input_variables = [\"restaurant_name\"],\n", " template=\"Suggest some menu items for {restaurant_name}. Return it as a comma separated list.\",\n", ")\n", "food_items_chain = LLMChain(llm=llm, prompt=prompt_template_items)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "1. Chicken Tikka Masala\n", "2. Vegetable Samosas\n", "3. Lamb Vindaloo\n", "4. Palak Paneer\n", "5. Tandoori Chicken\n", "6. Garlic Naan\n", "7. Chana Masala\n", "8. Aloo Gobi\n", "9. Butter Chicken\n", "10. Mango Lassi \n", "\n" ] } ], "source": [ "from langchain.chains import SimpleSequentialChain\n", "\n", "chain = SimpleSequentialChain(chains=[name_chain, food_items_chain])\n", "response = chain.run(\"Indian\")\n", "print(response)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sequential Chain" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "llm = OpenAI(temperature=0.7)\n", "\n", "prompt_template_name = PromptTemplate(\n", " input_variables=[\"cuisine\"],\n", " template = \"I want to open a restaurant for {cuisine} food. Suggest a fancy name for this.\",\n", ")\n", "\n", "name_chain = LLMChain(llm=llm, prompt=prompt_template_name, output_key=\"restaurant_name\")\n", "\n", "prompt_template_items = PromptTemplate(\n", " input_variables=[\"restaurant_name\"],\n", " template = \"Suggest some menu items for {restaurant_name}. Return the menu items as a single, comma separated string with no additional preamble.\",\n", ")\n", "\n", "food_items_chain = LLMChain(llm=llm, prompt=prompt_template_items, output_key=\"menu_items\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/chong-u/mambaforge/envs/codebasics-langchain-crash-course/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n", " warn_deprecated(\n" ] }, { "data": { "text/plain": [ "{'cuisine': 'Singaporean',\n", " 'restaurant_name': '\\n\\n\"Singapore Spice Emporium\"',\n", " 'menu_items': '\\n\\n\"Nasi Lemak, Hainanese Chicken Rice, Laksa, Chili Crab, Char Kway Teow, Satay, Rojak, Bak Kut Teh, Curry Puffs, Teh Tarik\"'}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.chains import SequentialChain\n", "\n", "chain = SequentialChain(\n", " chains=[name_chain, food_items_chain], \n", " input_variables=[\"cuisine\"], \n", " output_variables = [\"restaurant_name\", \"menu_items\"]\n", ")\n", "\n", "chain.invoke({\"cuisine\": \"Singaporean\"})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "codebasics-langchain-crash-course", "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.13" } }, "nbformat": 4, "nbformat_minor": 2 }