ai

package
v0.0.0-...-7334d01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

README

Go SDK AI Package

This package provides AI/LLM capabilities for the AgentField Go SDK, supporting both OpenAI and OpenRouter APIs with structured output support.

Features

  • OpenAI & OpenRouter Support: Works with both OpenAI API and OpenRouter for multi-model routing
  • Structured Outputs: JSON schema validation with Go struct support
  • Streaming: Support for streaming responses
  • Type-Safe: Automatic conversion from Go structs to JSON schemas
  • Functional Options: Clean, idiomatic Go API with functional options pattern
  • Automatic Configuration: Reads from environment variables by default

Quick Start

Basic Usage
import (
    "context"
    "github.com/Agent-Field/agentfield/sdk/go/agent"
    "github.com/Agent-Field/agentfield/sdk/go/ai"
)

// Create agent with AI configured
aiConfig := ai.DefaultConfig() // Reads from env vars
agent, err := agent.New(agent.Config{
    NodeID:   "my-agent",
    Version:  "1.0.0",
    AgentFieldURL: "http://localhost:8080",
    AIConfig: aiConfig,
})

// Make a simple AI call
response, err := agent.AI(ctx, "What is 2+2?")
fmt.Println(response.Text())
Structured Outputs
// Define your response schema
type WeatherResponse struct {
    Location    string  `json:"location" description:"City name"`
    Temperature float64 `json:"temperature" description:"Temperature in Celsius"`
    Conditions  string  `json:"conditions" description:"Weather conditions"`
}

// Call AI with schema
response, err := agent.AI(ctx, "What's the weather in Paris?",
    ai.WithSystem("You are a weather assistant"),
    ai.WithSchema(WeatherResponse{}))

// Parse response into struct
var weather WeatherResponse
response.Into(&weather)
fmt.Printf("%s: %.1f°C, %s\n", weather.Location, weather.Temperature, weather.Conditions)
Streaming Responses
chunks, errs := agent.AIStream(ctx, "Tell me a story")
for chunk := range chunks {
    if len(chunk.Choices) > 0 {
        fmt.Print(chunk.Choices[0].Delta.Content)
    }
}
if err := <-errs; err != nil {
    log.Fatal(err)
}

Configuration

Environment Variables

The SDK automatically reads from environment variables:

# For OpenAI (default)
export OPENAI_API_KEY="sk-..."
export AI_MODEL="gpt-4o"  # Optional, defaults to gpt-4o

# For OpenRouter
export OPENROUTER_API_KEY="sk-..."
export AI_MODEL="openai/gpt-4o"  # Use OpenRouter model format

# Custom base URL
export AI_BASE_URL="https://api.openai.com/v1"
Manual Configuration
aiConfig := &ai.Config{
    APIKey:      "sk-...",
    BaseURL:     "https://api.openai.com/v1",
    Model:       "gpt-4o",
    Temperature: 0.7,
    MaxTokens:   4096,
}
OpenRouter Configuration
aiConfig := &ai.Config{
    APIKey:   os.Getenv("OPENROUTER_API_KEY"),
    BaseURL:  "https://openrouter.ai/api/v1",
    Model:    "anthropic/claude-3.5-sonnet", // OpenRouter format: provider/model
    SiteURL:  "https://myapp.com",  // For OpenRouter rankings
    SiteName: "My AI App",
}

API Reference

AI Client
ai.NewClient(config *Config) (*Client, error)

Creates a new AI client with the given configuration.

client.Complete(ctx context.Context, prompt string, opts ...Option) (*Response, error)

Makes a chat completion request.

client.StreamComplete(ctx context.Context, prompt string, opts ...Option) (<-chan StreamChunk, <-chan error)

Makes a streaming chat completion request.

Agent Methods
agent.AI(ctx context.Context, prompt string, opts ...Option) (*Response, error)

Makes an AI call using the agent's configured AI client.

agent.AIStream(ctx context.Context, prompt string, opts ...Option) (<-chan StreamChunk, <-chan error)

Makes a streaming AI call.

Options

