Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Go: porting #18663 (#18726)

Wang Qi40326f1824cf
diff --git a/internal/entity/models/base_model.go b/internal/entity/models/base_model.go
index 5242c716d9..677e2903f4 100644
--- a/internal/entity/models/base_model.go
+++ b/internal/entity/models/base_model.go
@@ -568,12 +568,24 @@ func buildChatMessages(messages []Message) []map[string]any {
"role": msg.Role,
"content": msg.Content,
}
+ if msg.Name != nil {
+ apiMsg["name"] = msg.Name
+ }
if msg.ToolCallID != "" {
apiMsg["tool_call_id"] = msg.ToolCallID
}
if len(msg.ToolCalls) > 0 {
apiMsg["tool_calls"] = msg.ToolCalls
}
+ if msg.FunctionCall != nil {
+ apiMsg["function_call"] = msg.FunctionCall
+ }
+ if msg.Refusal != nil {
+ apiMsg["refusal"] = msg.Refusal
+ }
+ if msg.Audio != nil {
+ apiMsg["audio"] = msg.Audio
+ }
apiMessages[i] = apiMsg
}
return apiMessages
diff --git a/internal/entity/models/types.go b/internal/entity/models/types.go
index 9cb3977681..1b29804d7f 100644
--- a/internal/entity/models/types.go
+++ b/internal/entity/models/types.go
@@ -13,10 +13,14 @@ import (
// - []interface{}: multimodal content array where each element is map[string]interface{}
// (e.g., [{"type": "text", "text": "..."}, {"type": "image_url", "image_url": {"url": "..."}}])
type Message struct {
- Role string `json:"role"`
- Content interface{} `json:"content"`
- ToolCallID string `json:"tool_call_id,omitempty"`
- ToolCalls []map[string]interface{} `json:"tool_calls,omitempty"`
+ Role string `json:"role"`
+ Content interface{} `json:"content"`
+ Name interface{} `json:"name,omitempty"`
+ ToolCallID string `json:"tool_call_id,omitempty"`
+ ToolCalls []map[string]interface{} `json:"tool_calls,omitempty"`
+ FunctionCall interface{} `json:"function_call,omitempty"`
+ Refusal interface{} `json:"refusal,omitempty"`
+ Audio interface{} `json:"audio,omitempty"`
}
// ToolCallSession mirrors Python's common.mcp_tool_call_conn.ToolCallSession protocol.
diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.go
index 3ce5492ba9..cdcb7810b3 100644
--- a/internal/service/chat_pipeline.go
+++ b/internal/service/chat_pipeline.go
@@ -39,6 +39,11 @@ import (
"go.uber.org/zap"
)
+// Fields defined by the chat-completions message schema. Stored messages also
+// carry RAGFlow bookkeeping such as id, created_at, doc_ids, and conversationId,
+// which strict providers reject.
+var llmMessageFields = [...]string{"role", "content", "name", "tool_calls", "tool_call_id", "function_call", "refusal", "audio"}
+
// ChatPipelineService is the shared RAG chat pipeline engine used by both
// the OpenAI-compatible endpoint (/api/v1/openai/<chat_id>/chat/completions)
// and the regular chat completion endpoint (/api/v1/chat/completions).
@@ -928,14 +933,13 @@ func (s *ChatPipelineService) AsyncChat(
if role == "system" {
continue
}
- content := m["content"]
+ llmMessage := normalizeLLMMessage(m)
+ content := llmMessage["content"]
if contentStr, ok := content.(string); ok {
content = cleanCitationMarkers(contentStr)
}
- llmMessages = append(llmMessages, map[string]interface{}{
- "role": role,
- "content": content,
- })
+ llmMessage["content"] = content
+ llmMessages = append(llmMessages, llmMessage)
}
// Fit messages within token budget.
@@ -1380,14 +1384,13 @@ func (s *ChatPipelineService) AsyncChatSolo(
if role == "system" {
continue
}
- content := m["content"]
+ llmMessage := normalizeLLMMessage(m)
+ content := llmMessage["content"]
if contentStr, ok := content.(string); ok {
content = cleanCitationMarkers(contentStr)
}
- msg = append(msg, map[string]interface{}{
- "role": role,
- "content": content,
- })
+ llmMessage["content"] = content
+ msg = append(msg, llmMessage)
}
// Append text attachments to the last user message (no separator).
if attachmentsStr != "" && len(msg) > 0 {
@@ -1445,10 +1448,8 @@ func (s *ChatPipelineService) AsyncChatSolo(
content = converted["content"]
}
}
- chatMessages = append(chatMessages, modelModule.Message{
- Role: role,
- Content: content,
- })
+ m["content"] = content
+ chatMessages = append(chatMessages, modelMessageFromMap(m))
}
// 7. Drive the LLM: stream (per-delta with think markers) or non-stream (one-shot).
@@ -2548,15 +2549,40 @@ func (s *ChatPipelineService) buildChatMessages(systemContent string, messages [
}
for _, m := range messages {
role, _ := m["role"].(string)
- content := m["content"]
- if role == "" || content == nil {
+ if role == "" {
continue
}
- result = append(result, modelModule.Message{Role: role, Content: content})
+ result = append(result, modelMessageFromMap(m))
}
return result
}
+func normalizeLLMMessage(message map[string]interface{}) map[string]interface{} {
+ normalized := make(map[string]interface{}, len(llmMessageFields))
+ for _, field := range llmMessageFields {
+ if value, ok := message[field]; ok {
+ normalized[field] = value
+ }
+ }
+ return normalized
+}
+
+func modelMessageFromMap(message map[string]interface{}) modelModule.Message {
+ msg := modelModule.Message{Role: stringFromMap(message, "role"), Content: message["content"], Name: message["name"], FunctionCall: message["function_call"], Refusal: message["refusal"], Audio: message["audio"]}
+ msg.ToolCallID, _ = message["tool_call_id"].(string)
+ switch toolCalls := message["tool_calls"].(type) {
+ case []map[string]interface{}:
+ msg.ToolCalls = toolCalls
+ case []interface{}:
+ for _, toolCall := range toolCalls {
+ if value, ok := toolCall.(map[string]interface{}); ok {
+ msg.ToolCalls = append(msg.ToolCalls, value)
+ }
+ }
+ }
+ return msg
+}
+
// buildChatDriver creates a ChatModel wrapper from the chat.
func (s *ChatPipelineService) buildChatDriver(ctx context.Context, chat *entity.Chat, chatModel *modelModule.ChatModel) *modelModule.ChatModel {
if chatModel != nil {