-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
119 lines (102 loc) · 2.72 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"context"
"errors"
"fmt"
"io"
"os"
"github.com/coze-dev/coze-go"
)
// This use case teaches you how to use local plugin.
func main() {
// Get an access_token through personal access token or oauth.
token := os.Getenv("COZE_API_TOKEN")
botID := os.Getenv("PUBLISHED_BOT_ID")
userID := os.Getenv("USER_ID")
authCli := coze.NewTokenAuth(token)
// Init the Coze client through the access_token.
cozeCli := coze.NewCozeAPI(authCli, coze.WithBaseURL(os.Getenv("COZE_API_BASE")))
ctx := context.Background()
req := &coze.CreateChatsReq{
BotID: botID,
UserID: userID,
Messages: []*coze.Message{
coze.BuildUserQuestionText("What's the weather like in Shenzhen today?", nil),
},
}
var pluginEvent *coze.ChatEvent
var conversationID string
resp, err := cozeCli.Chat.Stream(ctx, req)
if err != nil {
fmt.Println("Error starting stream:", err)
return
}
defer resp.Close()
for {
event, err := resp.Recv()
if errors.Is(err, io.EOF) {
fmt.Println("Stream finished")
break
}
if err != nil {
fmt.Println(err)
break
}
if event.Event == coze.ChatEventConversationMessageDelta {
fmt.Print(event.Message.Content)
} else if event.Event == coze.ChatEventConversationChatCompleted {
fmt.Printf("Token usage:%d\n", event.Chat.Usage.TokenCount)
} else if event.Event == coze.ChatEventConversationChatRequiresAction {
fmt.Println("need action")
pluginEvent = event
conversationID = event.Chat.ConversationID
break
} else {
fmt.Printf("\n")
}
}
if pluginEvent == nil {
return
}
var toolOutputs []*coze.ToolOutput
for _, callInfo := range pluginEvent.Chat.RequiredAction.SubmitToolOutputs.ToolCalls {
callID := callInfo.ID
// you can handle different plugin by name.
functionName := callInfo.Function.Name
// you should unmarshal arguments if necessary.
argsJSON := callInfo.Function.Arguments
fmt.Printf("Function called: %s with args: %s\n", functionName, argsJSON)
toolOutputs = append(toolOutputs, &coze.ToolOutput{
ToolCallID: callID,
Output: "It is 18 to 21",
})
}
toolReq := &coze.SubmitToolOutputsChatReq{
ChatID: pluginEvent.Chat.ID,
ConversationID: conversationID,
ToolOutputs: toolOutputs,
}
resp2, err := cozeCli.Chat.StreamSubmitToolOutputs(ctx, toolReq)
if err != nil {
fmt.Println("Error submitting tool outputs:", err)
return
}
defer resp2.Close()
for {
event, err := resp2.Recv()
if errors.Is(err, io.EOF) {
fmt.Println("Stream finished")
break
}
if err != nil {
fmt.Println(err)
break
}
if event.Event == coze.ChatEventConversationMessageDelta {
fmt.Print(event.Message.Content)
} else {
fmt.Printf("\n")
}
}
fmt.Printf("done, log:%s\n", resp.Response().LogID())
}