Functional options for customizing AI requests:

  • ai.WithSystem(content string) - Add a system prompt
  • ai.WithModel(model string) - Override the default model
  • ai.WithTemperature(temp float64) - Set temperature (0.0-2.0)
  • ai.WithMaxTokens(tokens int) - Set max tokens
  • ai.WithStream() - Enable streaming
  • ai.WithJSONMode() - Enable JSON object mode
  • ai.WithSchema(schema interface{}) - Enable structured outputs with schema
Multimodal
  • ai.WithImageFile(path string) - Attach an image from a local file
  • ai.WithImageURL(url string) - Attach an image from a remote URL
  • ai.WithImageBytes(data []byte, mimeType string) - Add an image from raw bytes (SDK encodes automatically)
Multimodal Inputs (Images)

You can attach images files to AI requests.

// Image from file
response, _ := agent.AI(ctx, "Describe this image",
    ai.WithImageFile("./photo.jpg"),
)

// Image from URL
response, _ = agent.AI(ctx, "Describe this image",
    ai.WithImageURL("https://example.com/image.jpg"),
)

// Image from bytes
data, _ := os.ReadFile("image.png")
response, _ = agent.AI(ctx, "What's in this image?",
    ai.WithImageBytes(data, "image/png"),
)
Response Methods
  • response.Text() - Get the text content
  • response.JSON(dest interface{}) - Parse response as JSON
  • response.Into(dest interface{}) - Alias for JSON()

Structured Output Schema

Define schemas using Go structs with JSON tags:

type MyResponse struct {
    // Required field
    Name string `json:"name" description:"The person's name"`

    // Optional field (use omitempty)
    Age int `json:"age,omitempty" description:"The person's age"`

    // Array
    Hobbies []string `json:"hobbies" description:"List of hobbies"`

    // Nested object
    Address struct {
        Street string `json:"street"`
        City   string `json:"city"`
    } `json:"address"`
}

The SDK automatically converts Go structs to JSON schemas compatible with OpenAI's structured output format.

Using AI in Reasoners

You can use AI within your reasoners to create intelligent workflows:

agent.RegisterReasoner("smart_reasoner", func(ctx context.Context, input map[string]any) (any, error) {
    question := input["question"].(string)

    // Call AI within the reasoner
    response, err := agent.AI(ctx, question,
        ai.WithSystem("You are a helpful assistant"),
        ai.WithTemperature(0.7))
    if err != nil {
        return nil, err
    }

    return map[string]any{
        "answer": response.Text(),
        "model":  response.Model,
    }, nil
})

Comparison with Python SDK

The Go SDK provides similar functionality to the Python SDK's agent.ai() method:

Feature Python SDK Go SDK
Simple text calls agent.ai("prompt") agent.AI(ctx, "prompt")
System prompts system="..." kwarg ai.WithSystem("...") option
Structured outputs schema=Model kwarg ai.WithSchema(Model{}) option
Streaming stream=True kwarg agent.AIStream() method
Model override model="..." kwarg ai.WithModel("...") option
Temperature temperature=0.7 kwarg ai.WithTemperature(0.7) option

Error Handling

response, err := agent.AI(ctx, "prompt")
if err != nil {
    // Handle API errors
    log.Printf("AI call failed: %v", err)
    return
}

// Parse structured response
var result MyStruct
if err := response.Into(&result); err != nil {
    // Handle parsing errors
    log.Printf("Failed to parse response: %v", err)
    return
}

Performance Considerations

  1. Connection Pooling: The HTTP client uses connection pooling for efficient requests
  2. Context Cancellation: Always use contexts with timeouts for AI calls
  3. Streaming: Use streaming for long responses to improve perceived latency
  4. Model Selection: Choose appropriate models for your use case (faster models = lower latency)

Examples

See the examples/ai-agent directory for complete examples including:

  • Simple text responses
  • Structured outputs
  • Sentiment analysis
  • Agent reasoners with AI
  • Streaming responses
  • OpenRouter usage

License

Proprietary - Authorized users only

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SimpleAI

func SimpleAI(ctx context.Context, prompt string) (string, error)

SimpleAI makes a simple AI call with just a prompt.

