relay-openai.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package controller
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "github.com/gin-gonic/gin"
  7. "io"
  8. "net/http"
  9. "one-api/common"
  10. "strings"
  11. )
  12. func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*OpenAIErrorWithStatusCode, string) {
  13. responseText := ""
  14. scanner := bufio.NewScanner(resp.Body)
  15. scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
  16. if atEOF && len(data) == 0 {
  17. return 0, nil, nil
  18. }
  19. if i := strings.Index(string(data), "\n"); i >= 0 {
  20. return i + 1, data[0:i], nil
  21. }
  22. if atEOF {
  23. return len(data), data, nil
  24. }
  25. return 0, nil, nil
  26. })
  27. dataChan := make(chan string)
  28. stopChan := make(chan bool)
  29. go func() {
  30. for scanner.Scan() {
  31. data := scanner.Text()
  32. if len(data) < 6 { // ignore blank line or wrong format
  33. continue
  34. }
  35. if data[:6] != "data: " && data[:6] != "[DONE]" {
  36. continue
  37. }
  38. dataChan <- data
  39. data = data[6:]
  40. if !strings.HasPrefix(data, "[DONE]") {
  41. switch relayMode {
  42. case RelayModeChatCompletions:
  43. var streamResponse ChatCompletionsStreamResponse
  44. err := json.Unmarshal([]byte(data), &streamResponse)
  45. if err != nil {
  46. common.SysError("error unmarshalling stream response: " + err.Error())
  47. continue // just ignore the error
  48. }
  49. for _, choice := range streamResponse.Choices {
  50. responseText += choice.Delta.Content
  51. }
  52. case RelayModeCompletions:
  53. var streamResponse CompletionsStreamResponse
  54. err := json.Unmarshal([]byte(data), &streamResponse)
  55. if err != nil {
  56. common.SysError("error unmarshalling stream response: " + err.Error())
  57. continue
  58. }
  59. for _, choice := range streamResponse.Choices {
  60. responseText += choice.Text
  61. }
  62. }
  63. }
  64. }
  65. stopChan <- true
  66. }()
  67. setEventStreamHeaders(c)
  68. c.Stream(func(w io.Writer) bool {
  69. select {
  70. case data := <-dataChan:
  71. if strings.HasPrefix(data, "data: [DONE]") {
  72. data = data[:12]
  73. }
  74. // some implementations may add \r at the end of data
  75. data = strings.TrimSuffix(data, "\r")
  76. c.Render(-1, common.CustomEvent{Data: data})
  77. return true
  78. case <-stopChan:
  79. return false
  80. }
  81. })
  82. err := resp.Body.Close()
  83. if err != nil {
  84. return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), ""
  85. }
  86. return nil, responseText
  87. }
  88. func openaiHandler(c *gin.Context, resp *http.Response, consumeQuota bool, promptTokens int, model string) (*OpenAIErrorWithStatusCode, *Usage) {
  89. var textResponse TextResponse
  90. if consumeQuota {
  91. responseBody, err := io.ReadAll(resp.Body)
  92. if err != nil {
  93. return errorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
  94. }
  95. err = resp.Body.Close()
  96. if err != nil {
  97. return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
  98. }
  99. err = json.Unmarshal(responseBody, &textResponse)
  100. if err != nil {
  101. return errorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
  102. }
  103. if textResponse.Error.Type != "" {
  104. return &OpenAIErrorWithStatusCode{
  105. OpenAIError: textResponse.Error,
  106. StatusCode: resp.StatusCode,
  107. }, nil
  108. }
  109. // Reset response body
  110. resp.Body = io.NopCloser(bytes.NewBuffer(responseBody))
  111. }
  112. // We shouldn't set the header before we parse the response body, because the parse part may fail.
  113. // And then we will have to send an error response, but in this case, the header has already been set.
  114. // So the httpClient will be confused by the response.
  115. // For example, Postman will report error, and we cannot check the response at all.
  116. for k, v := range resp.Header {
  117. c.Writer.Header().Set(k, v[0])
  118. }
  119. c.Writer.WriteHeader(resp.StatusCode)
  120. _, err := io.Copy(c.Writer, resp.Body)
  121. if err != nil {
  122. return errorWrapper(err, "copy_response_body_failed", http.StatusInternalServerError), nil
  123. }
  124. err = resp.Body.Close()
  125. if err != nil {
  126. return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
  127. }
  128. if textResponse.Usage.TotalTokens == 0 {
  129. completionTokens := 0
  130. for _, choice := range textResponse.Choices {
  131. completionTokens += countTokenText(choice.Message.Content, model)
  132. }
  133. textResponse.Usage = Usage{
  134. PromptTokens: promptTokens,
  135. CompletionTokens: completionTokens,
  136. TotalTokens: promptTokens + completionTokens,
  137. }
  138. }
  139. return nil, &textResponse.Usage
  140. }