Skip to content

Commit 67e0ad4

Browse files
committed
Added more examples
1 parent e48e987 commit 67e0ad4

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ import (
323323
func add_two_numbers(ctx context.Context, agent llm.Agent) (string, error) {
324324
context := agent.Model(ctx, "claude-3-5-haiku-20241022").Context()
325325
toolkit := tool.NewToolKit()
326-
toolkit.Register(Adder{})
326+
toolkit.Register(&Adder{})
327327

328328
// Get the tool call
329329
if err := context.FromUser(ctx, "What is five plus seven?", llm.WithToolKit(toolkit)); err != nil {

examples/image_caption/main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/mutablelogic/go-llm"
9+
"github.com/mutablelogic/go-llm/pkg/agent"
10+
)
11+
12+
func main() {
13+
// Create a new agent which aggregates multiple providers
14+
agent, err := agent.New(
15+
agent.WithAnthropic(os.Getenv("ANTHROPIC_API_KEY")),
16+
agent.WithMistral(os.Getenv("MISTRAL_API_KEY")),
17+
agent.WithOpenAI(os.Getenv("OPENAI_API_KEY")),
18+
agent.WithOllama(os.Getenv("OLLAMA_URL")),
19+
)
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
// Check args
25+
if len(os.Args) != 3 {
26+
fmt.Println("Usage: image_caption <model> <filename>")
27+
os.Exit(-1)
28+
}
29+
30+
// Get a model
31+
model, err := agent.GetModel(context.TODO(), os.Args[1])
32+
if err != nil {
33+
panic(err)
34+
}
35+
36+
// Open file
37+
f, err := os.Open(os.Args[2])
38+
if err != nil {
39+
panic(err)
40+
}
41+
defer f.Close()
42+
43+
// Get image caption
44+
completion, err := model.Completion(context.TODO(), "Provide me with a description for this image", llm.WithAttachment(f))
45+
if err != nil {
46+
panic(err)
47+
}
48+
49+
fmt.Println(completion.Text(0))
50+
}

examples/tool_adder/main.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/mutablelogic/go-llm"
9+
"github.com/mutablelogic/go-llm/pkg/agent"
10+
"github.com/mutablelogic/go-llm/pkg/tool"
11+
)
12+
13+
///////////////////////////////////////////////////////////////////////////////
14+
15+
type Adder struct {
16+
A float64 `name:"a" help:"The first number" required:"true"`
17+
B float64 `name:"b" help:"The second number" required:"true"`
18+
}
19+
20+
func (Adder) Name() string {
21+
return "add_two_numbers"
22+
}
23+
24+
func (Adder) Description() string {
25+
return "Add two numbers together and return the result"
26+
}
27+
28+
func (a Adder) Run(context.Context) (any, error) {
29+
return a.A + a.B, nil
30+
}
31+
32+
///////////////////////////////////////////////////////////////////////////////
33+
34+
func main() {
35+
// Create a new agent which aggregates multiple providers
36+
agent, err := agent.New(
37+
agent.WithAnthropic(os.Getenv("ANTHROPIC_API_KEY")),
38+
agent.WithMistral(os.Getenv("MISTRAL_API_KEY")),
39+
agent.WithOpenAI(os.Getenv("OPENAI_API_KEY")),
40+
agent.WithOllama(os.Getenv("OLLAMA_URL")),
41+
)
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
// Check args
47+
if len(os.Args) != 4 {
48+
fmt.Println("Usage: tool_adder <model> <first_num> <second_num>")
49+
os.Exit(-1)
50+
}
51+
52+
// Get a model
53+
model, err := agent.GetModel(context.TODO(), os.Args[1])
54+
if err != nil {
55+
panic(err)
56+
}
57+
58+
// Register tool
59+
session := model.Context()
60+
toolkit := tool.NewToolKit()
61+
toolkit.Register(&Adder{})
62+
63+
// Get the tool call
64+
prompt := fmt.Sprintf("What is %v plus %v?", os.Args[2], os.Args[3])
65+
if err := session.FromUser(context.TODO(), prompt, llm.WithToolKit(toolkit), llm.WithToolChoice("any")); err != nil {
66+
panic(err)
67+
}
68+
69+
// Call tools
70+
for {
71+
calls := session.ToolCalls(0)
72+
if len(calls) == 0 {
73+
break
74+
}
75+
76+
// Print out any intermediate messages
77+
if session.Text(0) != "" {
78+
fmt.Println(session.Text(0))
79+
}
80+
81+
// Get the results from the toolkit
82+
fmt.Println("Running", calls)
83+
results, err := toolkit.Run(context.TODO(), calls...)
84+
if err != nil {
85+
panic(err)
86+
}
87+
88+
// Get another tool call or a user response
89+
if err := session.FromTool(context.TODO(), results...); err != nil {
90+
panic(err)
91+
}
92+
}
93+
94+
// Return the result
95+
fmt.Println(session.Text(0))
96+
}

0 commit comments

Comments
 (0)