func StructuredAI

func StructuredAI(ctx context.Context, prompt string, schema interface{}, dest interface{}) error

StructuredAI makes an AI call and returns structured data.

Types

type AudioData

type AudioData struct {
	Data   string `json:"data,omitempty"`
	Format string `json:"format"`
	URL    string `json:"url,omitempty"`
}

AudioData holds data for generated audio.

type AudioRequest

type AudioRequest struct {
	Text   string `json:"text"`
	Model  string `json:"model,omitempty"`
	Voice  string `json:"voice,omitempty"`
	Format string `json:"format,omitempty"`
}

AudioRequest holds parameters for audio generation.

type CallFunc

type CallFunc func(ctx context.Context, target string, input map[string]interface{}) (map[string]interface{}, error)

CallFunc is the function signature for dispatching tool calls. It maps to agent.Call(ctx, target, input).

type Choice

type Choice struct {
	Index        int     `json:"index"`
	Message      Message `json:"message"`
	FinishReason string  `json:"finish_reason"`
}

Choice represents a completion choice.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides AI/LLM capabilities using OpenAI or OpenRouter API.

func NewClient

func NewClient(config *Config) (*Client, error)

NewClient creates a new AI client with the given configuration.

func (*Client) Complete

func (c *Client) Complete(ctx context.Context, prompt string, opts ...Option) (*Response, error)

Complete makes a chat completion request.

func (*Client) CompleteWithMessages

func (c *Client) CompleteWithMessages(ctx context.Context, messages []Message, opts ...Option) (*Response, error)

CompleteWithMessages makes a chat completion request with custom messages.

func (*Client) ExecuteToolCallLoop

func (c *Client) ExecuteToolCallLoop(
	ctx context.Context,
	messages []Message,
	tools []ToolDefinition,
	config ToolCallConfig,
	callFn CallFunc,
	opts ...Option,
) (*Response, *ToolCallTrace, error)

ExecuteToolCallLoop runs the LLM tool-call loop: send messages with tools, dispatch any tool calls via callFn, feed results back, repeat until the LLM produces a final text response or limits are reached.

func (*Client) ExecuteToolCallLoopResult

func (c *Client) ExecuteToolCallLoopResult(
	ctx context.Context,
	messages []Message,
	tools []ToolDefinition,
	config ToolCallConfig,
	callFn CallFunc,
	opts ...Option,
) (*ToolCallResult, error)

ExecuteToolCallLoopResult runs the tool-call loop and returns a wrapped response plus the full execution trace.

func (*Client) StreamComplete

func (c *Client) StreamComplete(ctx context.Context, prompt string, opts ...Option) (<-chan StreamChunk, <-chan error)

StreamComplete makes a streaming chat completion request. Returns a channel of response chunks.

type Config

type Config struct {
	// API Key for OpenAI or OpenRouter
	APIKey string

	// BaseURL can be either OpenAI or OpenRouter endpoint
	// Default: https://api.openai.com/v1
	// OpenRouter: https://openrouter.ai/api/v1
	BaseURL string

	// Default model to use (e.g., "gpt-4o", "openai/gpt-4o" for OpenRouter)
	Model string

	// Default temperature for responses (0.0 to 2.0)
	Temperature float64

	// Default max tokens for responses
	MaxTokens int

	// HTTP timeout for requests
	Timeout time.Duration

	// Optional: Site URL for OpenRouter rankings
	SiteURL string

	// Optional: Site name for OpenRouter rankings
	SiteName string
}

Config holds AI/LLM configuration for making API calls.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with sensible defaults. It reads from environment variables: - OPENAI_API_KEY or OPENROUTER_API_KEY - AI_BASE_URL (defaults to OpenAI) - AI_MODEL (defaults to gpt-4o)

func (*Config) IsOpenRouter

func (c *Config) IsOpenRouter() bool

IsOpenRouter returns true if the base URL is for OpenRouter.

func (*Config) Validate

func (c *Config) Validate() error

Validate ensures the configuration is valid.

type ContentPart

