2023-10-30 22:31:31 +00:00
# LangChain Templates
2023-10-26 01:47:42 +00:00
2023-10-30 22:31:31 +00:00
LangChain Templates are the easiest and fastest way to build a production-ready LLM application.
2023-10-28 23:26:52 +00:00
These templates serve as a set of reference architectures for a wide variety of popular LLM use cases.
2023-10-30 22:31:31 +00:00
They are all in a standard format which make it easy to deploy them with [LangServe ](https://github.com/langchain-ai/langserve ).
🚩 We will be releasing a hosted version of LangServe for one-click deployments of LangChain applications. [Sign up here ](https://airtable.com/app0hN6sd93QcKubv/shrAjst60xXa6quV2 ) to get on the waitlist.
2023-10-26 01:47:42 +00:00
2023-10-28 23:26:52 +00:00
## Quick Start
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
To use, first install the LangChain CLI.
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
```shell
2023-11-03 19:10:32 +00:00
pip install -U langchain-cli
2023-10-26 15:28:01 +00:00
```
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
Next, create a new LangChain project:
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
```shell
2023-10-30 23:39:39 +00:00
langchain app new my-app
2023-10-26 15:28:01 +00:00
```
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
This will create a new directory called `my-app` with two folders:
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
- `app` : This is where LangServe code will live
- `packages` : This is where your chains or agents will live
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
To pull in an existing template as a package, you first need to go into your new project:
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
```shell
cd my-app
2023-10-26 01:47:42 +00:00
```
2023-10-28 23:26:52 +00:00
And you can the add a template as a project.
In this getting started guide, we will add a simple `pirate-speak` project.
All this project does is convert user input into pirate speak.
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
```shell
2023-10-30 23:39:39 +00:00
langchain app add pirate-speak
2023-10-26 01:47:42 +00:00
```
2023-10-28 23:26:52 +00:00
This will pull in the specified template into `packages/pirate-speak`
You will then be prompted if you want to install it.
This is the equivalent of running `pip install -e packages/pirate-speak` .
You should generally accept this (or run that same command afterwards).
We install it with `-e` so that if you modify the template at all (which you likely will) the changes are updated.
2023-10-26 01:47:42 +00:00
2023-10-28 23:26:52 +00:00
After that, it will ask you if you want to generate route code for this project.
This is code you need to add to your app to start using this chain.
If we accept, we will see the following code generated:
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
```shell
2023-10-28 23:26:52 +00:00
from pirate_speak.chain import chain as pirate_speak_chain
2023-10-30 23:39:39 +00:00
add_routes(app, pirate_speak_chain, path="/pirate-speak")
2023-10-26 15:28:01 +00:00
```
2023-10-26 01:47:42 +00:00
2023-10-28 23:26:52 +00:00
You can now edit the template you pulled down.
2023-12-18 01:56:56 +00:00
You can change the code files in `packages/pirate-speak` to use a different model, different prompt, different logic.
2023-10-28 23:26:52 +00:00
Note that the above code snippet always expects the final chain to be importable as `from pirate_speak.chain import chain` ,
so you should either keep the structure of the package similar enough to respect that or be prepared to update that code snippet.
2023-10-26 01:47:42 +00:00
2023-10-28 23:26:52 +00:00
Once you have done as much of that as you want, it is
2023-10-26 15:28:01 +00:00
In order to have LangServe use this project, you then need to modify `app/server.py` .
2023-10-28 23:26:52 +00:00
Specifically, you should add the above code snippet to `app/server.py` so that file looks like:
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
```python
from fastapi import FastAPI
from langserve import add_routes
2023-10-28 23:26:52 +00:00
from pirate_speak.chain import chain as pirate_speak_chain
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
app = FastAPI()
2023-10-26 01:47:42 +00:00
2023-10-30 23:39:39 +00:00
add_routes(app, pirate_speak_chain, path="/pirate-speak")
2023-10-26 15:28:01 +00:00
```
2023-10-26 01:47:42 +00:00
2023-10-30 22:31:31 +00:00
(Optional) Let's now configure LangSmith.
LangSmith will help us trace, monitor and debug LangChain applications.
2024-04-12 20:08:10 +00:00
You can sign up for LangSmith [here ](https://smith.langchain.com/ ).
2023-10-30 22:31:31 +00:00
If you don't have access, you can skip this section
```shell
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=< your-api-key >
export LANGCHAIN_PROJECT=< your-project > # if not specified, defaults to "default"
```
For this particular application, we will use OpenAI as the LLM, so we need to export our OpenAI API key:
```shell
export OPENAI_API_KEY=sk-...
```
2023-10-26 15:28:01 +00:00
You can then spin up production-ready endpoints, along with a playground, by running:
2023-10-26 01:47:42 +00:00
2023-10-26 15:28:01 +00:00
```shell
2023-10-30 23:39:39 +00:00
langchain serve
2023-10-26 15:28:01 +00:00
```
2023-10-26 01:47:42 +00:00
2023-10-28 23:26:52 +00:00
This now gives a fully deployed LangServe application.
2023-10-30 23:39:39 +00:00
For example, you get a playground out-of-the-box at [http://127.0.0.1:8000/pirate-speak/playground/ ](http://127.0.0.1:8000/pirate-speak/playground/ ):
2023-10-28 23:26:52 +00:00
2024-01-12 22:37:48 +00:00
![Screenshot of the LangServe Playground interface with input and output fields demonstrating pirate speak conversion. ](docs/playground.png "LangServe Playground Interface" )
2023-10-28 23:26:52 +00:00
Access API documentation at [http://127.0.0.1:8000/docs ](http://127.0.0.1:8000/docs )
2024-01-12 22:37:48 +00:00
![Screenshot of the API documentation interface showing available endpoints for the pirate-speak application. ](docs/docs.png "API Documentation Interface" )
2023-10-28 23:26:52 +00:00
Use the LangServe python or js SDK to interact with the API as if it were a regular [Runnable ](https://python.langchain.com/docs/expression_language/ ).
```python
from langserve import RemoteRunnable
2023-10-30 23:39:39 +00:00
api = RemoteRunnable("http://127.0.0.1:8000/pirate-speak")
2023-10-28 23:26:52 +00:00
api.invoke({"text": "hi"})
```
That's it for the quick start!
You have successfully downloaded your first template and deployed it with LangServe.
## Additional Resources
### [Index of Templates](docs/INDEX.md)
Explore the many templates available to use - from advanced RAG to agents.
### [Contributing](docs/CONTRIBUTING.md)
Want to contribute your own template? It's pretty easy! These instructions walk through how to do that.
### [Launching LangServe from a Package](docs/LAUNCHING_PACKAGE.md)
2023-10-26 01:47:42 +00:00
2023-10-28 23:26:52 +00:00
You can also launch LangServe from a package directly (without having to create a new project).
These instructions cover how to do that.