diff --git a/backend/app/rag/chat.py b/backend/app/rag/chat.py index 43767504..6df45b8c 100644 --- a/backend/app/rag/chat.py +++ b/backend/app/rag/chat.py @@ -504,24 +504,26 @@ def _external_chat(self) -> Generator[ChatEvent | str, None, None]: ), ) - refined_question = self.user_question + goal = self.user_question try: _fast_llm = self.chat_engine_config.get_fast_llama_llm(self.db_session) - refined_question = _fast_llm.predict( + goal = _fast_llm.predict( get_prompt_by_jinja2_template( - self.chat_engine_config.llm.condense_question_prompt, - graph_knowledges="", + self.chat_engine_config.llm.generate_goal_prompt, chat_history=self.chat_history, question=self.user_question, ), ) + goal = goal.strip() + if goal.startswith("Goal: "): + goal = goal[len("Goal: "):].strip() except Exception as e: logger.error(f"Failed to refine question: {e}") stream_chat_api_url = self.chat_engine_config.external_engine_config.stream_chat_api_url 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": refined_question + "goal": goal, } res = requests.post(stream_chat_api_url, json=chat_params, stream=True) @@ -537,6 +539,10 @@ def _external_chat(self) -> Generator[ChatEvent | str, None, None]: 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' @@ -549,6 +555,7 @@ def _external_chat(self) -> Generator[ChatEvent | str, None, None]: except Exception as e: logger.error(f"Failed to get task_id from chunk: {e}") + """ try: response_text = "" final_answer_gen = _fast_llm.stream( @@ -572,14 +579,15 @@ def _external_chat(self) -> Generator[ChatEvent | str, None, None]: payload=word, ) logger.error(f"Failed to refine question: {e}") - + """ + response_text = stackvm_response_text base_url = stream_chat_api_url.replace('/api/stream_execute_vm', '') db_assistant_message.content = response_text db_assistant_message.trace_url = f"{base_url}?task_id={task_id}" if task_id else "" db_assistant_message.meta = { "task_id": task_id, "stackvm_response_text": stackvm_response_text, - "goal": refined_question, + "goal": goal, } db_assistant_message.updated_at = datetime.now(UTC) db_assistant_message.finished_at = datetime.now(UTC) @@ -588,7 +596,7 @@ def _external_chat(self) -> Generator[ChatEvent | str, None, None]: db_user_message.meta = { "task_id": task_id, "stackvm_response_text": stackvm_response_text, - "goal": refined_question, + "goal": goal, } db_user_message.updated_at = datetime.now(UTC) db_user_message.finished_at = datetime.now(UTC) diff --git a/backend/app/rag/chat_config.py b/backend/app/rag/chat_config.py index 52e4e068..ce61c5b1 100644 --- a/backend/app/rag/chat_config.py +++ b/backend/app/rag/chat_config.py @@ -39,6 +39,7 @@ DEFAULT_REFINE_PROMPT, DEFAULT_FURTHER_QUESTIONS_PROMPT, DEFAULT_CONDENSE_ANSWER_PROMPT, + DEFAULT_GENERATE_GOAL_PROMPT, ) from app.models import ( ChatEngine as DBChatEngine, @@ -61,7 +62,7 @@ class LLMOption(BaseModel): refine_prompt: str = DEFAULT_REFINE_PROMPT further_questions_prompt: str = DEFAULT_FURTHER_QUESTIONS_PROMPT condense_answer_prompt: str = DEFAULT_CONDENSE_ANSWER_PROMPT - + generate_goal_prompt: str = DEFAULT_GENERATE_GOAL_PROMPT class VectorSearchOption(BaseModel): metadata_post_filters: Optional[MetadataFilters] = None diff --git a/backend/app/rag/default_prompt.py b/backend/app/rag/default_prompt.py index da952d94..62624638 100644 --- a/backend/app/rag/default_prompt.py +++ b/backend/app/rag/default_prompt.py @@ -262,4 +262,52 @@ - Do not add new information or opinions. Output the final answer for the user directly. +""" + +DEFAULT_GENERATE_GOAL_PROMPT = """\ +Given the conversation history between the User and Assistant, along with the latest follow-up question from the User, perform the following tasks: + +1. **Language Detection**: + - Analyze the User's Follow-up question to determine the language used. + +2. **Goal Generation**: + - Determine the latest User intent from the Follow-up question and the Chat history. + - Reformulate the latest User Follow-up question into a clear, standalone question suitable for processing by the agent. + - Specify the detected language for the answer. + - Define the desired answer format. + - Include any additional requirements as needed. + +3. **Output**: + - Produce a goal string in the following format: + "[Refined Question] (Answer language: [Detected Language], Answer format: [Format], Include necessary citations from source_uri)" + +**Example**: + +Chat history: + +[] + +Follow-up question: + +"tidb encryption at rest 会影响数据压缩比例吗?" + +Goal: + +"Does encryption at rest in TiDB affect the data compression ratio? (Answer language: Chinese, Answer format: text, Include necessary citations from source_uri)" + +**Your Task**: + +Chat history: + +{{chat_history}} + +--------------------- + +Follow-up question: + +{{question}} + +--------------------- + +Goal: """ \ No newline at end of file