type ContentPart struct {
	Type       string          `json:"type"` // "text" or "image_url" or "input_audio" or "file"
	Text       string          `json:"text,omitempty"`
	ImageURL   *ImageURLData   `json:"image_url,omitempty"`
	InputAudio *InputAudioData `json:"input_audio,omitempty"`
	InputFile  *InputFileData  `json:"file,omitempty"`
}

type ErrorDetail

type ErrorDetail struct {
	Message string `json:"message"`
	Type    string `json:"type"`
	Code    string `json:"code,omitempty"`
}

ErrorDetail contains error information.

type ErrorResponse

type ErrorResponse struct {
	Error ErrorDetail `json:"error"`
}

ErrorResponse represents an error from the API.

type FileData

type FileData struct {
	URL      string `json:"url,omitempty"`
	Data     string `json:"data,omitempty"`
	MimeType string `json:"mime_type,omitempty"`
	Filename string `json:"filename,omitempty"`
}

FileData holds data for a generated file.

type ImageConfig

type ImageConfig struct {
	AspectRatio               string   `json:"aspect_ratio,omitempty"`
	ImageSize                 string   `json:"image_size,omitempty"`
	SuperResolutionReferences []string `json:"super_resolution_references,omitempty"`
}

ImageConfig holds OpenRouter-specific image configuration.

type ImageData

type ImageData struct {
	URL           string `json:"url,omitempty"`
	B64JSON       string `json:"b64_json,omitempty"`
	RevisedPrompt string `json:"revised_prompt,omitempty"`
}

ImageData holds data for a generated image.

type ImageRequest

type ImageRequest struct {
	Prompt      string       `json:"prompt"`
	Model       string       `json:"model,omitempty"`
	Size        string       `json:"size,omitempty"`
	Quality     string       `json:"quality,omitempty"`
	ImageConfig *ImageConfig `json:"image_config,omitempty"`
}

ImageRequest holds parameters for image generation.

type ImageURLData

type ImageURLData struct {
	URL    string `json:"url"`
	Detail string `json:"detail,omitempty"`
}

ImageURLData holds the URL and optional detail level for image content parts.

type InputAudioData

type InputAudioData struct {
	Data   string `json:"data"`
	Format string `json:"format"`
}

InputAudioData holds the encoded data and the format for the audio content parts.

type InputFileData

type InputFileData struct {
	FileData string `json:"file_data"`
}

InputFileData holds the file data for a generic file content parts.

type JSONSchema

type JSONSchema struct {
	Name   string          `json:"name"`
	Strict bool            `json:"strict"`
	Schema json.RawMessage `json:"schema"`
}

JSONSchema defines the structure for structured outputs.

type MediaProvider

type MediaProvider interface {
	Name() string
	SupportedModalities() []string
	GenerateImage(ctx context.Context, req ImageRequest) (*MediaResponse, error)
	GenerateAudio(ctx context.Context, req AudioRequest) (*MediaResponse, error)
	GenerateVideo(ctx context.Context, req VideoRequest) (*MediaResponse, error)
}

MediaProvider defines the interface for media generation backends.

type MediaResponse

type MediaResponse struct {
	Text        string      `json:"text"`
	Images      []ImageData `json:"images,omitempty"`
	Audio       *AudioData  `json:"audio,omitempty"`
	Files       []FileData  `json:"files,omitempty"`
	Videos      []VideoData `json:"videos,omitempty"`
	RawResponse any         `json:"raw_response,omitempty"`
}

MediaResponse holds the result of a media generation call.

type MediaRouter

type MediaRouter struct {
	// contains filtered or unexported fields
}

MediaRouter dispatches (model, capability) pairs to the correct MediaProvider.

func NewMediaRouter

func NewMediaRouter() *MediaRouter

NewMediaRouter creates a new MediaRouter.

func (*MediaRouter) Register

func (r *MediaRouter) Register(prefix string, provider MediaProvider)

Register adds a provider with a model prefix. Longer prefixes match first.

func (*MediaRouter) Resolve

func (r *MediaRouter) Resolve(model, capability string) (MediaProvider, error)

Resolve finds the provider for a model and capability.

