feat: 增加流式响应类型和工具调用支持
新增 LLMToolCall 和 FunctionCall 结构体支持工具调用,扩展 ResponseType 枚举添加多种响应类型,优化 StreamResponse 结构体增加会话和工具调用字段,并改进引用事件处理逻辑。
This commit is contained in:
+32
-7
@@ -191,20 +191,45 @@ type KnowledgeQARequest struct {
|
||||
DisableTitle bool `json:"disable_title"` // Whether to disable auto title generation
|
||||
}
|
||||
|
||||
// LLMToolCall represents a function/tool call from the LLM
|
||||
type LLMToolCall struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"` // "function"
|
||||
Function FunctionCall `json:"function"`
|
||||
}
|
||||
|
||||
// FunctionCall represents the function details
|
||||
type FunctionCall struct {
|
||||
Name string `json:"name"`
|
||||
Arguments string `json:"arguments"` // JSON string
|
||||
}
|
||||
|
||||
type ResponseType string
|
||||
|
||||
const (
|
||||
ResponseTypeAnswer ResponseType = "answer"
|
||||
ResponseTypeReferences ResponseType = "references"
|
||||
ResponseTypeAnswer ResponseType = "answer"
|
||||
ResponseTypeReferences ResponseType = "references"
|
||||
ResponseTypeThinking ResponseType = "thinking"
|
||||
ResponseTypeToolCall ResponseType = "tool_call"
|
||||
ResponseTypeToolResult ResponseType = "tool_result"
|
||||
ResponseTypeError ResponseType = "error"
|
||||
ResponseTypeReflection ResponseType = "reflection"
|
||||
ResponseTypeSessionTitle ResponseType = "session_title"
|
||||
ResponseTypeAgentQuery ResponseType = "agent_query"
|
||||
ResponseTypeComplete ResponseType = "complete"
|
||||
)
|
||||
|
||||
// StreamResponse streaming response
|
||||
type StreamResponse struct {
|
||||
ID string `json:"id"` // Unique identifier
|
||||
ResponseType ResponseType `json:"response_type"` // Response type
|
||||
Content string `json:"content"` // Current content fragment
|
||||
Done bool `json:"done"` // Whether completed
|
||||
KnowledgeReferences []*SearchResult `json:"knowledge_references"` // Knowledge references
|
||||
ID string `json:"id"` // Unique identifier
|
||||
ResponseType ResponseType `json:"response_type"` // Response type
|
||||
Content string `json:"content"` // Current content fragment
|
||||
Done bool `json:"done"` // Whether completed
|
||||
KnowledgeReferences []*SearchResult `json:"knowledge_references,omitempty"` // Knowledge references
|
||||
SessionID string `json:"session_id,omitempty"` // Session ID (for agent_query event)
|
||||
AssistantMessageID string `json:"assistant_message_id,omitempty"` // Assistant Message ID (for agent_query event)
|
||||
ToolCalls []LLMToolCall `json:"tool_calls,omitempty"` // Tool calls for streaming (partial)
|
||||
Data map[string]interface{} `json:"data,omitempty"` // Additional metadata for enhanced display
|
||||
}
|
||||
|
||||
// KnowledgeQAStream knowledge Q&A streaming API
|
||||
|
||||
@@ -59,8 +59,19 @@ func buildStreamResponse(evt interfaces.StreamEvent, requestID string) *types.St
|
||||
|
||||
// Special handling for references event
|
||||
if evt.Type == types.ResponseTypeReferences {
|
||||
if refs, ok := evt.Data["references"].(types.References); ok {
|
||||
refsData := evt.Data["references"]
|
||||
logger.GetLogger(context.Background()).Info("buildStreamResponse references event",
|
||||
"refsData_type", fmt.Sprintf("%T", refsData),
|
||||
"refsData", refsData)
|
||||
if refs, ok := refsData.(types.References); ok {
|
||||
logger.GetLogger(context.Background()).Info("buildStreamResponse: matched types.References")
|
||||
response.KnowledgeReferences = refs
|
||||
} else if refs, ok := refsData.([]*types.SearchResult); ok {
|
||||
logger.GetLogger(context.Background()).Info("buildStreamResponse: matched []*types.SearchResult")
|
||||
response.KnowledgeReferences = types.References(refs)
|
||||
} else {
|
||||
logger.GetLogger(context.Background()).Warn("buildStreamResponse: references type assertion failed",
|
||||
"actual_type", fmt.Sprintf("%T", refsData))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user