|
| 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