|
@@ -0,0 +1,96 @@
|
|
|
+package controller
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
+ "github.com/gogf/gf/v2/os/glog"
|
|
|
+ v1 "go-gpt/api/v1"
|
|
|
+ "go-gpt/gpt"
|
|
|
+)
|
|
|
+
|
|
|
+type Message struct {
|
|
|
+ Text string `json:"text"`
|
|
|
+}
|
|
|
+
|
|
|
+var (
|
|
|
+ Chat = sChat{}
|
|
|
+ key = "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"
|
|
|
+ proxyUrl = "127.0.0.1:6153"
|
|
|
+)
|
|
|
+
|
|
|
+type sChat struct {
|
|
|
+}
|
|
|
+
|
|
|
+// Chat 这里不使用流数据
|
|
|
+func (c *sChat) Chat(ctx context.Context, req *v1.ChatReq) (res *v1.ChatRes, err error) {
|
|
|
+
|
|
|
+ client := gpt.NewClient(key, proxyUrl)
|
|
|
+ completion, err := client.ChatCompletion(ctx, &gpt.ChatCompletionRequest{
|
|
|
+ Model: gpt.GPT3Dot5Turbo,
|
|
|
+ Messages: []gpt.ChatCompletionRequestMessage{
|
|
|
+ {
|
|
|
+ Role: req.Role,
|
|
|
+ Content: req.Content,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ MaxTokens: 2048,
|
|
|
+ Temperature: 0.8,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ res = new(v1.ChatRes)
|
|
|
+ if len(completion.Choices) > 0 {
|
|
|
+ res.Text = completion.Choices[0].Message.Content
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// ChatStream 这里使用流数据
|
|
|
+func (c *sChat) ChatStream(ctx context.Context, req *v1.ChatStreamReq) (res *v1.ChatStreamRes, err error) {
|
|
|
+
|
|
|
+ re := g.RequestFromCtx(ctx)
|
|
|
+ re.Response.Header().Set("Transfer-Encoding", "chunked")
|
|
|
+ re.Response.Header().Set("Content-Type", "application/json")
|
|
|
+ w := re.Response.Writer
|
|
|
+ encoder := json.NewEncoder(w)
|
|
|
+
|
|
|
+ // 初始化client
|
|
|
+ client := gpt.NewClient(key, proxyUrl)
|
|
|
+ err = client.ChatCompletionStream(ctx, &gpt.ChatCompletionRequest{
|
|
|
+ Model: gpt.GPT3Dot5Turbo,
|
|
|
+ Messages: []gpt.ChatCompletionRequestMessage{
|
|
|
+ {
|
|
|
+ Role: req.Role,
|
|
|
+ Content: req.Content,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ MaxTokens: 150,
|
|
|
+ Temperature: 0.8,
|
|
|
+ }, func(response *gpt.ChatCompletionStreamResponse) {
|
|
|
+
|
|
|
+ // 检查Choices字段是否存在
|
|
|
+ if len(response.Choices) > 0 {
|
|
|
+ // 提取Content字段
|
|
|
+ content := response.Choices[0].Delta.Content
|
|
|
+ // 将Content字段作为一个JSON对象发送给客户端
|
|
|
+ message := Message{Text: content}
|
|
|
+ if err := encoder.Encode(message); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ re.Response.WriteHeader(200)
|
|
|
+ w.Flush()
|
|
|
+ } else {
|
|
|
+ re.Exit()
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ glog.Debug(ctx, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|