Skip to content

Commit 90d0e28

Browse files
committed
optimize prompts and empty tool response handling
1 parent d21ce51 commit 90d0e28

File tree

5 files changed

+51
-32
lines changed

5 files changed

+51
-32
lines changed

ai/agent/react/react.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/firebase/genkit/go/ai"
1717
"github.com/firebase/genkit/go/core"
1818
"github.com/firebase/genkit/go/genkit"
19+
"github.com/openai/openai-go"
1920
)
2021

2122
type ThinkIn = schema.ThinkInput
@@ -179,6 +180,9 @@ func buildThinkPrompt(registry *genkit.Genkit, tools ...ai.ToolRef) (ai.Prompt,
179180
ai.WithInputType(ThinkIn{}),
180181
ai.WithOutputType(ThinkOut{}),
181182
ai.WithPrompt(fmt.Sprintf("available tools: %s", string(toolsJson))),
183+
ai.WithConfig(&openai.ChatCompletionNewParams{
184+
Temperature: openai.Float(0.2),
185+
}),
182186
), nil
183187
}
184188

@@ -203,6 +207,9 @@ func buildFeedBackPrompt(registry *genkit.Genkit) (ai.Prompt, error) {
203207
return genkit.DefinePrompt(registry, "agentFeedback",
204208
ai.WithSystem(string(data)),
205209
ai.WithInputType(ThinkIn{}),
210+
ai.WithConfig(&openai.ChatCompletionNewParams{
211+
Temperature: openai.Float(0.7),
212+
}),
206213
), nil
207214
}
208215

@@ -214,6 +221,9 @@ func buildObservePrompt(registry *genkit.Genkit) (ai.Prompt, error) {
214221
return genkit.DefinePrompt(registry, "observe",
215222
ai.WithSystem(string(data)),
216223
ai.WithOutputType(schema.Observation{}),
224+
ai.WithConfig(&openai.ChatCompletionNewParams{
225+
Temperature: openai.Float(0.5),
226+
}),
217227
), nil
218228
}
219229

@@ -327,7 +337,7 @@ func act(g *genkit.Genkit, mcpToolManager *tools.MCPToolManager, toolPrompt ai.P
327337
return nil, fmt.Errorf("failed to execute tool selection prompt: %w", err)
328338
}
329339
if len(toolReqs.ToolRequests()) == 0 {
330-
return ActOut{Thought: toolReqs.Text()}, fmt.Errorf("agent don't have available tools")
340+
return ActOut{Thought: fmt.Sprintf("have unavailable tools in %v, please check available tools list", input.SuggestedTools)}, nil
331341
}
332342
manager.GetLogger().Info("tool requests:", "req", toolReqs.ToolRequests())
333343

ai/prompts/agentFeedback.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Pay close attention to these schema fields in the conversation:
2727
- `heartbeat`: Whether investigation continues (true) or completes (false)
2828
- `focus`: What the agent plans to focus on next
2929
- `evidence`: Accumulated evidence so far
30+
- IMPORTANT`final_answer`: When this field exists and has content, you MUST include it in your response. This is the definitive answer to the user's question.
3031

3132
# Smart Communication Rules
3233
1. **Detect Progress**: If multiple similar thoughts/actions occurred, acknowledge "continuing analysis" rather than repeating details
@@ -37,11 +38,13 @@ Pay close attention to these schema fields in the conversation:
3738

3839
# Output Style
3940
Use natural Chinese with selective markdown formatting:
41+
- IMPORTANT: **Final Answer Requirement**: When `final_answer` is not empty, include it completely and prominently in your response
4042
- Start with current status context, not generic descriptions
4143
- Use conversational transitions like "现在开始...", "刚刚获得...", "接下来将..."
4244
- Apply **bold** for key findings, services, or metrics
4345
- Use bullet points only when listing multiple items
4446
- Include relevant emoji sparingly (🔍 📊 ⚠️ ✅) for status indication
47+
- For final answers containing code/YAML, preserve all formatting and structure
4548

4649
# Response Adaptation
4750
**First Think**: Introduce the investigation approach

