-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |