1
0

non-stream support

This commit is contained in:
Antonio Cheong 2023-04-03 13:10:47 +08:00
parent 6aea144db4
commit a1a2a673b3
3 changed files with 58 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package types
type APIRequest struct {
Messages []api_message `json:"messages"`
Stream bool `json:"stream"`
}
type api_message struct {

View File

@ -52,3 +52,26 @@ type Delta struct {
Content string `json:"content"`
Role string `json:"role"`
}
type ChatCompletion struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Usage usage `json:"usage"`
Choices []Choice `json:"choices"`
}
type Msg struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Choice struct {
Index int `json:"index"`
Message Msg `json:"message"`
FinishReason interface{} `json:"finish_reason"`
}
type usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}

40
main.go
View File

@ -194,18 +194,46 @@ func main() {
})
return
}
_, err = c.Writer.WriteString("data: " + string(response_string) + "\n\n")
if err != nil {
c.JSON(500, gin.H{
"error": "error writing response",
})
return
if chat_request.Stream {
_, err = c.Writer.WriteString("data: " + string(response_string) + "\n\n")
if err != nil {
c.JSON(500, gin.H{
"error": "error writing response",
})
return
}
}
// Flush the response writer buffer to ensure that the client receives each line as it's written
c.Writer.Flush()
fulltext = tmp_fulltext
if finish_reason != nil {
if !chat_request.Stream {
full_response := responses.ChatCompletion{
ID: "chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK",
Object: "chat.completion",
Created: int64(chat_response.Message.CreateTime),
Model: "gpt-3.5-turbo-0301",
Choices: []responses.Choice{
{
Message: responses.Msg{
Content: fulltext,
Role: "assistant",
},
Index: 0,
FinishReason: "stop",
},
},
}
if err != nil {
c.JSON(500, gin.H{
"error": "error parsing response",
})
return
}
c.JSON(200, full_response)
return
}
c.String(200, "data: [DONE]")
break
}