-
Notifications
You must be signed in to change notification settings - Fork 14
/
chatgpt.go
50 lines (42 loc) · 839 Bytes
/
chatgpt.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
package chatgpt
import (
"github.com/google/uuid"
)
type chatGpt struct {
client *client
conversationId string
lastMessageId string
}
func NewChatGpt(c *client) *chatGpt {
return &chatGpt{
client: c,
lastMessageId: uuid.Must(uuid.NewRandom()).String(),
}
}
func (c *chatGpt) SendMessage(m string) (string, error) {
req := newRequest("next",
[]requestMessage{
{
ID: uuid.Must(uuid.NewRandom()).String(),
Role: "user",
Content: content{
ContentType: "text",
Parts: []string{m},
},
},
},
c.lastMessageId,
"text-davinci-002-render",
)
resp, err := c.client.Send(req)
if err != nil {
return "", err
}
if resp.Error != nil {
return "", nil
}
if c.conversationId == "" {
c.conversationId = resp.ConversationID
}
return resp.Message.Content.Parts[0], nil
}