{ "cells": [ { "cell_type": "markdown", "id": "d8a5c5d4", "metadata": {}, "source": [ "# LLM Chain\n", "\n", "This notebook showcases a simple LLM chain." ] }, { "cell_type": "code", "execution_count": 1, "id": "835e6978", "metadata": {}, "outputs": [], "source": [ "from langchain import PromptTemplate, OpenAI, LLMChain" ] }, { "cell_type": "markdown", "id": "06bcb078", "metadata": {}, "source": [ "### Single Input\n", "\n", "First, lets go over an example using a single input" ] }, { "cell_type": "code", "execution_count": 3, "id": "51a54c4d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mQuestion: What NFL team won the Super Bowl in the year Justin Beiber was born?\n", "\n", "Answer: Let's think step by step.\u001b[0m\n", "\n", "\u001b[1m> Finished LLMChain chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' Justin Bieber was born in 1994, so the NFL team that won the Super Bowl in 1994 was the Dallas Cowboys.'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "template = \"\"\"Question: {question}\n", "\n", "Answer: Let's think step by step.\"\"\"\n", "prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n", "llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0), verbose=True)\n", "\n", "question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n", "\n", "llm_chain.predict(question=question)" ] }, { "cell_type": "markdown", "id": "79c3ec4d", "metadata": {}, "source": [ "### Multiple Inputs\n", "Now lets go over an example using multiple inputs." ] }, { "cell_type": "code", "execution_count": 5, "id": "03dd6918", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mWrite a sad poem about ducks.\u001b[0m\n", "\n", "\u001b[1m> Finished LLMChain chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\"\\n\\nThe ducks swim in the pond,\\nTheir feathers so soft and warm,\\nBut they can't help but feel so forlorn.\\n\\nTheir quacks echo in the air,\\nBut no one is there to hear,\\nFor they have no one to share.\\n\\nThe ducks paddle around in circles,\\nTheir heads hung low in despair,\\nFor they have no one to care.\\n\\nThe ducks look up to the sky,\\nBut no one is there to see,\\nFor they have no one to be.\\n\\nThe ducks drift away in the night,\\nTheir hearts filled with sorrow and pain,\\nFor they have no one to gain.\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "template = \"\"\"Write a {adjective} poem about {subject}.\"\"\"\n", "prompt = PromptTemplate(template=template, input_variables=[\"adjective\", \"subject\"])\n", "llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0), verbose=True)\n", "\n", "llm_chain.predict(adjective=\"sad\", subject=\"ducks\")" ] }, { "cell_type": "code", "execution_count": null, "id": "8310cdaa", "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.10.8" } }, "nbformat": 4, "nbformat_minor": 5 }