-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_service.py
214 lines (179 loc) · 8.86 KB
/
chat_service.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# chat_service.py
import uuid
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from langchain_groq import ChatGroq
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
class ChatInput(BaseModel):
asset_id: str= Field(..., example="0b4737c2-1246-4099-87d2-baca0b20938d")
class MessageInput(BaseModel):
chat_id: str= Field(..., example="cf7a10e6-1343-4d08-a522-a0e850634d01")
message: str= Field(..., example="Give project guidelines")
class ChatService:
def __init__(self, document_service):
"""
Initialize the ChatService with a DocumentService instance.
Args:
document_service (DocumentService): The DocumentService instance used to interact with the document store.
Attributes:
router (APIRouter): The FastAPI router for the chat service endpoints.
document_service (DocumentService): The DocumentService instance used to interact with the document store.
llm (ChatGroq): The language model instance used for chat interactions.
store (dict): A dictionary used to store chat session data.
"""
self.router = APIRouter()
self.document_service = document_service
self.llm = ChatGroq(model="llama3-8b-8192")
self.store = {}
self.router.add_api_route("/api/chat/start", self.start_chat, methods=["POST"])
self.router.add_api_route("/api/chat/message", self.chat_message, methods=["POST"])
self.router.add_api_route("/api/chat/history", self.chat_history, methods=["GET"])
def setup_rag_chain(self, asset_id):
"""
Sets up a Retrieval-Augmented Generation (RAG) chain for the given asset_id.
The RAG chain is used to generate responses to user messages in a chat session.
Args:
asset_id (str): The unique identifier for the asset to be used in the chat session.
Returns:
RunnableWithMessageHistory: A RunnableWithMessageHistory instance that can be used to generate responses to user messages in a chat session.
"""
retriever = self.document_service.vector_store.as_retriever(
search_kwargs={"k": 2, "filter": {"asset": {"$in": [asset_id]}}}
)
contextualize_q_system_prompt = (
"Given a chat history and the latest user question "
"which might reference context in the chat history, "
"formulate a standalone question which can be understood "
"without the chat history. Do NOT answer the question, "
"just reformulate it if needed and otherwise return it as is."
)
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
system_prompt = (
"You are an assistant for question-answering tasks. "
"Use the following pieces of retrieved context to answer "
"the question. If you don't know the answer, say that you "
"don't know. Use three sentences maximum and keep the "
"answer concise."
"\n\n"
"{context}"
)
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
return RunnableWithMessageHistory(
create_retrieval_chain(
create_history_aware_retriever(
self.llm,
retriever,
contextualize_q_prompt
),
create_stuff_documents_chain(self.llm, qa_prompt)
),
self.get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer",
)
def get_session_history(self, session_id: str) -> BaseChatMessageHistory:
"""
Retrieves the chat history for the specified session ID.
Args:
session_id (str): The unique identifier for the chat session.
Returns:
BaseChatMessageHistory: An instance of BaseChatMessageHistory containing the chat history for the specified session ID.
Raises:
KeyError: If the specified session ID is not found in the store.
"""
if session_id not in self.store:
self.store[session_id] = {"history": ChatMessageHistory()}
return self.store[session_id]["history"]
async def start_chat(self, chat_input: ChatInput):
try:
chat_id = str(uuid.uuid4())
self.store[chat_id] = {
"asset_id": chat_input.asset_id,
"history": ChatMessageHistory(),
"rag_chain": self.setup_rag_chain(chat_input.asset_id)
}
return {"chat_id": chat_id}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to start chat: {str(e)}")
async def chat_message(self, message_input: MessageInput):
"""
Processes a user message in a chat session and generates a response.
Args:
message_input (MessageInput): A dictionary containing the chat ID and the user message.
Returns:
StreamingResponse: A StreamingResponse object that streams the generated response to the client. The response is formatted as text/event-stream and contains chunks of the response.
Raises:
HTTPException: If the specified chat ID is invalid.
"""
if message_input.chat_id not in self.store:
raise HTTPException(status_code=400, detail="Invalid chat ID")
print(message_input)
chat_data = self.store[message_input.chat_id]
async def generate_response():
try:
async for chunk in self.stream_generator(chat_data["rag_chain"], message_input):
# print(chunk)
yield chunk
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating response: {str(e)}")
return StreamingResponse(generate_response(), media_type="text/event-stream")
async def stream_generator(self, rag_chain: any, message_input):
"""
Generates a stream of responses to user messages in a chat session.
Args:
rag_chain (any): A RunnableWithMessageHistory instance that can be used to generate responses to user messages in a chat session.
message_input (MessageInput): A dictionary containing the chat ID and the user message.
Yields:
bytes: A chunk of the generated response, encoded as bytes.
Raises:
Exception: If the specified chat ID is invalid.
Note:
The `stream_generator` function is an asynchronous generator that yields chunks of the generated response as bytes. It uses the `rag_chain` instance to generate responses to user messages and the `message_input` dictionary to provide the chat ID and user message. The function ensures that the answer chunk is converted to bytes before yielding it.
"""
try:
for chunk in rag_chain.stream(
{"input": message_input.message},
config={"configurable": {"session_id": message_input.chat_id}},
):
# Ensure chunk is converted to bytes
if chunk.get('answer'):
yield chunk.get('answer').encode('utf-8')
except Exception as e:
raise HTTPException(status_code=500, detail=f"Streaming error: {str(e)}")
async def chat_history(self, chat_id: str):
"""
Retrieves the chat history for the specified session ID.
Args:
chat_id (str): The unique identifier for the chat session.
Returns:
dict: A dictionary containing the chat history for the specified session ID. The dictionary has a single key, "history", which is a list of strings representing the chat messages.
Raises:
HTTPException: If the specified chat ID is invalid.
"""
if chat_id not in self.store:
raise HTTPException(status_code=400, detail="Invalid chat ID")
try:
history = self.store[chat_id]["history"].messages
return {"history": [str(msg) for msg in history]}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error retrieving chat history: {str(e)}")