openai.go
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ai
import (
"context"
"errors"
"fmt"
"github.com/sashabaranov/go-openai"
"github.com/zeromicro/go-zero/core/logx"
"io"
)
func ChatGPT(gptModelCode, key string, question string, channel chan string) (answer string, err error) {
config := openai.DefaultConfig(key)
config.BaseURL = "http://47.251.84.160:8080/v1" // "https://api.openai.com/v1" //
c := openai.NewClientWithConfig(config)
ctx := context.Background()
req := openai.ChatCompletionRequest{
Model: gptModelCode,
MaxTokens: 2048,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: question,
},
},
Stream: true,
}
stream, err := c.CreateChatCompletionStream(ctx, req)
if err != nil {
fmt.Printf("ChatCompletionStream error: %v\n", err)
return
}
defer func() {
stream.Close()
return
}()
logx.Info("Stream response: ")
for {
response, recvErr := stream.Recv()
if errors.Is(recvErr, io.EOF) {
fmt.Println("\nStream finished")
break
}
logx.Info(response)
if recvErr != nil {
logx.Info("\nStream error: %v\n", recvErr)
break
}
content := response.Choices[0].Delta.Content
answer += content
channel <- content
}
return
}