Skip to content

Commit

Permalink
Add LangServe example
Browse files Browse the repository at this point in the history
  • Loading branch information
ssowonny committed Feb 10, 2024
1 parent 45df0c9 commit 928e42d
Show file tree
Hide file tree
Showing 4 changed files with 1,863 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/proofreading-bot-langserve/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# PlugBear Python SDK Example (LangChain)

This project introduces an example of integrating a LangServe application with
communication channels, such as Slack, using PlugBear. Check the [PlugBear Docs: LangServe](https://docs.plugbear.io/integrations/langchain/langserve) page to learn more.

## Prerequisites

- [Poetry](https://python-poetry.org)

## Development

### Installing Dependencies

Use [Poetry](https://python-poetry.org/) to install dependencies.

```bash
poetry install
```

### Running Server

Run the command below to run the server:

```bash
OPENAI_API_KEY="YOUR_OPENAI_API_KEY" \
PLUGBEAR_API_KEY="YOUR_PLUGBEAR_API_KEY" \
poetry run python main.py
```

You can obtain your `OPENAI_API_KEY` from the
[OpenAI API Keys](https://platform.openai.com/api-keys) page and your
`PLUGBEAR_API_KEY` from the
[PlugBear API Keys](https://auth.plugbear.io/org/api_keys) page.

### Testing Integration

Follow [PlugBear Documentation](https://docs.plugbear.io) to connect your app
to communication channels and test it.

Ask any math question to `@PlugBear` after connecting it. e.g.,
``@PlugBear This is an example sentence.``.
39 changes: 39 additions & 0 deletions examples/proofreading-bot-langserve/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations

import os
from operator import itemgetter

from fastapi import FastAPI
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import convert_to_messages
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from langserve import add_routes

import plugbear.fastapi

OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
PLUGBEAR_API_KEY = os.environ["PLUGBEAR_API_KEY"]

app = FastAPI()
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY)
output_parser = StrOutputParser()
prompt = ChatPromptTemplate.from_messages(
[
("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."),
MessagesPlaceholder("conversation"),
]
)
runnable = {"conversation": convert_to_messages} | prompt | llm | output_parser

add_routes(
app,
runnable,
path="/proofreading-bot-langserve",
)


if __name__ == "__main__":
import uvicorn

uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", default=8000)))
Loading

0 comments on commit 928e42d

Please sign in to comment.