Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakurasan committed Nov 17, 2024
1 parent 78e3be4 commit 0397e4f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/openai/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ func ChatProxy(c *gin.Context, chatReq *ChatCompletionRequest) {
return
}
defer resp.Body.Close()
teeReader := io.TeeReader(resp.Body, c.Writer)

var result string
if chatReq.Stream {
teeReader := io.TeeReader(resp.Body, c.Writer)
// 流式响应
scanner := bufio.NewScanner(teeReader)

Expand Down Expand Up @@ -312,8 +312,12 @@ func ChatProxy(c *gin.Context, chatReq *ChatCompletionRequest) {

}
} else {
if resp.StatusCode != http.StatusOK {
io.Copy(c.Writer, resp.Body)
return
}
// 处理非流式响应
body, err := io.ReadAll(teeReader)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return
Expand All @@ -337,6 +341,7 @@ func ChatProxy(c *gin.Context, chatReq *ChatCompletionRequest) {
}

}
c.JSON(http.StatusOK, string(body))
}
usagelog.CompletionCount = tokenizer.NumTokensFromStr(result, chatReq.Model)
usagelog.Cost = fmt.Sprintf("%.6f", tokenizer.Cost(usagelog.Model, usagelog.PromptCount, usagelog.CompletionCount))
Expand Down

0 comments on commit 0397e4f

Please sign in to comment.