feat: xunfei support functions

This commit is contained in:
MartialBE
2024-01-02 22:40:47 +08:00
committed by Buer
parent 3b8ae9a6cd
commit e052009eba
4 changed files with 95 additions and 30 deletions

View File

@@ -115,6 +115,16 @@ func (p *XunfeiProvider) requestOpenAI2Xunfei(request *types.ChatCompletionReque
}
}
xunfeiRequest := XunfeiChatRequest{}
if request.Tools != nil {
functions := make([]*types.ChatCompletionFunction, 0, len(request.Tools))
for _, tool := range request.Tools {
functions = append(functions, &tool.Function)
}
xunfeiRequest.Payload.Functions = &XunfeiChatPayloadFunctions{}
xunfeiRequest.Payload.Functions.Text = functions
}
xunfeiRequest.Header.AppId = p.apiId
xunfeiRequest.Parameter.Chat.Domain = p.domain
xunfeiRequest.Parameter.Chat.Temperature = request.Temperature
@@ -132,14 +142,31 @@ func (p *XunfeiProvider) responseXunfei2OpenAI(response *XunfeiChatResponse) *ty
},
}
}
choice := types.ChatCompletionChoice{
Index: 0,
Message: types.ChatCompletionMessage{
Role: "assistant",
Content: response.Payload.Choices.Text[0].Content,
},
Index: 0,
FinishReason: base.StopFinishReason,
}
xunfeiText := response.Payload.Choices.Text[0]
if xunfeiText.FunctionCall != nil {
choice.Message = types.ChatCompletionMessage{
Role: "assistant",
ToolCalls: []*types.ChatCompletionToolCalls{
{
Type: "function",
Function: *xunfeiText.FunctionCall,
},
},
}
} else {
choice.Message = types.ChatCompletionMessage{
Role: "assistant",
Content: xunfeiText.Content,
}
}
fullTextResponse := types.ChatCompletionResponse{
Object: "chat.completion",
Created: common.GetTimestamp(),

View File

@@ -7,30 +7,45 @@ type XunfeiMessage struct {
Content string `json:"content"`
}
type XunfeiChatPayloadMessage struct {
Text []XunfeiMessage `json:"text"`
}
type XunfeiChatPayloadFunctions struct {
Text []*types.ChatCompletionFunction `json:"text"`
}
type XunfeiChatPayload struct {
Message XunfeiChatPayloadMessage `json:"message"`
Functions *XunfeiChatPayloadFunctions `json:"functions,omitempty"`
}
type XunfeiParameterChat struct {
Domain string `json:"domain,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopK int `json:"top_k,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Auditing bool `json:"auditing,omitempty"`
}
type XunfeiChatRequestParameter struct {
Chat XunfeiParameterChat `json:"chat"`
}
type XunfeiChatRequest struct {
Header struct {
AppId string `json:"app_id"`
} `json:"header"`
Parameter struct {
Chat struct {
Domain string `json:"domain,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopK int `json:"top_k,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Auditing bool `json:"auditing,omitempty"`
} `json:"chat"`
} `json:"parameter"`
Payload struct {
Message struct {
Text []XunfeiMessage `json:"text"`
} `json:"message"`
} `json:"payload"`
Parameter XunfeiChatRequestParameter `json:"parameter"`
Payload XunfeiChatPayload `json:"payload"`
}
type XunfeiChatResponseTextItem struct {
Content string `json:"content"`
Role string `json:"role"`
Index int `json:"index"`
Content string `json:"content"`
Role string `json:"role"`
Index int `json:"index"`
ContentType string `json:"content_type,omitempty"`
FunctionCall *types.ChatCompletionToolCallsFunction `json:"function_call,omitempty"`
}
type XunfeiChatResponse struct {