mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-11 19:03:43 +08:00
Merge commit '2369025842b828ac38f4427fd1ebab8d03b1fe7f'
This commit is contained in:
@@ -1,105 +1,106 @@
|
||||
package ali
|
||||
|
||||
// import (
|
||||
// "github.com/Laisky/errors/v2"
|
||||
// "fmt"
|
||||
// "github.com/gin-gonic/gin"
|
||||
// "github.com/songquanpeng/one-api/common/config"
|
||||
// "github.com/songquanpeng/one-api/relay/adaptor"
|
||||
// "github.com/songquanpeng/one-api/relay/meta"
|
||||
// "github.com/songquanpeng/one-api/relay/model"
|
||||
// "github.com/songquanpeng/one-api/relay/relaymode"
|
||||
// "io"
|
||||
// "net/http"
|
||||
// )
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
// // https://help.aliyun.com/zh/dashscope/developer-reference/api-details
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/Laisky/one-api/relay/relaymode"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// type Adaptor struct {
|
||||
// }
|
||||
// https://help.aliyun.com/zh/dashscope/developer-reference/api-details
|
||||
|
||||
// func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
type Adaptor struct {
|
||||
}
|
||||
|
||||
// }
|
||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
|
||||
// func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
// fullRequestURL := ""
|
||||
// switch meta.Mode {
|
||||
// case relaymode.Embeddings:
|
||||
// fullRequestURL = fmt.Sprintf("%s/api/v1/services/embeddings/text-embedding/text-embedding", meta.BaseURL)
|
||||
// case relaymode.ImagesGenerations:
|
||||
// fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/text2image/image-synthesis", meta.BaseURL)
|
||||
// default:
|
||||
// fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/text-generation/generation", meta.BaseURL)
|
||||
// }
|
||||
}
|
||||
|
||||
// return fullRequestURL, nil
|
||||
// }
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
fullRequestURL := ""
|
||||
switch meta.Mode {
|
||||
case relaymode.Embeddings:
|
||||
fullRequestURL = fmt.Sprintf("%s/api/v1/services/embeddings/text-embedding/text-embedding", meta.BaseURL)
|
||||
case relaymode.ImagesGenerations:
|
||||
fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/text2image/image-synthesis", meta.BaseURL)
|
||||
default:
|
||||
fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/text-generation/generation", meta.BaseURL)
|
||||
}
|
||||
|
||||
// func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
||||
// adaptor.SetupCommonRequestHeader(c, req, meta)
|
||||
// if meta.IsStream {
|
||||
// req.Header.Set("Accept", "text/event-stream")
|
||||
// req.Header.Set("X-DashScope-SSE", "enable")
|
||||
// }
|
||||
// req.Header.Set("Authorization", "Bearer "+meta.APIKey)
|
||||
return fullRequestURL, nil
|
||||
}
|
||||
|
||||
// if meta.Mode == relaymode.ImagesGenerations {
|
||||
// req.Header.Set("X-DashScope-Async", "enable")
|
||||
// }
|
||||
// if c.GetString(config.KeyPlugin) != "" {
|
||||
// req.Header.Set("X-DashScope-Plugin", c.GetString(config.KeyPlugin))
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
||||
adaptor.SetupCommonRequestHeader(c, req, meta)
|
||||
if meta.IsStream {
|
||||
req.Header.Set("Accept", "text/event-stream")
|
||||
req.Header.Set("X-DashScope-SSE", "enable")
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+meta.APIKey)
|
||||
|
||||
// func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
||||
// if request == nil {
|
||||
// return nil, errors.New("request is nil")
|
||||
// }
|
||||
// switch relayMode {
|
||||
// case relaymode.Embeddings:
|
||||
// aliEmbeddingRequest := ConvertEmbeddingRequest(*request)
|
||||
// return aliEmbeddingRequest, nil
|
||||
// default:
|
||||
// aliRequest := ConvertRequest(*request)
|
||||
// return aliRequest, nil
|
||||
// }
|
||||
// }
|
||||
if meta.Mode == relaymode.ImagesGenerations {
|
||||
req.Header.Set("X-DashScope-Async", "enable")
|
||||
}
|
||||
if c.GetString(config.KeyPlugin) != "" {
|
||||
req.Header.Set("X-DashScope-Plugin", c.GetString(config.KeyPlugin))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
||||
// if request == nil {
|
||||
// return nil, errors.New("request is nil")
|
||||
// }
|
||||
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
||||
if request == nil {
|
||||
return nil, errors.New("request is nil")
|
||||
}
|
||||
switch relayMode {
|
||||
case relaymode.Embeddings:
|
||||
aliEmbeddingRequest := ConvertEmbeddingRequest(*request)
|
||||
return aliEmbeddingRequest, nil
|
||||
default:
|
||||
aliRequest := ConvertRequest(*request)
|
||||
return aliRequest, nil
|
||||
}
|
||||
}
|
||||
|
||||
// aliRequest := ConvertImageRequest(*request)
|
||||
// return aliRequest, nil
|
||||
// }
|
||||
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
||||
if request == nil {
|
||||
return nil, errors.New("request is nil")
|
||||
}
|
||||
|
||||
// func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
|
||||
// return adaptor.DoRequestHelper(a, c, meta, requestBody)
|
||||
// }
|
||||
aliRequest := ConvertImageRequest(*request)
|
||||
return aliRequest, nil
|
||||
}
|
||||
|
||||
// func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
||||
// if meta.IsStream {
|
||||
// err, usage = StreamHandler(c, resp)
|
||||
// } else {
|
||||
// switch meta.Mode {
|
||||
// case relaymode.Embeddings:
|
||||
// err, usage = EmbeddingHandler(c, resp)
|
||||
// case relaymode.ImagesGenerations:
|
||||
// err, usage = ImageHandler(c, resp)
|
||||
// default:
|
||||
// err, usage = Handler(c, resp)
|
||||
// }
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
|
||||
return adaptor.DoRequestHelper(a, c, meta, requestBody)
|
||||
}
|
||||
|
||||
// func (a *Adaptor) GetModelList() []string {
|
||||
// return ModelList
|
||||
// }
|
||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
||||
if meta.IsStream {
|
||||
err, usage = StreamHandler(c, resp)
|
||||
} else {
|
||||
switch meta.Mode {
|
||||
case relaymode.Embeddings:
|
||||
err, usage = EmbeddingHandler(c, resp)
|
||||
case relaymode.ImagesGenerations:
|
||||
err, usage = ImageHandler(c, resp)
|
||||
default:
|
||||
err, usage = Handler(c, resp)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// func (a *Adaptor) GetChannelName() string {
|
||||
// return "ali"
|
||||
// }
|
||||
func (a *Adaptor) GetModelList() []string {
|
||||
return ModelList
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetChannelName() string {
|
||||
return "ali"
|
||||
}
|
||||
|
||||
@@ -3,17 +3,18 @@ package ali
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ImageHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
|
||||
@@ -1,323 +1,279 @@
|
||||
package ali
|
||||
|
||||
// import (
|
||||
// "github.com/songquanpeng/one-api/common"
|
||||
// )
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
// // https://help.aliyun.com/document_detail/613695.html?spm=a2c4g.2399480.0.0.1adb778fAdzP9w#341800c0f8w0r
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// type AliMessage struct {
|
||||
// Content string `json:"content"`
|
||||
// Role string `json:"role"`
|
||||
// }
|
||||
// https://help.aliyun.com/document_detail/613695.html?spm=a2c4g.2399480.0.0.1adb778fAdzP9w#341800c0f8w0r
|
||||
|
||||
// type AliInput struct {
|
||||
// //Prompt string `json:"prompt"`
|
||||
// Messages []AliMessage `json:"messages"`
|
||||
// }
|
||||
const EnableSearchModelSuffix = "-internet"
|
||||
|
||||
// type AliParameters struct {
|
||||
// TopP float64 `json:"top_p,omitempty"`
|
||||
// TopK int `json:"top_k,omitempty"`
|
||||
// Seed uint64 `json:"seed,omitempty"`
|
||||
// EnableSearch bool `json:"enable_search,omitempty"`
|
||||
// }
|
||||
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,
|
||||
TopK: request.TopK,
|
||||
ResultFormat: "message",
|
||||
Tools: request.Tools,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// type AliChatRequest struct {
|
||||
// Model string `json:"model"`
|
||||
// Input AliInput `json:"input"`
|
||||
// Parameters AliParameters `json:"parameters,omitempty"`
|
||||
// }
|
||||
func ConvertEmbeddingRequest(request model.GeneralOpenAIRequest) *EmbeddingRequest {
|
||||
return &EmbeddingRequest{
|
||||
Model: "text-embedding-v1",
|
||||
Input: struct {
|
||||
Texts []string `json:"texts"`
|
||||
}{
|
||||
Texts: request.ParseInput(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// type AliEmbeddingRequest struct {
|
||||
// Model string `json:"model"`
|
||||
// Input struct {
|
||||
// Texts []string `json:"texts"`
|
||||
// } `json:"input"`
|
||||
// Parameters *struct {
|
||||
// TextType string `json:"text_type,omitempty"`
|
||||
// } `json:"parameters,omitempty"`
|
||||
// }
|
||||
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
|
||||
|
||||
// type AliEmbedding struct {
|
||||
// Embedding []float64 `json:"embedding"`
|
||||
// TextIndex int `json:"text_index"`
|
||||
// }
|
||||
return &imageRequest
|
||||
}
|
||||
|
||||
// type AliEmbeddingResponse struct {
|
||||
// Output struct {
|
||||
// Embeddings []AliEmbedding `json:"embeddings"`
|
||||
// } `json:"output"`
|
||||
// Usage AliUsage `json:"usage"`
|
||||
// AliError
|
||||
// }
|
||||
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
|
||||
}
|
||||
|
||||
// type AliError struct {
|
||||
// Code string `json:"code"`
|
||||
// Message string `json:"message"`
|
||||
// RequestId string `json:"request_id"`
|
||||
// }
|
||||
err = resp.Body.Close()
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
|
||||
// type AliUsage struct {
|
||||
// InputTokens int `json:"input_tokens"`
|
||||
// OutputTokens int `json:"output_tokens"`
|
||||
// TotalTokens int `json:"total_tokens"`
|
||||
// }
|
||||
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
|
||||
}
|
||||
|
||||
// type AliOutput struct {
|
||||
// Text string `json:"text"`
|
||||
// FinishReason string `json:"finish_reason"`
|
||||
// }
|
||||
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
|
||||
}
|
||||
|
||||
// type AliChatResponse struct {
|
||||
// Output AliOutput `json:"output"`
|
||||
// Usage AliUsage `json:"usage"`
|
||||
// AliError
|
||||
// }
|
||||
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},
|
||||
}
|
||||
|
||||
// func requestOpenAI2Ali(request GeneralOpenAIRequest) *AliChatRequest {
|
||||
// messages := make([]AliMessage, 0, len(request.Messages))
|
||||
// prompt := ""
|
||||
// for i := 0; i < len(request.Messages); i++ {
|
||||
// message := request.Messages[i]
|
||||
// if message.Role == "system" {
|
||||
// messages = append(messages, AliMessage{
|
||||
// User: message.Content,
|
||||
// Bot: "Okay",
|
||||
// })
|
||||
// continue
|
||||
// } else {
|
||||
// if i == len(request.Messages)-1 {
|
||||
// prompt = message.Content
|
||||
// break
|
||||
// }
|
||||
// messages = append(messages, AliMessage{
|
||||
// User: message.Content,
|
||||
// Bot: request.Messages[i+1].Content,
|
||||
// })
|
||||
// i++
|
||||
// }
|
||||
// }
|
||||
// return &AliChatRequest{
|
||||
// Model: request.Model,
|
||||
// Input: AliInput{
|
||||
// Prompt: prompt,
|
||||
// History: messages,
|
||||
// },
|
||||
// //Parameters: AliParameters{ // ChatGPT's parameters are not compatible with Ali's
|
||||
// // TopP: request.TopP,
|
||||
// // TopK: 50,
|
||||
// // //Seed: 0,
|
||||
// // //EnableSearch: false,
|
||||
// //},
|
||||
// }
|
||||
// }
|
||||
for _, item := range response.Output.Embeddings {
|
||||
openAIEmbeddingResponse.Data = append(openAIEmbeddingResponse.Data, openai.EmbeddingResponseItem{
|
||||
Object: `embedding`,
|
||||
Index: item.TextIndex,
|
||||
Embedding: item.Embedding,
|
||||
})
|
||||
}
|
||||
return &openAIEmbeddingResponse
|
||||
}
|
||||
|
||||
// func embeddingRequestOpenAI2Ali(request GeneralOpenAIRequest) *AliEmbeddingRequest {
|
||||
// return &AliEmbeddingRequest{
|
||||
// Model: "text-embedding-v1",
|
||||
// Input: struct {
|
||||
// Texts []string `json:"texts"`
|
||||
// }{
|
||||
// Texts: request.ParseInput(),
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
func responseAli2OpenAI(response *ChatResponse) *openai.TextResponse {
|
||||
fullTextResponse := openai.TextResponse{
|
||||
Id: response.RequestId,
|
||||
Object: "chat.completion",
|
||||
Created: helper.GetTimestamp(),
|
||||
Choices: response.Output.Choices,
|
||||
Usage: model.Usage{
|
||||
PromptTokens: response.Usage.InputTokens,
|
||||
CompletionTokens: response.Usage.OutputTokens,
|
||||
TotalTokens: response.Usage.InputTokens + response.Usage.OutputTokens,
|
||||
},
|
||||
}
|
||||
return &fullTextResponse
|
||||
}
|
||||
|
||||
// func aliEmbeddingHandler(c *gin.Context, resp *http.Response) (*OpenAIErrorWithStatusCode, *Usage) {
|
||||
// var aliResponse AliEmbeddingResponse
|
||||
// err := json.NewDecoder(resp.Body).Decode(&aliResponse)
|
||||
// if err != nil {
|
||||
// return errorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
||||
// }
|
||||
func streamResponseAli2OpenAI(aliResponse *ChatResponse) *openai.ChatCompletionsStreamResponse {
|
||||
if len(aliResponse.Output.Choices) == 0 {
|
||||
return nil
|
||||
}
|
||||
aliChoice := aliResponse.Output.Choices[0]
|
||||
var choice openai.ChatCompletionsStreamResponseChoice
|
||||
choice.Delta = aliChoice.Message
|
||||
if aliChoice.FinishReason != "null" {
|
||||
finishReason := aliChoice.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
|
||||
}
|
||||
|
||||
// err = resp.Body.Close()
|
||||
// if err != nil {
|
||||
// return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
||||
// }
|
||||
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)
|
||||
if response == nil {
|
||||
return true
|
||||
}
|
||||
//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
|
||||
}
|
||||
|
||||
// if aliResponse.Code != "" {
|
||||
// return &OpenAIErrorWithStatusCode{
|
||||
// OpenAIError: OpenAIError{
|
||||
// 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 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 *AliEmbeddingResponse) *OpenAIEmbeddingResponse {
|
||||
// openAIEmbeddingResponse := OpenAIEmbeddingResponse{
|
||||
// Object: "list",
|
||||
// Data: make([]OpenAIEmbeddingResponseItem, 0, len(response.Output.Embeddings)),
|
||||
// Model: "text-embedding-v1",
|
||||
// Usage: Usage{TotalTokens: response.Usage.TotalTokens},
|
||||
// }
|
||||
|
||||
// for _, item := range response.Output.Embeddings {
|
||||
// openAIEmbeddingResponse.Data = append(openAIEmbeddingResponse.Data, OpenAIEmbeddingResponseItem{
|
||||
// Object: `embedding`,
|
||||
// Index: item.TextIndex,
|
||||
// Embedding: item.Embedding,
|
||||
// })
|
||||
// }
|
||||
// return &openAIEmbeddingResponse
|
||||
// }
|
||||
|
||||
// func responseAli2OpenAI(response *AliChatResponse) *OpenAITextResponse {
|
||||
// choice := OpenAITextResponseChoice{
|
||||
// Index: 0,
|
||||
// Message: Message{
|
||||
// Role: "assistant",
|
||||
// Content: response.Output.Text,
|
||||
// },
|
||||
// FinishReason: response.Output.FinishReason,
|
||||
// }
|
||||
// fullTextResponse := OpenAITextResponse{
|
||||
// Id: response.RequestId,
|
||||
// Object: "chat.completion",
|
||||
// Created: common.GetTimestamp(),
|
||||
// Choices: []OpenAITextResponseChoice{choice},
|
||||
// Usage: Usage{
|
||||
// PromptTokens: response.Usage.InputTokens,
|
||||
// CompletionTokens: response.Usage.OutputTokens,
|
||||
// TotalTokens: response.Usage.InputTokens + response.Usage.OutputTokens,
|
||||
// },
|
||||
// }
|
||||
// return &fullTextResponse
|
||||
// }
|
||||
|
||||
// func streamResponseAli2OpenAI(aliResponse *AliChatResponse) *ChatCompletionsStreamResponse {
|
||||
// var choice ChatCompletionsStreamResponseChoice
|
||||
// choice.Delta.Content = aliResponse.Output.Text
|
||||
// if aliResponse.Output.FinishReason != "null" {
|
||||
// finishReason := aliResponse.Output.FinishReason
|
||||
// choice.FinishReason = &finishReason
|
||||
// }
|
||||
// response := ChatCompletionsStreamResponse{
|
||||
// Id: aliResponse.RequestId,
|
||||
// Object: "chat.completion.chunk",
|
||||
// Created: common.GetTimestamp(),
|
||||
// Model: "ernie-bot",
|
||||
// Choices: []ChatCompletionsStreamResponseChoice{choice},
|
||||
// }
|
||||
// return &response
|
||||
// }
|
||||
|
||||
// func aliStreamHandler(c *gin.Context, resp *http.Response) (*OpenAIErrorWithStatusCode, *Usage) {
|
||||
// var usage 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
|
||||
// }()
|
||||
// setEventStreamHeaders(c)
|
||||
// lastResponseText := ""
|
||||
// c.Stream(func(w io.Writer) bool {
|
||||
// select {
|
||||
// case data := <-dataChan:
|
||||
// var aliResponse AliChatResponse
|
||||
// err := json.Unmarshal([]byte(data), &aliResponse)
|
||||
// if err != nil {
|
||||
// common.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 {
|
||||
// common.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 errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
||||
// }
|
||||
// return nil, &usage
|
||||
// }
|
||||
|
||||
// func aliHandler(c *gin.Context, resp *http.Response) (*OpenAIErrorWithStatusCode, *Usage) {
|
||||
// var aliResponse AliChatResponse
|
||||
// responseBody, err := io.ReadAll(resp.Body)
|
||||
// if err != nil {
|
||||
// return errorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
|
||||
// }
|
||||
// err = resp.Body.Close()
|
||||
// if err != nil {
|
||||
// return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
||||
// }
|
||||
// err = json.Unmarshal(responseBody, &aliResponse)
|
||||
// if err != nil {
|
||||
// return errorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
||||
// }
|
||||
// if aliResponse.Code != "" {
|
||||
// return &OpenAIErrorWithStatusCode{
|
||||
// OpenAIError: OpenAIError{
|
||||
// Message: aliResponse.Message,
|
||||
// Type: aliResponse.Code,
|
||||
// Param: aliResponse.RequestId,
|
||||
// Code: aliResponse.Code,
|
||||
// },
|
||||
// StatusCode: resp.StatusCode,
|
||||
// }, nil
|
||||
// }
|
||||
// fullTextResponse := responseAli2OpenAI(&aliResponse)
|
||||
// jsonResponse, err := json.Marshal(fullTextResponse)
|
||||
// if err != nil {
|
||||
// return 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 Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
ctx := c.Request.Context()
|
||||
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
|
||||
}
|
||||
logger.Debugf(ctx, "response body: %s\n", responseBody)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ali
|
||||
|
||||
import (
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
|
||||
Reference in New Issue
Block a user