Documentation
¶
Index ¶
- Constants
- func Logf(ctx context.Context, format string, args ...any)
- func Progressf(ctx context.Context, format string, args ...any)
- func RunTools(ctx context.Context, tools []llm.Tool, calls []llm.ToolCall, ...) ([]providerschema.ContentBlock, error)
- func TruncateResult(ctx context.Context, result json.RawMessage, limit uint, measure MeasureFn) (json.RawMessage, error)
- func WithSession(ctx context.Context, session Session) context.Context
- type Base
- type CredentialsProvider
- type MeasureFn
- type Namespace
- type Opt
- type Respond
- type Session
- type Toolkit
- func (self *Toolkit) Add(ns Namespace) error
- func (self *Toolkit) AvailableTools(ctx context.Context, names ...string) (_ []llm.Tool, skipped []string, err error)
- func (self *Toolkit) CallTool(ctx context.Context, name string, input json.RawMessage) (_ json.RawMessage, err error)
- func (self *Toolkit) ClientOpts() []client.ClientOpt
- func (self *Toolkit) Credential(ctx context.Context, key string) (_ string, err error)
- func (self *Toolkit) GetTool(ctx context.Context, name string) (_ *schema.Tool, err error)
- func (self *Toolkit) ListTools(ctx context.Context, req schema.ToolListRequest) (_ *schema.ToolList, err error)
- func (self *Toolkit) SystemPrompts(ctx context.Context, names ...string) (_ []string, err error)
- func (self *Toolkit) ToolNames(ctx context.Context, names ...string) (_ []string, err error)
- func (self *Toolkit) Tools(ctx context.Context, names ...string) (_ []llm.Tool, err error)
Constants ¶
const ( // RespondName is the name of the tool a model calls to reply with JSON RespondName = "respond" // RespondPrompt is added to the system prompt when the reply has a schema RespondPrompt = "Reply in JSON, by calling the " + RespondName + " tool with your final answer as its result, which must match the tool's schema. Don't reply with text instead." // RespondNudge is the message sent when the model replies with text, when // the reply has a schema RespondNudge = "Reply by calling the " + RespondName + " tool with your answer as its result, which must match the tool's schema." )
const ToolTable = "tool"
ToolTable is the table of the events of tools, whose rows are their canonical names, such as agent:segment
Variables ¶
This section is empty.
Functions ¶
func Logf ¶ added in v0.6.0
Logf logs what the tool run in the context is doing, as a sentence in plain English, such as "Getting the current weather for Berlin", which is shown to the user, to its session's logger, at the info level
func Progressf ¶ added in v0.6.0
Progressf says how the tool run in the context is getting on, as a sentence in plain English, such as "Segmenting the transcript", which is shown to the user, in place of what it said before
func RunTools ¶ added in v0.6.0
func RunTools(ctx context.Context, tools []llm.Tool, calls []llm.ToolCall, timeout time.Duration, session func(llm.ToolCall) Session) ([]providerschema.ContentBlock, error)
RunTools runs the tools a model called, which are found in the tools by the name the model called them by, in parallel, each in the session the function returns for its call, unless the function is nil, and returns their results, in the order of the calls, as content blocks of a message of the results. A call of a tool which isn't one of the tools, or which fails, has its error as its result, which the model can correct. Each tool has the timeout to return, unless it's zero, after which its context is done, and its result is that it timed out, without waiting for it to return, so a tool which doesn't return when its context is done can't keep the others waiting. Returns the context's error, when it's done, without waiting for the tools to return.
func TruncateResult ¶ added in v0.6.0
func TruncateResult(ctx context.Context, result json.RawMessage, limit uint, measure MeasureFn) (json.RawMessage, error)
TruncateResult returns the result of a tool, as JSON, so it's at most the limit of tokens, as the function measures them, so a model is sent no more of a tool's result than fits in its context. A result which fits, or when the limit is zero, is returned as it is. Otherwise, its longest strings, arrays and objects are shortened, as little as fits, with notes of what was left out of each, so the result is valid JSON of the same shape, or, when that doesn't fit, or it isn't JSON, it's the start of the result as text, as a JSON string, with a note of what was left out of it, or, when even that doesn't fit, a note that it was left out. Returns the function's error, when it fails.
Types ¶
type Base ¶ added in v0.5.0
type Base struct{}
Base provides default implementations of the optional llm.Tool methods. Embed Base in a tool to avoid boilerplate, and override the methods the tool needs.
func (Base) OutputSchema ¶ added in v0.5.0
func (Base) OutputSchema() *jsonschema.Schema
OutputSchema returns nil, as the tool has no structured output schema
type CredentialsProvider ¶ added in v0.5.0
type CredentialsProvider interface {
// Expand returns the value with each placeholder replaced with its
// credential, or an error when a credential doesn't exist
Expand(ctx context.Context, value string) (string, error)
}
CredentialsProvider expands credentials, such as a tool's API key, whose placeholders, such as ${WEATHERAPI_KEY} or $WEATHERAPI_KEY, are replaced with the credentials they name, as the registry's credentials provider does, so one provider can be both's
type MeasureFn ¶ added in v0.6.0
MeasureFn returns how many tokens the text is, for the model a tool's result is sent to
type Namespace ¶ added in v0.6.0
type Namespace interface {
Name() string
SystemPrompt() string
Tools() []llm.Tool
// Notify sets the function which the namespace calls, once it's added to
// a toolkit, when one of its tools is created, updated, or deleted, with
// the action, and the tool's name, after Tools returns it as it is. A
// namespace whose tools don't change, once it's added, doesn't call it.
// It's called without the namespace's lock held, as Tools is called.
Notify(func(action eventschema.EventAction, name string))
}
Namespace is a namespace of tools: its name, which is an identifier, its system prompt, which says how to use its tools, and is added to the system prompt of a model which can call any of them, or is empty, and its tools
type Opt ¶ added in v0.5.0
type Opt func(*opt) error
Opt is a functional option for toolkit configuration.
func WithBroadcaster ¶ added in v0.6.0
func WithBroadcaster(b *broadcaster.Broadcaster) Opt
WithBroadcaster sets the broadcaster which the toolkit publishes the changes to its tools to, as they're added, and as a namespace's tools are created, updated or deleted, whose table is ToolTable, and whose rows are the tools' canonical names
func WithClientOpts ¶ added in v0.5.0
WithClientOpts sets the client options for the HTTP clients of tools which call APIs, e.g. for timeouts.
func WithCredentialsProvider ¶ added in v0.5.0
func WithCredentialsProvider(provider CredentialsProvider) Opt
WithCredentialsProvider sets the provider of credentials, such as API keys, for tools which need them, which are environment variables, without one, or with a nil provider
func WithTracer ¶
WithTracer sets the tracer used for tracing operations.
type Respond ¶ added in v0.5.0
type Respond struct {
Base
// contains filtered or unexported fields
}
Respond is the tool a model calls to reply with JSON which conforms to a schema. The tool's input is an object, as the input of a tool must be, whose result is the reply, so the reply can be any JSON. The tool's result is the reply, when it conforms, or an error the model can correct.
func NewRespond ¶ added in v0.5.0
func NewRespond(schema json.RawMessage) (*Respond, error)
NewRespond returns the tool a model calls to reply with JSON which conforms to the schema. Returns an error if the schema isn't a JSON schema.
func (*Respond) Description ¶ added in v0.5.0
func (*Respond) InputSchema ¶ added in v0.5.0
func (t *Respond) InputSchema() *jsonschema.Schema
type Session ¶
type Session interface {
// ID returns the identifier of the session the tool runs in, such as
// the conversation whose model called it, or the nil UUID when it
// isn't run in one, so a tool which asks a model, such as an agent, can
// continue from it
ID() uuid.UUID
// Logger returns the logger the tool logs to as it runs, such as to say
// what it's doing, which what runs it can show, such as by streaming it
Logger() *slog.Logger
// Progress says how the tool is getting on, as it runs, as MCP's
// progress notification does: how far it's got, which increases each
// time, out of the total, when it's known, or zero, and a message, such
// as that an agent it runs is replying, which what runs it can show, in
// place of its progress before, rather than keeping it, as a line it
// logs is
Progress(progress, total float64, message string)
// Usage adds the tokens the tool used, such as when it asks a model, as
// an agent does, to those of its result, so what runs it counts them
Usage(llm.Usage)
}
Session is what a tool can use as it runs, from what runs it, such as an agent loop, which a tool gets from its context with SessionFromContext
func NewSession ¶ added in v0.6.0
NewSession returns the session of the tool with the name, such as the name the model called it by, in the session with the identifier, or the nil UUID, when it isn't run in one. Its logger streams what the tool logs, at the info level or above, to the function, as log lines of the level, when it isn't info, the message, and its attributes, as key=value, which are shown to the user, so needn't say which tool it is, and its progress is streamed to the function too, unless it's nil. What it logs, at every level, and its progress, are added to the span, as events, with the tool's name, unless the span is nil. The function is called from each goroutine the tool, or tools run in parallel, stream from, so must be safe to call at once.
func SessionFromContext ¶
SessionFromContext returns the session of the tool run in the context, or, when it isn't run in one, a session whose logger discards what's logged, so it's never nil
type Toolkit ¶
type Toolkit struct {
// contains filtered or unexported fields
}
Toolkit is a set of tools, by name, which models can call
func (*Toolkit) Add ¶ added in v0.5.0
Add adds the namespace, and its tools, to the toolkit, by their canonical name, which is the namespace's name and the tool's name separated by a colon, such as "weatherapi:current". The namespace's name must be an identifier. Returns an error, and adds none of the tools, if the namespace is nil, isn't valid or has already been added, or a tool is nil, its name isn't valid, or two tools have the same name.
func (*Toolkit) AvailableTools ¶ added in v0.5.0
func (self *Toolkit) AvailableTools(ctx context.Context, names ...string) (_ []llm.Tool, skipped []string, err error)
AvailableTools returns the tools with the names, as with Tools, except a name which isn't a tool, or a namespace which doesn't exist, is skipped, rather than an error, as when the tools of a namespace aren't available. Returns the names which were skipped, and the same errors as Tools otherwise.
func (*Toolkit) CallTool ¶ added in v0.5.0
func (self *Toolkit) CallTool(ctx context.Context, name string, input json.RawMessage) (_ json.RawMessage, err error)
CallTool runs the tool with the name, which is its canonical name, such as "weatherapi:current", or its name without a namespace, such as "current", with the input, which may be empty, and returns its result as JSON, or nil when the tool has no result. Returns ErrNotFound if there is no tool with the name, ErrConflict if the name has no namespace, and more than one namespace has a tool with the name, or ErrBadParameter if the input isn't valid JSON, or doesn't match the tool's input schema. The tool is run without holding the toolkit's lock, so tools can be added and removed while it runs.
func (*Toolkit) ClientOpts ¶ added in v0.5.0
ClientOpts returns the client options for the HTTP clients of tools which call APIs
func (*Toolkit) Credential ¶ added in v0.5.0
Credential returns the value of the credential with the key, such as a tool's API key, from the toolkit's credential provider. Returns ErrNotFound when the credential doesn't exist, or is empty, as when its environment variable is set to nothing, so it's as if it isn't set.
func (*Toolkit) GetTool ¶ added in v0.5.0
GetTool returns the tool with the name, which is its canonical name, such as "weatherapi:current", or its name without a namespace, such as "current". Returns ErrNotFound if there is no tool with the name, or ErrConflict if the name has no namespace, and more than one namespace has a tool with the name.
func (*Toolkit) ListTools ¶ added in v0.5.0
func (self *Toolkit) ListTools(ctx context.Context, req schema.ToolListRequest) (_ *schema.ToolList, err error)
ListTools returns the tools in the toolkit, or the tools in the namespace in the request, in order of canonical name, with their number
func (*Toolkit) SystemPrompts ¶ added in v0.6.0
SystemPrompts returns the system prompts of the namespaces of the tools with the names, as Tools returns them, to add to the system prompt of a model which can call them, in the order of the names, once each, without those which are empty, or the same as one before. Each namespace's system prompt is read once, as it can change, such as one which has the time. A namespace named by its name and "*", such as "memory:*", or by "*", has its system prompt, even when it has no tools. Returns the same errors as Tools.
func (*Toolkit) ToolNames ¶ added in v0.5.0
ToolNames returns the names, as with Tools, as canonical names, such as "weatherapi:current" for "current". A namespace and "*", such as "ollama:*", or "*", is returned as it is, so it includes tools added to the namespace, or the toolkit, later. Returns the same errors as Tools.
func (*Toolkit) Tools ¶ added in v0.5.0
Tools returns the tools with the names, which are their canonical names, their names without a namespace, a namespace and "*", such as "ollama:*", for all the tools in the namespace, or "*" for all the tools. No names are no tools. Each tool is wrapped with a new name, which is its namespace and name separated by two underscores, such as "weatherapi__current". Returns ErrNotFound if there is no tool with a name, or no namespace for "*", ErrConflict if a name without a namespace is ambiguous, or two tools have the same new name, or ErrBadParameter if a tool is named more than once, including by a "*", or its new name isn't a valid tool name, such as when it's too long.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
httpclient
httpclient implements an API client for the Home Assistant REST API https://developers.home-assistant.io/docs/api/rest/
|
httpclient implements an API client for the Home Assistant REST API https://developers.home-assistant.io/docs/api/rest/ |
|
httpclient
httpclient implements an API client for NewsAPI https://newsapi.org/docs
|
httpclient implements an API client for NewsAPI https://newsapi.org/docs |
|
httpclient
httpclient implements an API client for Ollama's web search and web fetch https://docs.ollama.com/capabilities/web-search
|
httpclient implements an API client for Ollama's web search and web fetch https://docs.ollama.com/capabilities/web-search |
|
httpclient
httpclient implements an API client for WeatherAPI https://www.weatherapi.com/docs/
|
httpclient implements an API client for WeatherAPI https://www.weatherapi.com/docs/ |