mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-27 02:42:00 +08:00
Support stream for multimodal chat (#14537)
### What problem does this PR solve? Support stream for multimodal chat ### Type of change - [x] Refactoring
This commit is contained in:
@@ -60,7 +60,7 @@ func (z *DeepSeekModel) Name() string {
|
||||
return "deepseek"
|
||||
}
|
||||
|
||||
func (z *DeepSeekModel) ChatWithMessages(modelName string, apiConfig *APIConfig, messages []Message, chatModelConfig *ChatConfig) (*ChatResponse, error) {
|
||||
func (z *DeepSeekModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) {
|
||||
if len(messages) == 0 {
|
||||
return nil, fmt.Errorf("messages is empty")
|
||||
}
|
||||
@@ -227,8 +227,12 @@ func (z *DeepSeekModel) ChatWithMessages(modelName string, apiConfig *APIConfig,
|
||||
return chatResponse, nil
|
||||
}
|
||||
|
||||
// ChatStreamlyWithSender sends a message and streams response via sender function (best performance, no channel)
|
||||
func (z *DeepSeekModel) ChatStreamlyWithSender(modelName, message *string, apiConfig *APIConfig, chatModelConfig *ChatConfig, sender func(*string, *string) error) error {
|
||||
// ChatStreamlyWithSender sends messages and streams response via sender function (best performance, no channel)
|
||||
func (z *DeepSeekModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig, sender func(*string, *string) error) error {
|
||||
if len(messages) == 0 {
|
||||
return fmt.Errorf("messages is empty")
|
||||
}
|
||||
|
||||
var region = "default"
|
||||
if apiConfig.Region != nil {
|
||||
region = *apiConfig.Region
|
||||
@@ -236,77 +240,83 @@ func (z *DeepSeekModel) ChatStreamlyWithSender(modelName, message *string, apiCo
|
||||
|
||||
url := fmt.Sprintf("%s/chat/completions", z.BaseURL[region])
|
||||
|
||||
// Convert messages to API format
|
||||
apiMessages := make([]map[string]interface{}, len(messages))
|
||||
for i, msg := range messages {
|
||||
apiMessages[i] = map[string]interface{}{
|
||||
"role": msg.Role,
|
||||
"content": msg.Content,
|
||||
}
|
||||
}
|
||||
|
||||
// Build request body with streaming enabled
|
||||
reqBody := map[string]interface{}{
|
||||
"model": modelName,
|
||||
"messages": []map[string]string{
|
||||
{"role": "user", "content": *message},
|
||||
},
|
||||
"stream": false,
|
||||
"model": modelName,
|
||||
"messages": apiMessages,
|
||||
"stream": true,
|
||||
"temperature": 1,
|
||||
}
|
||||
|
||||
if chatModelConfig.Stream != nil {
|
||||
reqBody["stream"] = *chatModelConfig.Stream
|
||||
}
|
||||
if chatModelConfig != nil {
|
||||
if chatModelConfig.Stream != nil {
|
||||
reqBody["stream"] = *chatModelConfig.Stream
|
||||
}
|
||||
|
||||
if chatModelConfig.MaxTokens != nil {
|
||||
reqBody["max_tokens"] = *chatModelConfig.MaxTokens
|
||||
}
|
||||
if chatModelConfig.MaxTokens != nil {
|
||||
reqBody["max_tokens"] = *chatModelConfig.MaxTokens
|
||||
}
|
||||
|
||||
if chatModelConfig.Temperature != nil {
|
||||
reqBody["temperature"] = *chatModelConfig.Temperature
|
||||
}
|
||||
if chatModelConfig.Temperature != nil {
|
||||
reqBody["temperature"] = *chatModelConfig.Temperature
|
||||
}
|
||||
|
||||
if chatModelConfig.DoSample != nil {
|
||||
reqBody["do_sample"] = *chatModelConfig.DoSample
|
||||
}
|
||||
if chatModelConfig.DoSample != nil {
|
||||
reqBody["do_sample"] = *chatModelConfig.DoSample
|
||||
}
|
||||
|
||||
if chatModelConfig.TopP != nil {
|
||||
reqBody["top_p"] = *chatModelConfig.TopP
|
||||
}
|
||||
if chatModelConfig.TopP != nil {
|
||||
reqBody["top_p"] = *chatModelConfig.TopP
|
||||
}
|
||||
|
||||
if chatModelConfig.Stop != nil {
|
||||
reqBody["stop"] = *chatModelConfig.Stop
|
||||
}
|
||||
if chatModelConfig.Stop != nil {
|
||||
reqBody["stop"] = *chatModelConfig.Stop
|
||||
}
|
||||
|
||||
if chatModelConfig.Thinking != nil {
|
||||
if *chatModelConfig.Thinking {
|
||||
var thinkingFlag string
|
||||
switch *chatModelConfig.Effort {
|
||||
case "none":
|
||||
thinkingFlag = "disabled"
|
||||
chatModelConfig.Thinking = nil
|
||||
break
|
||||
case "low":
|
||||
thinkingFlag = "disabled"
|
||||
chatModelConfig.Thinking = nil
|
||||
break
|
||||
case "medium":
|
||||
thinkingFlag = "disabled"
|
||||
chatModelConfig.Thinking = nil
|
||||
break
|
||||
case "high":
|
||||
thinkingFlag = "enabled"
|
||||
reqBody["reasoning_effort"] = "high"
|
||||
break
|
||||
case "default":
|
||||
thinkingFlag = "enabled"
|
||||
reqBody["reasoning_effort"] = "high"
|
||||
break
|
||||
case "max":
|
||||
thinkingFlag = "enabled"
|
||||
reqBody["reasoning_effort"] = "max"
|
||||
break
|
||||
default:
|
||||
return fmt.Errorf("invalid effort level")
|
||||
}
|
||||
reqBody["thinking"] = map[string]interface{}{
|
||||
"type": thinkingFlag,
|
||||
}
|
||||
} else {
|
||||
reqBody["thinking"] = map[string]interface{}{
|
||||
"type": "disabled",
|
||||
if chatModelConfig.Thinking != nil {
|
||||
if *chatModelConfig.Thinking {
|
||||
var thinkingFlag string
|
||||
switch *chatModelConfig.Effort {
|
||||
case "none":
|
||||
thinkingFlag = "disabled"
|
||||
break
|
||||
case "low":
|
||||
thinkingFlag = "disabled"
|
||||
break
|
||||
case "medium":
|
||||
thinkingFlag = "disabled"
|
||||
break
|
||||
case "high":
|
||||
thinkingFlag = "enabled"
|
||||
reqBody["reasoning_effort"] = "high"
|
||||
break
|
||||
case "default":
|
||||
thinkingFlag = "enabled"
|
||||
reqBody["reasoning_effort"] = "high"
|
||||
break
|
||||
case "max":
|
||||
thinkingFlag = "enabled"
|
||||
reqBody["reasoning_effort"] = "max"
|
||||
break
|
||||
default:
|
||||
return fmt.Errorf("invalid effort level")
|
||||
}
|
||||
reqBody["thinking"] = map[string]interface{}{
|
||||
"type": thinkingFlag,
|
||||
}
|
||||
} else {
|
||||
reqBody["thinking"] = map[string]interface{}{
|
||||
"type": "disabled",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user