package chat import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "strings" "github.com/Tencent/WeKnora/internal/models/provider" "github.com/Tencent/WeKnora/internal/types" secutils "github.com/Tencent/WeKnora/internal/utils" ) const anthropicVersion = "2023-06-01" type AnthropicChat struct { modelName string modelID string baseURL string apiKey string customHeaders map[string]string } type anthropicMessage struct { Role string `json:"role"` Content string `json:"content"` } type anthropicRequest struct { Model string `json:"model"` MaxTokens int `json:"max_tokens"` System string `json:"system,omitempty"` Messages []anthropicMessage `json:"messages"` Temperature *float64 `json:"temperature,omitempty"` TopP *float64 `json:"top_p,omitempty"` } type anthropicResponse struct { ID string `json:"id"` Type string `json:"type"` Role string `json:"role"` Content []struct { Type string `json:"type"` Text string `json:"text"` } `json:"content"` StopReason string `json:"stop_reason"` Usage struct { InputTokens int `json:"input_tokens"` OutputTokens int `json:"output_tokens"` } `json:"usage"` Error *struct { Type string `json:"type"` Message string `json:"message"` } `json:"error,omitempty"` } func NewAnthropicChat(config *ChatConfig) (*AnthropicChat, error) { if config.BaseURL != "" { if err := secutils.ValidateURLForSSRF(config.BaseURL); err != nil { return nil, fmt.Errorf("baseURL SSRF check failed: %w", err) } } if strings.TrimSpace(config.APIKey) == "" { return nil, fmt.Errorf("Anthropic provider: API key is required") } baseURL := strings.TrimRight(config.BaseURL, "/") if baseURL == "" { baseURL = provider.AnthropicBaseURL } return &AnthropicChat{ modelName: config.ModelName, modelID: config.ModelID, baseURL: baseURL, apiKey: config.APIKey, customHeaders: config.CustomHeaders, }, nil } func (c *AnthropicChat) Chat(ctx context.Context, messages []Message, opts *ChatOptions) (*types.ChatResponse, error) { reqBody := c.buildRequest(messages, opts) jsonData, err := json.Marshal(reqBody) if err != nil { return nil, fmt.Errorf("marshal request: %w", err) } ctx, cancel := withLLMTimeout(ctx, defaultChatTimeout) defer cancel() endpoint := c.baseURL + "/messages" if err := secutils.ValidateURLForSSRF(endpoint); err != nil { return nil, fmt.Errorf("endpoint SSRF check failed: %w", err) } httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(jsonData)) if err != nil { return nil, fmt.Errorf("create request: %w", err) } httpReq.Header.Set("Content-Type", "application/json") httpReq.Header.Set("x-api-key", c.apiKey) httpReq.Header.Set("anthropic-version", anthropicVersion) secutils.ApplyCustomHeaders(httpReq, c.customHeaders) resp, err := rawHTTPClient.Do(httpReq) if err != nil { return nil, fmt.Errorf("send request: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("read response: %w", err) } var chatResp anthropicResponse if err := json.Unmarshal(body, &chatResp); err != nil { return nil, fmt.Errorf("decode response: %w", err) } if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { if chatResp.Error != nil && chatResp.Error.Message != "" { return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, chatResp.Error.Message) } return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body)) } return c.parseResponse(&chatResp), nil } func (c *AnthropicChat) ChatStream(ctx context.Context, messages []Message, opts *ChatOptions) (<-chan types.StreamResponse, error) { return nil, fmt.Errorf("Anthropic streaming chat is not supported yet") } func (c *AnthropicChat) GetModelName() string { return c.modelName } func (c *AnthropicChat) GetModelID() string { return c.modelID } func (c *AnthropicChat) buildRequest(messages []Message, opts *ChatOptions) anthropicRequest { req := anthropicRequest{ Model: c.modelName, MaxTokens: 1024, Messages: make([]anthropicMessage, 0, len(messages)), } if opts != nil { if opts.MaxTokens > 0 { req.MaxTokens = opts.MaxTokens } else if opts.MaxCompletionTokens > 0 { req.MaxTokens = opts.MaxCompletionTokens } if opts.Temperature > 0 { temperature := opts.Temperature req.Temperature = &temperature } if opts.TopP > 0 { topP := opts.TopP req.TopP = &topP } } var systemParts []string for _, msg := range messages { content := strings.TrimSpace(msg.Content) if content == "" { content = textFromMultiContent(msg.MultiContent) } if content == "" { continue } switch msg.Role { case "system": systemParts = append(systemParts, content) case "assistant": req.Messages = append(req.Messages, anthropicMessage{Role: "assistant", Content: content}) case "user": req.Messages = append(req.Messages, anthropicMessage{Role: "user", Content: content}) default: req.Messages = append(req.Messages, anthropicMessage{Role: "user", Content: content}) } } req.System = strings.Join(systemParts, "\n\n") return req } func textFromMultiContent(parts []MessageContentPart) string { if len(parts) == 0 { return "" } textParts := make([]string, 0, len(parts)) for _, part := range parts { if part.Type == "text" && strings.TrimSpace(part.Text) != "" { textParts = append(textParts, strings.TrimSpace(part.Text)) } } return strings.Join(textParts, "\n") } func (c *AnthropicChat) parseResponse(resp *anthropicResponse) *types.ChatResponse { parts := make([]string, 0, len(resp.Content)) for _, part := range resp.Content { if part.Type == "text" && part.Text != "" { parts = append(parts, part.Text) } } inputTokens := resp.Usage.InputTokens outputTokens := resp.Usage.OutputTokens return &types.ChatResponse{ Content: strings.Join(parts, ""), FinishReason: resp.StopReason, Usage: types.TokenUsage{ PromptTokens: inputTokens, CompletionTokens: outputTokens, TotalTokens: inputTokens + outputTokens, }, } }