{ "cells": [ { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# !pip install -q langchain\n", "# !pip install -q openai" ] }, { "cell_type": "code", "execution_count": 7, "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": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\"Imperial Dragon Palace\"\n" ] } ], "source": [ "from langchain.llms import OpenAI\n", "llm = OpenAI(temperature=0.6)\n", "name = llm(\"I want to open a restaurant for Chinese food. Suggest a fancy name for this.\")\n", "print(name)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'I want to open a restaurant for Mexican food. Suggest a fancy name for this.'" ] }, "execution_count": 10, "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": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\n\\n\"La Cantina de Oro\"'" ] }, "execution_count": 15, "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": 19, "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": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "1. Spicy Chicken Curry\n", "2. Lamb Vindaloo\n", "3. Vegetable Samosas\n", "4. Tandoori Shrimp\n", "5. Naan Bread\n", "6. Chicken Tikka Masala\n", "7. Aloo Gobi (Potato and Cauliflower Curry)\n", "8. Palak Paneer (Spinach and Cheese Curry)\n", "9. Mango Lassi\n", "10. Biryani Rice\n", "11. Chana Masala (Chickpea Curry)\n", "12. Chicken Korma\n", "13. Garlic Naan\n", "14. Masala Dosa (Stuffed Crepe)\n", "15. Gulab Jamun (Sweet Fried Dumplings)\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": 24, "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}.\",\n", ")\n", "\n", "food_items_chain = LLMChain(llm=llm, prompt=prompt_template_items, output_key=\"menu_items\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'cuisine': 'Singaporean',\n", " 'restaurant_name': '\\n\\n\"Singapore Spice House\"',\n", " 'menu_items': '\\n\\n1. Singaporean Chili Crab\\n2. Hainanese Chicken Rice\\n3. Laksa Soup\\n4. Char Kway Teow (Stir-fried noodles with shrimp, egg, and sausage)\\n5. Satay Skewers (grilled marinated meat on skewers)\\n6. Nasi Lemak (coconut rice dish with fried chicken, fried anchovies, peanuts, and sambal chili sauce)\\n7. Roti Prata (Indian-style flatbread served with curry)\\n8. Bak Kut Teh (pork rib soup with herbs and spices)\\n9. Murtabak (stuffed pancake with chicken or mutton)\\n10. Singaporean-style Fried Rice\\n11. Beef Rendang (spicy braised beef in coconut milk)\\n12. Chilli Prawns\\n13. Gado Gado (Indonesian salad with peanut sauce)\\n14. Sambal Stingray (grilled stingray with spicy sambal sauce)\\n15. Biryani Rice (fragrant rice dish with spices and choice of meat)'}" ] }, "execution_count": 25, "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({\"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 }