📚 For the most up-to-date and comprehensive API documentation, visit: pkg.go.dev/github.com/jieliu2000/anyi
This document provides a comprehensive reference for the Anyi framework's public API. It covers the core interfaces, methods, and data structures you'll use when building AI applications.
The Client interface is the primary way to interact with LLM providers.
type Client interface {
Chat(messages []chat.Message, options *chat.Options) (*chat.Message, chat.ResponseInfo, error)
GetProvider() string
GetModel() string
}Chat(messages []chat.Message, options *chat.Options) (*chat.Message, chat.ResponseInfo, error)Sends a chat request to the LLM provider.
Parameters:
messages: Array of chat messages forming the conversationoptions: Optional configuration for the request
Returns:
*chat.Message: The response message from the LLMchat.ResponseInfo: Metadata about the response (tokens used, model info, etc.)error: Error if the request fails
GetProvider() stringReturns the name of the LLM provider (e.g., "openai", "anthropic").
GetModel() stringReturns the model name being used (e.g., "gpt-4", "claude-3-opus").
Represents a single message in a conversation.
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
Images []string `json:"images,omitempty"`
Function *Function `json:"function,omitempty"`
}Fields:
Role: The role of the message sender ("user", "assistant", "system")Content: The text content of the messageImages: Array of image URLs for multimodal messagesFunction: Function call information (for function calling)
Configuration options for chat requests.
type Options struct {
Temperature *float64 `json:"temperature,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"`
PresencePenalty *float64 `json:"presence_penalty,omitempty"`
Stop []string `json:"stop,omitempty"`
Functions []Function `json:"functions,omitempty"`
}Fields:
Temperature: Controls randomness (0.0-2.0, default varies by provider)MaxTokens: Maximum number of tokens to generateTopP: Nucleus sampling parameter (0.0-1.0)FrequencyPenalty: Penalty for token frequency (-2.0 to 2.0)PresencePenalty: Penalty for token presence (-2.0 to 2.0)Stop: Array of stop sequencesFunctions: Available functions for function calling
Contains metadata about the LLM response.
type ResponseInfo struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
Model string `json:"model"`
Provider string `json:"provider"`
}Fields:
PromptTokens: Number of tokens in the inputCompletionTokens: Number of tokens in the responseTotalTokens: Total tokens used (prompt + completion)Model: The model that generated the responseProvider: The provider that handled the request
func NewClient(name string, config interface{}) (Client, error)Creates a new named client and registers it in the global registry.
Parameters:
name: Unique name for the clientconfig: Provider-specific configuration
Returns:
Client: The created client instanceerror: Error if client creation fails
func GetClient(name string) (Client, error)Retrieves a previously registered client by name.
Parameters:
name: Name of the client to retrieve
Returns:
Client: The client instanceerror: Error if client not found
func ListClients() []stringReturns a list of all registered client names.
func Config(config *AnyiConfig) errorApplies a configuration to set up clients and flows.
Parameters:
config: Configuration structure containing clients and flows
Returns:
error: Error if configuration fails
func ConfigFromFile(filename string) errorLoads configuration from a file (supports YAML, JSON, TOML).
Parameters:
filename: Path to the configuration file
Returns:
error: Error if loading fails
type Flow interface {
Run() (*FlowContext, error)
RunWithInput(input interface{}) (*FlowContext, error)
GetName() string
}Run() (*FlowContext, error)Executes the flow without initial input.
RunWithInput(input interface{}) (*FlowContext, error)Executes the flow with the provided input.
Parameters:
input: Initial input for the flow (string or structured data)
Returns:
*FlowContext: The final flow context after executionerror: Error if flow execution fails
GetName() stringReturns the name of the flow.
func GetFlow(name string) (Flow, error)Retrieves a configured flow by name.
Parameters:
name: Name of the flow to retrieve
Returns:
Flow: The flow instanceerror: Error if flow not found
type Step struct {
Name string
ClientName string
Executor Executor
Validator Validator
MaxRetryTimes int
VarsImmutable bool
TextImmutable bool
MemoryImmutable bool
}Fields:
Name: Step identifierClientName: Client to use for this stepExecutor: Executor instanceValidator: Validator instanceMaxRetryTimes: Maximum retry attemptsVarsImmutable: When true, prevents modification of context variables during step executionTextImmutable: When true, prevents modification of context text during step executionMemoryImmutable: When true, prevents modification of context memory during step execution
type FlowContext struct {
Text string
Memory interface{}
Think string
Images []string
}Fields:
Text: Current text content being processedMemory: Structured data for complex workflowsThink: Extracted thinking process from LLM responsesImages: Array of image URLs for multimodal processing
func NewFlowContext(text string) *FlowContextCreates a new flow context with initial text.
func NewFlowContextWithMemory(memory interface{}) *FlowContextCreates a new flow context with structured memory data.
The framework defines several error types for different scenarios:
ErrClientNotFound: Returned when a requested client is not registeredErrFlowNotFound: Returned when a requested flow is not foundErrInvalidConfig: Returned when configuration is invalidErrProviderNotSupported: Returned when an unsupported provider is specified
type Config struct {
APIKey string
Model string
BaseURL string
OrgID string
Temperature *float64
MaxTokens *int
}type Config struct {
APIKey string
Model string
BaseURL string
Version string
Temperature *float64
MaxTokens *int
}type Config struct {
Model string
BaseURL string
Options map[string]interface{}
}type Function struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"`
}Fields:
Name: Function nameDescription: Human-readable descriptionParameters: JSON Schema defining the function parameters
Always check for errors when calling API methods:
client, err := anyi.GetClient("my-client")
if err != nil {
log.Fatalf("Failed to get client: %v", err)
}
response, info, err := client.Chat(messages, options)
if err != nil {
log.Printf("Chat failed: %v", err)
return
}- Reuse clients when possible to avoid unnecessary initialization overhead
- Use appropriate timeouts for long-running operations
- Implement proper retry logic for transient failures
- Use environment variables for sensitive information like API keys
- Validate configuration before using it in production
- Use structured configuration files for complex setups
This API reference covers the essential interfaces and functions you'll use when working with Anyi. For implementation examples and tutorials, see the other documentation sections.