Skip to content

Commit b766d82

Browse files
committed
feat(model/ark): add tool web search example
1 parent 4dee932 commit b766d82

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

components/model/ark/examples/generate/generate.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ func main() {
3333
ctx := context.Background()
3434

3535
// Get ARK_API_KEY and ARK_MODEL_ID: https://www.volcengine.com/docs/82379/1399008
36-
chatModel, err := ark.NewChatModel(ctx, &ark.ChatModelConfig{
37-
APIKey: os.Getenv("ARK_API_KEY"),
38-
Model: os.Getenv("ARK_MODEL_ID"),
39-
})
40-
36+
chatModel, err := NewChatModel(ctx)
4137
if err != nil {
4238
log.Fatalf("NewChatModel failed, err=%v", err)
4339
}
4440

41+
// You can also use ResponsesAPIChatModel
42+
//chatModel, err := NewResponsesAPIChatModel(ctx)
43+
//if err != nil {
44+
// log.Fatalf("NewChatModel failed, err=%v", err)
45+
//}
46+
4547
inMsgs := []*schema.Message{
4648
{
4749
Role: schema.User,
@@ -87,3 +89,17 @@ func main() {
8789
respBody, _ = json.MarshalIndent(msg, " ", " ")
8890
log.Printf(" body: %s\n", string(respBody))
8991
}
92+
93+
func NewChatModel(ctx context.Context) (*ark.ChatModel, error) {
94+
return ark.NewChatModel(ctx, &ark.ChatModelConfig{
95+
APIKey: os.Getenv("ARK_API_KEY"),
96+
Model: os.Getenv("ARK_MODEL_ID"),
97+
})
98+
}
99+
100+
func NewResponsesAPIChatModel(ctx context.Context) (*ark.ResponsesAPIChatModel, error) {
101+
return ark.NewResponsesAPIChatModel(ctx, &ark.ResponsesAPIConfig{
102+
APIKey: os.Getenv("ARK_API_KEY"),
103+
Model: os.Getenv("ARK_MODEL_ID"),
104+
})
105+
}

components/model/ark/examples/intent_tool/intent_tool.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ func main() {
3333
ctx := context.Background()
3434

3535
// Get ARK_API_KEY and ARK_MODEL_ID: https://www.volcengine.com/docs/82379/1399008
36-
chatModel, err := ark.NewChatModel(ctx, &ark.ChatModelConfig{
37-
APIKey: os.Getenv("ARK_API_KEY"),
38-
Model: os.Getenv("ARK_MODEL_ID"), // the model needs to support image input, such as Doubao-Seed-1.6.
39-
})
36+
chatModel, err := NewChatModel(ctx)
4037
if err != nil {
4138
log.Printf("NewChatModel failed, err=%v", err)
4239
return
@@ -46,6 +43,15 @@ func main() {
4643
textToolCall(ctx, chatModel)
4744
fmt.Printf("\n==========image tool call==========\n")
4845
imageToolCall(ctx, chatModel)
46+
47+
fmt.Printf("\n==========web search tool call==========\n")
48+
// Generate a ResponsesAPIChatModel that supports tool web search
49+
responsesAPIChatModel, err := NewResponsesAPIChatModel(ctx)
50+
if err != nil {
51+
log.Printf("NewResponsesAPIChatModel failed, err=%v", err)
52+
}
53+
toolWebSearchCall(ctx, responsesAPIChatModel)
54+
4955
}
5056

5157
func textToolCall(ctx context.Context, chatModel model.ToolCallingChatModel) {
@@ -179,3 +185,41 @@ func imageToolCall(ctx context.Context, chatModel model.ToolCallingChatModel) {
179185
}
180186
fmt.Printf("output: \n%v", resp)
181187
}
188+
189+
func toolWebSearchCall(ctx context.Context, chatModel model.ToolCallingChatModel) {
190+
// You can also enable the built-in web search tool by passing Options during Generate or Stream
191+
//limit := int64(1)
192+
//options := make([]model.Option,0)
193+
//options = append(options, ark.WithEnableToolWebSearch(&ark.EnableToolWebSearch{
194+
// Limit: &limit,
195+
// Sources: []ark.Source{ark.SourceOfMoji},
196+
//}))
197+
198+
resp, err := chatModel.Generate(ctx, []*schema.Message{
199+
{Role: schema.User, Content: "What's the weather like today?"},
200+
})
201+
if err != nil {
202+
log.Fatalf("WithTools failed, err=%v", err)
203+
}
204+
205+
fmt.Printf("output: \n%v", resp)
206+
}
207+
208+
func NewChatModel(ctx context.Context) (*ark.ChatModel, error) {
209+
return ark.NewChatModel(ctx, &ark.ChatModelConfig{
210+
APIKey: os.Getenv("ARK_API_KEY"),
211+
Model: os.Getenv("ARK_MODEL_ID"),
212+
})
213+
}
214+
215+
func NewResponsesAPIChatModel(ctx context.Context) (*ark.ResponsesAPIChatModel, error) {
216+
limit := int64(1)
217+
return ark.NewResponsesAPIChatModel(ctx, &ark.ResponsesAPIConfig{
218+
APIKey: os.Getenv("ARK_API_KEY"),
219+
Model: os.Getenv("ARK_MODEL_ID"),
220+
EnableToolWebSearch: &ark.EnableToolWebSearch{
221+
Limit: &limit,
222+
Sources: []ark.Source{ark.SourceOfMoji},
223+
},
224+
})
225+
}

0 commit comments

Comments
 (0)