type Message

type Message struct {
	Role       string        `json:"role"`
	Content    []ContentPart `json:"content"`
	ToolCalls  []ToolCall    `json:"tool_calls,omitempty"`
	ToolCallID string        `json:"tool_call_id,omitempty"`
}

func (Message) MarshalJSON

func (m Message) MarshalJSON() ([]byte, error)

MarshalJSON serializes a Message. If the content is a single text part, it serializes content as a plain string for maximum API compatibility.

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(data []byte) error

type MessageDelta

type MessageDelta struct {
	Role    string `json:"role,omitempty"`
	Content string `json:"content,omitempty"`
}

MessageDelta represents the incremental message content.

type OpenRouterMediaProvider

type OpenRouterMediaProvider struct {
	APIKey  string
	BaseURL string
	Client  *http.Client
}

OpenRouterMediaProvider implements MediaProvider for OpenRouter's media APIs.

func NewOpenRouterMediaProvider

func NewOpenRouterMediaProvider(apiKey string) (*OpenRouterMediaProvider, error)

NewOpenRouterMediaProvider creates a provider. If apiKey is empty, reads OPENROUTER_API_KEY. Returns error if no API key is available.

func (*OpenRouterMediaProvider) GenerateAudio

func (p *OpenRouterMediaProvider) GenerateAudio(ctx context.Context, req AudioRequest) (*MediaResponse, error)

GenerateAudio uses streaming chat completions with audio modality.

func (*OpenRouterMediaProvider) GenerateImage

func (p *OpenRouterMediaProvider) GenerateImage(ctx context.Context, req ImageRequest) (*MediaResponse, error)

GenerateImage uses chat completions with image modality.

func (*OpenRouterMediaProvider) GenerateVideo

func (p *OpenRouterMediaProvider) GenerateVideo(ctx context.Context, req VideoRequest) (*MediaResponse, error)

GenerateVideo submits a video job, polls until complete, downloads result.

func (*OpenRouterMediaProvider) Name

func (p *OpenRouterMediaProvider) Name() string

func (*OpenRouterMediaProvider) SupportedModalities

func (p *OpenRouterMediaProvider) SupportedModalities() []string

type Option

type Option func(*Request) error

Option is a functional option for configuring an AI request.

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey overrides the client's configured API key for this request only.

func WithAudioFile

func WithAudioFile(path string, mediaType string) Option

WithAudioFile reads a local audio file, base64 encodes it, and attaches it to the request. The format string must be explicitly provided (e.g., "mp3", "wav").

func WithAudioURL

func WithAudioURL(url string, mediaType string) Option

WithAudioURL downloads an audio file from the provided URL, base64 encodes it, and attaches it to the request. The network connection is safely closed after reading. The format string must be explicitly provided (e.g., "mp3", "wav").

func WithFile

func WithFile(path string, mediaType string) Option

WithFile attaches a generic file to the request and base64 encodes it. The mediaType must be explicitly provided by the caller.

Common supported MediaTypes for AI models include:

  • "application/pdf" (PDF documents)
  • "text/plain" (Standard text files)
  • "text/csv" (CSV data)
  • "text/markdown" (Markdown files)
  • "application/json" (JSON files)
  • "text/html" (HTML documents)
  • "application/vnd.openxmlformats-officedocument.wordprocessingml.document" (Word .docx)
  • "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" (Excel .xlsx)

func WithImageBytes

func WithImageBytes(data []byte, mimeType string) Option

WithImageBytes attaches an image from raw bytes (SDK encodes automatically).

func WithImageFile

func WithImageFile(path string) Option

Image options

func WithImageURL

func WithImageURL(url string) Option

WithImageURL attaches an image from a remote URL.

func WithJSONMode

func WithJSONMode() Option

WithJSONMode enables JSON object mode (non-strict).

func WithMaxTokens

func WithMaxTokens(tokens int) Option

WithMaxTokens sets the maximum tokens.

func WithModel

func WithModel(model string) Option

WithModel overrides the default model.

func WithSchema

func WithSchema(schema interface{}) Option