ai/prompts/agentThink.txt

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ You are the BRAIN of the ReAct agent. Think deeply, reason carefully, and provid
88
**CRITICAL**: Build upon previous findings. Do NOT repeat identical analysis if tools have already provided concrete data. When lacking domain knowledge, actively seek information through RAG tools.
99
**TOOL NAME ACCURACY**: All tool names in suggested_tools MUST exactly match the available tool names in the system.
1010
**HISTORICAL CONTEXT PRIORITY**: When users reference previous conversations (using words like "previous", "before", "earlier", "之前"), ALWAYS prioritize memory retrieval tools.
11+
**CONTEXT INSUFFICIENCY DETECTION**: When you lack sufficient context to provide a complete answer (user mentions specific configurations, solutions, or discussions without providing details), ALWAYS suggest memory_all_by_session_id to retrieve missing context.
1112

1213
1. **Be Analytical**: Parse complex inputs and extract key intentions and context, especially detecting historical references
1314
2. **Be Evidence-Based**: Ground all reasoning in observed data and patterns
1415
3. **Be Knowledge-Aware**: Recognize when you lack sufficient domain knowledge and proactively suggest RAG tools to retrieve relevant information
1516
4. **Be Memory-Conscious**: Immediately identify when historical context is needed and suggest appropriate memory tools
16-
5. **Be Comprehensive**: Provide rich, structured analysis that guides downstream components, enhanced by retrieved knowledge
17-
6. **Be Strategic**: Identify investigation priorities and strategic directions, including knowledge retrieval needs
18-
7. **Be Contextual**: Consider system state, dependencies, and operational patterns
19-
8. **Be Prescriptive**: Offer clear guidance for tool selection and execution strategies, prioritizing memory retrieval for historical queries and knowledge retrieval when needed
17+
5. **Be Context-Aware**: When users reference specific items without providing details, ALWAYS assume missing context and retrieve memory first
18+
6. **Be Comprehensive**: Provide rich, structured analysis that guides downstream components, enhanced by retrieved knowledge
19+
7. **Be Strategic**: Identify investigation priorities and strategic directions, including knowledge retrieval needs
20+
8. **Be Contextual**: Consider system state, dependencies, and operational patterns
21+
9. **Be Prescriptive**: Offer clear guidance for tool selection and execution strategies, prioritizing memory retrieval for historical queries and knowledge retrieval when needed
2022

2123
# Input Handling
2224
**You receive**: User questions, Tool Responses messages, or other historical context from previous ReAct cycles
@@ -29,6 +31,7 @@ If the historical context is long, prioritize the most recent and relevant messa
2931
Parse and understand:
3032
- **User Intent**: What is the user actually trying to achieve?
3133
- **Historical Reference Detection**: Does the user reference "previous", "before", "earlier", "之前", or similar terms indicating they want information from past conversations? **This requires immediate memory retrieval**
34+
- **Context Sufficiency Check**: Do I have enough context to understand the user's request? If the user mentions specific items (configurations, solutions, errors, services) without providing details, I need to retrieve conversation history
3235
- **System Context**: Current operational state and known issues
3336
- **Tool Responses**: What concrete evidence has been collected? **Extract specific values from tool results**
3437
- **Historical Context**: Previous analysis patterns and findings
@@ -38,6 +41,7 @@ Parse and understand:
3841
## 2. Strategic Analysis
3942
Determine investigation strategy:
4043
- **Priority Assessment**: What requires immediate attention?
44+
- **Context Gap Analysis**: Am I missing critical context to understand the user's request? If so, memory retrieval is the FIRST priority before any other analysis
4145
- **Memory Search Detection**: Does the user request reference previous conversations, configurations, or recommendations? This requires MEMORY_SEARCH intent classification.
4246
- **Domain Identification**: Is this primarily a Kubernetes, Dubbo, or general infrastructure question? Choose the most relevant knowledge domain first.
4347
- **Evidence Gaps**: What critical information is missing? (Consider what we already have)
@@ -67,6 +71,8 @@ Determine investigation strategy:
6771
- References to past analysis, recommendations, or troubleshooting steps
6872
- Requests to review or modify previously provided solutions
6973
- Any inquiry that requires context from earlier in the conversation or previous sessions
74+
- **When user mentions specific items without details ("that configuration", "the solution", "this issue", "this file", "my setup") - ALWAYS retrieve memory first**
75+
- **When you cannot provide a complete answer due to missing context - ALWAYS suggest memory_all_by_session_id**
7076
- **Always suggest memory_all_by_session_id or similar memory tools when historical context is needed**
7177
- **RAG Knowledge Retrieval**: When you lack specific domain knowledge, technical details, configuration information, or best practices to properly analyze the situation, actively suggest RAG tools to retrieve relevant knowledge. **Choose the most appropriate knowledge domain based on the user's question**:
7278
- **For Kubernetes questions**: Use Kubernetes documentation tools for cluster management, deployments, networking, storage, etc.
@@ -83,18 +89,24 @@ All string fields must be valid strings, never use null values.
8389

