feat(dingtalk): enable secure multimodal image QA

This commit is contained in:
姚章浩
2026-07-16 00:17:16 +08:00
parent 4022040e98
commit c9c4f0aaa5
59 changed files with 3279 additions and 429 deletions
+31 -35
View File
@@ -21,14 +21,15 @@ import (
// providerAdapter(见 provider.go),thinking 编码委托给 ThinkingStrategy
// (见 thinking.go)。
type RemoteAPIChat struct {
modelName string
client *openai.Client
modelID string
baseURL string
apiKey string
provider provider.ProviderName
appID string
appSecret string
modelName string
client *openai.Client
modelID string
baseURL string
apiKey string
provider provider.ProviderName
supportsVision bool
appID string
appSecret string
// customHeaders 为用户在模型配置中指定的自定义 HTTP 请求头(类似 OpenAI Python SDK 的 extra_headers)。
customHeaders map[string]string
@@ -38,6 +39,8 @@ type RemoteAPIChat struct {
thinkingOverride ThinkingStrategy
}
func (c *RemoteAPIChat) SupportsImageInput() bool { return c.supportsVision }
// NewRemoteAPIChat 创建远程 API 聊天实例
func NewRemoteAPIChat(chatConfig *ChatConfig) (*RemoteAPIChat, error) {
if chatConfig.BaseURL != "" {
@@ -103,6 +106,7 @@ func NewRemoteAPIChat(chatConfig *ChatConfig) (*RemoteAPIChat, error) {
baseURL: chatConfig.BaseURL,
apiKey: apiKey,
provider: providerName,
supportsVision: chatConfig.SupportsVision,
appID: chatConfig.AppID,
appSecret: chatConfig.AppSecret,
customHeaders: chatConfig.CustomHeaders,
@@ -120,7 +124,11 @@ func (c *RemoteAPIChat) authCreds() authCreds {
// transform and parameter shaping (but not thinking, which may wrap the body).
func (c *RemoteAPIChat) shapedRequest(messages []Message, opts *ChatOptions, isStream bool) openai.ChatCompletionRequest {
req := c.BuildChatCompletionRequest(messages, opts, isStream)
req.Messages = c.adapter.TransformMessages(req.Messages)
// WeKnora Cloud text models retain the legacy plain-text downgrade, while
// vision-capable Chat models must keep the OpenAI-compatible image parts.
if c.provider != provider.ProviderWeKnoraCloud || !c.supportsVision {
req.Messages = c.adapter.TransformMessages(req.Messages)
}
c.adapter.ShapeRequest(&req, opts, isStream)
return req
}
@@ -180,15 +188,7 @@ func (c *RemoteAPIChat) Chat(ctx context.Context, messages []Message, opts *Chat
c.logRequest(timeoutCtx, req, false)
resp, err := c.client.CreateChatCompletion(timeoutCtx, req)
if err != nil {
if isMultimodalNotSupportedError(err) {
logger.Warnf(timeoutCtx, "[LLM Request] Model %s does not support multimodal, retrying without images", c.modelName)
cleaned := stripImagesFromMessages(messages)
req = c.shapedRequest(cleaned, opts, false)
resp, err = c.client.CreateChatCompletion(timeoutCtx, req)
}
if err != nil {
return nil, fmt.Errorf("create chat completion: %w", err)
}
return nil, fmt.Errorf("create chat completion failed")
}
result, err := c.parseCompletionResponse(&resp)
@@ -231,13 +231,13 @@ func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, cu
resp, err := rawHTTPClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("send request: %w", err)
return nil, fmt.Errorf("send chat request failed")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
return nil, remoteAPIStatusError(resp.StatusCode, body)
}
body, err := io.ReadAll(resp.Body)
@@ -259,6 +259,10 @@ func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, cu
return result, nil
}
func remoteAPIStatusError(status int, _ []byte) error {
return fmt.Errorf("API request failed with status %d", status)
}
// ChatStream 进行流式聊天
func (c *RemoteAPIChat) ChatStream(ctx context.Context, messages []Message, opts *ChatOptions) (<-chan types.StreamResponse, error) {
// 仅在调用方未设置 deadline 时附加兜底超时;流式调用默认超时更长,
@@ -287,17 +291,9 @@ func (c *RemoteAPIChat) ChatStream(ctx context.Context, messages []Message, opts
stream, err := c.client.CreateChatCompletionStream(timeoutCtx, req)
if err != nil {
if isMultimodalNotSupportedError(err) {
logger.Warnf(timeoutCtx, "[LLM Stream] Model %s does not support multimodal, retrying without images", c.modelName)
cleaned := stripImagesFromMessages(messages)
req = c.shapedRequest(cleaned, opts, true)
stream, err = c.client.CreateChatCompletionStream(timeoutCtx, req)
}
if err != nil {
cancel()
close(streamChan)
return nil, fmt.Errorf("create chat completion stream: %w", err)
}
cancel()
close(streamChan)
return nil, fmt.Errorf("create chat completion stream failed")
}
go func() {
@@ -363,13 +359,13 @@ func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, endpoint stri
resp, err := rawHTTPClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("send request: %w", err)
return nil, fmt.Errorf("send chat request failed")
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
resp.Body.Close()
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
return nil, remoteAPIStatusError(resp.StatusCode, body)
}
streamChan := make(chan types.StreamResponse)