feat: 新增智能体功能模块,支持自定义智能体创建、配置和选择
This commit is contained in:
@@ -21,6 +21,8 @@ type AgentConfig struct {
|
||||
UseCustomSystemPrompt bool `json:"use_custom_system_prompt"` // Whether to use custom system prompt instead of default
|
||||
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search tool is enabled
|
||||
WebSearchMaxResults int `json:"web_search_max_results"` // Maximum number of web search results (default: 5)
|
||||
MultiTurnEnabled bool `json:"multi_turn_enabled"` // Whether multi-turn conversation is enabled
|
||||
HistoryTurns int `json:"history_turns"` // Number of history turns to keep in context
|
||||
SearchTargets SearchTargets `json:"-"` // Pre-computed unified search targets (runtime only)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CustomAgentType represents the type of the custom agent
|
||||
type CustomAgentType string
|
||||
|
||||
const (
|
||||
// CustomAgentTypeNormal represents the normal RAG-based chat mode
|
||||
CustomAgentTypeNormal CustomAgentType = "normal"
|
||||
// CustomAgentTypeAgent represents the ReAct agent mode with tool calling
|
||||
CustomAgentTypeAgent CustomAgentType = "agent"
|
||||
// CustomAgentTypeCustom represents user-defined custom agents
|
||||
CustomAgentTypeCustom CustomAgentType = "custom"
|
||||
)
|
||||
|
||||
// BuiltinAgentID constants for built-in agents
|
||||
const (
|
||||
BuiltinAgentNormalID = "builtin-normal"
|
||||
BuiltinAgentAgentID = "builtin-agent"
|
||||
)
|
||||
|
||||
// CustomAgent represents a configurable AI agent (similar to GPTs)
|
||||
type CustomAgent struct {
|
||||
// Unique identifier of the agent
|
||||
ID string `yaml:"id" json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
// Name of the agent
|
||||
Name string `yaml:"name" json:"name" gorm:"type:varchar(255);not null"`
|
||||
// Description of the agent
|
||||
Description string `yaml:"description" json:"description" gorm:"type:text"`
|
||||
// Avatar/Icon of the agent (emoji or icon name)
|
||||
Avatar string `yaml:"avatar" json:"avatar" gorm:"type:varchar(64)"`
|
||||
// Whether this is a built-in agent (normal mode / agent mode)
|
||||
IsBuiltin bool `yaml:"is_builtin" json:"is_builtin" gorm:"default:false"`
|
||||
// Type of the agent: normal, agent, custom
|
||||
Type CustomAgentType `yaml:"type" json:"type" gorm:"type:varchar(32);default:'custom'"`
|
||||
// Tenant ID
|
||||
TenantID uint64 `yaml:"tenant_id" json:"tenant_id" gorm:"index"`
|
||||
// Created by user ID
|
||||
CreatedBy string `yaml:"created_by" json:"created_by" gorm:"type:varchar(36)"`
|
||||
|
||||
// Agent configuration
|
||||
Config CustomAgentConfig `yaml:"config" json:"config" gorm:"type:json"`
|
||||
|
||||
// Timestamps
|
||||
CreatedAt time.Time `yaml:"created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `yaml:"updated_at" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `yaml:"deleted_at" json:"deleted_at" gorm:"index"`
|
||||
}
|
||||
|
||||
// CustomAgentConfig represents the configuration of a custom agent
|
||||
type CustomAgentConfig struct {
|
||||
// Agent mode: "normal" for RAG mode, "agent" for ReAct agent mode
|
||||
AgentMode string `yaml:"agent_mode" json:"agent_mode"`
|
||||
// System prompt for the agent
|
||||
SystemPrompt string `yaml:"system_prompt" json:"system_prompt"`
|
||||
// Model ID to use for conversations
|
||||
ModelID string `yaml:"model_id" json:"model_id"`
|
||||
// Temperature for LLM (0-1)
|
||||
Temperature float64 `yaml:"temperature" json:"temperature"`
|
||||
// Maximum iterations for ReAct loop (only for agent type)
|
||||
MaxIterations int `yaml:"max_iterations" json:"max_iterations"`
|
||||
// Allowed tools (only for agent type)
|
||||
AllowedTools []string `yaml:"allowed_tools" json:"allowed_tools"`
|
||||
// Associated knowledge base IDs
|
||||
KnowledgeBases []string `yaml:"knowledge_bases" json:"knowledge_bases"`
|
||||
// Whether to allow user to select knowledge bases when no KnowledgeBases are configured
|
||||
// If true (default), user can freely select any knowledge base
|
||||
// If false, knowledge base selection is disabled for this agent
|
||||
AllowUserKBSelection *bool `yaml:"allow_user_kb_selection" json:"allow_user_kb_selection"`
|
||||
// Whether web search is enabled
|
||||
WebSearchEnabled bool `yaml:"web_search_enabled" json:"web_search_enabled"`
|
||||
// Maximum web search results
|
||||
WebSearchMaxResults int `yaml:"web_search_max_results" json:"web_search_max_results"`
|
||||
// Whether reflection is enabled (only for agent type)
|
||||
ReflectionEnabled bool `yaml:"reflection_enabled" json:"reflection_enabled"`
|
||||
// Welcome message displayed when agent is selected
|
||||
MultiTurnEnabled bool `yaml:"multi_turn_enabled" json:"multi_turn_enabled"`
|
||||
// Number of history turns to keep in context
|
||||
HistoryTurns int `yaml:"history_turns" json:"history_turns"`
|
||||
}
|
||||
|
||||
// Value implements driver.Valuer interface for CustomAgentConfig
|
||||
func (c CustomAgentConfig) Value() (driver.Value, error) {
|
||||
return json.Marshal(c)
|
||||
}
|
||||
|
||||
// Scan implements sql.Scanner interface for CustomAgentConfig
|
||||
func (c *CustomAgentConfig) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
b, ok := value.([]byte)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(b, c)
|
||||
}
|
||||
|
||||
// TableName returns the table name for CustomAgent
|
||||
func (CustomAgent) TableName() string {
|
||||
return "custom_agents"
|
||||
}
|
||||
|
||||
// EnsureDefaults sets default values for the agent
|
||||
func (a *CustomAgent) EnsureDefaults() {
|
||||
if a == nil {
|
||||
return
|
||||
}
|
||||
if a.Type == "" {
|
||||
a.Type = CustomAgentTypeCustom
|
||||
}
|
||||
if a.Config.Temperature == 0 {
|
||||
a.Config.Temperature = 0.7
|
||||
}
|
||||
if a.Config.MaxIterations == 0 {
|
||||
a.Config.MaxIterations = 10
|
||||
}
|
||||
if a.Config.WebSearchMaxResults == 0 {
|
||||
a.Config.WebSearchMaxResults = 5
|
||||
}
|
||||
if a.Config.HistoryTurns == 0 {
|
||||
a.Config.HistoryTurns = 5
|
||||
}
|
||||
}
|
||||
|
||||
// IsAgentMode returns true if this agent uses ReAct agent mode
|
||||
func (a *CustomAgent) IsAgentMode() bool {
|
||||
return a.Config.AgentMode == "agent"
|
||||
}
|
||||
|
||||
// GetBuiltinNormalAgent returns the built-in normal mode agent
|
||||
func GetBuiltinNormalAgent(tenantID uint64) *CustomAgent {
|
||||
return &CustomAgent{
|
||||
ID: BuiltinAgentNormalID,
|
||||
Name: "普通模式",
|
||||
Description: "基于知识库的 RAG 问答,快速准确地回答问题",
|
||||
Avatar: "💬",
|
||||
IsBuiltin: true,
|
||||
Type: CustomAgentTypeNormal,
|
||||
TenantID: tenantID,
|
||||
Config: CustomAgentConfig{
|
||||
AgentMode: "normal",
|
||||
SystemPrompt: "",
|
||||
Temperature: 0.7,
|
||||
WebSearchEnabled: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GetBuiltinAgentAgent returns the built-in agent mode agent
|
||||
func GetBuiltinAgentAgent(tenantID uint64) *CustomAgent {
|
||||
return &CustomAgent{
|
||||
ID: BuiltinAgentAgentID,
|
||||
Name: "Agent 模式",
|
||||
Description: "ReAct 推理框架,支持多步思考和工具调用",
|
||||
Avatar: "🤖",
|
||||
IsBuiltin: true,
|
||||
Type: CustomAgentTypeAgent,
|
||||
TenantID: tenantID,
|
||||
Config: CustomAgentConfig{
|
||||
AgentMode: "agent",
|
||||
SystemPrompt: "",
|
||||
Temperature: 0.7,
|
||||
MaxIterations: 10,
|
||||
AllowedTools: []string{"knowledge_search", "web_search"},
|
||||
WebSearchEnabled: true,
|
||||
WebSearchMaxResults: 5,
|
||||
ReflectionEnabled: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GetBuiltinAgents returns all built-in agents for a tenant
|
||||
func GetBuiltinAgents(tenantID uint64) []*CustomAgent {
|
||||
return []*CustomAgent{
|
||||
GetBuiltinNormalAgent(tenantID),
|
||||
GetBuiltinAgentAgent(tenantID),
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,10 @@ type ContextManager interface {
|
||||
|
||||
// GetContextStats returns statistics about the context
|
||||
GetContextStats(ctx context.Context, sessionID string) (*ContextStats, error)
|
||||
|
||||
// SetSystemPrompt sets or updates the system prompt for a session
|
||||
// If a system message exists, it will be replaced; otherwise, a new one will be added at the beginning
|
||||
SetSystemPrompt(ctx context.Context, sessionID string, systemPrompt string) error
|
||||
}
|
||||
|
||||
// ContextStats contains statistics about session context
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
// Package interfaces defines the interface contracts for custom agent management
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// CustomAgentService defines the custom agent service interface
|
||||
// Provides high-level operations for agent creation, querying, updating, and deletion
|
||||
type CustomAgentService interface {
|
||||
// CreateAgent creates a new custom agent
|
||||
// Parameters:
|
||||
// - ctx: Context information, carrying request tracking, user identity, etc.
|
||||
// - agent: Agent object containing basic information and configuration
|
||||
// Returns:
|
||||
// - Created agent object (including automatically generated ID)
|
||||
// - Possible errors such as insufficient permissions, validation errors, etc.
|
||||
CreateAgent(ctx context.Context, agent *types.CustomAgent) (*types.CustomAgent, error)
|
||||
|
||||
// GetAgentByID retrieves agent information by ID
|
||||
// Parameters:
|
||||
// - ctx: Context information
|
||||
// - id: Unique identifier of the agent
|
||||
// Returns:
|
||||
// - Agent object, if found (including built-in agents)
|
||||
// - Possible errors such as not existing, insufficient permissions, etc.
|
||||
GetAgentByID(ctx context.Context, id string) (*types.CustomAgent, error)
|
||||
|
||||
// ListAgents lists all agents under the current tenant (including built-in agents)
|
||||
// Parameters:
|
||||
// - ctx: Context information, containing tenant information
|
||||
// Returns:
|
||||
// - List of agent objects (built-in agents first, then custom agents sorted by creation time)
|
||||
// - Possible errors such as insufficient permissions, etc.
|
||||
ListAgents(ctx context.Context) ([]*types.CustomAgent, error)
|
||||
|
||||
// UpdateAgent updates agent information
|
||||
// Parameters:
|
||||
// - ctx: Context information
|
||||
// - agent: Agent object containing update information
|
||||
// Returns:
|
||||
// - Updated agent object
|
||||
// - Possible errors such as not existing, insufficient permissions, cannot modify built-in, etc.
|
||||
UpdateAgent(ctx context.Context, agent *types.CustomAgent) (*types.CustomAgent, error)
|
||||
|
||||
// DeleteAgent deletes an agent
|
||||
// Parameters:
|
||||
// - ctx: Context information
|
||||
// - id: Unique identifier of the agent
|
||||
// Returns:
|
||||
// - Possible errors such as not existing, insufficient permissions, cannot delete built-in, etc.
|
||||
DeleteAgent(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
// CustomAgentRepository defines the custom agent repository interface
|
||||
// Responsible for agent data persistence and retrieval
|
||||
type CustomAgentRepository interface {
|
||||
// CreateAgent creates an agent record
|
||||
// Parameters:
|
||||
// - ctx: Context information
|
||||
// - agent: Agent object
|
||||
// Returns:
|
||||
// - Possible errors such as database connection failure, unique constraint conflicts, etc.
|
||||
CreateAgent(ctx context.Context, agent *types.CustomAgent) error
|
||||
|
||||
// GetAgentByID queries an agent by ID
|
||||
// Parameters:
|
||||
// - ctx: Context information
|
||||
// - id: Agent ID
|
||||
// Returns:
|
||||
// - Agent object, if found
|
||||
// - Possible errors such as record not existing, database errors, etc.
|
||||
GetAgentByID(ctx context.Context, id string) (*types.CustomAgent, error)
|
||||
|
||||
// ListAgentsByTenantID lists all agents for a specific tenant
|
||||
// Parameters:
|
||||
// - ctx: Context information
|
||||
// - tenantID: Tenant ID
|
||||
// Returns:
|
||||
// - List of agent objects
|
||||
// - Possible errors such as database errors, etc.
|
||||
ListAgentsByTenantID(ctx context.Context, tenantID uint64) ([]*types.CustomAgent, error)
|
||||
|
||||
// UpdateAgent updates an agent record
|
||||
// Parameters:
|
||||
// - ctx: Context information
|
||||
// - agent: Agent object containing update information
|
||||
// Returns:
|
||||
// - Possible errors such as record not existing, database errors, etc.
|
||||
UpdateAgent(ctx context.Context, agent *types.CustomAgent) error
|
||||
|
||||
// DeleteAgent deletes an agent record
|
||||
// Parameters:
|
||||
// - ctx: Context information
|
||||
// - id: Agent ID
|
||||
// Returns:
|
||||
// - Possible errors such as record not existing, database errors, etc.
|
||||
DeleteAgent(ctx context.Context, id string) error
|
||||
}
|
||||
@@ -31,10 +31,12 @@ type SessionService interface {
|
||||
// knowledgeIDs: list of specific knowledge (file) IDs to search
|
||||
// summaryModelID: optional summary model ID override (if empty, uses session/KB default)
|
||||
// webSearchEnabled: whether to enable web search to supplement knowledge base results
|
||||
// customAgent: optional custom agent for config override (multiTurnEnabled, historyTurns)
|
||||
// Events are emitted through eventBus (references, answer chunks, completion)
|
||||
KnowledgeQA(ctx context.Context,
|
||||
session *types.Session, query string, knowledgeBaseIDs []string, knowledgeIDs []string,
|
||||
assistantMessageID string, summaryModelID string, webSearchEnabled bool, eventBus *event.EventBus,
|
||||
customAgent *types.CustomAgent,
|
||||
) error
|
||||
// KnowledgeQAByEvent performs knowledge-based question answering by event
|
||||
KnowledgeQAByEvent(ctx context.Context, chatManage *types.ChatManage, eventList []types.EventType) error
|
||||
@@ -42,12 +44,14 @@ type SessionService interface {
|
||||
SearchKnowledge(ctx context.Context, knowledgeBaseID, query string) ([]*types.SearchResult, error)
|
||||
// AgentQA performs agent-based question answering with conversation history and streaming support
|
||||
// eventBus is optional - if nil, uses service's default EventBus
|
||||
// customAgent is optional - if provided, uses custom agent configuration instead of tenant defaults
|
||||
AgentQA(
|
||||
ctx context.Context,
|
||||
session *types.Session,
|
||||
query string,
|
||||
assistantMessageID string,
|
||||
eventBus *event.EventBus,
|
||||
customAgent *types.CustomAgent,
|
||||
) error
|
||||
// ClearContext clears the LLM context for a session
|
||||
ClearContext(ctx context.Context, sessionID string) error
|
||||
|
||||
Reference in New Issue
Block a user