Building a Proofreading Bot Using LangChain
You can build a proofreading bot utilizing LangChain and Runbear by following the instructions. You can find a working code example in the LangChain Proofreading Bot Example repository.
Prerequisites
This guide assumes that you have an existing LLM application built using LangChain.
Installation
To install the Runbear Python SDK, run the following command:
pip install 'plugbear[fastapi]'
Step-by-Step Guide
Getting a Runbear API Key
You can create or manage Runbear API keys on the Runbear API Keys page. Please copy the key for use in the subsequent step.
Configuring FastAPI with Runbear Endpoint
Register an endpoint for Runbear using FastAPI. Adjust the endpoint to meet your requirements.
import contextlib
import plugbear.fastapi
from fastapi import FastAPI
# Find your API key on the Runbear API Keys page.
PLUGBEAR_API_KEY = os.environ["PLUGBEAR_API_KEY"]
@contextlib.asynccontextmanager
async def lifespan(app: FastAPI):
await plugbear.fastapi.register(
app,
llm_func=handle_request,
api_key=PLUGBEAR_API_KEY,
endpoint="/plugbear",
)
yield
app = FastAPI(lifespan=lifespan)
Defining the Handling Function
You can easily convert Runbear messages into LangChain messages and invoke the LangChain pipeline. Follow the code example below:
async def handle_request(request: plugbear.fastapi.Request) -> str:
""" Handle the request received from Runbear.
"""
# Convert Runbear messages to LangChain messages.
messages = [(message.role, message.content)
for message in request.messages]
# Build prompt using the system message and Runbear 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
Configure Your App in Runbear
- Navigate to the Assistatns menu and click Add App.
- Select Runbear Python SDK as your app type.
- In the Your LLM App Endpoint field, enter the endpoint with the path you set earlier. e.g.,
https://your.domain.com/plugbear
What's Next
You have successfully developed a proofreading bot 🎉 You are now ready to integrate the application into your communication channels, such as Slack.
Connect the app you added to communication channels. Check Connecting Channels with LLM Apps for more details.