Skip to content

Commit

Permalink
fix: fast conv message
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Nov 20, 2024
1 parent dd480e6 commit f79f3ac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
31 changes: 14 additions & 17 deletions service/aiproxy/relay/adaptor/baidu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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
Expand Down
11 changes: 2 additions & 9 deletions service/aiproxy/relay/adaptor/zhipu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: "",
}
Expand Down
23 changes: 9 additions & 14 deletions service/aiproxy/relay/adaptor/zhipu/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

0 comments on commit f79f3ac

Please sign in to comment.