Add /api/get-message-answer API

This commit is contained in:
Yang Luo
2023-05-01 23:15:51 +08:00
parent eea2e1d271
commit 2cd6f9df8e
7 changed files with 159 additions and 2 deletions

View File

@ -17,6 +17,7 @@ package ai
import (
"context"
"fmt"
"io"
"strings"
"time"
@ -73,3 +74,43 @@ func QueryAnswerSafe(authToken string, question string) string {
return res
}
func QueryAnswerStream(authToken string, question string, writer io.Writer, builder *strings.Builder) error {
client := getProxyClientFromToken(authToken)
ctx := context.Background()
respStream, err := client.CreateCompletionStream(
ctx,
openai.CompletionRequest{
Model: openai.GPT3TextDavinci003,
Prompt: question,
MaxTokens: 50,
Stream: true,
},
)
if err != nil {
return err
}
defer respStream.Close()
for {
completion, streamErr := respStream.Recv()
if streamErr != nil {
if streamErr == io.EOF {
break
}
return streamErr
}
// Write the streamed data as Server-Sent Events
if _, err := fmt.Fprintf(writer, "data: %s\n\n", completion.Choices[0].Text); err != nil {
return err
}
// Append the response to the strings.Builder
builder.WriteString(completion.Choices[0].Text)
}
return nil
}