Skip to content

Commit

Permalink
Added more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe committed Feb 7, 2025
1 parent e48e987 commit 67e0ad4
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ import (
func add_two_numbers(ctx context.Context, agent llm.Agent) (string, error) {
context := agent.Model(ctx, "claude-3-5-haiku-20241022").Context()
toolkit := tool.NewToolKit()
toolkit.Register(Adder{})
toolkit.Register(&Adder{})

// Get the tool call
if err := context.FromUser(ctx, "What is five plus seven?", llm.WithToolKit(toolkit)); err != nil {
Expand Down
50 changes: 50 additions & 0 deletions examples/image_caption/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"context"
"fmt"
"os"

"github.com/mutablelogic/go-llm"
"github.com/mutablelogic/go-llm/pkg/agent"
)

func main() {
// Create a new agent which aggregates multiple providers
agent, err := agent.New(
agent.WithAnthropic(os.Getenv("ANTHROPIC_API_KEY")),
agent.WithMistral(os.Getenv("MISTRAL_API_KEY")),
agent.WithOpenAI(os.Getenv("OPENAI_API_KEY")),
agent.WithOllama(os.Getenv("OLLAMA_URL")),
)
if err != nil {
panic(err)
}

// Check args
if len(os.Args) != 3 {
fmt.Println("Usage: image_caption <model> <filename>")
os.Exit(-1)
}

// Get a model
model, err := agent.GetModel(context.TODO(), os.Args[1])
if err != nil {
panic(err)
}

// Open file
f, err := os.Open(os.Args[2])
if err != nil {
panic(err)
}
defer f.Close()

// Get image caption
completion, err := model.Completion(context.TODO(), "Provide me with a description for this image", llm.WithAttachment(f))
if err != nil {
panic(err)
}

fmt.Println(completion.Text(0))
}
96 changes: 96 additions & 0 deletions examples/tool_adder/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"context"
"fmt"
"os"

"github.com/mutablelogic/go-llm"
"github.com/mutablelogic/go-llm/pkg/agent"
"github.com/mutablelogic/go-llm/pkg/tool"
)

///////////////////////////////////////////////////////////////////////////////

type Adder struct {
A float64 `name:"a" help:"The first number" required:"true"`
B float64 `name:"b" help:"The second number" required:"true"`
}

func (Adder) Name() string {
return "add_two_numbers"
}

func (Adder) Description() string {
return "Add two numbers together and return the result"
}

func (a Adder) Run(context.Context) (any, error) {
return a.A + a.B, nil
}

///////////////////////////////////////////////////////////////////////////////

func main() {
// Create a new agent which aggregates multiple providers
agent, err := agent.New(
agent.WithAnthropic(os.Getenv("ANTHROPIC_API_KEY")),
agent.WithMistral(os.Getenv("MISTRAL_API_KEY")),
agent.WithOpenAI(os.Getenv("OPENAI_API_KEY")),
agent.WithOllama(os.Getenv("OLLAMA_URL")),
)
if err != nil {
panic(err)
}

// Check args
if len(os.Args) != 4 {
fmt.Println("Usage: tool_adder <model> <first_num> <second_num>")
os.Exit(-1)
}

// Get a model
model, err := agent.GetModel(context.TODO(), os.Args[1])
if err != nil {
panic(err)
}

// Register tool
session := model.Context()
toolkit := tool.NewToolKit()
toolkit.Register(&Adder{})

// Get the tool call
prompt := fmt.Sprintf("What is %v plus %v?", os.Args[2], os.Args[3])
if err := session.FromUser(context.TODO(), prompt, llm.WithToolKit(toolkit), llm.WithToolChoice("any")); err != nil {
panic(err)
}

// Call tools
for {
calls := session.ToolCalls(0)
if len(calls) == 0 {
break
}

// Print out any intermediate messages
if session.Text(0) != "" {
fmt.Println(session.Text(0))
}

// Get the results from the toolkit
fmt.Println("Running", calls)
results, err := toolkit.Run(context.TODO(), calls...)
if err != nil {
panic(err)
}

// Get another tool call or a user response
if err := session.FromTool(context.TODO(), results...); err != nil {
panic(err)
}
}

// Return the result
fmt.Println(session.Text(0))
}

0 comments on commit 67e0ad4

Please sign in to comment.