From f79f3ac3d860fc7c4667246b44f7992dc116c4f1 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Wed, 20 Nov 2024 11:29:33 +0800 Subject: [PATCH] fix: fast conv message --- service/aiproxy/relay/adaptor/baidu/main.go | 31 +++++++++----------- service/aiproxy/relay/adaptor/zhipu/main.go | 11 ++----- service/aiproxy/relay/adaptor/zhipu/model.go | 23 ++++++--------- 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/service/aiproxy/relay/adaptor/baidu/main.go b/service/aiproxy/relay/adaptor/baidu/main.go index 6b77fbbc22c..b9ddf8b869b 100644 --- a/service/aiproxy/relay/adaptor/baidu/main.go +++ b/service/aiproxy/relay/adaptor/baidu/main.go @@ -36,16 +36,16 @@ type Message struct { } type ChatRequest struct { - Temperature *float64 `json:"temperature,omitempty"` - TopP *float64 `json:"top_p,omitempty"` - PenaltyScore *float64 `json:"penalty_score,omitempty"` - System string `json:"system,omitempty"` - UserID string `json:"user_id,omitempty"` - Messages []Message `json:"messages"` - MaxOutputTokens int `json:"max_output_tokens,omitempty"` - Stream bool `json:"stream,omitempty"` - DisableSearch bool `json:"disable_search,omitempty"` - EnableCitation bool `json:"enable_citation,omitempty"` + Temperature *float64 `json:"temperature,omitempty"` + TopP *float64 `json:"top_p,omitempty"` + PenaltyScore *float64 `json:"penalty_score,omitempty"` + System string `json:"system,omitempty"` + UserID string `json:"user_id,omitempty"` + Messages []model.Message `json:"messages"` + MaxOutputTokens int `json:"max_output_tokens,omitempty"` + Stream bool `json:"stream,omitempty"` + DisableSearch bool `json:"disable_search,omitempty"` + EnableCitation bool `json:"enable_citation,omitempty"` } type Error struct { @@ -57,7 +57,7 @@ var baiduTokenStore sync.Map func ConvertRequest(request *model.GeneralOpenAIRequest) *ChatRequest { baiduRequest := ChatRequest{ - Messages: make([]Message, 0, len(request.Messages)), + Messages: request.Messages, Temperature: request.Temperature, TopP: request.TopP, Stream: request.Stream, @@ -80,14 +80,11 @@ func ConvertRequest(request *model.GeneralOpenAIRequest) *ChatRequest { baiduRequest.PenaltyScore = &mappedScore } - for _, message := range request.Messages { + for i, message := range request.Messages { if message.Role == "system" { baiduRequest.System = message.StringContent() - } else { - baiduRequest.Messages = append(baiduRequest.Messages, Message{ - Role: message.Role, - Content: message.StringContent(), - }) + request.Messages = append(request.Messages[:i], request.Messages[i+1:]...) + break } } return &baiduRequest diff --git a/service/aiproxy/relay/adaptor/zhipu/main.go b/service/aiproxy/relay/adaptor/zhipu/main.go index 85c35561b48..5924e3a9acd 100644 --- a/service/aiproxy/relay/adaptor/zhipu/main.go +++ b/service/aiproxy/relay/adaptor/zhipu/main.go @@ -80,15 +80,8 @@ func GetToken(apikey string) string { } func ConvertRequest(request *model.GeneralOpenAIRequest) *Request { - messages := make([]Message, 0, len(request.Messages)) - for _, message := range request.Messages { - messages = append(messages, Message{ - Role: message.Role, - Content: message.StringContent(), - }) - } return &Request{ - Prompt: messages, + Prompt: request.Messages, Temperature: request.Temperature, TopP: request.TopP, Incremental: false, @@ -108,7 +101,7 @@ func responseZhipu2OpenAI(response *Response) *openai.TextResponse { Index: i, Message: model.Message{ Role: choice.Role, - Content: strings.Trim(choice.Content, "\""), + Content: strings.Trim(choice.Content.(string), "\""), }, FinishReason: "", } diff --git a/service/aiproxy/relay/adaptor/zhipu/model.go b/service/aiproxy/relay/adaptor/zhipu/model.go index 391a71c400a..e773812cc5b 100644 --- a/service/aiproxy/relay/adaptor/zhipu/model.go +++ b/service/aiproxy/relay/adaptor/zhipu/model.go @@ -6,24 +6,19 @@ import ( "github.com/labring/sealos/service/aiproxy/relay/model" ) -type Message struct { - Role string `json:"role"` - Content string `json:"content"` -} - type Request struct { - Temperature *float64 `json:"temperature,omitempty"` - TopP *float64 `json:"top_p,omitempty"` - RequestID string `json:"request_id,omitempty"` - Prompt []Message `json:"prompt"` - Incremental bool `json:"incremental,omitempty"` + Temperature *float64 `json:"temperature,omitempty"` + TopP *float64 `json:"top_p,omitempty"` + RequestID string `json:"request_id,omitempty"` + Prompt []model.Message `json:"prompt"` + Incremental bool `json:"incremental,omitempty"` } type ResponseData struct { - TaskID string `json:"task_id"` - RequestID string `json:"request_id"` - TaskStatus string `json:"task_status"` - Choices []Message `json:"choices"` + TaskID string `json:"task_id"` + RequestID string `json:"request_id"` + TaskStatus string `json:"task_status"` + Choices []model.Message `json:"choices"` model.Usage `json:"usage"` }