mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-22 07:05:36 +00:00
629 lines
19 KiB
Plaintext
629 lines
19 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "raw",
|
|
"id": "63ee3f93",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"sidebar_position: 0\n",
|
|
"---"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9316da0d",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Build a Simple LLM Application\n",
|
|
"\n",
|
|
"In this quickstart we'll show you how to build a simple LLM application. This application will translate text from English into another language. This is a relatively simple LLM application - it's just a single LLM call plus some prompting. Still, this is a great way to get started with LangChain - a lot of features can be built with just some prompting and an LLM call!\n",
|
|
"\n",
|
|
"## Concepts\n",
|
|
"\n",
|
|
"Concepts we will cover are:\n",
|
|
"\n",
|
|
"- Using [language models](/docs/concepts/#chat-models)\n",
|
|
"\n",
|
|
"- Using [PromptTemplates](/docs/concepts/#prompt-templates) and [OutputParsers](/docs/concepts/#output-parsers)\n",
|
|
"\n",
|
|
"- [Chaining](/docs/concepts/#langchain-expression-language) a PromptTemplate + LLM + OutputParser using LangChain\n",
|
|
"\n",
|
|
"- Debugging and tracing your application using [LangSmith](/docs/concepts/#langsmith)\n",
|
|
"\n",
|
|
"- Deploying your application with [LangServe](/docs/concepts/#langserve)\n",
|
|
"\n",
|
|
"That's a fair amount to cover! Let's dive in.\n",
|
|
"\n",
|
|
"## Setup\n",
|
|
"\n",
|
|
"### Jupyter Notebook\n",
|
|
"\n",
|
|
"This guide (and most of the other guides in the documentation) uses [Jupyter notebooks](https://jupyter.org/) and assumes the reader is as well. Jupyter notebooks are perfect for learning how to work with LLM systems because oftentimes things can go wrong (unexpected output, API down, etc) and going through guides in an interactive environment is a great way to better understand them.\n",
|
|
"\n",
|
|
"You do not NEED to go through the guide in a Jupyter Notebook, but it is recommended. See [here](https://jupyter.org/install) for instructions on how to install.\n",
|
|
"\n",
|
|
"### Installation\n",
|
|
"\n",
|
|
"To install LangChain run:\n",
|
|
"\n",
|
|
"```{=mdx}\n",
|
|
"import Tabs from '@theme/Tabs';\n",
|
|
"import TabItem from '@theme/TabItem';\n",
|
|
"import CodeBlock from \"@theme/CodeBlock\";\n",
|
|
"\n",
|
|
"<Tabs>\n",
|
|
" <TabItem value=\"pip\" label=\"Pip\" default>\n",
|
|
" <CodeBlock language=\"bash\">pip install langchain</CodeBlock>\n",
|
|
" </TabItem>\n",
|
|
" <TabItem value=\"conda\" label=\"Conda\">\n",
|
|
" <CodeBlock language=\"bash\">conda install langchain -c conda-forge</CodeBlock>\n",
|
|
" </TabItem>\n",
|
|
"</Tabs>\n",
|
|
"\n",
|
|
"```\n",
|
|
"\n",
|
|
"\n",
|
|
"For more details, see our [Installation guide](/docs/get_started/installation).\n",
|
|
"\n",
|
|
"### LangSmith\n",
|
|
"\n",
|
|
"Many of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls.\n",
|
|
"As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your chain or agent.\n",
|
|
"The best way to do this is with [LangSmith](https://smith.langchain.com).\n",
|
|
"\n",
|
|
"After you sign up at the link above, make sure to set your environment variables to start logging traces:\n",
|
|
"\n",
|
|
"```shell\n",
|
|
"export LANGCHAIN_TRACING_V2=\"true\"\n",
|
|
"export LANGCHAIN_API_KEY=\"...\"\n",
|
|
"```\n",
|
|
"\n",
|
|
"Or, if in a notebook, you can set them with:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"import getpass\n",
|
|
"import os\n",
|
|
"\n",
|
|
"os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
|
"os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()\n",
|
|
"```"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e5558ca9",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Detailed walkthrough\n",
|
|
"\n",
|
|
"In this guide we will build an application to translate user input from one language to another.\n",
|
|
"\n",
|
|
"\n",
|
|
"## Using Language Models\n",
|
|
"\n",
|
|
"First up, let's learn how to use a language model by itself. LangChain supports many different language models that you can use interchangably - select the one you want to use below!\n",
|
|
"\n",
|
|
"```{=mdx}\n",
|
|
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
|
|
"\n",
|
|
"<ChatModelTabs openaiParams={`model=\"gpt-4\"`} />\n",
|
|
"```"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "e4b41234",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# | output: false\n",
|
|
"# | echo: false\n",
|
|
"\n",
|
|
"from langchain_openai import ChatOpenAI\n",
|
|
"\n",
|
|
"model = ChatOpenAI(model=\"gpt-4\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ca5642ff",
|
|
"metadata": {},
|
|
"source": [
|
|
"Let's first use the model directly. `ChatModel`s are instances of LangChain \"Runnables\", which means they expose a standard interface for interacting with them. To just simply call the model, we can pass in a list of messages to the `.invoke` method."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"id": "1b2481f0",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"AIMessage(content='ciao!', response_metadata={'token_usage': {'completion_tokens': 3, 'prompt_tokens': 20, 'total_tokens': 23}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-fc5d7c88-9615-48ab-a3c7-425232b562c5-0')"
|
|
]
|
|
},
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
|
"\n",
|
|
"messages = [\n",
|
|
" SystemMessage(content=\"Translate the following from English into Italian\"),\n",
|
|
" HumanMessage(content=\"hi!\"),\n",
|
|
"]\n",
|
|
"\n",
|
|
"model.invoke(messages)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f83373db",
|
|
"metadata": {},
|
|
"source": [
|
|
"If we've enable LangSmith, we can see that this run is logged to LangSmith, and can see the [LangSmith trace](https://smith.langchain.com/public/88baa0b2-7c1a-4d09-ba30-a47985dde2ea/r)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "32bd03ed",
|
|
"metadata": {},
|
|
"source": [
|
|
"## OutputParsers\n",
|
|
"\n",
|
|
"Notice that the response from the model is an `AIMessage`. This contains a string response along with other metadata about the response. Oftentimes we may just want to work with the string response. We can parse out just this response by using a simple output parser.\n",
|
|
"\n",
|
|
"We first import the simple output parser."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"id": "d7ae9c58",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain_core.output_parsers import StrOutputParser\n",
|
|
"\n",
|
|
"parser = StrOutputParser()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "eaebe33a",
|
|
"metadata": {},
|
|
"source": [
|
|
"One way to use it is to use it by itself. For example, we could save the result of the language model call and then pass it to the parser."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"id": "6bacb837",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"result = model.invoke(messages)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"id": "efb8da87",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'Ciao!'"
|
|
]
|
|
},
|
|
"execution_count": 19,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"parser.invoke(result)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d508b79d",
|
|
"metadata": {},
|
|
"source": [
|
|
"More commonly, we can \"chain\" the model with this output parser. This means this output parser will get called everytime in this chain. This chain takes on the input type of the language model (string or list of message) and returns the output type of the output parser (string).\n",
|
|
"\n",
|
|
"We can easily create the chain using the `|` operator. The `|` operator is used in LangChain to combine two elements together."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"id": "9449cfa6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"chain = model | parser"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 21,
|
|
"id": "3e82f933",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'Ciao!'"
|
|
]
|
|
},
|
|
"execution_count": 21,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"chain.invoke(messages)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "dd009096",
|
|
"metadata": {},
|
|
"source": [
|
|
"If we now look at LangSmith, we can see that the chain has two steps: first the language model is called, then the result of that is passed to the output parser. We can see the [LangSmith trace]( https://smith.langchain.com/public/f1bdf656-2739-42f7-ac7f-0f1dd712322f/r)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1ab8da31",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Prompt Templates\n",
|
|
"\n",
|
|
"Right now we are passing a list of messages directly into the language model. Where does this list of messages come from? Usually it constructed from a combination of user input and application logic. This application logic usually takes the raw user input and transforms it into a list of messages ready to pass to the language model. Common transformations include adding a system message or formatting a template with the user input.\n",
|
|
"\n",
|
|
"PromptTemplates are a concept in LangChain designed to assist with this transformation. They take in raw user input and return data (a prompt) that is ready to pass into a language model. \n",
|
|
"\n",
|
|
"Let's create a PromptTemplate here. It will take in two user variables:\n",
|
|
"\n",
|
|
"- `language`: The language to translate text into\n",
|
|
"- `text`: The text to translate"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 23,
|
|
"id": "3e73cc20",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain_core.prompts import ChatPromptTemplate"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "7e876c2a",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, let's create a string that we will format to be the system message:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 24,
|
|
"id": "fd75ecde",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"system_template = \"Translate the following into {language}:\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fedf6f13",
|
|
"metadata": {},
|
|
"source": [
|
|
"Next, we can create the PromptTemplate. This will be a combination of the `system_template` as well as a simpler template for where the put the text"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 25,
|
|
"id": "88e566f3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"prompt_template = ChatPromptTemplate.from_messages(\n",
|
|
" [(\"system\", system_template), (\"user\", \"{text}\")]\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d9711ba6",
|
|
"metadata": {},
|
|
"source": [
|
|
"The input to this prompt template is a dictionary. We can play around with this prompt template by itself to see what it does by itself"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 27,
|
|
"id": "f781b3cb",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"ChatPromptValue(messages=[SystemMessage(content='Translate the following into italian:'), HumanMessage(content='hi')])"
|
|
]
|
|
},
|
|
"execution_count": 27,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"result = prompt_template.invoke({\"language\": \"italian\", \"text\": \"hi\"})\n",
|
|
"\n",
|
|
"result"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1a49ba9e",
|
|
"metadata": {},
|
|
"source": [
|
|
"We can see that it returns a `ChatPromptValue` that consists of two messages. If we want to access the messages directly we do:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 28,
|
|
"id": "2159b619",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[SystemMessage(content='Translate the following into italian:'),\n",
|
|
" HumanMessage(content='hi')]"
|
|
]
|
|
},
|
|
"execution_count": 28,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"result.to_messages()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5a4267a8",
|
|
"metadata": {},
|
|
"source": [
|
|
"We can now combine this with the model and the output parser from above. This will chain all three components together."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 29,
|
|
"id": "6c6beb4b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"chain = prompt_template | model | parser"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 30,
|
|
"id": "3e45595a",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'ciao'"
|
|
]
|
|
},
|
|
"execution_count": 30,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"chain.invoke({\"language\": \"italian\", \"text\": \"hi\"})"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "0b19cecb",
|
|
"metadata": {},
|
|
"source": [
|
|
"If we take a look at the LangSmith trace, we can see all three components show up in the [LangSmith trace](https://smith.langchain.com/public/bc49bec0-6b13-4726-967f-dbd3448b786d/r)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a515ddd0",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Serving with LangServe\n",
|
|
"\n",
|
|
"Now that we've built an application, we need to serve it. That's where LangServe comes in.\n",
|
|
"LangServe helps developers deploy LangChain chains as a REST API. You do not need to use LangServe to use LangChain, but in this guide we'll show how you can deploy your app with LangServe.\n",
|
|
"\n",
|
|
"While the first part of this guide was intended to be run in a Jupyter Notebook, we will now move out of that. We will be creating a Python file and then interacting with it from the command line.\n",
|
|
"\n",
|
|
"Install with:\n",
|
|
"```bash\n",
|
|
"pip install \"langserve[all]\"\n",
|
|
"```\n",
|
|
"\n",
|
|
"### Server\n",
|
|
"\n",
|
|
"To create a server for our application we'll make a `serve.py` file. This will contain our logic for serving our application. It consists of three things:\n",
|
|
"1. The definition of our chain that we just built above\n",
|
|
"2. Our FastAPI app\n",
|
|
"3. A definition of a route from which to serve the chain, which is done with `langserve.add_routes`\n",
|
|
"\n",
|
|
"\n",
|
|
"```python\n",
|
|
"#!/usr/bin/env python\n",
|
|
"from typing import List\n",
|
|
"\n",
|
|
"from fastapi import FastAPI\n",
|
|
"from langchain_core.prompts import ChatPromptTemplate\n",
|
|
"from langchain_core.output_parsers import StrOutputParser\n",
|
|
"from langchain_openai import ChatOpenAI\n",
|
|
"from langserve import add_routes\n",
|
|
"\n",
|
|
"# 1. Create prompt template\n",
|
|
"system_template = \"Translate the following into {language}:\"\n",
|
|
"prompt_template = ChatPromptTemplate.from_messages([\n",
|
|
" ('system', system_template),\n",
|
|
" ('user', '{text}')\n",
|
|
"])\n",
|
|
"\n",
|
|
"# 2. Create model\n",
|
|
"model = ChatOpenAI()\n",
|
|
"\n",
|
|
"# 3. Create parser\n",
|
|
"parser = StrOutputParser()\n",
|
|
"\n",
|
|
"# 4. Create chain\n",
|
|
"chain = prompt_template | model | parser\n",
|
|
"\n",
|
|
"\n",
|
|
"# 4. App definition\n",
|
|
"app = FastAPI(\n",
|
|
" title=\"LangChain Server\",\n",
|
|
" version=\"1.0\",\n",
|
|
" description=\"A simple API server using LangChain's Runnable interfaces\",\n",
|
|
")\n",
|
|
"\n",
|
|
"# 5. Adding chain route\n",
|
|
"\n",
|
|
"add_routes(\n",
|
|
" app,\n",
|
|
" chain,\n",
|
|
" path=\"/chain\",\n",
|
|
")\n",
|
|
"\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" import uvicorn\n",
|
|
"\n",
|
|
" uvicorn.run(app, host=\"localhost\", port=8000)\n",
|
|
"```\n",
|
|
"\n",
|
|
"And that's it! If we execute this file:\n",
|
|
"```bash\n",
|
|
"python serve.py\n",
|
|
"```\n",
|
|
"we should see our chain being served at [http://localhost:8000](http://localhost:8000).\n",
|
|
"\n",
|
|
"### Playground\n",
|
|
"\n",
|
|
"Every LangServe service comes with a simple built-in UI for configuring and invoking the application with streaming output and visibility into intermediate steps.\n",
|
|
"Head to [http://localhost:8000/chain/playground/](http://localhost:8000/chain/playground/) to try it out! Pass in the same inputs as before - `{\"language\": \"italian\", \"text\": \"hi\"}` - and it should respond same as before.\n",
|
|
"\n",
|
|
"### Client\n",
|
|
"\n",
|
|
"Now let's set up a client for programmatically interacting with our service. We can easily do this with the `[langserve.RemoteRunnable](/docs/langserve#client)`.\n",
|
|
"Using this, we can interact with the served chain as if it were running client-side."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 31,
|
|
"id": "85174643",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'Ciao'"
|
|
]
|
|
},
|
|
"execution_count": 31,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from langserve import RemoteRunnable\n",
|
|
"\n",
|
|
"remote_chain = RemoteRunnable(\"http://localhost:8000/chain/\")\n",
|
|
"remote_chain.invoke({\"language\": \"italian\", \"text\": \"hi\"})"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "480b78a9",
|
|
"metadata": {},
|
|
"source": [
|
|
"To learn more about the many other features of LangServe [head here](/docs/langserve)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "befdb168",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Conclusion\n",
|
|
"\n",
|
|
"That's it! In this tutorial we've walked through creating our first simple LLM application. We've learned how to work with language models, how to parse their outputs, how to create a prompt template, how to get great observability into chains you create with LangSmith, and how to deploy them with LangServe.\n",
|
|
"\n",
|
|
"This just scratches the surface of what you will want to learn to become a proficient AI Engineer. Luckily - we've got a lot of other resources!\n",
|
|
"\n",
|
|
"For more in-depth tutorials, check out out [Tutorials](/docs/tutorials) section.\n",
|
|
"\n",
|
|
"If you have specific questions on how to accomplish particular tasks, see our [How-To Guides](/docs/how_to) section.\n",
|
|
"\n",
|
|
"For reading up on the core concepts of LangChain, we've got detailed [Conceptual Guides](/docs/concepts)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "a3d3e206",
|
|
"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.1"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|