non-stream support
This commit is contained in:
parent
6aea144db4
commit
a1a2a673b3
@ -2,6 +2,7 @@ package types
|
||||
|
||||
type APIRequest struct {
|
||||
Messages []api_message `json:"messages"`
|
||||
Stream bool `json:"stream"`
|
||||
}
|
||||
|
||||
type api_message struct {
|
||||
|
@ -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
40
main.go
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user