{ "cells": [ { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# !pip install -q langchain==0.1.9\n", "# !pip install -q openai==1.13.3" ] }, { "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": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'cuisine': 'Singaporean',\n", " 'restaurant_name': '\\n\"Straits Flavors\"',\n", " 'menu_items': '\\n\\nSambal Shrimp, Roti Canai, Nasi Lemak, Beef Rendang, Satay Skewers, Hainanese Chicken Rice, Char Kway Teow, Laksa Soup, Rojak Salad, Curry Puffs'}" ] }, "execution_count": 13, "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": "markdown", "metadata": {}, "source": [ "## Agents" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "!pip install -q wikipedia\n", "!pip install -q numexpr # for llm-math" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "notebookRunGroups": { "groupValue": "1" } }, "outputs": [], "source": [ "from langchain.agents import AgentType, initialize_agent, load_tools\n", "from langchain.llms import OpenAI" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "tools = load_tools([\"wikipedia\", \"llm-math\"], llm=llm)\n", "\n", "initialize_agent(\n", " tools,\n", " llm,\n", " agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n", ")" ] }, { "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 }