-
Notifications
You must be signed in to change notification settings - Fork 581
🐛 Bugfix: Enhance prompt generation with knowledge base display names part2 #2813
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
Changes from all commits
9fb206e
7104317
5cba40e
bb402b8
edf66b4
9d2929e
65f7dec
399f379
95ceeef
66dd82a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,8 @@ | |||||
| from consts.exceptions import AppException | ||||||
| from database.agent_db import search_agent_info_by_agent_id, query_all_agent_info_by_tenant_id, \ | ||||||
| query_sub_agents_id_list | ||||||
| from database.tool_db import query_tools_by_ids | ||||||
| from database.knowledge_db import get_knowledge_name_map_by_index_names | ||||||
| from database.tool_db import query_tools_by_ids, query_tool_instances_by_id | ||||||
| from services.agent_service import ( | ||||||
| get_enable_tool_id_by_agent_id, | ||||||
| _check_agent_name_duplicate, | ||||||
|
|
@@ -29,7 +30,7 @@ | |||||
| logger = logging.getLogger("prompt_service") | ||||||
|
|
||||||
|
|
||||||
| def gen_system_prompt_streamable(agent_id: int, model_id: int, task_description: str, user_id: str, tenant_id: str, language: str, tool_ids: Optional[List[int]] = None, sub_agent_ids: Optional[List[int]] = None): | ||||||
| def gen_system_prompt_streamable(agent_id: int, model_id: int, task_description: str, user_id: str, tenant_id: str, language: str, tool_ids: Optional[List[int]] = None, sub_agent_ids: Optional[List[int]] = None, knowledge_base_display_names: Optional[List[str]] = None): | ||||||
| try: | ||||||
| for system_prompt in generate_and_save_system_prompt_impl( | ||||||
| agent_id=agent_id, | ||||||
|
|
@@ -39,7 +40,8 @@ | |||||
| tenant_id=tenant_id, | ||||||
| language=language, | ||||||
| tool_ids=tool_ids, | ||||||
| sub_agent_ids=sub_agent_ids | ||||||
| sub_agent_ids=sub_agent_ids, | ||||||
| knowledge_base_display_names=knowledge_base_display_names | ||||||
| ): | ||||||
| # SSE format, each message ends with \n\n | ||||||
| yield f"data: {json.dumps({'success': True, 'data': system_prompt}, ensure_ascii=False)}\n\n" | ||||||
|
|
@@ -63,7 +65,8 @@ | |||||
| tenant_id: str, | ||||||
| language: str, | ||||||
| tool_ids: Optional[List[int]] = None, | ||||||
| sub_agent_ids: Optional[List[int]] = None): | ||||||
| sub_agent_ids: Optional[List[int]] = None, | ||||||
| knowledge_base_display_names: Optional[List[str]] = None): | ||||||
| # Get description of tool and agent from frontend-provided IDs | ||||||
| # Frontend always provides tool_ids and sub_agent_ids (could be empty arrays) | ||||||
|
|
||||||
|
|
@@ -77,6 +80,18 @@ | |||||
| tool_info_list = get_enabled_tool_description_for_generate_prompt( | ||||||
| tenant_id=tenant_id, agent_id=agent_id) | ||||||
|
|
||||||
| # Get knowledge base display names for few-shot examples | ||||||
| # Priority: frontend-provided > database query | ||||||
| if knowledge_base_display_names: | ||||||
| logger.debug(f"Using frontend-provided knowledge base display names: {knowledge_base_display_names}") | ||||||
| else: | ||||||
| knowledge_base_display_names = get_knowledge_base_display_names( | ||||||
| tool_info_list=tool_info_list, | ||||||
| agent_id=agent_id, | ||||||
| tenant_id=tenant_id | ||||||
| ) | ||||||
| logger.debug(f"Using database query for knowledge base display names: {knowledge_base_display_names}") | ||||||
|
Comment on lines
+83
to
+93
|
||||||
|
|
||||||
| # Handle sub-agent IDs | ||||||
| if sub_agent_ids and len(sub_agent_ids) > 0: | ||||||
| sub_agent_info_list = [] | ||||||
|
|
@@ -114,7 +129,7 @@ | |||||
|
|
||||||
| # Collect results and yield non-name fields immediately, but hold name fields for duplicate checking | ||||||
| for result_data in generate_system_prompt(sub_agent_info_list, task_description, tool_info_list, tenant_id, | ||||||
| model_id, language): | ||||||
| model_id, language, knowledge_base_display_names): | ||||||
| result_type = result_data["type"] | ||||||
| final_results[result_type] = result_data["content"] | ||||||
|
|
||||||
|
|
@@ -223,7 +238,7 @@ | |||||
| raise Exception("Failed to generate prompt content.") | ||||||
|
|
||||||
|
|
||||||
| def generate_system_prompt(sub_agent_info_list, task_description, tool_info_list, tenant_id: str, model_id: int, language: str = LANGUAGE["ZH"]): | ||||||
| def generate_system_prompt(sub_agent_info_list, task_description, tool_info_list, tenant_id: str, model_id: int, language: str = LANGUAGE["ZH"], knowledge_base_display_names: Optional[List[str]] = None): | ||||||
| """Main function for generating system prompts""" | ||||||
| prompt_for_generate = get_prompt_generate_prompt_template(language) | ||||||
|
|
||||||
|
|
@@ -233,7 +248,8 @@ | |||||
| sub_agent_info_list=sub_agent_info_list, | ||||||
| task_description=task_description, | ||||||
| tool_info_list=tool_info_list, | ||||||
| language=language | ||||||
| language=language, | ||||||
| knowledge_base_display_names=knowledge_base_display_names | ||||||
| ) | ||||||
|
|
||||||
| # Initialize state | ||||||
|
|
@@ -352,7 +368,7 @@ | |||||
| last_results[tag] = latest[tag] | ||||||
|
|
||||||
|
|
||||||
| def join_info_for_generate_system_prompt(prompt_for_generate, sub_agent_info_list, task_description, tool_info_list, language: str = LANGUAGE["ZH"]): | ||||||
| def join_info_for_generate_system_prompt(prompt_for_generate, sub_agent_info_list, task_description, tool_info_list, language: str = LANGUAGE["ZH"], knowledge_base_display_names: Optional[List[str]] = None): | ||||||
| input_label = "Inputs" if language == 'en' else "接受输入" | ||||||
| output_label = "Output type" if language == 'en' else "返回输出类型" | ||||||
|
|
||||||
|
|
@@ -361,12 +377,21 @@ | |||||
| for tool in tool_info_list]) | ||||||
| assistant_description = "\n".join( | ||||||
| [f"- {sub_agent_info['name']}: {sub_agent_info['description']}" for sub_agent_info in sub_agent_info_list]) | ||||||
| # Generate content using template | ||||||
| content = Template(prompt_for_generate["USER_PROMPT"], undefined=StrictUndefined).render({ | ||||||
|
|
||||||
| # Build template context | ||||||
| template_context = { | ||||||
| "task_description": task_description, | ||||||
| "tool_description": tool_description, | ||||||
| "assistant_description": assistant_description | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| # Add knowledge base display names for few-shot examples if available | ||||||
| if knowledge_base_display_names: | ||||||
| kb_names_str = ", ".join(f'"{name}"' for name in knowledge_base_display_names) | ||||||
|
||||||
| kb_names_str = ", ".join(f'"{name}"' for name in knowledge_base_display_names) | |
| kb_names_str = ", ".join(json.dumps(name, ensure_ascii=False) for name in knowledge_base_display_names) |
Check failure on line 407 in backend/services/prompt_service.py
SonarQubeCloud / SonarCloud Code Analysis
Refactor this function to reduce its Cognitive Complexity from 21 to the 15 allowed.
See more on https://sonarcloud.io/project/issues?id=ModelEngine-Group_nexent&issues=AZ2awlpt0EzrWDwS91uu&open=AZ2awlpt0EzrWDwS91uu&pullRequest=2813
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -591,6 +591,18 @@ export default function AgentGenerateDetail({ | |||||||||||||||||||||||||||||||||||||
| businessLogicModelName: businessInfo.businessLogicModelName, | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Extract knowledge base display names from selected tools | ||||||||||||||||||||||||||||||||||||||
| // This allows the backend to use frontend-configured display names without database lookup | ||||||||||||||||||||||||||||||||||||||
| const knowledgeBaseDisplayNames: string[] = []; | ||||||||||||||||||||||||||||||||||||||
| if (Array.isArray(editedAgent.tools)) { | ||||||||||||||||||||||||||||||||||||||
| for (const tool of editedAgent.tools) { | ||||||||||||||||||||||||||||||||||||||
| if (typeof tool === "object" && tool.display_names && Array.isArray(tool.display_names)) { | ||||||||||||||||||||||||||||||||||||||
| knowledgeBaseDisplayNames.push(...tool.display_names); | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+598
to
+601
|
||||||||||||||||||||||||||||||||||||||
| if (Array.isArray(editedAgent.tools)) { | |
| for (const tool of editedAgent.tools) { | |
| if (typeof tool === "object" && tool.display_names && Array.isArray(tool.display_names)) { | |
| knowledgeBaseDisplayNames.push(...tool.display_names); | |
| const seenKnowledgeBaseDisplayNames = new Set<string>(); | |
| if (Array.isArray(editedAgent.tools)) { | |
| for (const tool of editedAgent.tools) { | |
| if ( | |
| typeof tool === "object" && | |
| tool.display_names && | |
| Array.isArray(tool.display_names) | |
| ) { | |
| for (const displayName of tool.display_names) { | |
| if (!seenKnowledgeBaseDisplayNames.has(displayName)) { | |
| seenKnowledgeBaseDisplayNames.add(displayName); | |
| knowledgeBaseDisplayNames.push(displayName); | |
| } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.