text.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package ali
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "one-api/common"
  8. "one-api/dto"
  9. "one-api/relay/helper"
  10. "one-api/service"
  11. "strings"
  12. "github.com/gin-gonic/gin"
  13. )
  14. // https://help.aliyun.com/document_detail/613695.html?spm=a2c4g.2399480.0.0.1adb778fAdzP9w#341800c0f8w0r
  15. const EnableSearchModelSuffix = "-internet"
  16. func requestOpenAI2Ali(request dto.GeneralOpenAIRequest) *dto.GeneralOpenAIRequest {
  17. if request.TopP >= 1 {
  18. request.TopP = 0.999
  19. } else if request.TopP <= 0 {
  20. request.TopP = 0.001
  21. }
  22. return &request
  23. }
  24. func embeddingRequestOpenAI2Ali(request dto.EmbeddingRequest) *AliEmbeddingRequest {
  25. return &AliEmbeddingRequest{
  26. Model: request.Model,
  27. Input: struct {
  28. Texts []string `json:"texts"`
  29. }{
  30. Texts: request.ParseInput(),
  31. },
  32. }
  33. }
  34. func aliEmbeddingHandler(c *gin.Context, resp *http.Response) (*dto.OpenAIErrorWithStatusCode, *dto.Usage) {
  35. var aliResponse AliEmbeddingResponse
  36. err := json.NewDecoder(resp.Body).Decode(&aliResponse)
  37. if err != nil {
  38. return service.OpenAIErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
  39. }
  40. common.CloseResponseBodyGracefully(resp)
  41. if aliResponse.Code != "" {
  42. return &dto.OpenAIErrorWithStatusCode{
  43. Error: dto.OpenAIError{
  44. Message: aliResponse.Message,
  45. Type: aliResponse.Code,
  46. Param: aliResponse.RequestId,
  47. Code: aliResponse.Code,
  48. },
  49. StatusCode: resp.StatusCode,
  50. }, nil
  51. }
  52. model := c.GetString("model")
  53. if model == "" {
  54. model = "text-embedding-v4"
  55. }
  56. fullTextResponse := embeddingResponseAli2OpenAI(&aliResponse, model)
  57. jsonResponse, err := json.Marshal(fullTextResponse)
  58. if err != nil {
  59. return service.OpenAIErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
  60. }
  61. c.Writer.Header().Set("Content-Type", "application/json")
  62. c.Writer.WriteHeader(resp.StatusCode)
  63. _, err = c.Writer.Write(jsonResponse)
  64. return nil, &fullTextResponse.Usage
  65. }
  66. func embeddingResponseAli2OpenAI(response *AliEmbeddingResponse, model string) *dto.OpenAIEmbeddingResponse {
  67. openAIEmbeddingResponse := dto.OpenAIEmbeddingResponse{
  68. Object: "list",
  69. Data: make([]dto.OpenAIEmbeddingResponseItem, 0, len(response.Output.Embeddings)),
  70. Model: model,
  71. Usage: dto.Usage{TotalTokens: response.Usage.TotalTokens},
  72. }
  73. for _, item := range response.Output.Embeddings {
  74. openAIEmbeddingResponse.Data = append(openAIEmbeddingResponse.Data, dto.OpenAIEmbeddingResponseItem{
  75. Object: `embedding`,
  76. Index: item.TextIndex,
  77. Embedding: item.Embedding,
  78. })
  79. }
  80. return &openAIEmbeddingResponse
  81. }
  82. func responseAli2OpenAI(response *AliResponse) *dto.OpenAITextResponse {
  83. choice := dto.OpenAITextResponseChoice{
  84. Index: 0,
  85. Message: dto.Message{
  86. Role: "assistant",
  87. Content: response.Output.Text,
  88. },
  89. FinishReason: response.Output.FinishReason,
  90. }
  91. fullTextResponse := dto.OpenAITextResponse{
  92. Id: response.RequestId,
  93. Object: "chat.completion",
  94. Created: common.GetTimestamp(),
  95. Choices: []dto.OpenAITextResponseChoice{choice},
  96. Usage: dto.Usage{
  97. PromptTokens: response.Usage.InputTokens,
  98. CompletionTokens: response.Usage.OutputTokens,
  99. TotalTokens: response.Usage.InputTokens + response.Usage.OutputTokens,
  100. },
  101. }
  102. return &fullTextResponse
  103. }
  104. func streamResponseAli2OpenAI(aliResponse *AliResponse) *dto.ChatCompletionsStreamResponse {
  105. var choice dto.ChatCompletionsStreamResponseChoice
  106. choice.Delta.SetContentString(aliResponse.Output.Text)
  107. if aliResponse.Output.FinishReason != "null" {
  108. finishReason := aliResponse.Output.FinishReason
  109. choice.FinishReason = &finishReason
  110. }
  111. response := dto.ChatCompletionsStreamResponse{
  112. Id: aliResponse.RequestId,
  113. Object: "chat.completion.chunk",
  114. Created: common.GetTimestamp(),
  115. Model: "ernie-bot",
  116. Choices: []dto.ChatCompletionsStreamResponseChoice{choice},
  117. }
  118. return &response
  119. }
  120. func aliStreamHandler(c *gin.Context, resp *http.Response) (*dto.OpenAIErrorWithStatusCode, *dto.Usage) {
  121. var usage dto.Usage
  122. scanner := bufio.NewScanner(resp.Body)
  123. scanner.Split(bufio.ScanLines)
  124. dataChan := make(chan string)
  125. stopChan := make(chan bool)
  126. go func() {
  127. for scanner.Scan() {
  128. data := scanner.Text()
  129. if len(data) < 5 { // ignore blank line or wrong format
  130. continue
  131. }
  132. if data[:5] != "data:" {
  133. continue
  134. }
  135. data = data[5:]
  136. dataChan <- data
  137. }
  138. stopChan <- true
  139. }()
  140. helper.SetEventStreamHeaders(c)
  141. lastResponseText := ""
  142. c.Stream(func(w io.Writer) bool {
  143. select {
  144. case data := <-dataChan:
  145. var aliResponse AliResponse
  146. err := json.Unmarshal([]byte(data), &aliResponse)
  147. if err != nil {
  148. common.SysError("error unmarshalling stream response: " + err.Error())
  149. return true
  150. }
  151. if aliResponse.Usage.OutputTokens != 0 {
  152. usage.PromptTokens = aliResponse.Usage.InputTokens
  153. usage.CompletionTokens = aliResponse.Usage.OutputTokens
  154. usage.TotalTokens = aliResponse.Usage.InputTokens + aliResponse.Usage.OutputTokens
  155. }
  156. response := streamResponseAli2OpenAI(&aliResponse)
  157. response.Choices[0].Delta.SetContentString(strings.TrimPrefix(response.Choices[0].Delta.GetContentString(), lastResponseText))
  158. lastResponseText = aliResponse.Output.Text
  159. jsonResponse, err := json.Marshal(response)
  160. if err != nil {
  161. common.SysError("error marshalling stream response: " + err.Error())
  162. return true
  163. }
  164. c.Render(-1, common.CustomEvent{Data: "data: " + string(jsonResponse)})
  165. return true
  166. case <-stopChan:
  167. c.Render(-1, common.CustomEvent{Data: "data: [DONE]"})
  168. return false
  169. }
  170. })
  171. common.CloseResponseBodyGracefully(resp)
  172. return nil, &usage
  173. }
  174. func aliHandler(c *gin.Context, resp *http.Response) (*dto.OpenAIErrorWithStatusCode, *dto.Usage) {
  175. var aliResponse AliResponse
  176. responseBody, err := io.ReadAll(resp.Body)
  177. if err != nil {
  178. return service.OpenAIErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
  179. }
  180. common.CloseResponseBodyGracefully(resp)
  181. err = json.Unmarshal(responseBody, &aliResponse)
  182. if err != nil {
  183. return service.OpenAIErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
  184. }
  185. if aliResponse.Code != "" {
  186. return &dto.OpenAIErrorWithStatusCode{
  187. Error: dto.OpenAIError{
  188. Message: aliResponse.Message,
  189. Type: aliResponse.Code,
  190. Param: aliResponse.RequestId,
  191. Code: aliResponse.Code,
  192. },
  193. StatusCode: resp.StatusCode,
  194. }, nil
  195. }
  196. fullTextResponse := responseAli2OpenAI(&aliResponse)
  197. jsonResponse, err := json.Marshal(fullTextResponse)
  198. if err != nil {
  199. return service.OpenAIErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
  200. }
  201. c.Writer.Header().Set("Content-Type", "application/json")
  202. c.Writer.WriteHeader(resp.StatusCode)
  203. _, err = c.Writer.Write(jsonResponse)
  204. return nil, &fullTextResponse.Usage
  205. }