WithSchema enables structured output with a JSON schema. Accepts either a Go struct (will be converted to JSON schema) or json.RawMessage.

func WithStream

func WithStream() Option

WithStream enables streaming responses.

func WithSystem

func WithSystem(content string) Option

WithSystem adds a system message to the request.

func WithTemperature

func WithTemperature(temp float64) Option

WithTemperature sets the temperature.

func WithTools

func WithTools(tools []ToolDefinition) Option

WithTools sets tool definitions for the request.

type PromptConfig

type PromptConfig struct {
	// ToolCallLimitReached is sent back to the model when it asks for more tool
	// calls than MaxToolCalls allows.
	ToolCallLimitReached string
	// ToolErrorFormatter formats tool execution failures before they are sent
	// back to the model. It may return a raw string or any JSON-marshable value.
	ToolErrorFormatter func(toolName string, err error) interface{}
	// ToolResultFormatter formats successful tool results before they are sent
	// back to the model. It may return a raw string or any JSON-marshable value.
	ToolResultFormatter func(toolName string, result map[string]interface{}) interface{}
}

PromptConfig customizes the tool-call loop's tool-facing prompt content.

func DefaultPromptConfig

func DefaultPromptConfig() PromptConfig

DefaultPromptConfig returns the default prompt content for tool execution.

type Request

type Request struct {
	// Messages for the chat completion
	Messages []Message `json:"messages"`

	// APIKeyOverride overrides the client's configured API key for this request only.
	// This is used to support per-call api_key overrides for parity with the Python SDK.
	APIKeyOverride string `json:"-"`

	// Model to use (overrides default)
	Model string `json:"model,omitempty"`

	// Temperature (0.0 to 2.0)
	Temperature *float64 `json:"temperature,omitempty"`

	// Maximum tokens to generate
	MaxTokens *int `json:"max_tokens,omitempty"`

	// Enable streaming
	Stream bool `json:"stream,omitempty"`

	// Response format for structured outputs
	ResponseFormat *ResponseFormat `json:"response_format,omitempty"`

	// Tools available for the model to call
	Tools []ToolDefinition `json:"tools,omitempty"`

	// ToolChoice controls how the model selects tools ("auto", "none", or specific)
	ToolChoice interface{} `json:"tool_choice,omitempty"`
}

Request represents an AI completion request.

type Response

type Response struct {
	ID      string   `json:"id"`
	Object  string   `json:"object"`
	Created int64    `json:"created"`
	Model   string   `json:"model"`
	Choices []Choice `json:"choices"`
	Usage   *Usage   `json:"usage,omitempty"`
}

Response represents the API response from OpenAI/OpenRouter.

func (*Response) HasToolCalls

func (r *Response) HasToolCalls() bool

HasToolCalls returns true if the response contains tool calls.

func (*Response) Into

func (r *Response) Into(dest interface{}) error

Into is an alias for JSON for ergonomic usage.

func (*Response) JSON

func (r *Response) JSON(dest interface{}) error

JSON parses the response content as JSON into the provided destination.

func (*Response) Text

func (r *Response) Text() string

Text returns the text content from the first choice.

func (*Response) ToolCalls

func (r *Response) ToolCalls() []ToolCall

ToolCalls returns the tool calls from the first choice, or nil.

type ResponseFormat

type ResponseFormat struct {
	Type       string      `json:"type"` // "json_object" or "json_schema"
	JSONSchema *JSONSchema `json:"json_schema,omitempty"`
}

ResponseFormat specifies the desired output format.

type SSEDecoder

type SSEDecoder struct {
	// contains filtered or unexported fields
}

SSEDecoder decodes Server-Sent Events from a stream.

func NewSSEDecoder

func NewSSEDecoder(r io.Reader) *SSEDecoder

NewSSEDecoder creates a new SSE decoder.

func (*SSEDecoder) Decode

func (d *SSEDecoder) Decode() (StreamChunk, error)

Decode reads and decodes the next SSE chunk.

type StreamChunk

type StreamChunk struct {
	ID      string        `json:"id"`
	Object  string        `json:"object"`
	Created int64         `json:"created"`
	Model   string        `json:"model"`
	Choices []StreamDelta `json:"choices"`
}

