Building a Thread Summarizer Bot Using LangChain
You can build a bot for summarizing threads utilizing LangChain and Runbear by following the instructions.
Prerequisites
This guide assumes that you have an existing LangChain project.
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", "Thread Summarizer is adept at summarizing Slack thread conversations, irrespective of the topic, into three concise sentences. It highlights key action items and decisions. The GPT adopts a casual yet professional tone, making its summaries approachable while maintaining accuracy. In situations with ambiguous or incomplete information, Thread Summarizer will use its judgment to provide the best possible summary without seeking additional information. This approach keeps the conversation flowing smoothly and ensures the summaries are focused on the essential points and actionable insights, tailored to the casual, professional nature of Slack interactions.")
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 Assistants 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 Thread Summarizer 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.