ai-gateway/relay/channel/openai/main.go

145 lines
4.2 KiB
Go
Raw Normal View History

2024-01-14 11:21:03 +00:00
package openai
2023-07-22 09:48:45 +00:00
import (
"bufio"
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"io"
"net/http"
"one-api/common"
2024-01-14 11:21:03 +00:00
"one-api/relay/constant"
2023-07-22 09:48:45 +00:00
"strings"
)
2024-01-14 11:21:03 +00:00
func StreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*ErrorWithStatusCode, string) {
2023-07-22 09:48:45 +00:00
responseText := ""
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) < 6 { // ignore blank line or wrong format
continue
}
if data[:6] != "data: " && data[:6] != "[DONE]" {
continue
}
2023-07-22 09:48:45 +00:00
dataChan <- data
data = data[6:]
if !strings.HasPrefix(data, "[DONE]") {
switch relayMode {
2024-01-14 11:21:03 +00:00
case constant.RelayModeChatCompletions:
2023-07-22 09:48:45 +00:00
var streamResponse ChatCompletionsStreamResponse
err := json.Unmarshal([]byte(data), &streamResponse)
if err != nil {
common.SysError("error unmarshalling stream response: " + err.Error())
continue // just ignore the error
2023-07-22 09:48:45 +00:00
}
for _, choice := range streamResponse.Choices {
responseText += choice.Delta.Content
}
2024-01-14 11:21:03 +00:00
case constant.RelayModeCompletions:
2023-07-22 09:48:45 +00:00
var streamResponse CompletionsStreamResponse
err := json.Unmarshal([]byte(data), &streamResponse)
if err != nil {
common.SysError("error unmarshalling stream response: " + err.Error())
continue
2023-07-22 09:48:45 +00:00
}
for _, choice := range streamResponse.Choices {
responseText += choice.Text
}
}
}
}
stopChan <- true
}()
2024-01-14 11:21:03 +00:00
common.SetEventStreamHeaders(c)
2023-07-22 09:48:45 +00:00
c.Stream(func(w io.Writer) bool {
select {
case data := <-dataChan:
if strings.HasPrefix(data, "data: [DONE]") {
data = data[:12]
}
// some implementations may add \r at the end of data
data = strings.TrimSuffix(data, "\r")
c.Render(-1, common.CustomEvent{Data: data})
return true
case <-stopChan:
return false
}
})
err := resp.Body.Close()
if err != nil {
2024-01-14 11:21:03 +00:00
return ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), ""
2023-07-22 09:48:45 +00:00
}
return nil, responseText
}
2024-01-14 11:21:03 +00:00
func Handler(c *gin.Context, resp *http.Response, promptTokens int, model string) (*ErrorWithStatusCode, *Usage) {
var textResponse SlimTextResponse
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
2024-01-14 11:21:03 +00:00
return ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
2023-07-22 09:48:45 +00:00
}
err = resp.Body.Close()
if err != nil {
2024-01-14 11:21:03 +00:00
return ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
}
err = json.Unmarshal(responseBody, &textResponse)
if err != nil {
2024-01-14 11:21:03 +00:00
return ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
}
if textResponse.Error.Type != "" {
2024-01-14 11:21:03 +00:00
return &ErrorWithStatusCode{
Error: textResponse.Error,
StatusCode: resp.StatusCode,
}, nil
}
// Reset response body
resp.Body = io.NopCloser(bytes.NewBuffer(responseBody))
2023-07-22 09:48:45 +00:00
// We shouldn't set the header before we parse the response body, because the parse part may fail.
// And then we will have to send an error response, but in this case, the header has already been set.
2024-01-14 11:21:03 +00:00
// So the HTTPClient will be confused by the response.
2023-07-22 09:48:45 +00:00
// For example, Postman will report error, and we cannot check the response at all.
for k, v := range resp.Header {
c.Writer.Header().Set(k, v[0])
}
c.Writer.WriteHeader(resp.StatusCode)
_, err = io.Copy(c.Writer, resp.Body)
2023-07-22 09:48:45 +00:00
if err != nil {
2024-01-14 11:21:03 +00:00
return ErrorWrapper(err, "copy_response_body_failed", http.StatusInternalServerError), nil
2023-07-22 09:48:45 +00:00
}
err = resp.Body.Close()
if err != nil {
2024-01-14 11:21:03 +00:00
return ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
2023-07-22 09:48:45 +00:00
}
if textResponse.Usage.TotalTokens == 0 {
completionTokens := 0
for _, choice := range textResponse.Choices {
2024-01-14 11:21:03 +00:00
completionTokens += CountTokenText(choice.Message.StringContent(), model)
}
textResponse.Usage = Usage{
PromptTokens: promptTokens,
CompletionTokens: completionTokens,
TotalTokens: promptTokens + completionTokens,
}
}
2023-07-22 09:48:45 +00:00
return nil, &textResponse.Usage
}