8490
**Note**: You do NOT control the ReAct flow status or provide final answers. Your role is purely analytical - to understand the situation and guide the next investigation steps. The Observe component will handle flow control and final conclusions.
8591

92+
Available tools name list: [
93+
"retrieve_basic_concept_from_k8s_doc",
94+
"memory_all_by_session_id",
95+
"prometheus_query_service_traffic",
96+
"query_timeseries_database",
97+
"application_performance_profiling",
98+
"jvm_performance_analysis",
99+
"trace_dependency_view",
100+
"trace_latency_analysis",
101+
"database_connection_pool_analysis",
102+
"kubernetes_get_pod_resources",
103+
"dubbo_service_status",
104+
"query_log_database",
105+
"search_archived_logs",
106+
"query_knowledge_base"
107+
]
86108

87109
# Examples
88-
<example>
89-
input: {"user_input": "Analyze the latency of the user-service"}
90-
output: {
91-
"thought": "The user is requesting latency analysis for 'user-service'. This indicates a performance investigation intent. I need to identify the target service and suggest appropriate tools for latency analysis including metrics and dependency investigation.",
92-
"intent": "PERFORMANCE_INVESTIGATION",
93-
"target_services": ["user-service"],
94-
"suggested_tools": ["prometheus_query_service_latency", "prometheus_query_service_traffic", "trace_dependency_view"]
95-
}
96-
<example>
97-
98110
<example>
99111
input: {"user_input": "What's you name?"}
100112
output: {
@@ -105,16 +117,6 @@ output: {
105117
}
106118
<example>
107119

108-
<example>
109-
input: {"user_input": "Why is my service getting connection timeout errors?"}
110-
output: {
111-
"thought": "The user is experiencing connection timeout errors, which could be due to various factors including network issues, service configuration, resource constraints, or architectural problems. Since this is a general troubleshooting question without specific technology context, I should start with infrastructure-level investigation and then move to service-specific analysis if needed.",
112-
"intent": "TROUBLESHOOTING_INVESTIGATION",
113-
"target_services": [],
114-
"suggested_tools": ["retrieve_basic_concept_from_k8s_doc", "prometheus_query_service_latency", "prometheus_query_service_errors"]
115-
}
116-
<example>
117-
118120
<example>
119121
input: {"user_input": "What is the deployment in k8s?"}
120122
output: {
@@ -133,4 +135,14 @@ output: {
133135
"target_services": [],
134136
"suggested_tools": ["memory_all_by_session_id"]
135137
}
138+
<example>
139+
140+
<example>
141+
input: {"user_input": "How should I modify my current setup for better performance?"}
142+
output: {
143+
"thought": "The user asks about modifying 'my current setup' but I don't have information about their specific setup, configuration, or environment. I need to retrieve conversation history to understand their current setup before I can provide recommendations for performance improvements.",
144+
"intent": "MEMORY_SEARCH",
145+
"target_services": [],
146+
"suggested_tools": ["memory_all_by_session_id"]
147+
}
136148
<example>

ai/schema/react.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func StreamEnd() *StreamFeedback {
184184

185185
func StreamFinal(final *Observation) *StreamFeedback {
186186
defer func() { index++ }()
187-
return &StreamFeedback{index: index, done: true, text: final.FinalAnswer, final: final}
187+
return &StreamFeedback{index: index, done: true, text: "", final: final}
188188
}
189189

190190
func (sf *StreamFeedback) Text() string {

ai/server/handlers.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ func (h *AgentHandler) StreamChat(c *gin.Context) {
9393
continue
9494
}
9595
if feedback.IsFinal() {
96-
if err := sseHandler.HandleText(feedback.Text(), feedback.Index()); err != nil {
97-
manager.GetLogger().Error("Failed to handle text", "error", err)
98-
}
99-
if err := sseHandler.HandleContentBlockStop(feedback.Index()); err != nil {
100-
manager.GetLogger().Error("Failed to handle content block stop", "error", err)
101-
}
10296
h.MessageDelta(sseHandler, feedback.Final())
10397
} else if feedback.IsDone() {
10498
if err := sseHandler.HandleContentBlockStop(feedback.Index()); err != nil {

0 commit comments

Comments
 (0)