-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-lc01-simple-llm-app.qmd
190 lines (128 loc) · 5.8 KB
/
16-lc01-simple-llm-app.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
---
title: LangChain 01 --Build a Simple LLM Application with LangChain
jupyter: python3
---
## Overview
We're going to explore a number of practical applications of LLMs.
We'll start with LangChain and build a simple application that translates text from English into another language.
We'll touch briefly on LangChain's logging and tracing features via LangSmith.
And then we'll deploy the application locally with LangServe.
This is adapted from [LangChain's Build a Simple LLM App](https://python.langchain.com/docs/tutorials/llm_chain/)
and [llm_chain.ipynb](https://github.com/langchain-ai/langchain/blob/master/docs/docs/tutorials/llm_chain.ipynb).
More specifically, you'll have a high level LangChain overview of:
- Using [language models](https://python.langchain.com/docs/concepts/chat_models/)
- Using [PromptTemplates](https://python.langchain.com/docs/concepts/prompt_templates/)
and [OutputParsers](https://python.langchain.com/docs/concepts/output_parsers/)
- Using [LangChain Expression Language (LCEL)](https://python.langchain.com/docs/concepts/lcel/) to chain components together
- Debugging and tracing your application using [LangSmith](https://docs.smith.langchain.com/)
- Deploying your application with [LangServe](https://python.langchain.com/docs/concepts/#langserve)
## Setup
### Installation
To install LangChain run:
::: {.panel-tabset}
## Pip
```bash
pip install langchain langchain-openai
```
## Conda
```bash
conda install langchain langchain-openai -c conda-forge
```
:::
For more details, see the
[Installation guide](https://python.langchain.com/docs/how_to/installation/).
## Example Application
We're going to use this
[Jupyter notebook](https://github.com/trgardos/ml-549-fa24/blob/main/src/langchain/build-simple-llm-app-with-langchain.ipynb)
to build the application.
You can clone this repo and run everything locally.
You can also run it in Google Colab, but you'll need to install the dependencies there,
and run the cell to set the environment variables.
[![](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/trgardos/ml-549-fa24/blob/main/src/langchain/build-simple-llm-app-with-langchain.ipynb)
## Serving with LangServe
In the previous section we wrote a script that programmatically invokes our simple chain.
In this section we'll see how to serve the application with LangServe.
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.
This part needs to be run in a Python script and executed from the command line.
Install with:
```bash
pip install "langserve[all]"
```
### Server
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:
1. The definition of our chain that we just built above
2. A FastAPI app
3. A definition of a route from which to serve the chain, which is done with `langserve.add_routes`
```{.python filename="serve.py"}
#!/usr/bin/env python
from fastapi import FastAPI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from langserve import add_routes
# 1. Create prompt template
system_template = "Translate the following into {language}:"
prompt_template = ChatPromptTemplate.from_messages([
('system', system_template),
('user', '{text}')
])
# 2. Create model
model = ChatOpenAI()
# 3. Create parser
parser = StrOutputParser()
# 4. Create chain
chain = prompt_template | model | parser
# 5. App definition
app = FastAPI(
title="LangChain Server",
version="1.0",
description="A simple API server using LangChain's Runnable interfaces",
)
# 6. Adding chain route
add_routes(
app,
chain,
path="/chain",
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8000)
```
And that's it! If we execute this file:
```bash
python serve.py
```
we should see our chain being served at [http://localhost:8000](http://localhost:8000).
### Playground
Every LangServe service comes with a simple
[built-in UI](https://github.com/langchain-ai/langserve/blob/main/README.md#playground)
for configuring and invoking the application with streaming output and visibility
into intermediate steps.
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.
### Client
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).
Using this, we can interact with the served chain as if it were running client-side.
```{.python filename="client.py"}
from langserve import RemoteRunnable
remote_chain = RemoteRunnable("http://localhost:8000/chain/")
remote_chain.invoke({"language": "italian", "text": "hi"})
print(type(result))
print(result)
```
To learn more about the many other features of LangServe [head here](https://python.langchain.com/docs/langserve/).
## Conclusion
For further reading on the core concepts of LangChain, see [Conceptual Guides](https://python.langchain.com/docs/concepts).
See more detailed guides:
- [LangChain Expression Language (LCEL)](https://python.langchain.com/docs/how_to/#langchain-expression-language-lcel)
- [Prompt templates](https://python.langchain.com/docs/how_to/#prompt-templates)
- [Chat models](https://python.langchain.com/docs/how_to/#chat-models)
- [Output parsers](https://python.langchain.com/docs/how_to/#output-parsers)
- [LangServe](https://python.langchain.com/docs/langserve/)
And the LangSmith docs:
- [LangSmith](https://docs.smith.langchain.com)