Skip to content

Commit

Permalink
Add LangChain Proofreading Bot example (#8)
Browse files Browse the repository at this point in the history
* Add LangChain Proofreading Bot example

* Refine README.md

* Update examples/proofreading-bot-langchain/main.py

Co-authored-by: Byeonghoon Yoo <[email protected]>

---------

Co-authored-by: Byeonghoon Yoo <[email protected]>
  • Loading branch information
ssowonny and isac322 authored Jan 2, 2024
1 parent 3bbd5b7 commit 45df0c9
Show file tree
Hide file tree
Showing 4 changed files with 1,607 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/proofreading-bot-langchain/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 LangChain application with
communication channels, such as Slack, using PlugBear. Check [Building a Proofreading Bot Using LangChain](http://localhost:3001/use-cases/proofreading-bot/langchain) for detailed guide.

## 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
into communication channels and test it.

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

import contextlib
import os
from collections.abc import AsyncGenerator

from fastapi import FastAPI
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

import plugbear.fastapi

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

llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY)


async def handle_request(request: plugbear.fastapi.Request) -> str:
""" Handle the request received from PlugBear.
"""

# Convert PlugBear messages to LangChain messages.
messages = [(message.role, message.content)
for message in request.messages]

# Build prompt using the system message and PlugBear messages.
system_prompt = ("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.")
prompt = ChatPromptTemplate.from_messages(
[system_prompt] + messages)

# Invoke the LangChain pipeline.
output_parser = StrOutputParser()
chain = prompt | llm | output_parser
answer = chain.invoke({})

# Returning the generated message.
return answer


@contextlib.asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
await plugbear.fastapi.register(
app,
llm_func=handle_request,
api_key=PLUGBEAR_API_KEY,
endpoint="/plugbear",
)
yield


app = FastAPI(lifespan=lifespan)

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 45df0c9

Please sign in to comment.