diff --git a/lab-chains-in-langchain.ipynb b/lab-chains-in-langchain.ipynb index 290f3bb..71c724c 100644 --- a/lab-chains-in-langchain.ipynb +++ b/lab-chains-in-langchain.ipynb @@ -59,7 +59,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", "metadata": { "tags": [] @@ -67,7 +67,7 @@ "outputs": [], "source": [ "import pandas as pd\n", - "df = pd.read_csv('./lab/data/Data.csv')" + "df = pd.read_csv('./data/Data.csv')\n" ] }, { @@ -139,7 +139,7 @@ "1 Waterproof Phone Pouch I loved the waterproof sac, although the openi...\n", "2 Luxury Air Mattress This mattress had a small hole in the top of i...\n", "3 Pillows Insert This is the best throw pillow fillers on Amazo...\n", - "4 Milk Frother Handheld\\n  I loved this product. But they only seem to l..." + "4 Milk Frother Handheld\\n \u00a0I loved this product. But they only seem to l..." ] }, "execution_count": 15, @@ -190,8 +190,9 @@ "metadata": {}, "outputs": [], "source": [ - "#Replace None by your own value and justify\n", - "llm = ChatOpenAI(temperature=None)\n" + "# Using temperature=0.0 for deterministic, factual product descriptions.\n", + "# A low temperature avoids creative variance so the description stays focused on attributes.\n", + "llm = ChatOpenAI(temperature=0.0)\n" ] }, { @@ -201,9 +202,9 @@ "metadata": {}, "outputs": [], "source": [ - "prompt = ChatPromptTemplate.from_template( #Write a query that would take a variable to describe any product\n", - " \n", - ")" + "prompt = ChatPromptTemplate.from_template(\n", + " \"Write a concise marketing description (2-3 sentences) for the following product: {product}\"\n", + ")\n" ] }, { @@ -224,8 +225,8 @@ "metadata": {}, "outputs": [], "source": [ - "product = #Select a product type to be describe\n", - "chain.run(product)" + "product = df.Product[0] # pick the first product from the dataset\n", + "chain.run(product)\n" ] }, { @@ -255,13 +256,13 @@ "source": [ "llm = ChatOpenAI(temperature=0.9)\n", "\n", - "# prompt template 1\n", + "# prompt template 1: suggest a company name that could make the given product\n", "first_prompt = ChatPromptTemplate.from_template(\n", - " #Repeat the initial query or create a new query that would feed into the second prompt\n", + " \"What is a good, catchy company name for a business that makes {product}?\"\n", ")\n", "\n", "# Chain 1\n", - "chain_one = LLMChain(llm=llm, prompt=first_prompt)" + "chain_one = LLMChain(llm=llm, prompt=first_prompt)\n" ] }, { @@ -272,12 +273,12 @@ "outputs": [], "source": [ "\n", - "# prompt template 2\n", + "# prompt template 2: take the company name from chain 1 and produce a short description\n", "second_prompt = ChatPromptTemplate.from_template(\n", - " #Write the second prompt query that takes an input variable whose input will come from the previous prompt\"\n", + " \"Write a 20-word marketing description for the following company: {company_name}\"\n", ")\n", "# chain 2\n", - "chain_two = LLMChain(llm=llm, prompt=second_prompt)" + "chain_two = LLMChain(llm=llm, prompt=second_prompt)\n" ] }, { @@ -310,6 +311,28 @@ "**Repeat the above twice for different products**" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Repeat #1: different product\n", + "product_2 = df.Product[1]\n", + "overall_simple_chain.run(product_2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Repeat #2: different product\n", + "product_3 = df.Product[2]\n", + "overall_simple_chain.run(product_3)\n" + ] + }, { "cell_type": "markdown", "id": "7b5ce18c", @@ -339,11 +362,11 @@ "\n", "\n", "first_prompt = ChatPromptTemplate.from_template(\n", - " #This prompt should translate a review\n", + " \"Translate the following review to English:\\n\\n{Review}\"\n", ")\n", "\n", - "chain_one = LLMChain(llm=llm, prompt=first_prompt, \n", - " output_key=None #Give a name to your output\n", + "chain_one = LLMChain(llm=llm, prompt=first_prompt,\n", + " output_key=\"English_Review\"\n", " )\n" ] }, @@ -355,11 +378,11 @@ "outputs": [], "source": [ "second_prompt = ChatPromptTemplate.from_template(\n", - " #Write a promplt to summarize a review\n", + " \"Summarize the following review in one short sentence:\\n\\n{English_Review}\"\n", ")\n", "\n", - "chain_two = LLMChain(llm=llm, prompt=second_prompt, \n", - " output_key=None #give a name to this output\n", + "chain_two = LLMChain(llm=llm, prompt=second_prompt,\n", + " output_key=\"summary\"\n", " )\n" ] }, @@ -370,13 +393,13 @@ "metadata": {}, "outputs": [], "source": [ - "# prompt template 3: translate to english or other language\n", + "# prompt template 3: detect the language of the original review\n", "third_prompt = ChatPromptTemplate.from_template(\n", - " None\n", + " \"What language is the following review written in? Respond with only the language name.\\n\\n{Review}\"\n", ")\n", "# chain 3: input= Review and output= language\n", "chain_three = LLMChain(llm=llm, prompt=third_prompt,\n", - " output_key=None\n", + " output_key=\"language\"\n", " )\n" ] }, @@ -388,12 +411,13 @@ "outputs": [], "source": [ "\n", - "# prompt template 4: follow up message that take as inputs the two previous prompts' variables\n", + "# prompt template 4: follow up message that takes the summary and language as inputs\n", "fourth_prompt = ChatPromptTemplate.from_template(\n", - " None\n", + " \"Write a polite, helpful follow-up reply to the customer in {language}. \"\n", + " \"Address the points raised in this summary:\\n\\n{summary}\"\n", ")\n", "chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n", - " output_key=None\n", + " output_key=\"followup_message\"\n", " )\n" ] }, @@ -404,14 +428,14 @@ "metadata": {}, "outputs": [], "source": [ - "# overall_chain: input= Review \n", - "# and output= English_Review,summary, followup_message\n", + "# overall_chain: input= Review\n", + "# and output= English_Review, summary, followup_message\n", "overall_chain = SequentialChain(\n", " chains=[chain_one, chain_two, chain_three, chain_four],\n", - " input_variables=None,\n", - " output_variables=[None, None, None],\n", + " input_variables=[\"Review\"],\n", + " output_variables=[\"English_Review\", \"summary\", \"followup_message\"],\n", " verbose=True\n", - ")" + ")\n" ] }, { @@ -422,7 +446,7 @@ "outputs": [], "source": [ "review = df.Review[5]\n", - "overall_chain(review)" + "overall_chain({\"Review\": review})\n" ] }, { @@ -433,6 +457,28 @@ "**Repeat the above twice for different products or reviews**" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Repeat #1: different review\n", + "review_2 = df.Review[1]\n", + "overall_chain({\"Review\": review_2})\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Repeat #2: different review\n", + "review_3 = df.Review[3]\n", + "overall_chain({\"Review\": review_3})\n" + ] + }, { "cell_type": "markdown", "id": "3041ea4c", @@ -702,6 +748,26 @@ "source": [ "**Repeat the above at least once for different inputs and chains executions - Be creative!**" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Creative router query #1 -> should route to computer science (default chain since none defined explicitly)\n", + "chain.run(\"What is the time complexity of merge sort and why?\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Creative router query #2 -> should route to biology\n", + "chain.run(\"How does photosynthesis convert sunlight into chemical energy?\")\n" + ] } ], "metadata": { @@ -725,4 +791,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file