Skip to content

Commit 8048d48

Browse files
committed
test: add test with tool
1 parent 966b249 commit 8048d48

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

providertests/provider_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package providertests
22

33
import (
4+
"context"
45
"strings"
56
"testing"
67

@@ -37,3 +38,47 @@ func TestSimple(t *testing.T) {
3738
})
3839
}
3940
}
41+
42+
func TestTool(t *testing.T) {
43+
for _, pair := range languageModelBuilders {
44+
t.Run(pair.name, func(t *testing.T) {
45+
r := newRecorder(t)
46+
47+
languageModel, err := pair.builder(r)
48+
if err != nil {
49+
t.Fatalf("failed to build language model: %v", err)
50+
}
51+
52+
type WeatherInput struct {
53+
Location string `json:"location" description:"the city"`
54+
}
55+
56+
weatherTool := ai.NewAgentTool(
57+
"weather",
58+
"Get weather information for a location",
59+
func(ctx context.Context, input WeatherInput, _ ai.ToolCall) (ai.ToolResponse, error) {
60+
return ai.NewTextResponse("40 C"), nil
61+
},
62+
)
63+
64+
agent := ai.NewAgent(
65+
languageModel,
66+
ai.WithSystemPrompt("You are a helpful assistant"),
67+
ai.WithTools(weatherTool),
68+
)
69+
result, err := agent.Generate(t.Context(), ai.AgentCall{
70+
Prompt: "What's the weather in Florence?",
71+
})
72+
if err != nil {
73+
t.Fatalf("failed to generate: %v", err)
74+
}
75+
76+
want1 := "Florence"
77+
want2 := "40"
78+
got := result.Response.Content.Text()
79+
if !strings.Contains(got, want1) || !strings.Contains(got, want2) {
80+
t.Fatalf("unexpected response: got %q, want %q %q", got, want1, want2)
81+
}
82+
})
83+
}
84+
}

providertests/testdata/TestTool/anthropic-claude-sonnet.yaml

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

providertests/testdata/TestTool/openai-gpt-4o.yaml

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)