StreamChunk represents a streaming response chunk.

type StreamDelta

type StreamDelta struct {
	Index        int          `json:"index"`
	Delta        MessageDelta `json:"delta"`
	FinishReason *string      `json:"finish_reason"`
}

StreamDelta represents a delta in a streaming response.

type ToolCall

type ToolCall struct {
	ID       string           `json:"id"`
	Type     string           `json:"type"` // "function"
	Function ToolCallFunction `json:"function"`
}

ToolCall represents a tool call made by the model.

type ToolCallConfig

type ToolCallConfig struct {
	MaxTurns     int
	MaxToolCalls int
	SystemPrompt string
	PromptConfig *PromptConfig
}

ToolCallConfig configures the tool-call execution loop.

func DefaultToolCallConfig

func DefaultToolCallConfig() ToolCallConfig

DefaultToolCallConfig returns default configuration for the tool-call loop.

type ToolCallFunction

type ToolCallFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

ToolCallFunction contains the function name and arguments from a tool call.

type ToolCallRecord

type ToolCallRecord struct {
	ToolName  string
	Arguments map[string]interface{}
	Result    map[string]interface{}
	Error     string
	LatencyMs float64
	Turn      int
}

ToolCallRecord records a single tool call for observability.

type ToolCallResult

type ToolCallResult struct {
	Response *Response
	Trace    *ToolCallTrace
}

ToolCallResult wraps a tool-call response and its execution trace.

func (*ToolCallResult) Text

func (r *ToolCallResult) Text() string

Text returns the final text response from the tool-call loop.

type ToolCallTrace

type ToolCallTrace struct {
	Calls          []ToolCallRecord
	TotalTurns     int
	TotalToolCalls int
	FinalResponse  string
}

ToolCallTrace records the full trace of a tool-call loop.

type ToolDefinition

type ToolDefinition struct {
	Type     string       `json:"type"` // "function"
	Function ToolFunction `json:"function"`
}

ToolDefinition describes a tool available to the model.

func CapabilitiesToToolDefinitions

func CapabilitiesToToolDefinitions(capabilities []types.AgentCapability) []ToolDefinition

CapabilitiesToToolDefinitions converts discovery capabilities to tool definitions.

func CapabilityToToolDefinition

func CapabilityToToolDefinition(cap interface{}) ToolDefinition

CapabilityToToolDefinition converts a ReasonerCapability or SkillCapability to a ToolDefinition.

type ToolFunction

type ToolFunction struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	Parameters  map[string]interface{} `json:"parameters"`
}

ToolFunction describes the function within a tool definition.

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage represents token usage information.

type VideoData

type VideoData struct {
	URL         string  `json:"url,omitempty"`
	Data        string  `json:"data,omitempty"`
	MimeType    string  `json:"mime_type,omitempty"`
	Filename    string  `json:"filename,omitempty"`
	Duration    float64 `json:"duration,omitempty"`
	Resolution  string  `json:"resolution,omitempty"`
	AspectRatio string  `json:"aspect_ratio,omitempty"`
	HasAudio    bool    `json:"has_audio,omitempty"`
	CostUSD     float64 `json:"cost_usd,omitempty"`
}

VideoData holds data for a generated video.

type VideoRequest

type VideoRequest struct {
	Prompt          string           `json:"prompt"`
	Model           string           `json:"model"`
	Duration        int              `json:"duration,omitempty"`
	Resolution      string           `json:"resolution,omitempty"`
	AspectRatio     string           `json:"aspect_ratio,omitempty"`
	GenerateAudio   *bool            `json:"generate_audio,omitempty"`
	Seed            *int             `json:"seed,omitempty"`
	FrameImages     []map[string]any `json:"frame_images,omitempty"`
	InputReferences []map[string]any `json:"input_references,omitempty"`
	PollInterval    time.Duration    `json:"-"`
	Timeout         time.Duration    `json:"-"`
	Extra           map[string]any   `json:"-"`
}

VideoRequest holds parameters for video generation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL