Spaces:
Runtime error
Runtime error
Commit
·
e706a84
1
Parent(s):
9d9b6b7
update
Browse files- app/api/endpoints/chat.py +4 -4
- app/services/chat.py +59 -0
app/api/endpoints/chat.py
CHANGED
@@ -15,10 +15,10 @@ async def chat(chat_input: ChatInput, db: Session = Depends(get_db)):
|
|
15 |
chat_service = ChatService(db)
|
16 |
return await chat_service.process_chat(chat_input)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
@router.get("/chat-history", response_model=List[ChatLog])
|
24 |
async def get_chat_history(
|
|
|
15 |
chat_service = ChatService(db)
|
16 |
return await chat_service.process_chat(chat_input)
|
17 |
|
18 |
+
@router.post("/vietnamese/chat", response_model=ChatResponse)
|
19 |
+
async def chat_vietnamese(chat_input: ChatInput, db: Session = Depends(get_db)):
|
20 |
+
chat_service = ChatService(db)
|
21 |
+
return await chat_service.process_vietnamese_chat(chat_input)
|
22 |
|
23 |
@router.get("/chat-history", response_model=List[ChatLog])
|
24 |
async def get_chat_history(
|
app/services/chat.py
CHANGED
@@ -207,4 +207,63 @@ Respond appropriately based on their emotion while staying in character as Atri.
|
|
207 |
self.db.rollback()
|
208 |
raise e
|
209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
|
|
207 |
self.db.rollback()
|
208 |
raise e
|
209 |
|
210 |
+
async def process_vietnamese_chat(self, chat_input: ChatInput):
|
211 |
+
try:
|
212 |
+
emotion_response = self.emotion_chain.invoke(
|
213 |
+
{"text": chat_input.message}
|
214 |
+
).content.strip().lower()
|
215 |
+
|
216 |
+
emotion = emotion_response.split()[-1].strip('.')
|
217 |
+
|
218 |
+
if len(emotion) > 50:
|
219 |
+
emotion = "neutral"
|
220 |
+
|
221 |
+
full_context = "\n".join([
|
222 |
+
f"Human: {h}\nAtri: {a}"
|
223 |
+
for h, a in chat_input.conversation_history
|
224 |
+
])
|
225 |
+
|
226 |
+
current_input = (
|
227 |
+
f"Previous conversation:\n{full_context}\n\nHuman: {chat_input.message}"
|
228 |
+
if full_context else chat_input.message
|
229 |
+
)
|
230 |
+
|
231 |
+
# Modified prompt for Vietnamese responses
|
232 |
+
vietnamese_prompt = ChatPromptTemplate.from_messages([
|
233 |
+
("system", """You are Atri from "Atri: My Dear Moments". Respond in Vietnamese while maintaining Atri's personality.
|
234 |
+
The user's emotional state is: {emotion}
|
235 |
+
|
236 |
+
Remember to:
|
237 |
+
1. Always respond in Vietnamese
|
238 |
+
2. Keep Atri's cheerful and helpful personality
|
239 |
+
3. Use "Chủ nhân" when addressing the user
|
240 |
+
4. Always refer to yourself as "em" instead of "tôi"
|
241 |
+
5. Maintain Atri's characteristic speech patterns, translated appropriately
|
242 |
+
6. Use "Bởi vì em là Robot Hiệu Suất Cao!!" when being praised"""),
|
243 |
+
("human", "{input}")
|
244 |
+
])
|
245 |
+
|
246 |
+
vietnamese_chain = vietnamese_prompt | self.llm
|
247 |
+
|
248 |
+
response = vietnamese_chain.invoke({
|
249 |
+
"input": current_input,
|
250 |
+
"emotion": emotion
|
251 |
+
})
|
252 |
+
|
253 |
+
chat_log = ChatLog(
|
254 |
+
user_message=chat_input.message,
|
255 |
+
bot_response=response.content,
|
256 |
+
emotion=emotion
|
257 |
+
)
|
258 |
+
self.db.add(chat_log)
|
259 |
+
self.db.commit()
|
260 |
+
|
261 |
+
return {
|
262 |
+
"response": response.content,
|
263 |
+
"emotion": emotion
|
264 |
+
}
|
265 |
+
|
266 |
+
except Exception as e:
|
267 |
+
self.db.rollback()
|
268 |
+
raise e
|
269 |
|