-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathchat_flow.py
More file actions
825 lines (744 loc) · 32 KB
/
chat_flow.py
File metadata and controls
825 lines (744 loc) · 32 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import json
import logging
from datetime import datetime, UTC
from typing import List, Optional, Generator, Tuple, Any
from urllib.parse import urljoin
from uuid import UUID
import requests
from langfuse.llama_index import LlamaIndexInstrumentor
from langfuse.llama_index._context import langfuse_instrumentor_context
from llama_index.core import get_response_synthesizer
from llama_index.core.base.llms.types import ChatMessage, MessageRole
from llama_index.core.schema import NodeWithScore
from llama_index.core.prompts.rich import RichPromptTemplate
from sqlmodel import Session
from app.core.config import settings
from app.exceptions import ChatNotFound
from app.models import (
User,
Chat as DBChat,
ChatVisibility,
ChatMessage as DBChatMessage,
)
from app.rag.chat.config import ChatEngineConfig
from app.rag.chat.retrieve.retrieve_flow import SourceDocument, RetrieveFlow
from app.rag.chat.stream_protocol import (
ChatEvent,
ChatStreamDataPayload,
ChatStreamMessagePayload,
)
from app.rag.llms.dspy import get_dspy_lm_by_llama_llm
from app.rag.retrievers.knowledge_graph.schema import KnowledgeGraphRetrievalResult
from app.rag.types import ChatEventType, ChatMessageSate
from app.rag.utils import parse_goal_response_format
from app.repositories import chat_repo
from app.site_settings import SiteSetting
from app.utils.tracing import LangfuseContextManager
logger = logging.getLogger(__name__)
def parse_chat_messages(
chat_messages: List[ChatMessage],
) -> tuple[str, List[ChatMessage]]:
user_question = chat_messages[-1].content
chat_history = chat_messages[:-1]
return user_question, chat_history
class ChatFlow:
_trace_manager: LangfuseContextManager
def __init__(
self,
*,
db_session: Session,
user: User,
browser_id: str,
origin: str,
chat_messages: List[ChatMessage],
engine_name: str = "default",
chat_id: Optional[UUID] = None,
) -> None:
self.chat_id = chat_id
self.db_session = db_session
self.user = user
self.browser_id = browser_id
self.engine_name = engine_name
# Load chat engine and chat session.
self.user_question, self.chat_history = parse_chat_messages(chat_messages)
if chat_id:
# FIXME:
# only chat owner or superuser can access the chat,
# anonymous user can only access anonymous chat by track_id
self.db_chat_obj = chat_repo.get(self.db_session, chat_id)
if not self.db_chat_obj:
raise ChatNotFound(chat_id)
try:
self.engine_config = ChatEngineConfig.load_from_db(
db_session, self.db_chat_obj.engine.name
)
self.db_chat_engine = self.engine_config.get_db_chat_engine()
except Exception as e:
logger.error(f"Failed to load chat engine config: {e}")
self.engine_config = ChatEngineConfig.load_from_db(
db_session, engine_name
)
self.db_chat_engine = self.engine_config.get_db_chat_engine()
logger.info(
f"Init ChatFlow for chat {chat_id} (chat_engine: {self.db_chat_obj.engine.name})"
)
self.chat_history = [
ChatMessage(role=m.role, content=m.content, additional_kwargs={})
for m in chat_repo.get_messages(self.db_session, self.db_chat_obj)
]
else:
self.engine_config = ChatEngineConfig.load_from_db(db_session, engine_name)
self.db_chat_engine = self.engine_config.get_db_chat_engine()
self.db_chat_obj = chat_repo.create(
self.db_session,
DBChat(
# TODO: title should be generated by the LLM
title=self.user_question[:100],
engine_id=self.db_chat_engine.id,
engine_options=self.engine_config.screenshot(),
user_id=self.user.id if self.user else None,
browser_id=self.browser_id,
origin=origin,
visibility=(
ChatVisibility.PUBLIC
if not self.user
else ChatVisibility.PRIVATE
),
),
)
chat_id = self.db_chat_obj.id
# Notice: slack/discord bots may create a new chat with history messages.
now = datetime.now(UTC)
for i, m in enumerate(self.chat_history):
chat_repo.create_message(
session=self.db_session,
chat=self.db_chat_obj,
chat_message=DBChatMessage(
role=m.role,
content=m.content,
ordinal=i + 1,
created_at=now,
updated_at=now,
finished_at=now,
),
)
# Init Langfuse for tracing.
enable_langfuse = (
SiteSetting.langfuse_secret_key and SiteSetting.langfuse_public_key
)
instrumentor = LlamaIndexInstrumentor(
host=SiteSetting.langfuse_host,
secret_key=SiteSetting.langfuse_secret_key,
public_key=SiteSetting.langfuse_public_key,
enabled=enable_langfuse,
)
self._trace_manager = LangfuseContextManager(instrumentor)
# Init LLM.
self._llm = self.engine_config.get_llama_llm(self.db_session)
self._fast_llm = self.engine_config.get_fast_llama_llm(self.db_session)
self._fast_dspy_lm = get_dspy_lm_by_llama_llm(self._fast_llm)
# Load knowledge bases.
self.knowledge_bases = self.engine_config.get_knowledge_bases(self.db_session)
self.knowledge_base_ids = [kb.id for kb in self.knowledge_bases]
# Init retrieve flow.
self.retrieve_flow = RetrieveFlow(
db_session=self.db_session,
engine_name=self.engine_name,
engine_config=self.engine_config,
llm=self._llm,
fast_llm=self._fast_llm,
knowledge_bases=self.knowledge_bases,
)
def chat(self) -> Generator[ChatEvent | str, None, None]:
try:
with self._trace_manager.observe(
trace_name="ChatFlow",
user_id=(
self.user.email if self.user else f"anonymous-{self.browser_id}"
),
metadata={
"is_external_engine": self.engine_config.is_external_engine,
"chat_engine_config": self.engine_config.screenshot(),
},
tags=[f"chat_engine:{self.engine_name}"],
release=settings.ENVIRONMENT,
) as trace:
trace.update(
input={
"user_question": self.user_question,
"chat_history": self.chat_history,
}
)
if self.engine_config.is_external_engine:
yield from self._external_chat()
else:
response_text, source_documents = yield from self._builtin_chat()
trace.update(output=response_text)
except Exception as e:
logger.exception(e)
yield ChatEvent(
event_type=ChatEventType.ERROR_PART,
payload="Encountered an error while processing the chat. Please try again later.",
)
def _builtin_chat(
self,
) -> Generator[ChatEvent | str, None, Tuple[Optional[str], List[Any]]]:
ctx = langfuse_instrumentor_context.get().copy()
db_user_message, db_assistant_message = yield from self._chat_start()
langfuse_instrumentor_context.get().update(ctx)
# 1. Retrieve Knowledge graph related to the user question.
(
knowledge_graph,
knowledge_graph_context,
) = yield from self._search_knowledge_graph(user_question=self.user_question)
# 2. Refine the user question using knowledge graph and chat history.
refined_question = yield from self._refine_user_question(
user_question=self.user_question,
chat_history=self.chat_history,
knowledge_graph_context=knowledge_graph_context,
refined_question_prompt=self.engine_config.llm.condense_question_prompt,
)
# 3. Check if the question provided enough context information or need to clarify.
if self.engine_config.clarify_question:
need_clarify, need_clarify_response = yield from self._clarify_question(
user_question=refined_question,
chat_history=self.chat_history,
knowledge_graph_context=knowledge_graph_context,
)
if need_clarify:
yield from self._chat_finish(
db_assistant_message=db_assistant_message,
db_user_message=db_user_message,
response_text=need_clarify_response,
knowledge_graph=knowledge_graph,
source_documents=[],
)
return None, []
# 4. Use refined question to search for relevant chunks.
relevant_chunks = yield from self._search_relevance_chunks(
user_question=refined_question
)
# 5. Generate a response using the refined question and related chunks
response_text, source_documents = yield from self._generate_answer(
user_question=refined_question,
knowledge_graph_context=knowledge_graph_context,
relevant_chunks=relevant_chunks,
)
yield from self._chat_finish(
db_assistant_message=db_assistant_message,
db_user_message=db_user_message,
response_text=response_text,
knowledge_graph=knowledge_graph,
source_documents=source_documents,
)
return response_text, source_documents
def _chat_start(
self,
) -> Generator[ChatEvent, None, Tuple[DBChatMessage, DBChatMessage]]:
db_user_message = chat_repo.create_message(
session=self.db_session,
chat=self.db_chat_obj,
chat_message=DBChatMessage(
role=MessageRole.USER.value,
trace_url=self._trace_manager.trace_url,
content=self.user_question.strip(),
),
)
db_assistant_message = chat_repo.create_message(
session=self.db_session,
chat=self.db_chat_obj,
chat_message=DBChatMessage(
role=MessageRole.ASSISTANT.value,
trace_url=self._trace_manager.trace_url,
content="",
),
)
yield ChatEvent(
event_type=ChatEventType.DATA_PART,
payload=ChatStreamDataPayload(
chat=self.db_chat_obj,
user_message=db_user_message,
assistant_message=db_assistant_message,
),
)
return db_user_message, db_assistant_message
def _search_knowledge_graph(
self,
user_question: str,
annotation_silent: bool = False,
) -> Generator[ChatEvent, None, Tuple[KnowledgeGraphRetrievalResult, str]]:
kg_config = self.engine_config.knowledge_graph
if kg_config is None or kg_config.enabled is False:
return KnowledgeGraphRetrievalResult(), ""
with self._trace_manager.span(
name="search_knowledge_graph", input=user_question
) as span:
if not annotation_silent:
if kg_config.using_intent_search:
yield ChatEvent(
event_type=ChatEventType.MESSAGE_ANNOTATIONS_PART,
payload=ChatStreamMessagePayload(
state=ChatMessageSate.KG_RETRIEVAL,
display="Identifying The Question's Intents and Perform Knowledge Graph Search",
),
)
else:
yield ChatEvent(
event_type=ChatEventType.MESSAGE_ANNOTATIONS_PART,
payload=ChatStreamMessagePayload(
state=ChatMessageSate.KG_RETRIEVAL,
display="Searching the Knowledge Graph for Relevant Context",
),
)
knowledge_graph, knowledge_graph_context = (
self.retrieve_flow.search_knowledge_graph(user_question)
)
span.end(
output={
"knowledge_graph": knowledge_graph,
"knowledge_graph_context": knowledge_graph_context,
}
)
return knowledge_graph, knowledge_graph_context
def _refine_user_question(
self,
user_question: str,
chat_history: Optional[List[ChatMessage]] = [],
refined_question_prompt: Optional[str] = None,
knowledge_graph_context: str = "",
annotation_silent: bool = False,
) -> Generator[ChatEvent, None, str]:
with self._trace_manager.span(
name="refine_user_question",
input={
"user_question": user_question,
"chat_history": chat_history,
"knowledge_graph_context": knowledge_graph_context,
},
) as span:
if not annotation_silent:
yield ChatEvent(
event_type=ChatEventType.MESSAGE_ANNOTATIONS_PART,
payload=ChatStreamMessagePayload(
state=ChatMessageSate.REFINE_QUESTION,
display="Query Rewriting for Enhanced Information Retrieval",
),
)
prompt_template = RichPromptTemplate(refined_question_prompt)
refined_question = self._fast_llm.predict(
prompt_template,
graph_knowledges=knowledge_graph_context,
chat_history=chat_history,
question=user_question,
current_date=datetime.now().strftime("%Y-%m-%d"),
)
if not annotation_silent:
yield ChatEvent(
event_type=ChatEventType.MESSAGE_ANNOTATIONS_PART,
payload=ChatStreamMessagePayload(
state=ChatMessageSate.REFINE_QUESTION,
message=refined_question,
),
)
span.end(output=refined_question)
return refined_question
def _clarify_question(
self,
user_question: str,
chat_history: Optional[List[ChatMessage]] = [],
knowledge_graph_context: str = "",
) -> Generator[ChatEvent, None, Tuple[bool, str]]:
"""
Check if the question clear and provided enough context information, otherwise, it is necessary to
stop the conversation early and ask the user for the further clarification.
Args:
user_question: str
knowledge_graph_context: str
Returns:
bool: Determine whether further clarification of the issue is needed from the user.
str: The content of the questions that require clarification from the user.
"""
with self._trace_manager.span(
name="clarify_question",
input={
"user_question": user_question,
"knowledge_graph_context": knowledge_graph_context,
},
) as span:
prompt_template = RichPromptTemplate(
self.engine_config.llm.clarifying_question_prompt
)
prediction = self._fast_llm.predict(
prompt_template,
graph_knowledges=knowledge_graph_context,
chat_history=chat_history,
question=user_question,
)
# TODO: using structured output to get the clarity result.
clarity_result = prediction.strip().strip(".\"'!")
need_clarify = clarity_result.lower() != "false"
need_clarify_response = clarity_result if need_clarify else ""
if need_clarify:
yield ChatEvent(
event_type=ChatEventType.TEXT_PART,
payload=need_clarify_response,
)
span.end(
output={
"need_clarify": need_clarify,
"need_clarify_response": need_clarify_response,
}
)
return need_clarify, need_clarify_response
def _search_relevance_chunks(
self, user_question: str
) -> Generator[ChatEvent, None, List[NodeWithScore]]:
with self._trace_manager.span(
name="search_relevance_chunks", input=user_question
) as span:
yield ChatEvent(
event_type=ChatEventType.MESSAGE_ANNOTATIONS_PART,
payload=ChatStreamMessagePayload(
state=ChatMessageSate.SEARCH_RELATED_DOCUMENTS,
display="Retrieving the Most Relevant Documents",
),
)
relevance_chunks = self.retrieve_flow.search_relevant_chunks(user_question)
span.end(
output={
"relevance_chunks": relevance_chunks,
}
)
return relevance_chunks
def _generate_answer(
self,
user_question: str,
knowledge_graph_context: str,
relevant_chunks: List[NodeWithScore],
) -> Generator[ChatEvent, None, Tuple[str, List[SourceDocument]]]:
with self._trace_manager.span(
name="generate_answer", input=user_question
) as span:
# Initialize response synthesizer.
text_qa_template = RichPromptTemplate(
template_str=self.engine_config.llm.text_qa_prompt
)
text_qa_template = text_qa_template.partial_format(
current_date=datetime.now().strftime("%Y-%m-%d"),
graph_knowledges=knowledge_graph_context,
original_question=self.user_question,
)
response_synthesizer = get_response_synthesizer(
llm=self._llm, text_qa_template=text_qa_template, streaming=True
)
# Initialize response.
response = response_synthesizer.synthesize(
query=user_question,
nodes=relevant_chunks,
)
source_documents = self.retrieve_flow.get_source_documents_from_nodes(
response.source_nodes
)
yield ChatEvent(
event_type=ChatEventType.MESSAGE_ANNOTATIONS_PART,
payload=ChatStreamMessagePayload(
state=ChatMessageSate.SOURCE_NODES,
context=source_documents,
),
)
# Generate response.
yield ChatEvent(
event_type=ChatEventType.MESSAGE_ANNOTATIONS_PART,
payload=ChatStreamMessagePayload(
state=ChatMessageSate.GENERATE_ANSWER,
display="Generating a Precise Answer with AI",
),
)
response_text = ""
for word in response.response_gen:
response_text += word
yield ChatEvent(
event_type=ChatEventType.TEXT_PART,
payload=word,
)
if not response_text:
raise Exception("Got empty response from LLM")
span.end(
output=response_text,
metadata={
"source_documents": source_documents,
},
)
return response_text, source_documents
def _post_verification(
self, user_question: str, response_text: str, chat_id: UUID, message_id: int
) -> Optional[str]:
# post verification to external service, will return the post verification result url
post_verification_url = self.engine_config.post_verification_url
post_verification_token = self.engine_config.post_verification_token
if not post_verification_url:
return None
external_request_id = f"{chat_id}_{message_id}"
qa_content = f"User question: {user_question}\n\nAnswer:\n{response_text}"
with self._trace_manager.span(
name="post_verification",
input={
"external_request_id": external_request_id,
"qa_content": qa_content,
},
) as span:
try:
resp = requests.post(
post_verification_url,
json={
"external_request_id": external_request_id,
"qa_content": qa_content,
},
headers=(
{
"Authorization": f"Bearer {post_verification_token}",
}
if post_verification_token
else {}
),
timeout=10,
)
resp.raise_for_status()
job_id = resp.json()["job_id"]
post_verification_link = urljoin(
f"{post_verification_url}/", str(job_id)
)
span.end(
output={
"post_verification_link": post_verification_link,
}
)
return post_verification_link
except Exception as e:
logger.exception("Failed to post verification: %s", e.message)
return None
def _chat_finish(
self,
db_assistant_message: ChatMessage,
db_user_message: ChatMessage,
response_text: str,
knowledge_graph: KnowledgeGraphRetrievalResult = KnowledgeGraphRetrievalResult(),
source_documents: Optional[List[SourceDocument]] = [],
annotation_silent: bool = False,
):
if not annotation_silent:
yield ChatEvent(
event_type=ChatEventType.MESSAGE_ANNOTATIONS_PART,
payload=ChatStreamMessagePayload(
state=ChatMessageSate.FINISHED,
),
)
post_verification_result_url = self._post_verification(
self.user_question,
response_text,
self.db_chat_obj.id,
db_assistant_message.id,
)
db_assistant_message.sources = [s.model_dump() for s in source_documents]
db_assistant_message.graph_data = knowledge_graph.to_stored_graph_dict()
db_assistant_message.content = response_text
db_assistant_message.post_verification_result_url = post_verification_result_url
db_assistant_message.updated_at = datetime.now(UTC)
db_assistant_message.finished_at = datetime.now(UTC)
self.db_session.add(db_assistant_message)
db_user_message.graph_data = knowledge_graph.to_stored_graph_dict()
db_user_message.updated_at = datetime.now(UTC)
db_user_message.finished_at = datetime.now(UTC)
self.db_session.add(db_user_message)
self.db_session.commit()
yield ChatEvent(
event_type=ChatEventType.DATA_PART,
payload=ChatStreamDataPayload(
chat=self.db_chat_obj,
user_message=db_user_message,
assistant_message=db_assistant_message,
),
)
# TODO: Separate _external_chat() method into another ExternalChatFlow class, but at the same time, we need to
# share some common methods through ChatMixin or BaseChatFlow.
def _external_chat(self) -> Generator[ChatEvent | str, None, None]:
ctx = langfuse_instrumentor_context.get().copy()
db_user_message, db_assistant_message = yield from self._chat_start()
langfuse_instrumentor_context.get().update(ctx)
cache_messages = None
goal, response_format = self.user_question, {}
if settings.ENABLE_QUESTION_CACHE and len(self.chat_history) == 0:
try:
logger.info(
f"start to find_best_answer_for_question with question: {self.user_question}"
)
cache_messages = chat_repo.find_best_answer_for_question(
self.db_session, self.user_question
)
if cache_messages and len(cache_messages) > 0:
logger.info(
f"find_best_answer_for_question result {len(cache_messages)} for question {self.user_question}"
)
except Exception as e:
logger.error(
f"Failed to find best answer for question {self.user_question}: {e}"
)
if not cache_messages or len(cache_messages) == 0:
try:
# 1. Generate the goal with the user question, knowledge graph and chat history.
goal, response_format = yield from self._generate_goal()
# 2. Check if the goal provided enough context information or need to clarify.
if self.engine_config.clarify_question:
(
need_clarify,
need_clarify_response,
) = yield from self._clarify_question(
user_question=goal, chat_history=self.chat_history
)
if need_clarify:
yield from self._chat_finish(
db_assistant_message=db_assistant_message,
db_user_message=db_user_message,
response_text=need_clarify_response,
annotation_silent=True,
)
return
except Exception as e:
goal = self.user_question
logger.warning(
f"Failed to generate refined goal, fallback to use user question as goal directly: {e}",
exc_info=True,
extra={},
)
cache_messages = None
if settings.ENABLE_QUESTION_CACHE:
try:
logger.info(
f"start to find_recent_assistant_messages_by_goal with goal: {goal}, response_format: {response_format}"
)
cache_messages = chat_repo.find_recent_assistant_messages_by_goal(
self.db_session,
{"goal": goal, "Lang": response_format.get("Lang", "English")},
90,
)
logger.info(
f"find_recent_assistant_messages_by_goal result {len(cache_messages)} for goal {goal}"
)
except Exception as e:
logger.error(
f"Failed to find recent assistant messages by goal: {e}"
)
stream_chat_api_url = (
self.engine_config.external_engine_config.stream_chat_api_url
)
if cache_messages and len(cache_messages) > 0:
stackvm_response_text = cache_messages[0].content
task_id = cache_messages[0].meta.get("task_id")
for chunk in stackvm_response_text.split(". "):
if chunk:
if not chunk.endswith("."):
chunk += ". "
yield ChatEvent(
event_type=ChatEventType.TEXT_PART,
payload=chunk,
)
else:
logger.debug(
f"Chatting with external chat engine (api_url: {stream_chat_api_url}) to answer for user question: {self.user_question}"
)
chat_params = {
"goal": goal,
"response_format": response_format,
"namespace_name": "Default",
}
res = requests.post(stream_chat_api_url, json=chat_params, stream=True)
# Notice: External type chat engine doesn't support non-streaming mode for now.
stackvm_response_text = ""
task_id = None
for line in res.iter_lines():
if not line:
continue
# Append to final response text.
chunk = line.decode("utf-8")
if chunk.startswith("0:"):
word = json.loads(chunk[2:])
stackvm_response_text += word
yield ChatEvent(
event_type=ChatEventType.TEXT_PART,
payload=word,
)
else:
yield line + b"\n"
try:
if chunk.startswith("8:") and task_id is None:
states = json.loads(chunk[2:])
if len(states) > 0:
# accesss task by http://endpoint/?task_id=$task_id
task_id = states[0].get("task_id")
except Exception as e:
logger.error(f"Failed to get task_id from chunk: {e}")
response_text = stackvm_response_text
base_url = stream_chat_api_url.replace("/api/stream_execute_vm", "")
try:
post_verification_result_url = self._post_verification(
goal,
response_text,
self.db_chat_obj.id,
db_assistant_message.id,
)
db_assistant_message.post_verification_result_url = (
post_verification_result_url
)
except Exception:
logger.error(
"Specific error occurred during post verification job.", exc_info=True
)
trace_url = f"{base_url}?task_id={task_id}" if task_id else ""
message_meta = {
"task_id": task_id,
"goal": goal,
**response_format,
}
db_assistant_message.content = response_text
db_assistant_message.trace_url = trace_url
db_assistant_message.meta = message_meta
db_assistant_message.updated_at = datetime.now(UTC)
db_assistant_message.finished_at = datetime.now(UTC)
self.db_session.add(db_assistant_message)
db_user_message.trace_url = trace_url
db_user_message.meta = message_meta
db_user_message.updated_at = datetime.now(UTC)
db_user_message.finished_at = datetime.now(UTC)
self.db_session.add(db_user_message)
self.db_session.commit()
yield ChatEvent(
event_type=ChatEventType.DATA_PART,
payload=ChatStreamDataPayload(
chat=self.db_chat_obj,
user_message=db_user_message,
assistant_message=db_assistant_message,
),
)
def _generate_goal(self) -> Generator[ChatEvent, None, Tuple[str, dict]]:
try:
refined_question = yield from self._refine_user_question(
user_question=self.user_question,
chat_history=self.chat_history,
refined_question_prompt=self.engine_config.llm.generate_goal_prompt,
annotation_silent=True,
)
goal = refined_question.strip()
if goal.startswith("Goal: "):
goal = goal[len("Goal: ") :].strip()
except Exception as e:
logger.error(f"Failed to refine question with related knowledge graph: {e}")
goal = self.user_question
response_format = {}
try:
clean_goal, response_format = parse_goal_response_format(goal)
logger.info(f"clean goal: {clean_goal}, response_format: {response_format}")
if clean_goal:
goal = clean_goal
except Exception as e:
logger.error(f"Failed to parse goal and response format: {e}")
return goal, response_format