This commit replaces the Axum/Rust backend with a Gin/Go implementation. The original Rust code has been archived in the 'rust' branch.
144 lines
3.6 KiB
Go
144 lines
3.6 KiB
Go
package providers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"llm-proxy/internal/config"
|
|
"llm-proxy/internal/models"
|
|
"github.com/go-resty/resty/v2"
|
|
)
|
|
|
|
type DeepSeekProvider struct {
|
|
client *resty.Client
|
|
config config.DeepSeekConfig
|
|
apiKey string
|
|
}
|
|
|
|
func NewDeepSeekProvider(cfg config.DeepSeekConfig, apiKey string) *DeepSeekProvider {
|
|
return &DeepSeekProvider{
|
|
client: resty.New(),
|
|
config: cfg,
|
|
apiKey: apiKey,
|
|
}
|
|
}
|
|
|
|
func (p *DeepSeekProvider) Name() string {
|
|
return "deepseek"
|
|
}
|
|
|
|
func (p *DeepSeekProvider) ChatCompletion(ctx context.Context, req *models.UnifiedRequest) (*models.ChatCompletionResponse, error) {
|
|
messagesJSON, err := MessagesToOpenAIJSON(req.Messages)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to convert messages: %w", err)
|
|
}
|
|
|
|
body := BuildOpenAIBody(req, messagesJSON, false)
|
|
|
|
// Sanitize for deepseek-reasoner
|
|
if req.Model == "deepseek-reasoner" {
|
|
delete(body, "temperature")
|
|
delete(body, "top_p")
|
|
delete(body, "presence_penalty")
|
|
delete(body, "frequency_penalty")
|
|
|
|
// Ensure assistant messages have content and reasoning_content
|
|
if msgs, ok := body["messages"].([]interface{}); ok {
|
|
for _, m := range msgs {
|
|
if msg, ok := m.(map[string]interface{}); ok {
|
|
if msg["role"] == "assistant" {
|
|
if msg["reasoning_content"] == nil {
|
|
msg["reasoning_content"] = " "
|
|
}
|
|
if msg["content"] == nil || msg["content"] == "" {
|
|
msg["content"] = ""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
resp, err := p.client.R().
|
|
SetContext(ctx).
|
|
SetHeader("Authorization", "Bearer "+p.apiKey).
|
|
SetBody(body).
|
|
Post(fmt.Sprintf("%s/chat/completions", p.config.BaseURL))
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("request failed: %w", err)
|
|
}
|
|
|
|
if !resp.IsSuccess() {
|
|
return nil, fmt.Errorf("DeepSeek API error (%d): %s", resp.StatusCode(), resp.String())
|
|
}
|
|
|
|
var respJSON map[string]interface{}
|
|
if err := json.Unmarshal(resp.Body(), &respJSON); err != nil {
|
|
return nil, fmt.Errorf("failed to parse response: %w", err)
|
|
}
|
|
|
|
return ParseOpenAIResponse(respJSON, req.Model)
|
|
}
|
|
|
|
func (p *DeepSeekProvider) ChatCompletionStream(ctx context.Context, req *models.UnifiedRequest) (<-chan *models.ChatCompletionStreamResponse, error) {
|
|
messagesJSON, err := MessagesToOpenAIJSON(req.Messages)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to convert messages: %w", err)
|
|
}
|
|
|
|
body := BuildOpenAIBody(req, messagesJSON, true)
|
|
|
|
// Sanitize for deepseek-reasoner
|
|
if req.Model == "deepseek-reasoner" {
|
|
delete(body, "temperature")
|
|
delete(body, "top_p")
|
|
delete(body, "presence_penalty")
|
|
delete(body, "frequency_penalty")
|
|
|
|
// Ensure assistant messages have content and reasoning_content
|
|
if msgs, ok := body["messages"].([]interface{}); ok {
|
|
for _, m := range msgs {
|
|
if msg, ok := m.(map[string]interface{}); ok {
|
|
if msg["role"] == "assistant" {
|
|
if msg["reasoning_content"] == nil {
|
|
msg["reasoning_content"] = " "
|
|
}
|
|
if msg["content"] == nil || msg["content"] == "" {
|
|
msg["content"] = ""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
resp, err := p.client.R().
|
|
SetContext(ctx).
|
|
SetHeader("Authorization", "Bearer "+p.apiKey).
|
|
SetBody(body).
|
|
SetDoNotParseResponse(true).
|
|
Post(fmt.Sprintf("%s/chat/completions", p.config.BaseURL))
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("request failed: %w", err)
|
|
}
|
|
|
|
if !resp.IsSuccess() {
|
|
return nil, fmt.Errorf("DeepSeek API error (%d): %s", resp.StatusCode(), resp.String())
|
|
}
|
|
|
|
ch := make(chan *models.ChatCompletionStreamResponse)
|
|
|
|
go func() {
|
|
defer close(ch)
|
|
err := StreamOpenAI(resp.RawBody(), ch)
|
|
if err != nil {
|
|
fmt.Printf("DeepSeek Stream error: %v\n", err)
|
|
}
|
|
}()
|
|
|
|
return ch, nil
|
|
}
|