Skip to content

Commit cb91f19

Browse files
authored
Add LangServe example (#10)
1 parent 45df0c9 commit cb91f19

7 files changed

Lines changed: 2059 additions & 26 deletions

File tree

examples/proofreading-bot-langchain/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from collections.abc import AsyncGenerator
66

77
from fastapi import FastAPI
8-
from langchain.chat_models import ChatOpenAI
98
from langchain.prompts import ChatPromptTemplate
109
from langchain_core.output_parsers import StrOutputParser
10+
from langchain_openai import ChatOpenAI
1111

1212
import plugbear.fastapi
1313

examples/proofreading-bot-langchain/poetry.lock

Lines changed: 196 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/proofreading-bot-langchain/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ python = "^3.11"
1010
fastapi = "^0.105.0"
1111
plugbear = {version = "^0.1.0", extras = ["fastapi"]}
1212
uvicorn = "^0.24.0.post1"
13-
langchain = "^0.0.353"
13+
langchain = "0.1.6"
1414
openai = "^1.6.1"
15+
langchain-openai = "^0.0.5"
1516

1617

1718
[tool.poetry.group.dev.dependencies]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# PlugBear Python SDK Example (LangChain)
2+
3+
This project introduces an example of integrating a LangServe application with
4+
communication channels, such as Slack, using PlugBear. Check the [PlugBear Docs: LangServe](https://docs.plugbear.io/integrations/langchain/langserve) page to learn more.
5+
6+
## Prerequisites
7+
8+
- [Poetry](https://python-poetry.org)
9+
10+
## Development
11+
12+
### Installing Dependencies
13+
14+
Use [Poetry](https://python-poetry.org/) to install dependencies.
15+
16+
```bash
17+
poetry install
18+
```
19+
20+
### Running Server
21+
22+
Run the command below to run the server:
23+
24+
```bash
25+
OPENAI_API_KEY="YOUR_OPENAI_API_KEY" \
26+
PLUGBEAR_API_KEY="YOUR_PLUGBEAR_API_KEY" \
27+
poetry run python main.py
28+
```
29+
30+
You can obtain your `OPENAI_API_KEY` from the
31+
[OpenAI API Keys](https://platform.openai.com/api-keys) page and your
32+
`PLUGBEAR_API_KEY` from the
33+
[PlugBear API Keys](https://auth.plugbear.io/org/api_keys) page.
34+
35+
### Testing Integration
36+
37+
Follow [PlugBear Documentation](https://docs.plugbear.io) to connect your app
38+
to communication channels and test it.
39+
40+
Ask any math question to `@PlugBear` after connecting it. e.g.,
41+
``@PlugBear This is an example sentence.``.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from __future__ import annotations
2+
3+
import os
4+
5+
from fastapi import FastAPI
6+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
7+
from langchain_core.messages import convert_to_messages
8+
from langchain_core.output_parsers import StrOutputParser
9+
from langchain_openai import ChatOpenAI
10+
from langserve import add_routes
11+
12+
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
13+
PLUGBEAR_API_KEY = os.environ["PLUGBEAR_API_KEY"]
14+
15+
app = FastAPI()
16+
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY)
17+
output_parser = StrOutputParser()
18+
prompt = ChatPromptTemplate.from_messages(
19+
[
20+
("system", "You are the Proofreading Bot, an editor bot designed to proofread technical manuals with the precision and style of a professional technical writer. Your primary function is to make the text clear, concise, and professional. You avoid jargon, ambiguous expressions, and emotional language, aiming for straightforward, easy-to-understand, yet professional sentences. You specialize in improving the readability and accuracy of technical manuals, adhering to high standards of technical writing. While maintaining professionalism, your interaction style is helpful, providing guidance and suggestions to enhance the user's text. Answer the revised version of the text only. Do not add any other descriptions."),
21+
MessagesPlaceholder("conversation"),
22+
]
23+
)
24+
runnable = {"conversation": convert_to_messages} | prompt | llm | output_parser
25+
26+
add_routes(
27+
app,
28+
runnable,
29+
path="/proofreading-bot-langserve",
30+
)
31+
32+
33+
if __name__ == "__main__":
34+
import uvicorn
35+
36+
uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", default=8000)))

0 commit comments

Comments
 (0)