1
0

feat response fmt like openai api

This commit is contained in:
David xu 2023-06-20 15:24:37 +08:00 committed by GitHub
parent 43fc705f70
commit b666c0928c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 7 deletions

View File

@ -7,9 +7,11 @@ import (
"strings" "strings"
) )
func ConvertToString(chatgpt_response *chatgpt_types.ChatGPTResponse, previous_text *typings.StringStruct) string { func ConvertToString(chatgpt_response *chatgpt_types.ChatGPTResponse, previous_text *typings.StringStruct, role bool) string {
translated_response := official_types.NewChatCompletionChunk(strings.ReplaceAll(chatgpt_response.Message.Content.Parts[0], *&previous_text.Text, "")) translated_response := official_types.NewChatCompletionChunk(strings.ReplaceAll(chatgpt_response.Message.Content.Parts[0], *&previous_text.Text, ""))
translated_response.Choices[0].Delta.Role = chatgpt_response.Message.Author.Role if role {
translated_response.Choices[0].Delta.Role = chatgpt_response.Message.Author.Role
}
previous_text.Text = chatgpt_response.Message.Content.Parts[0] previous_text.Text = chatgpt_response.Message.Content.Parts[0]
return "data: " + translated_response.String() + "\n\n" return "data: " + translated_response.String() + "\n\n"

View File

@ -158,6 +158,7 @@ func Handler(c *gin.Context, response *http.Response, token string, translated_r
var finish_reason string var finish_reason string
var previous_text typings.StringStruct var previous_text typings.StringStruct
var original_response chatgpt_types.ChatGPTResponse var original_response chatgpt_types.ChatGPTResponse
var isRole = true
for { for {
line, err := reader.ReadString('\n') line, err := reader.ReadString('\n')
if err != nil { if err != nil {
@ -186,10 +187,11 @@ func Handler(c *gin.Context, response *http.Response, token string, translated_r
if original_response.Message.Author.Role != "assistant" || original_response.Message.Content.Parts == nil { if original_response.Message.Author.Role != "assistant" || original_response.Message.Content.Parts == nil {
continue continue
} }
if original_response.Message.Metadata.MessageType != "next" && original_response.Message.Metadata.MessageType != "continue" { if original_response.Message.Metadata.MessageType != "next" && original_response.Message.Metadata.MessageType != "continue" || original_response.Message.EndTurn != nil {
continue continue
} }
response_string := chatgpt_response_converter.ConvertToString(&original_response, &previous_text) response_string := chatgpt_response_converter.ConvertToString(&original_response, &previous_text, isRole)
isRole = false
if stream { if stream {
_, err = c.Writer.WriteString(response_string) _, err = c.Writer.WriteString(response_string)
if err != nil { if err != nil {

View File

@ -22,8 +22,8 @@ type Choices struct {
} }
type Delta struct { type Delta struct {
Content string `json:"content"` Content string `json:"content,omitempty"`
Role string `json:"role"` Role string `json:"role,omitempty"`
} }
func NewChatCompletionChunk(text string) ChatCompletionChunk { func NewChatCompletionChunk(text string) ChatCompletionChunk {
@ -37,7 +37,6 @@ func NewChatCompletionChunk(text string) ChatCompletionChunk {
Index: 0, Index: 0,
Delta: Delta{ Delta: Delta{
Content: text, Content: text,
Role: "assistant",
}, },
FinishReason: nil, FinishReason: nil,
}, },