feat: implement final_answer tool and enhance agent duration tracking
- Introduced the final_answer tool to ensure agents submit their complete responses as the last action, improving response accuracy and consistency. - Updated the agent engine to log detailed information about tool calls and responses, including the total duration of agent execution. - Enhanced the chat message model to include agent_duration_ms, tracking the total execution time from query start to answer delivery. - Implemented JSON field extraction for streaming responses, allowing for more efficient handling of tool call outputs. These changes significantly improve the agent's response handling and ensure that final answers are consistently delivered, enhancing overall user experience.
This commit is contained in:
@@ -18,6 +18,7 @@ const (
|
||||
ToolDataSchema = "data_schema"
|
||||
ToolWebSearch = "web_search"
|
||||
ToolWebFetch = "web_fetch"
|
||||
ToolFinalAnswer = "final_answer"
|
||||
// Skills-related tools (only available when skills are enabled)
|
||||
ToolExecuteSkillScript = "execute_skill_script"
|
||||
ToolReadSkill = "read_skill"
|
||||
@@ -46,6 +47,7 @@ func AvailableToolDefinitions() []AvailableTool {
|
||||
{Name: ToolDataSchema, Label: "查看数据元信息", Description: "获取表格文件的元信息"},
|
||||
{Name: ToolReadSkill, Label: "读取技能", Description: "按需读取技能内容以学习专业能力"},
|
||||
{Name: ToolExecuteSkillScript, Label: "执行技能脚本", Description: "在沙箱环境中执行技能脚本"},
|
||||
{Name: ToolFinalAnswer, Label: "提交最终回答", Description: "提交最终回答给用户"},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,5 +64,6 @@ func DefaultAllowedTools() []string {
|
||||
ToolDatabaseQuery,
|
||||
ToolDataAnalysis,
|
||||
ToolDataSchema,
|
||||
ToolFinalAnswer,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
var finalAnswerTool = BaseTool{
|
||||
name: ToolFinalAnswer,
|
||||
description: `Submit your final answer to the user's question.
|
||||
|
||||
## When to Use This Tool
|
||||
|
||||
You MUST call this tool as your LAST action when you are ready to deliver your final response to the user.
|
||||
After gathering all necessary information through other tools (search, retrieval, analysis, etc.),
|
||||
synthesize your findings and submit the complete answer through this tool.
|
||||
|
||||
## Important Rules
|
||||
|
||||
1. NEVER end your turn without calling this tool
|
||||
2. The answer parameter must contain your complete, well-formatted response
|
||||
3. Include all citations, structure, and formatting in the answer
|
||||
4. This should always be the last tool you call
|
||||
|
||||
## Parameters
|
||||
|
||||
- **answer**: Your complete final answer in Markdown format, including all citations and formatting`,
|
||||
schema: json.RawMessage(`{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"answer": {
|
||||
"type": "string",
|
||||
"description": "Your complete final answer in Markdown format. Include all citations, structure, images, and formatting."
|
||||
}
|
||||
},
|
||||
"required": ["answer"]
|
||||
}`),
|
||||
}
|
||||
|
||||
// FinalAnswerInput defines the input parameters for the final answer tool
|
||||
type FinalAnswerInput struct {
|
||||
Answer string `json:"answer"`
|
||||
}
|
||||
|
||||
// FinalAnswerTool submits the agent's final answer to the user
|
||||
type FinalAnswerTool struct {
|
||||
BaseTool
|
||||
}
|
||||
|
||||
// NewFinalAnswerTool creates a new final answer tool instance
|
||||
func NewFinalAnswerTool() *FinalAnswerTool {
|
||||
return &FinalAnswerTool{
|
||||
BaseTool: finalAnswerTool,
|
||||
}
|
||||
}
|
||||
|
||||
// Execute executes the final answer tool
|
||||
func (t *FinalAnswerTool) Execute(ctx context.Context, args json.RawMessage) (*types.ToolResult, error) {
|
||||
logger.Infof(ctx, "[Tool][FinalAnswer] Execute started")
|
||||
|
||||
var input FinalAnswerInput
|
||||
if err := json.Unmarshal(args, &input); err != nil {
|
||||
logger.Errorf(ctx, "[Tool][FinalAnswer] Failed to parse args: %v", err)
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: fmt.Sprintf("Failed to parse args: %v", err),
|
||||
}, err
|
||||
}
|
||||
|
||||
if input.Answer == "" {
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: "answer must be a non-empty string",
|
||||
}, fmt.Errorf("answer must be a non-empty string")
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[Tool][FinalAnswer] Answer length: %d characters", len(input.Answer))
|
||||
|
||||
return &types.ToolResult{
|
||||
Success: true,
|
||||
Output: input.Answer,
|
||||
}, nil
|
||||
}
|
||||
@@ -37,7 +37,7 @@ Each thought can build on, question, or revise previous insights as understandin
|
||||
- Generates a solution hypothesis
|
||||
- Verifies the hypothesis based on the Chain of Thought steps
|
||||
- Repeats the process until satisfied
|
||||
- Provides a correct answer
|
||||
- When thinking is complete, you can call the final_answer tool to deliver your answer if all your thinking is complete. NEVER include the final answer directly in a thought.
|
||||
|
||||
## Parameters Explained
|
||||
|
||||
@@ -78,8 +78,8 @@ Each thought can build on, question, or revise previous insights as understandin
|
||||
7. Generate a solution hypothesis when appropriate
|
||||
8. Verify the hypothesis based on the Chain of Thought steps
|
||||
9. Repeat the process until satisfied with the solution
|
||||
10. Provide a single, ideally correct answer as the final output
|
||||
11. Only set next_thought_needed to false when truly done and a satisfactory answer is reached`,
|
||||
10. Only set next_thought_needed to false when truly done and a satisfactory answer is reached
|
||||
11. NEVER include the final answer in the thought content. When thinking is complete, you can call the final_answer tool to deliver the final answer to the user`,
|
||||
schema: json.RawMessage(`{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
Reference in New Issue
Block a user