Documentation
¶
Index ¶
- func Complete[T any](ctx context.Context, a *Agent, opts ...Option) (out T, err error)
- func SetChatCompleter(c ChatCompleter)
- type Agent
- type AssistantMessage
- type ChatCompleter
- type Chunk
- type CompletionRequest
- type CompletionResponse
- type CompletionUsage
- type Container
- type FinishReason
- type ForgetfulMemory
- type Handoff
- type InMemoryStorage
- func (s *InMemoryStorage) Delete(ctx context.Context, filename string) error
- func (s *InMemoryStorage) Exists(ctx context.Context, filename string) (bool, error)
- func (s *InMemoryStorage) Read(ctx context.Context, filename string) ([]byte, error)
- func (s *InMemoryStorage) Write(ctx context.Context, filename string, content []byte) error
- type Memory
- type Message
- type MessageBlock
- type MessageBlockType
- type Option
- func SystemPrompt(text string) Option
- func WithApprovals(calls ...string) Option
- func WithApprover(aa ...func(call ToolCall) ToolCallApproval) Option
- func WithAssistantMessage(text string) Option
- func WithAutoApproveAll() Option
- func WithAutoApproveTools(names ...string) Option
- func WithBetas(betas ...string) Option
- func WithBuiltinTool(name, kind string) Option
- func WithChatCompleter(completer ChatCompleter) Option
- func WithContainer(container *Container) Option
- func WithDescription(desc string) Option
- func WithFinalizer(ff ...func(*AssistantMessage) error) Option
- func WithHandoffTool(agents ...*Agent) Option
- func WithInlineTool[In any, Out any](name, desc string, fn func(context.Context, In) (Out, error)) Option
- func WithMaxTokens(maxTokens int64) Option
- func WithMemory(memory Memory) Option
- func WithMessages(messages []Message) Option
- func WithMetadata(key string, value any) Option
- func WithModel(model string) Option
- func WithModelMapper(fn func(string) string) Option
- func WithModelMapping(mapping map[string]string) Option
- func WithOptionLoader(loaders ...OptionLoader) Option
- func WithOptions(opts ...Option) Option
- func WithOrchestratorTool(agents ...*Agent) Option
- func WithOutputTool[T any]() Option
- func WithReasoning(config *Reasoning) Option
- func WithRejections(calls ...string) Option
- func WithSpecialistTool(agents ...*Agent) Option
- func WithStorageReadTool(storage Storage) Option
- func WithStorageTools(storage Storage) Option
- func WithStructuredOutput() Option
- func WithSystemMessage(text string) Option
- func WithTag(tags ...string) Option
- func WithTemperature(temperature float32) Option
- func WithTool(tool Tool, fn func(context.Context, []byte) (any, error)) Option
- func WithToolParallelism(limit int) Option
- func WithToolset(tools Toolset) Option
- func WithTopK(topK int32) Option
- func WithTopP(topP float32) Option
- func WithUserMessage(text string) Option
- func WithValues(values map[string]any) Option
- func WithoutCache() Option
- type OptionLoader
- type Output
- type Reasoning
- type Skill
- type StaticMemory
- type StaticToolset
- type Storage
- type StreamChunkType
- type Streamer
- type SystemMessage
- type Tool
- type ToolApprovalRequest
- type ToolCall
- type ToolCallApproval
- type ToolChoice
- type ToolError
- type ToolHandlerFunc
- type ToolResult
- type Toolset
- type UserMessage
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Complete ¶
Example ¶
ctx := context.Background()
type Output struct {
Answer int `json:"answer"`
Unit string `json:"unit"`
}
a := agent.New("assistant",
agent.WithModel("claude-sonnet-4-5"),
agent.WithSystemMessage("You are a helpful assistant. Communicate your final answer using `complete_task` tool."),
agent.WithAutoApproveAll(),
)
_ = a.Append(ctx, agent.NewUserMessage("How many days are in a year?"))
out, err := agent.Complete[Output](ctx, a)
if err != nil {
panic(err)
}
fmt.Printf("%d %s\n", out.Answer, out.Unit)
func SetChatCompleter ¶
func SetChatCompleter(c ChatCompleter)
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
func (Agent) Run ¶
Example (QuickStart) ¶
package main
import (
"context"
"fmt"
"github.com/anthropics/anthropic-sdk-go/option"
agent "github.com/eolymp/go-agent"
"github.com/eolymp/go-agent/anthropic"
)
func main() {
ctx := context.Background()
agent.SetChatCompleter(anthropic.New(
option.WithAPIKey("your-api-key"),
))
a := agent.New("assistant",
agent.WithModel("claude-sonnet-4-5"),
agent.WithSystemMessage("You are a helpful assistant."),
)
_ = a.Append(ctx, agent.NewUserMessage("What is 2+2?"))
reply, err := a.Run(ctx)
if err != nil {
panic(err)
}
fmt.Println(reply.Text())
}
Output:
Example (WithToolApproval) ¶
package main
import (
"context"
"errors"
"fmt"
agent "github.com/eolymp/go-agent"
)
func main() {
ctx := context.Background()
type Input struct {
A int `json:"a"`
B int `json:"b"`
}
a := agent.New("calculator",
agent.WithModel("claude-sonnet-4-5"),
agent.WithSystemMessage("You are a calculator."),
agent.WithInlineTool("add", "Add two numbers", func(ctx context.Context, in Input) (int, error) {
return in.A + in.B, nil
}),
)
_ = a.Append(ctx, agent.NewUserMessage("What is 123 + 456?"))
var approved, rejected []string
for {
reply, err := a.Run(ctx, agent.WithApprovals(approved...), agent.WithRejections(rejected...))
approved, rejected = nil, nil
var req agent.ToolApprovalRequest
if errors.As(err, &req) {
for _, call := range req.Calls {
fmt.Printf("Approve tool call %q? (y/n): ", call.Name)
var input string
fmt.Scan(&input)
if input == "y" {
approved = append(approved, call.ID)
} else {
rejected = append(rejected, call.ID)
}
}
continue
}
if err != nil {
panic(err)
}
fmt.Println(reply.Text())
break
}
}
Output:
Example (WithTools) ¶
package main
import (
"context"
"fmt"
agent "github.com/eolymp/go-agent"
)
func main() {
ctx := context.Background()
type Input struct {
A int `json:"a"`
B int `json:"b"`
}
a := agent.New("calculator",
agent.WithModel("claude-sonnet-4-5"),
agent.WithSystemMessage("You are a calculator."),
agent.WithAutoApproveAll(),
agent.WithInlineTool("add", "Add two numbers", func(ctx context.Context, in Input) (int, error) {
return in.A + in.B, nil
}),
)
_ = a.Append(ctx, agent.NewUserMessage("What is 123 + 456?"))
reply, err := a.Run(ctx)
if err != nil {
panic(err)
}
fmt.Println(reply.Text())
}
Output:
type AssistantMessage ¶
type AssistantMessage struct {
Content []MessageBlock `json:"content"`
}
func LastMessageAsAssistant ¶
func LastMessageAsAssistant(memory Memory) (AssistantMessage, bool)
func NewAssistantMessage ¶
func NewAssistantMessage(text ...string) AssistantMessage
func (AssistantMessage) Reasoning ¶
func (m AssistantMessage) Reasoning() string
func (AssistantMessage) Text ¶
func (m AssistantMessage) Text() string
func (AssistantMessage) Unmarshal ¶
func (m AssistantMessage) Unmarshal(v any) error
type ChatCompleter ¶
type ChatCompleter interface {
// Complete performs a chat completion request and returns the response.
Complete(ctx context.Context, req CompletionRequest) (*CompletionResponse, error)
}
ChatCompleter defines the interface for chat completion operations. This abstraction allows for different LLM providers (OpenAI, Anthropic, Google, etc.)
type Chunk ¶
type Chunk struct {
Type StreamChunkType
Index int
Text string // For text and thinking deltas
Call *ToolCall // For tool calls (both user and server tools)
Signature string // For thinking signature
Result *ToolResult // For inline tool results
Usage *CompletionUsage
FinishReason FinishReason
}
type CompletionRequest ¶
type CompletionRequest struct {
Model string
Messages []Message
Tools []Tool
ToolChoice ToolChoice
ParallelToolCalls bool
MaxTokens *int64
Temperature *float32
TopP *float32
TopK *int32
UseCache *bool
Container *Container
Betas []string
Reasoning *Reasoning
StreamCallback func(ctx context.Context, chunk Chunk) error
}
type CompletionResponse ¶
type CompletionResponse struct {
Content []MessageBlock
FinishReason FinishReason
Usage CompletionUsage
Model string
}
CompletionResponse represents a provider-agnostic chat completion response.
type CompletionUsage ¶
type CompletionUsage struct {
PromptTokens int `json:"prompt_tokens,omitempty"`
CompletionTokens int `json:"completion_tokens,omitempty"`
ThinkingTokens int `json:"thinking_tokens,omitempty"`
TotalTokens int `json:"total_tokens,omitempty"`
CachedPromptTokens int `json:"cached_prompt_tokens,omitempty"`
}
CompletionUsage represents token usage information for a completion.
type FinishReason ¶
type FinishReason int
FinishReason indicates why the model stopped generating.
const ( // FinishReasonStop indicates natural stop point FinishReasonStop FinishReason = iota // FinishReasonLength indicates max tokens reached FinishReasonLength // FinishReasonToolCalls indicates model wants to call tools FinishReasonToolCalls // FinishReasonContentFilter indicates content was filtered FinishReasonContentFilter )
func (FinishReason) String ¶
func (f FinishReason) String() string
String returns the string representation of FinishReason for debugging.
type ForgetfulMemory ¶
type ForgetfulMemory struct {
// contains filtered or unexported fields
}
ForgetfulMemory keeps memory for the last user message, every new user message erases all memories.
func NewForgetfulMemory ¶
func NewForgetfulMemory() *ForgetfulMemory
type Handoff ¶
type Handoff struct {
Agent *Agent
}
Handoff error communicates the agent has decided to handover conversation to another agent This is a sentinel error which stops agentic loop to restart it using another agent.
type InMemoryStorage ¶
type InMemoryStorage struct {
// contains filtered or unexported fields
}
func NewInMemoryStorage ¶
func NewInMemoryStorage() *InMemoryStorage
func (*InMemoryStorage) Delete ¶
func (s *InMemoryStorage) Delete(ctx context.Context, filename string) error
type Message ¶
type Message interface {
// contains filtered or unexported methods
}
func LastMessage ¶
type MessageBlock ¶
type MessageBlock struct {
Type MessageBlockType `json:"type"`
Text string `json:"text,omitempty"`
Signature string `json:"signature,omitempty"`
ToolCall *ToolCall `json:"toolcall,omitempty"`
ToolResult *ToolResult `json:"tool_result,omitempty"`
}
type MessageBlockType ¶
type MessageBlockType string
const ( MessageBlockTypeText MessageBlockType = "text" MessageBlockTypeToolCall MessageBlockType = "tool_call" MessageBlockTypeReasoning MessageBlockType = "reasoning" MessageBlockTypeSignature MessageBlockType = "signature" MessageBlockTypeServerToolCall MessageBlockType = "server_tool_call" MessageBlockTypeToolResult MessageBlockType = "tool_result" )
type Option ¶
type Option func(*Agent)
func SystemPrompt ¶
SystemPrompt is for backwards compatibility Deprecated, use WithSystemMessage instead
func WithApprovals ¶
WithApprovals creates approver which approves specific calls
func WithApprover ¶
func WithApprover(aa ...func(call ToolCall) ToolCallApproval) Option
func WithAssistantMessage ¶
func WithAutoApproveAll ¶
func WithAutoApproveAll() Option
WithAutoApproveAll creates approver which approves all calls automatically
func WithAutoApproveTools ¶
WithAutoApproveTools creates approver which approves calls for specific tools automatically
func WithBuiltinTool ¶
func WithChatCompleter ¶
func WithChatCompleter(completer ChatCompleter) Option
func WithContainer ¶
func WithDescription ¶
func WithFinalizer ¶
func WithFinalizer(ff ...func(*AssistantMessage) error) Option
func WithHandoffTool ¶
func WithInlineTool ¶
func WithMaxTokens ¶
func WithMemory ¶
func WithMessages ¶
func WithMetadata ¶
func WithModelMapper ¶
func WithModelMapping ¶
func WithOptionLoader ¶
func WithOptionLoader(loaders ...OptionLoader) Option
func WithOptions ¶
func WithOrchestratorTool ¶
func WithOutputTool ¶
func WithReasoning ¶
func WithRejections ¶
WithRejections creates approver which rejects specific calls
func WithSpecialistTool ¶
func WithStorageReadTool ¶
func WithStorageTools ¶
func WithStructuredOutput ¶
func WithStructuredOutput() Option
func WithSystemMessage ¶
func WithTemperature ¶
func WithToolParallelism ¶
func WithToolset ¶
func WithUserMessage ¶
func WithValues ¶
func WithoutCache ¶
func WithoutCache() Option
type OptionLoader ¶
OptionLoader is called in the beginning of the agentic loop to load dynamic options
type Output ¶
type Output struct {
Payload any
}
Output error communicates, the final tool has been called, and it has produced the result (output). This is a sentinel error which stops agentic loop to communicate result immediately.
type StaticMemory ¶
type StaticMemory struct {
// contains filtered or unexported fields
}
StaticMemory keeps all messages in-memory.
func NewStaticMemory ¶
func NewStaticMemory() *StaticMemory
func (*StaticMemory) List ¶
func (m *StaticMemory) List() []Message
type StaticToolset ¶
type StaticToolset struct {
// contains filtered or unexported fields
}
func NewStaticToolset ¶
func NewStaticToolset() *StaticToolset
func (*StaticToolset) Add ¶
func (t *StaticToolset) Add(tool Tool, handler ToolHandlerFunc)
func (*StaticToolset) List ¶
func (t *StaticToolset) List() []Tool
type Storage ¶
type Storage interface {
Exists(ctx context.Context, filename string) (bool, error)
Read(ctx context.Context, filename string) ([]byte, error)
Write(ctx context.Context, filename string, content []byte) error
Delete(ctx context.Context, filename string) error
}
Storage provides an interface to interact with "persistent" storage. This interface is used by set of tools which allow agent to create persistent objects, like content or code snippets.
type StreamChunkType ¶
type StreamChunkType int
const ( StreamChunkTypeText StreamChunkType = iota // a text delta StreamChunkTypeToolCallStart // the start of a new tool call (just call id and tool name) StreamChunkTypeToolCallDelta // a delta in tool call arguments StreamChunkTypeToolCallExecute // a tool is being executed (comes from agent, not LLM) StreamChunkTypeToolCallComplete // a tool has finished (comes from agent, not LLM) StreamChunkTypeReasoning // thinking content delta (extended reasoning) StreamChunkTypeSignature // thinking signature for verification StreamChunkTypeServerToolCallStart // built-in tool call start (web_search, bash, etc.) StreamChunkTypeServerToolCallDelta // built-in tool call arguments delta StreamChunkTypeToolResult // inline tool result from server StreamChunkTypeUsage // usage statistics update StreamChunkTypeFinish // the completion has finished )
func (StreamChunkType) String ¶
func (s StreamChunkType) String() string
type SystemMessage ¶
type SystemMessage struct {
Content string `json:"content"`
}
func NewSystemMessage ¶
func NewSystemMessage(text string) SystemMessage
type Tool ¶
type Tool struct {
Name string
Type string
Description string
InputSchema *jsonschema.Schema
OutputSchema *jsonschema.Schema
DeferLoading bool
}
type ToolApprovalRequest ¶
type ToolApprovalRequest struct {
Calls []ToolCall `json:"calls,omitempty"`
}
func (ToolApprovalRequest) Error ¶
func (r ToolApprovalRequest) Error() string
type ToolCallApproval ¶
type ToolCallApproval int
const ( ToolCallUndecided ToolCallApproval = iota ToolCallApproved ToolCallRejected )
type ToolChoice ¶
type ToolChoice int
ToolChoice represents how the model should use tools during completion.
const ( // ToolChoiceAuto allows the model to decide whether to use tools ToolChoiceAuto ToolChoice = iota // ToolChoiceRequired forces the model to use at least one tool ToolChoiceRequired // ToolChoiceNone prevents the model from using any tools ToolChoiceNone )
func (ToolChoice) String ¶
func (t ToolChoice) String() string
String returns the string representation of ToolChoice for debugging.
type ToolError ¶
func NewToolError ¶
type ToolResult ¶
func NewToolResult ¶
func NewToolResult(callID string, result any) ToolResult
func (ToolResult) String ¶
func (c ToolResult) String() string
type UserMessage ¶
type UserMessage struct {
Content string `json:"content"`
}
func LastMessageAsUser ¶
func LastMessageAsUser(memory Memory) (UserMessage, bool)
func NewUserMessage ¶
func NewUserMessage(text string) UserMessage