Add vendor assessment agent
Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
This commit is contained in:
@@ -29,6 +29,7 @@ type (
|
||||
IsError bool
|
||||
}
|
||||
|
||||
// ToolDescriptor describes a tool's name and LLM definition.
|
||||
ToolDescriptor interface {
|
||||
Name() string
|
||||
Definition() llm.Tool
|
||||
@@ -38,7 +39,31 @@ type (
|
||||
ToolDescriptor
|
||||
Execute(ctx context.Context, arguments string) (ToolResult, error)
|
||||
}
|
||||
)
|
||||
|
||||
// ResultJSON marshals v to JSON and returns a successful ToolResult.
|
||||
func ResultJSON(v any) ToolResult {
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return ToolResult{
|
||||
Content: fmt.Sprintf("cannot marshal tool result: %s", err),
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
return ToolResult{Content: string(data)}
|
||||
}
|
||||
|
||||
// ResultError returns an error ToolResult with the given message.
|
||||
func ResultError(msg string) ToolResult {
|
||||
return ToolResult{Content: msg, IsError: true}
|
||||
}
|
||||
|
||||
// ResultErrorf returns an error ToolResult with a formatted message.
|
||||
func ResultErrorf(format string, args ...any) ToolResult {
|
||||
return ToolResult{Content: fmt.Sprintf(format, args...), IsError: true}
|
||||
}
|
||||
|
||||
type (
|
||||
functionTool[P any] struct {
|
||||
name string
|
||||
description string
|
||||
@@ -48,20 +73,30 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// FunctionTool creates a tool whose parameters are typed by P. The JSON
|
||||
// schema advertised to the LLM is generated from P at construction time.
|
||||
//
|
||||
// Schema generation is derived from a compile-time Go type: a failure
|
||||
// here is a programmer error (bad struct tag, unsupported type), not a
|
||||
// runtime condition, so we panic rather than returning an error. The
|
||||
// same applies to the required-fields metadata parsed back out of the
|
||||
// generated schema.
|
||||
func FunctionTool[P any](
|
||||
name string,
|
||||
description string,
|
||||
fn func(ctx context.Context, params P) (ToolResult, error),
|
||||
) (Tool, error) {
|
||||
) Tool {
|
||||
schema, err := jsonSchemaFor[P]()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create tool %q: %w", name, err)
|
||||
panic(fmt.Sprintf("agent: cannot generate JSON schema for tool %q: %s", name, err))
|
||||
}
|
||||
|
||||
var parsed struct {
|
||||
Required []string `json:"required"`
|
||||
}
|
||||
_ = json.Unmarshal(schema, &parsed)
|
||||
if err := json.Unmarshal(schema, &parsed); err != nil {
|
||||
panic(fmt.Sprintf("agent: cannot parse generated schema for tool %q: %s", name, err))
|
||||
}
|
||||
|
||||
return &functionTool[P]{
|
||||
name: name,
|
||||
@@ -69,7 +104,7 @@ func FunctionTool[P any](
|
||||
fn: fn,
|
||||
schema: schema,
|
||||
requiredFields: parsed.Required,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *functionTool[P]) Name() string { return t.name }
|
||||
|
||||
Reference in New Issue
Block a user