stop chunk
This commit is contained in:
parent
42e3b688c7
commit
99eb85350f
11
handlers.go
11
handlers.go
@ -116,6 +116,7 @@ func nightmare(c *gin.Context) {
|
||||
// Response content type is application/json
|
||||
c.Header("Content-Type", "application/json")
|
||||
}
|
||||
c.Status(200)
|
||||
for {
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
@ -151,10 +152,7 @@ func nightmare(c *gin.Context) {
|
||||
translated_response := responses.NewChatCompletionChunk(original_response.Message.Content.Parts[0])
|
||||
|
||||
// Stream the response to the client
|
||||
response_string, err := json.Marshal(translated_response)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
response_string := translated_response.String()
|
||||
if original_request.Stream {
|
||||
_, err = c.Writer.WriteString("data: " + string(response_string) + "\n\n")
|
||||
if err != nil {
|
||||
@ -174,7 +172,10 @@ func nightmare(c *gin.Context) {
|
||||
c.JSON(200, full_response)
|
||||
return
|
||||
}
|
||||
c.String(200, "data: [DONE]")
|
||||
final_line := responses.StopChunk()
|
||||
c.Writer.WriteString("data: " + final_line.String() + "\n\n")
|
||||
|
||||
c.Writer.WriteString("data: [DONE]")
|
||||
break
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package responses
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
Author Author `json:"author"`
|
||||
@ -42,8 +44,13 @@ type ChatCompletionChunk struct {
|
||||
Choices []Choices `json:"choices"`
|
||||
}
|
||||
|
||||
func (chunk *ChatCompletionChunk) String() string {
|
||||
resp, _ := json.Marshal(chunk)
|
||||
return string(resp)
|
||||
}
|
||||
|
||||
type Choices struct {
|
||||
Delta Delta `json:"delta"`
|
||||
Delta *Delta `json:"delta"`
|
||||
Index int `json:"index"`
|
||||
FinishReason interface{} `json:"finish_reason"`
|
||||
}
|
||||
@ -62,7 +69,7 @@ func NewChatCompletionChunk(text string) ChatCompletionChunk {
|
||||
Choices: []Choices{
|
||||
{
|
||||
Index: 0,
|
||||
Delta: Delta{
|
||||
Delta: &Delta{
|
||||
Content: text,
|
||||
Role: "assistant",
|
||||
},
|
||||
@ -72,6 +79,21 @@ func NewChatCompletionChunk(text string) ChatCompletionChunk {
|
||||
}
|
||||
}
|
||||
|
||||
func StopChunk() ChatCompletionChunk {
|
||||
return ChatCompletionChunk{
|
||||
ID: "chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK",
|
||||
Object: "chat.completion.chunk",
|
||||
Created: 0,
|
||||
Model: "gpt-3.5-turbo-0301",
|
||||
Choices: []Choices{
|
||||
{
|
||||
Index: 0,
|
||||
FinishReason: "stop",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type ChatCompletion struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
@ -112,8 +134,7 @@ func NewChatCompletion(full_test string) ChatCompletion {
|
||||
Content: full_test,
|
||||
Role: "assistant",
|
||||
},
|
||||
Index: 0,
|
||||
FinishReason: "stop",
|
||||
Index: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
Reference in New Issue
Block a user