-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
69 lines (59 loc) · 1.97 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/coze-dev/coze-go"
)
func main() {
// Get an access_token through personal access token or oauth.
token := os.Getenv("COZE_API_TOKEN")
authCli := coze.NewTokenAuth(token)
botID := os.Getenv("COZE_BOT_ID")
// Init the Coze client through the access_token.
cozeCli := coze.NewCozeAPI(authCli, coze.WithBaseURL(os.Getenv("COZE_API_BASE")))
ctx := context.Background()
// Create a new conversation
resp, err := cozeCli.Conversations.Create(ctx, &coze.CreateConversationsReq{BotID: botID})
if err != nil {
fmt.Println("Error creating conversation:", err)
return
}
fmt.Println("create conversations:", resp.Conversation)
fmt.Println(resp.LogID())
conversationID := resp.Conversation.ID
// Retrieve the conversation
getResp, err := cozeCli.Conversations.Retrieve(ctx, &coze.RetrieveConversationsReq{ConversationID: conversationID})
if err != nil {
fmt.Println("Error retrieving conversation:", err)
return
}
fmt.Println("retrieve conversations:", getResp)
fmt.Println(getResp.LogID())
// you can manually create message for conversation
createMessageReq := &coze.CreateMessageReq{}
createMessageReq.Role = coze.MessageRoleAssistant
createMessageReq.ConversationID = conversationID
createMessageReq.SetObjectContext([]*coze.MessageObjectString{
coze.NewFileMessageObjectByURL(os.Getenv("IMAGE_FILE_PATH")),
coze.NewTextMessageObject("hello"),
coze.NewImageMessageObjectByURL(os.Getenv("IMAGE_FILE_PATH")),
})
time.Sleep(time.Second)
msgs, err := cozeCli.Conversations.Messages.Create(ctx, createMessageReq)
if err != nil {
fmt.Println("Error creating message:", err)
return
}
fmt.Println(msgs)
fmt.Println(msgs.LogID())
// Clear the conversation
clearResp, err := cozeCli.Conversations.Clear(ctx, &coze.ClearConversationsReq{ConversationID: conversationID})
if err != nil {
fmt.Println("Error clearing conversation:", err)
return
}
fmt.Println(clearResp)
fmt.Println(clearResp.LogID())
}