forked from williamfzc/chat-gpt-ppt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_gpt35.go
89 lines (74 loc) · 1.82 KB
/
client_gpt35.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package chat_gpt_ppt
import (
"context"
"fmt"
"strings"
gogpt "github.com/sashabaranov/go-gpt3"
)
type ChatGPTClient struct {
token string
client *gogpt.Client
topics []string
}
func (c *ChatGPTClient) Prepare(topics []string) error {
c.client = gogpt.NewClient(c.token)
topicsStr := strings.Join(topics, "\n")
// prompt
_, err := c.client.CreateChatCompletion(context.Background(), gogpt.ChatCompletionRequest{
Model: gogpt.GPT3Dot5Turbo,
Messages: []gogpt.ChatCompletionMessage{
{
Role: `system`,
Content: fmt.Sprintf(`
Here is a prompt:
I would like to prepare a speech in pure markdown format,
with language and topic consistent with the outline provided.
You do not need to add any additional replies or explanations.
Here is the global outline:
%s
Then, I will begin to provide you with titles,
and you will generate content for each page accordingly.
Each title corresponds to one page of content, with a maximum of 100 words per page.
Conciseness is key.
`, topicsStr),
},
},
})
if err != nil {
return err
}
return nil
}
func NewGpt35Client() Client {
return &ChatGPTClient{}
}
func (c *ChatGPTClient) FillTopic(topic string) (*Topic, error) {
gptClient := gogpt.NewClient(c.token)
ctx := context.Background()
resp, err := gptClient.CreateChatCompletion(ctx, gogpt.ChatCompletionRequest{
Model: gogpt.GPT3Dot5Turbo,
Messages: []gogpt.ChatCompletionMessage{
{
Role: `system`,
Content: fmt.Sprintf(`
generate one slide,
- within 100 words
- with markdown format, such as 'ordered list'
- without title
- about '%s'
`, topic),
},
},
})
if err != nil {
return nil, err
}
logger.Printf("topic %s done \n", topic)
return &Topic{
Title: topic,
Content: resp.Choices[0].Message.Content,
}, nil
}
func (c *ChatGPTClient) SetToken(token string) {
c.token = token
}