Added support for tools
This commit is contained in:
parent
920f59e630
commit
9a00f00d47
27
main.go
27
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
@ -19,24 +20,27 @@ func main() {
|
||||||
}
|
}
|
||||||
return scanner.Text(), true
|
return scanner.Text(), true
|
||||||
}
|
}
|
||||||
|
tools := []ToolDefinition{}
|
||||||
|
|
||||||
agent := NewAgent(&client, getUserMessage)
|
agent := NewAgent(&client, getUserMessage, tools)
|
||||||
err := agent.Run(context.TODO())
|
err := agent.Run(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %s\n", err.Error())
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAgent(client *anthropic.Client, getUserMessage func() (string, bool)) *Agent {
|
func NewAgent(client *anthropic.Client, getUserMessage func() (string, bool), tools []ToolDefinition) *Agent {
|
||||||
return &Agent{
|
return &Agent{
|
||||||
client: client,
|
client: client,
|
||||||
getUserMessage: getUserMessage,
|
getUserMessage: getUserMessage,
|
||||||
|
tools: tools,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Agent struct {
|
type Agent struct {
|
||||||
client *anthropic.Client
|
client *anthropic.Client
|
||||||
getUserMessage func() (string, bool)
|
getUserMessage func() (string, bool)
|
||||||
|
tools []ToolDefinition
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) Run(ctx context.Context) error {
|
func (a *Agent) Run(ctx context.Context) error {
|
||||||
|
@ -72,10 +76,29 @@ func (a *Agent) Run(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) runInference(ctx context.Context, conversation []anthropic.MessageParam) (*anthropic.Message, error) {
|
func (a *Agent) runInference(ctx context.Context, conversation []anthropic.MessageParam) (*anthropic.Message, error) {
|
||||||
|
anthropicTools := []anthropic.ToolUnionParam{}
|
||||||
|
for _, tool := range a.tools {
|
||||||
|
anthropicTools = append(anthropicTools, anthropic.ToolUnionParam{
|
||||||
|
OfTool: &anthropic.ToolParam{
|
||||||
|
Name: tool.Name,
|
||||||
|
Description: anthropic.String(tool.Description),
|
||||||
|
InputSchema: tool.InputSchema,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
message, err := a.client.Messages.New(ctx, anthropic.MessageNewParams{
|
message, err := a.client.Messages.New(ctx, anthropic.MessageNewParams{
|
||||||
Model: anthropic.ModelClaude3_7SonnetLatest,
|
Model: anthropic.ModelClaude3_7SonnetLatest,
|
||||||
MaxTokens: int64(1024),
|
MaxTokens: int64(1024),
|
||||||
Messages: conversation,
|
Messages: conversation,
|
||||||
|
Tools: anthropicTools,
|
||||||
})
|
})
|
||||||
return message, err
|
return message, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ToolDefinition struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
InputSchema anthropic.ToolInputSchemaParam `json:"input_schema"`
|
||||||
|
Function func(input json.RawMessage) (string, error)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue