1
0

Merge pull request #70 from xqdoo00o/test

This commit is contained in:
Antonio Cheong 2023-06-20 17:09:00 +08:00 committed by GitHub
commit da4346a545
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"
)
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.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]
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 previous_text typings.StringStruct
var original_response chatgpt_types.ChatGPTResponse
var isRole = true
for {
line, err := reader.ReadString('\n')
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 {
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
}
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 {
_, err = c.Writer.WriteString(response_string)
if err != nil {

View File

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