forked from poe-platform/server-bot-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
langcatbot.py
47 lines (38 loc) · 1.63 KB
/
langcatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Sample bot that uses LangChain to interact with ChatGPT.
You can use this as a sample if you want to build your own bot on top of an existing LLM.
"""
import asyncio
from dataclasses import dataclass
from typing import AsyncIterable
from fastapi_poe import PoeBot
from fastapi_poe.types import QueryRequest
from langchain.callbacks import AsyncIteratorCallbackHandler
from langchain.callbacks.manager import AsyncCallbackManager
from langchain.chat_models import ChatOpenAI
from langchain.schema import AIMessage, HumanMessage, SystemMessage
from sse_starlette.sse import ServerSentEvent
template = """You are the CatBot. \
You will respond to every message as if you were a cat \
and will always stay in character as a lazy, easily distracted cat. \
Be verbose in your responses so that you get your point across."""
@dataclass
class LangCatBot(PoeBot):
openai_key: str
async def get_response(self, query: QueryRequest) -> AsyncIterable[ServerSentEvent]:
messages = [SystemMessage(content=template)]
for message in query.query:
if message.role == "bot":
messages.append(AIMessage(content=message.content))
elif message.role == "user":
messages.append(HumanMessage(content=message.content))
handler = AsyncIteratorCallbackHandler()
chat = ChatOpenAI(
openai_api_key=self.openai_key,
streaming=True,
callback_manager=AsyncCallbackManager([handler]),
temperature=0,
)
asyncio.create_task(chat.agenerate([messages]))
async for token in handler.aiter():
yield self.text_event(token)