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

1
.gitignore vendored
View File

@ -8,4 +8,5 @@ build
logs
data
tmp/
test/
.env

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,
},
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 XunfeiChatRequest struct {
Header struct {
AppId string `json:"app_id"`
} `json:"header"`
Parameter struct {
Chat struct {
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"`
} `json:"chat"`
} `json:"parameter"`
Payload struct {
Message struct {
Text []XunfeiMessage `json:"text"`
} `json:"message"`
} `json:"payload"`
}
type XunfeiChatRequestParameter struct {
Chat XunfeiParameterChat `json:"chat"`
}
type XunfeiChatRequest struct {
Header struct {
AppId string `json:"app_id"`
} `json:"header"`
Parameter XunfeiChatRequestParameter `json:"parameter"`
Payload XunfeiChatPayload `json:"payload"`
}
type XunfeiChatResponseTextItem struct {
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 {

View File

@ -5,12 +5,23 @@ const (
ContentTypeImageURL = "image_url"
)
type ChatCompletionToolCallsFunction struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}
type ChatCompletionToolCalls struct {
Id string `json:"id"`
Type string `json:"type"`
Function ChatCompletionToolCallsFunction `json:"function"`
}
type ChatCompletionMessage struct {
Role string `json:"role"`
Content any `json:"content"`
Content any `json:"content,omitempty"`
Name *string `json:"name,omitempty"`
FunctionCall any `json:"function_call,omitempty"`
ToolCalls any `json:"tool_calls,omitempty"`
FunctionCall *ChatCompletionToolCallsFunction `json:"function_call,omitempty"`
ToolCalls []*ChatCompletionToolCalls `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
@ -112,12 +123,23 @@ type ChatCompletionRequest struct {
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
LogitBias any `json:"logit_bias,omitempty"`
User string `json:"user,omitempty"`
Functions any `json:"functions,omitempty"`
Functions []*ChatCompletionFunction `json:"functions,omitempty"`
FunctionCall any `json:"function_call,omitempty"`
Tools any `json:"tools,omitempty"`
Tools []*ChatCompletionTool `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
}
type ChatCompletionFunction struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters any `json:"parameters"`
}
type ChatCompletionTool struct {
Type string `json:"type"`
Function ChatCompletionFunction `json:"function"`
}
type ChatCompletionChoice struct {
Index int `json:"index"`
Message ChatCompletionMessage `json:"message"`