mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-29 22:56:39 +08:00
449 lines
13 KiB
Go
449 lines
13 KiB
Go
package ali
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/songquanpeng/one-api/common"
|
|
"github.com/songquanpeng/one-api/common/helper"
|
|
"github.com/songquanpeng/one-api/common/logger"
|
|
"github.com/songquanpeng/one-api/relay/channel/openai"
|
|
"github.com/songquanpeng/one-api/relay/model"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// https://help.aliyun.com/document_detail/613695.html?spm=a2c4g.2399480.0.0.1adb778fAdzP9w#341800c0f8w0r
|
|
|
|
const EnableSearchModelSuffix = "-internet"
|
|
|
|
func ConvertRequest(request model.GeneralOpenAIRequest) *ChatRequest {
|
|
messages := make([]Message, 0, len(request.Messages))
|
|
for i := 0; i < len(request.Messages); i++ {
|
|
message := request.Messages[i]
|
|
messages = append(messages, Message{
|
|
Content: message.StringContent(),
|
|
Role: strings.ToLower(message.Role),
|
|
})
|
|
}
|
|
enableSearch := false
|
|
aliModel := request.Model
|
|
if strings.HasSuffix(aliModel, EnableSearchModelSuffix) {
|
|
enableSearch = true
|
|
aliModel = strings.TrimSuffix(aliModel, EnableSearchModelSuffix)
|
|
}
|
|
if request.TopP >= 1 {
|
|
request.TopP = 0.9999
|
|
}
|
|
return &ChatRequest{
|
|
Model: aliModel,
|
|
Input: Input{
|
|
Messages: messages,
|
|
},
|
|
Parameters: Parameters{
|
|
EnableSearch: enableSearch,
|
|
IncrementalOutput: request.Stream,
|
|
Seed: uint64(request.Seed),
|
|
MaxTokens: request.MaxTokens,
|
|
Temperature: request.Temperature,
|
|
TopP: request.TopP,
|
|
},
|
|
}
|
|
}
|
|
|
|
func ConvertEmbeddingRequest(request model.GeneralOpenAIRequest) *EmbeddingRequest {
|
|
return &EmbeddingRequest{
|
|
Model: "text-embedding-v1",
|
|
Input: struct {
|
|
Texts []string `json:"texts"`
|
|
}{
|
|
Texts: request.ParseInput(),
|
|
},
|
|
}
|
|
}
|
|
|
|
func ConvertImageRequest(request model.ImageRequest) *ImageRequest {
|
|
var imageRequest ImageRequest
|
|
imageRequest.Input.Prompt = request.Prompt
|
|
imageRequest.Model = request.Model
|
|
imageRequest.Parameters.Size = strings.Replace(request.Size, "x", "*", -1)
|
|
imageRequest.Parameters.N = request.N
|
|
imageRequest.ResponseFormat = request.ResponseFormat
|
|
|
|
return &imageRequest
|
|
}
|
|
|
|
func EmbeddingHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
|
var aliResponse EmbeddingResponse
|
|
err := json.NewDecoder(resp.Body).Decode(&aliResponse)
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
|
|
err = resp.Body.Close()
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
|
|
if aliResponse.Code != "" {
|
|
return &model.ErrorWithStatusCode{
|
|
Error: model.Error{
|
|
Message: aliResponse.Message,
|
|
Type: aliResponse.Code,
|
|
Param: aliResponse.RequestId,
|
|
Code: aliResponse.Code,
|
|
},
|
|
StatusCode: resp.StatusCode,
|
|
}, nil
|
|
}
|
|
|
|
fullTextResponse := embeddingResponseAli2OpenAI(&aliResponse)
|
|
jsonResponse, err := json.Marshal(fullTextResponse)
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
c.Writer.WriteHeader(resp.StatusCode)
|
|
_, err = c.Writer.Write(jsonResponse)
|
|
return nil, &fullTextResponse.Usage
|
|
}
|
|
|
|
func embeddingResponseAli2OpenAI(response *EmbeddingResponse) *openai.EmbeddingResponse {
|
|
openAIEmbeddingResponse := openai.EmbeddingResponse{
|
|
Object: "list",
|
|
Data: make([]openai.EmbeddingResponseItem, 0, len(response.Output.Embeddings)),
|
|
Model: "text-embedding-v1",
|
|
Usage: model.Usage{TotalTokens: response.Usage.TotalTokens},
|
|
}
|
|
|
|
for _, item := range response.Output.Embeddings {
|
|
openAIEmbeddingResponse.Data = append(openAIEmbeddingResponse.Data, openai.EmbeddingResponseItem{
|
|
Object: `embedding`,
|
|
Index: item.TextIndex,
|
|
Embedding: item.Embedding,
|
|
})
|
|
}
|
|
return &openAIEmbeddingResponse
|
|
}
|
|
|
|
func responseAli2OpenAI(response *ChatResponse) *openai.TextResponse {
|
|
choice := openai.TextResponseChoice{
|
|
Index: 0,
|
|
Message: model.Message{
|
|
Role: "assistant",
|
|
Content: response.Output.Text,
|
|
},
|
|
FinishReason: response.Output.FinishReason,
|
|
}
|
|
fullTextResponse := openai.TextResponse{
|
|
Id: response.RequestId,
|
|
Object: "chat.completion",
|
|
Created: helper.GetTimestamp(),
|
|
Choices: []openai.TextResponseChoice{choice},
|
|
Usage: model.Usage{
|
|
PromptTokens: response.Usage.InputTokens,
|
|
CompletionTokens: response.Usage.OutputTokens,
|
|
TotalTokens: response.Usage.InputTokens + response.Usage.OutputTokens,
|
|
},
|
|
}
|
|
return &fullTextResponse
|
|
}
|
|
|
|
func streamResponseAli2OpenAI(aliResponse *ChatResponse) *openai.ChatCompletionsStreamResponse {
|
|
var choice openai.ChatCompletionsStreamResponseChoice
|
|
choice.Delta.Content = aliResponse.Output.Text
|
|
if aliResponse.Output.FinishReason != "null" {
|
|
finishReason := aliResponse.Output.FinishReason
|
|
choice.FinishReason = &finishReason
|
|
}
|
|
response := openai.ChatCompletionsStreamResponse{
|
|
Id: aliResponse.RequestId,
|
|
Object: "chat.completion.chunk",
|
|
Created: helper.GetTimestamp(),
|
|
Model: "qwen",
|
|
Choices: []openai.ChatCompletionsStreamResponseChoice{choice},
|
|
}
|
|
return &response
|
|
}
|
|
|
|
func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
|
var usage model.Usage
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
if atEOF && len(data) == 0 {
|
|
return 0, nil, nil
|
|
}
|
|
if i := strings.Index(string(data), "\n"); i >= 0 {
|
|
return i + 1, data[0:i], nil
|
|
}
|
|
if atEOF {
|
|
return len(data), data, nil
|
|
}
|
|
return 0, nil, nil
|
|
})
|
|
dataChan := make(chan string)
|
|
stopChan := make(chan bool)
|
|
go func() {
|
|
for scanner.Scan() {
|
|
data := scanner.Text()
|
|
if len(data) < 5 { // ignore blank line or wrong format
|
|
continue
|
|
}
|
|
if data[:5] != "data:" {
|
|
continue
|
|
}
|
|
data = data[5:]
|
|
dataChan <- data
|
|
}
|
|
stopChan <- true
|
|
}()
|
|
common.SetEventStreamHeaders(c)
|
|
//lastResponseText := ""
|
|
c.Stream(func(w io.Writer) bool {
|
|
select {
|
|
case data := <-dataChan:
|
|
var aliResponse ChatResponse
|
|
err := json.Unmarshal([]byte(data), &aliResponse)
|
|
if err != nil {
|
|
logger.SysError("error unmarshalling stream response: " + err.Error())
|
|
return true
|
|
}
|
|
if aliResponse.Usage.OutputTokens != 0 {
|
|
usage.PromptTokens = aliResponse.Usage.InputTokens
|
|
usage.CompletionTokens = aliResponse.Usage.OutputTokens
|
|
usage.TotalTokens = aliResponse.Usage.InputTokens + aliResponse.Usage.OutputTokens
|
|
}
|
|
response := streamResponseAli2OpenAI(&aliResponse)
|
|
//response.Choices[0].Delta.Content = strings.TrimPrefix(response.Choices[0].Delta.Content, lastResponseText)
|
|
//lastResponseText = aliResponse.Output.Text
|
|
jsonResponse, err := json.Marshal(response)
|
|
if err != nil {
|
|
logger.SysError("error marshalling stream response: " + err.Error())
|
|
return true
|
|
}
|
|
c.Render(-1, common.CustomEvent{Data: "data: " + string(jsonResponse)})
|
|
return true
|
|
case <-stopChan:
|
|
c.Render(-1, common.CustomEvent{Data: "data: [DONE]"})
|
|
return false
|
|
}
|
|
})
|
|
err := resp.Body.Close()
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
return nil, &usage
|
|
}
|
|
|
|
func Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
|
var aliResponse ChatResponse
|
|
responseBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
err = resp.Body.Close()
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
err = json.Unmarshal(responseBody, &aliResponse)
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
if aliResponse.Code != "" {
|
|
return &model.ErrorWithStatusCode{
|
|
Error: model.Error{
|
|
Message: aliResponse.Message,
|
|
Type: aliResponse.Code,
|
|
Param: aliResponse.RequestId,
|
|
Code: aliResponse.Code,
|
|
},
|
|
StatusCode: resp.StatusCode,
|
|
}, nil
|
|
}
|
|
fullTextResponse := responseAli2OpenAI(&aliResponse)
|
|
fullTextResponse.Model = "qwen"
|
|
jsonResponse, err := json.Marshal(fullTextResponse)
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
c.Writer.WriteHeader(resp.StatusCode)
|
|
_, err = c.Writer.Write(jsonResponse)
|
|
return nil, &fullTextResponse.Usage
|
|
}
|
|
|
|
func ImageHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
|
apiKey := c.Request.Header.Get("Authorization")
|
|
apiKey = strings.TrimPrefix(apiKey, "Bearer ")
|
|
responseFormat := c.GetString("response_format")
|
|
|
|
var aliTaskResponse TaskResponse
|
|
responseBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
err = resp.Body.Close()
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
err = json.Unmarshal(responseBody, &aliTaskResponse)
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
|
|
if aliTaskResponse.Message != "" {
|
|
logger.SysError("aliAsyncTask err: " + string(responseBody))
|
|
return openai.ErrorWrapper(errors.New(aliTaskResponse.Message), "ali_async_task_failed", http.StatusInternalServerError), nil
|
|
}
|
|
|
|
aliResponse, err, _ := asyncTaskWait(aliTaskResponse.Output.TaskId, apiKey)
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "ali_async_task_wait_failed", http.StatusInternalServerError), nil
|
|
}
|
|
|
|
if aliResponse.Output.TaskStatus != "SUCCEEDED" {
|
|
return &model.ErrorWithStatusCode{
|
|
Error: model.Error{
|
|
Message: aliResponse.Output.Message,
|
|
Type: "ali_error",
|
|
Param: "",
|
|
Code: aliResponse.Output.Code,
|
|
},
|
|
StatusCode: resp.StatusCode,
|
|
}, nil
|
|
}
|
|
|
|
fullTextResponse := responseAli2OpenAIImage(aliResponse, responseFormat)
|
|
jsonResponse, err := json.Marshal(fullTextResponse)
|
|
if err != nil {
|
|
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
|
|
}
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
c.Writer.WriteHeader(resp.StatusCode)
|
|
_, err = c.Writer.Write(jsonResponse)
|
|
return nil, &fullTextResponse.Usage
|
|
}
|
|
|
|
func asyncTask(taskID string, key string) (*TaskResponse, error, []byte) {
|
|
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/tasks/%s", taskID)
|
|
|
|
var aliResponse TaskResponse
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return &aliResponse, err, nil
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+key)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
logger.SysError("aliAsyncTask client.Do err: " + err.Error())
|
|
return &aliResponse, err, nil
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
responseBody, err := io.ReadAll(resp.Body)
|
|
|
|
var response TaskResponse
|
|
err = json.Unmarshal(responseBody, &response)
|
|
if err != nil {
|
|
logger.SysError("aliAsyncTask NewDecoder err: " + err.Error())
|
|
return &aliResponse, err, nil
|
|
}
|
|
|
|
return &response, nil, responseBody
|
|
}
|
|
|
|
func asyncTaskWait(taskID string, key string) (*TaskResponse, error, []byte) {
|
|
waitSeconds := 2
|
|
step := 0
|
|
|
|
var taskResponse TaskResponse
|
|
var responseBody []byte
|
|
|
|
for {
|
|
step++
|
|
rsp, err, body := asyncTask(taskID, key)
|
|
responseBody = body
|
|
if err != nil {
|
|
return &taskResponse, err, responseBody
|
|
}
|
|
|
|
if rsp.Output.TaskStatus == "" {
|
|
return &taskResponse, nil, responseBody
|
|
}
|
|
|
|
switch rsp.Output.TaskStatus {
|
|
case "FAILED":
|
|
fallthrough
|
|
case "CANCELED":
|
|
fallthrough
|
|
case "SUCCEEDED":
|
|
fallthrough
|
|
case "UNKNOWN":
|
|
return rsp, nil, responseBody
|
|
}
|
|
|
|
time.Sleep(time.Duration(waitSeconds) * time.Second)
|
|
}
|
|
|
|
return &taskResponse, nil, responseBody
|
|
}
|
|
|
|
func responseAli2OpenAIImage(response *TaskResponse, responseFormat string) *openai.ImageResponse {
|
|
imageRespones := openai.ImageResponse{}
|
|
|
|
for _, data := range response.Output.Results {
|
|
var b64Json string
|
|
if responseFormat == "b64_json" {
|
|
// 读取 data.Url 的图片数据并转存到 b64Json
|
|
imageData, err := getImageData(data.Url)
|
|
if err != nil {
|
|
// 处理获取图片数据失败的情况
|
|
logger.SysError("getImageData Error getting image data: " + err.Error())
|
|
continue
|
|
}
|
|
|
|
// 将图片数据转为 Base64 编码的字符串
|
|
b64Json = Base64Encode(imageData)
|
|
} else {
|
|
// 如果 responseFormat 不是 "b64_json",则直接使用 data.B64Image
|
|
b64Json = data.B64Image
|
|
}
|
|
|
|
imageRespones.Data = append(imageRespones.Data, openai.ImageData{
|
|
Url: data.Url,
|
|
B64Json: b64Json,
|
|
RevisedPrompt: "",
|
|
})
|
|
}
|
|
return &imageRespones
|
|
}
|
|
|
|
func getImageData(url string) ([]byte, error) {
|
|
response, err := http.Get(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
imageData, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return imageData, nil
|
|
}
|
|
|
|
func Base64Encode(data []byte) string {
|
|
b64Json := base64.StdEncoding.EncodeToString(data)
|
|
return b64Json
|
|
}
|