Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance goal generation #358

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions backend/app/rag/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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'

Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion backend/app/rag/chat_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions backend/app/rag/default_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Loading