️ feat: channel support plugin settings (#89)

This commit is contained in:
Buer
2024-03-08 14:49:33 +08:00
committed by GitHub
parent 41134576f2
commit d8d880bf85
17 changed files with 243 additions and 65 deletions

View File

@@ -15,8 +15,6 @@ type aliStreamHandler struct {
lastStreamResponse string
}
const AliEnableSearchModelSuffix = "-internet"
func (p *AliProvider) CreateChatCompletion(request *types.ChatCompletionRequest) (*types.ChatCompletionResponse, *types.OpenAIErrorWithStatusCode) {
req, errWithCode := p.getAliChatRequest(request)
if errWithCode != nil {
@@ -70,7 +68,7 @@ func (p *AliProvider) getAliChatRequest(request *types.ChatCompletionRequest) (*
headers["X-DashScope-SSE"] = "enable"
}
aliRequest := convertFromChatOpenai(request)
aliRequest := p.convertFromChatOpenai(request)
// 创建请求
req, err := p.Requester.NewRequest(http.MethodPost, fullRequestURL, p.Requester.WithBody(aliRequest), p.Requester.WithHeader(headers))
if err != nil {
@@ -110,7 +108,7 @@ func (p *AliProvider) convertToChatOpenai(response *AliChatResponse, request *ty
}
// 阿里云聊天请求体
func convertFromChatOpenai(request *types.ChatCompletionRequest) *AliChatRequest {
func (p *AliProvider) convertFromChatOpenai(request *types.ChatCompletionRequest) *AliChatRequest {
messages := make([]AliMessage, 0, len(request.Messages))
for i := 0; i < len(request.Messages); i++ {
message := request.Messages[i]
@@ -141,24 +139,35 @@ func convertFromChatOpenai(request *types.ChatCompletionRequest) *AliChatRequest
}
enableSearch := false
aliModel := request.Model
if strings.HasSuffix(aliModel, AliEnableSearchModelSuffix) {
enableSearch = true
aliModel = strings.TrimSuffix(aliModel, AliEnableSearchModelSuffix)
}
return &AliChatRequest{
Model: aliModel,
aliChatRequest := &AliChatRequest{
Model: request.Model,
Input: AliInput{
Messages: messages,
},
Parameters: AliParameters{
ResultFormat: "message",
EnableSearch: enableSearch,
IncrementalOutput: request.Stream,
},
}
p.pluginHandle(aliChatRequest)
return aliChatRequest
}
func (p *AliProvider) pluginHandle(request *AliChatRequest) {
if p.Channel.Plugin == nil {
return
}
plugin := p.Channel.Plugin.Data()
// 检测是否开启了 web_search 插件
if pWeb, ok := plugin["web_search"]; ok {
if enable, ok := pWeb["enable"].(bool); ok && enable {
request.Parameters.EnableSearch = true
}
}
}
// 转换为OpenAI聊天流式请求体
@@ -185,11 +194,11 @@ func (h *aliStreamHandler) handlerStream(rawLine *[]byte, dataChan chan string,
return
}
h.convertToOpenaiStream(&aliResponse, dataChan, errChan)
h.convertToOpenaiStream(&aliResponse, dataChan)
}
func (h *aliStreamHandler) convertToOpenaiStream(aliResponse *AliChatResponse, dataChan chan string, errChan chan error) {
func (h *aliStreamHandler) convertToOpenaiStream(aliResponse *AliChatResponse, dataChan chan string) {
content := aliResponse.Output.Choices[0].Message.StringContent()
var choice types.ChatCompletionStreamChoice