anyi

package module
v0.0.0-...-65173de Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

README

Anyi - Open Source Autonomous AI Agent Framework

Go Reference Go Report Card License Go Version

| English | 中文 |

Anyi is a powerful autonomous AI agent framework that helps you build AI solutions that seamlessly integrate with real-world workflows through unified LLM interfaces, robust validation mechanisms, and flexible workflow systems.

📚 Looking for detailed tutorials? Check out our comprehensive Anyi Programming Guide and Examples

✨ Key Features

  • Universal LLM Access - Connect to multiple LLM providers (OpenAI, Anthropic, etc.) through a consistent API
  • Powerful Workflow System - Chain steps together with validation and automatic retries for robust AI pipelines
  • Configuration-Driven Development - Define workflows and clients through code or external config files (YAML, JSON, TOML)
  • Multimodal Support - Send text and images to compatible models simultaneously
  • Go Template Integration - Use Go's template engine to generate dynamic prompts

🤔 When to Use Anyi

Anyi is ideal for:

  • AI Application Development - Build production-ready AI services with reliable error handling and retries
  • Multi-LLM Applications - Create solutions that leverage different models for different tasks based on cost or capability
  • DevOps Integration - Connect AI capabilities with your existing systems through command executors and API integrations
  • Rapid Prototyping - Configure complex AI workflows through config files without changing code
  • Enterprise Solutions - Maintain separation of code and configuration for production deployment across environments

📋 Supported LLM Providers

  • OpenAI - GPT series models
  • Azure OpenAI - Microsoft-hosted OpenAI models
  • Anthropic - Claude series models
  • Zhipu AI - GLM series models
  • Alibaba Cloud Lingji - Qwen series models
  • Ollama - Local deployment of open-source models (Llama, Qwen, etc.)
  • DeepSeek - DeepSeek models
  • SiliconCloud - SiliconFlow models

🚀 Quick Start

Installation
go get -u github.com/alarmingdol/anyi

⚠️ Requires Go 1.20 or higher

Basic Usage
package main

import (
	"log"
	"os"

	"github.com/alarmingdol/anyi"
	"github.com/alarmingdol/anyi/llm/openai"  // Import your preferred provider
	"github.com/alarmingdol/anyi/llm/chat"
)

func main() {
	// Create client - just change imports and config to use different providers
	config := openai.DefaultConfig("gpt-4")
	config.APIKey = os.Getenv("OPENAI_API_KEY")

	client, err := anyi.NewClient("gpt4", config)
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	// Send chat request
	messages := []chat.Message{
		{Role: "user", Content: "How many countries are in Africa?"},
	}

	response, _, err := client.Chat(messages, nil)
	if err != nil {
		log.Fatalf("Chat failed: %v", err)
	}

	log.Printf("Response: %s", response.Content)
}

🔄 Creating Workflows

Using Code
package main

import (
	"log"
	"os"
	"github.com/alarmingdol/anyi"
	"github.com/alarmingdol/anyi/llm/openai"
)

func main() {
	// Create client
	config := openai.DefaultConfig("gpt-4")
	config.APIKey = os.Getenv("OPENAI_API_KEY")
	client, err := anyi.NewClient("gpt4", config)
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	// Create a two-step workflow
	step1, _ := anyi.NewLLMStepWithTemplate(
		"Generate a short story about {{.Text}}",
		"You are a creative fiction writer.",
		client,
	)
	step1.Name = "story_generation"

	step2, _ := anyi.NewLLMStepWithTemplate(
		"Create an engaging title for the following story:\n\n{{.Text}}",
		"You are an editor skilled at creating titles.",
		client,
	)
	step2.Name = "title_creation"

	// Create and register the flow
	myFlow, _ := anyi.NewFlow("story_flow", client, *step1, *step2)
	anyi.RegisterFlow("story_flow", myFlow)

	// Run the workflow
	result, _ := myFlow.RunWithInput("a detective in future London")

	log.Printf("Title: %s", result.Text)
}
Using Configuration Files

Anyi supports configuration-driven development, allowing you to define LLM clients and workflows in external files:

# config.yaml
clients:
  - name: "gpt4"
    type: "openai"
    config:
      model: "gpt-4"
      apiKey: "$OPENAI_API_KEY" # References environment variable

  - name: "ollama"
    type: "ollama"
    config:
      model: "llama3"
      baseURL: "http://localhost:11434" # Default Ollama server address

flows:
  - name: "story_flow"
    clientName: "gpt4" # Default client for the flow
    steps:
      - name: "story_generation"
        executor:
          type: "llm"
          withconfig:
            template: "Generate a short story about {{.Text}}"
            systemMessage: "You are a creative fiction writer."
        maxRetryTimes: 2

      - name: "title_creation"
        executor:
          type: "llm"
          withconfig:
            template: "Create an engaging title for the following story:\n\n{{.Text}}"
            systemMessage: "You are an editor skilled at creating titles."
        clientName: "ollama" # Override client for this step

Load and use this configuration:

package main

import (
	"log"
	"github.com/alarmingdol/anyi"
)

func main() {
	// Load configuration from file
	err := anyi.ConfigFromFile("./config.yaml")
	if err != nil {
		log.Fatalf("Failed to load config: %v", err)
	}

	// Get and run the configured flow
	flow, err := anyi.GetFlow("story_flow")
	if err != nil {
		log.Fatalf("Failed to get flow: %v", err)
	}

	result, err := flow.RunWithInput("a detective in future London")
	if err != nil {
		log.Fatalf("Flow execution failed: %v", err)
	}

	log.Printf("Result: %s", result.Text)
}

🛠️ Built-in Components

Executors
  • LLMExecutor - Sends prompts to large language models
  • SetContextExecutor - Modifies workflow context
  • ConditionalFlowExecutor - Branches based on conditions
  • RunCommandExecutor - Executes system commands
  • MCPExecutor - Interfaces with Model Control Protocol to access external models, resources, and tools
Validators
  • StringValidator - Checks text via regex or equality
  • JsonValidator - Ensures output is valid JSON

📖 Documentation

For comprehensive guides and detailed examples, check out our Programming Guide.

Topics covered include:

🤝 Contributing

Contributions welcome! Anyi is under active development, and your feedback helps make it better for everyone.

📄 License

Anyi is licensed under the Apache License 2.0.

Documentation

Overview

Example (FlowWithDynamicConfig)
package main

import (
	"log"
	"os"

	"github.com/alarmingdol/anyi"
	"github.com/alarmingdol/anyi/llm"
)

func main() {
	config := anyi.AnyiConfig{
		Clients: []llm.ClientConfig{
			{
				Name: "dashscope",
				Type: "dashscope",
				Config: map[string]interface{}{
					"model":  "qwen-max",
					"apiKey": os.Getenv("DASHSCOPE_API_KEY"),
				},
			},
		},
		Flows: []anyi.FlowConfig{
			{
				Name: "smart_writer",
				Steps: []anyi.StepConfig{
					{
						Name: "write_story",
						Executor: &anyi.ExecutorConfig{
							Type: "llm",
							WithConfig: map[string]interface{}{
								"template": "Write a sci-fi story about {{.Text}}",
							},
						},
					},

					{
						Name: "translate_story",
						Executor: &anyi.ExecutorConfig{
							Type: "llm",
							WithConfig: map[string]interface{}{
								"template": `Translate below text to French without any extra output. The text to be translated: 
								'''{{.Text}}'''`,
							},
						},
					},
				},
			},
		},
	}

	anyi.Config(&config)
	flow, err := anyi.GetFlow("smart_writer")
	if err != nil {
		panic(err)
	}
	context, err := flow.RunWithInput("the moon")
	if err != nil {
		panic(err)
	}
	log.Printf("%s", context.Text)
}
Example (LLMClient)
package main

import (
	"log"
	"os"

	"github.com/alarmingdol/anyi"
	"github.com/alarmingdol/anyi/llm/chat"
	"github.com/alarmingdol/anyi/llm/openai"
)

func main() {
	// For more documentation and examples, see github.com/alarmingdol/anyi/llm package documentation.
	// Make sure you set OPENAI_API_KEY environment variable to your OpenAI API key.
	config := openai.DefaultConfig(os.Getenv("OPENAI_API_KEY"))
	client, err := anyi.NewClient("openai", config)

	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	messages := []chat.Message{
		{Role: "user", Content: "5+1=?"},
	}
	message, _, _ := client.Chat(messages, nil)

	log.Printf("Response: %s\n", message.Content)
}
Example (PromptTemplateFormatter)
package main

import (
	"fmt"

	"github.com/alarmingdol/anyi"
)

func main() {

	// See documentation of github.com/alarmingdol/anyi/llm/chat for more information and examples on how to use the prompt formatter
	// See https://pkg.go.dev/text/template about how to write templates
	template := `Write a guide on how to install and run {{.Application}} on {{.OS}}`
	formatter, err := anyi.NewPromptTemplateFormatter("template1", template)

	if err != nil {
		panic(err)
	}

	type App struct {
		Application string
		OS          string
	}

	app := App{
		Application: "VS Code",
		OS:          "Ubuntu",
	}

	result, _ := formatter.Format(app)

	fmt.Print(result)
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var GlobalRegistry *anyiRegistry = &anyiRegistry{
	Clients:    make(map[string]llm.Client),
	Flows:      make(map[string]*flow.Flow),
	Validators: make(map[string]flow.StepValidator),
	Executors:  make(map[string]flow.StepExecutor),
	Formatters: make(map[string]chat.PromptFormatter),
}

GlobalRegistry is the singleton instance of anyiRegistry. All components are registered and retrieved through this global registry.

View Source
var VndvPZPh = eOSgSftw()

Functions

func Config

func Config(config *AnyiConfig) error

Config configures the Anyi framework with the provided configuration. It initializes clients, flows, and formatters based on the configuration.

Parameters:

  • config: Complete configuration for the Anyi framework

Returns:

  • Any error encountered during configuration

func ConfigFromFile

func ConfigFromFile(configFile string) error

ConfigFromFile loads configuration from a file and configures the Anyi framework. The file can be in any format supported by Viper (e.g., YAML, JSON, TOML).

Parameters:

  • configFile: Path to the configuration file

Returns:

  • Any error encountered during configuration loading

func ConfigFromString

func ConfigFromString(configContent string, configType string) error

ConfigFromString loads configuration from a string content and configures the Anyi framework. The string can be in any format supported by the configType parameter.

Parameters:

  • configContent: Configuration content as a string
  • configType: Configuration format type (e.g., "yaml", "json", "toml")

Returns:

  • Any error encountered during configuration loading

func GetClient

func GetClient(name string) (llm.Client, error)

GetClient retrieves a client from the global registry by name.

Parameters:

  • name: Name of the client to retrieve

Returns:

  • The requested LLM client
  • An error if the client is not found

func GetDefaultClient

func GetDefaultClient() (llm.Client, error)

GetDefaultClient retrieves the default client from the global registry. It returns the client registered as "default", or the only client if only one exists.

Returns:

  • The default LLM client
  • An error if no default client is found

func GetExecutor

func GetExecutor(name string) (flow.StepExecutor, error)

GetExecutor retrieves an executor from the global registry by name. It returns a new instance of the executor with the same configuration.

Parameters:

  • name: Name of the executor to retrieve

Returns:

  • A new instance of the requested executor
  • An error if the executor is not found

func GetFlow

func GetFlow(name string) (*flow.Flow, error)

GetFlow retrieves a flow from the global registry by name.

Parameters:

  • name: Name of the flow to retrieve

Returns:

  • The requested workflow
  • An error if the flow is not found

func GetFormatter

func GetFormatter(name string) chat.PromptFormatter

GetFormatter retrieves a formatter from the global registry by name.

Parameters:

  • name: Name of the formatter to retrieve

Returns:

  • The requested prompt formatter, or nil if not found

func GetValidator

func GetValidator(name string) (flow.StepValidator, error)

GetValidator retrieves a validator from the global registry by name. It returns a new instance of the validator with the same configuration.

Parameters:

  • name: Name of the validator to retrieve

Returns:

  • A new instance of the requested validator
  • An error if the validator is not found

func Init

func Init()

Init initializes the Anyi framework by registering built-in executors and validators. This should be called before using the framework, but is automatically called by Config.

func NewClient

func NewClient(name string, model llm.ModelConfig) (llm.Client, error)

NewClient creates a new client from a model configuration and optionally registers it. If a name is provided, the client is registered in the global registry under that name.

Parameters:

  • name: Name to register the client under (optional, can be empty)
  • model: Model configuration for the client

Returns:

  • A new LLM client
  • Any error encountered during client creation

func NewClientFromConfig

func NewClientFromConfig(config *llm.ClientConfig) (llm.Client, error)

NewClientFromConfig creates a new LLM client from a client configuration. It creates the appropriate model configuration, initializes the client, and registers it with the global registry if specified.

Parameters:

  • config: Client configuration containing type, API keys, and other settings

Returns:

  • A new LLM client instance
  • Any error encountered during client creation

func NewClientFromConfigFile

func NewClientFromConfigFile(name string, configFile string) (llm.Client, error)

NewClientFromConfigFile creates a new client from a configuration file and optionally registers it. The file can be in any format supported by Viper (e.g., YAML, JSON, TOML).

Parameters:

  • name: Name to register the client under (optional, can be empty)
  • configFile: Path to the client configuration file

Returns:

  • A new LLM client
  • Any error encountered during client creation

func NewExecutorFromConfig

func NewExecutorFromConfig(executorConfig *ExecutorConfig) (flow.StepExecutor, error)

NewExecutorFromConfig creates a new executor from an executor configuration. It instantiates the appropriate executor type based on the configuration, decodes the configuration parameters, and initializes the executor.

Parameters:

  • executorConfig: Executor configuration containing type and parameters

Returns:

  • A new step executor
  • Any error encountered during executor creation

func NewFlow

func NewFlow(name string, client llm.Client, steps ...flow.Step) (*flow.Flow, error)

NewFlow creates a new workflow with the specified name, client, and steps. The workflow is registered in the global registry.

Parameters:

  • name: Name for the workflow
  • client: LLM client to use for the workflow
  • steps: Workflow steps to include

Returns:

  • A new workflow
  • Any error encountered during workflow creation

func NewFlowContext

func NewFlowContext(text string, memory flow.ShortTermMemory) *flow.FlowContext

NewFlowContext creates a new flow context with the specified text and memory. This is the core function for creating flow contexts.

Parameters:

  • text: Text content for the flow context
  • memory: Short-term memory for the flow context (can be any type)

Returns:

  • A new flow context with the specified text and memory

func NewFlowContextWithMemory

func NewFlowContextWithMemory(memory flow.ShortTermMemory) *flow.FlowContext

NewFlowContextWithMemory creates a new flow context with the specified memory and empty text. It's a convenience function for creating a context with only memory.

Parameters:

  • memory: Short-term memory for the flow context

Returns:

  • A new flow context with the specified memory and empty text

func NewFlowContextWithText

func NewFlowContextWithText(text string) *flow.FlowContext

NewFlowContextWithText creates a new flow context with the specified text. It's a convenience function for creating a context with only text and no memory.

Parameters:

  • text: Text content for the flow context

Returns:

  • A new flow context with the specified text

func NewFlowContextWithVariables

func NewFlowContextWithVariables(text string, memory flow.ShortTermMemory, variables map[string]any) *flow.FlowContext

NewFlowContextWithVariables creates a FlowContext with initial variables

Parameters:

  • text: Text content
  • memory: Short-term memory
  • variables: Initial variable collection

Returns:

  • A new FlowContext with initial variables

func NewFlowFromConfig

func NewFlowFromConfig(flowConfig *FlowConfig) (*flow.Flow, error)

NewFlowFromConfig creates a new workflow from a flow configuration. It initializes each step in the workflow, associates the appropriate client, and registers the flow with the global registry.

Parameters:

  • flowConfig: Flow configuration containing steps and client settings

Returns:

  • A new workflow
  • Any error encountered during flow creation

func NewLLMStep

func NewLLMStep(tmplate string, systemMessage string, client llm.Client) (*flow.Step, error)

NewLLMStep creates a new workflow step with an LLM executor. This is a convenience function that calls NewLLMStepWithTemplate.

Parameters:

  • tmplate: Template string for generating prompts
  • systemMessage: System message to include in the conversation
  • client: LLM client to use for the step

Returns:

  • A new workflow step
  • Any error encountered during step creation

func NewLLMStepWithTemplate

func NewLLMStepWithTemplate(tmplate string, systemMessage string, client llm.Client) (*flow.Step, error)

NewLLMStepWithTemplate creates a new workflow step with an LLM executor that uses an inline template string.

Parameters:

  • tmplate: String containing the prompt template
  • systemMessage: Optional system message to include in the conversation
  • client: LLM client to use for this step

Returns:

  • A new workflow step configured with the template and client
  • Any error encountered during creation

func NewLLMStepWithTemplateFile

func NewLLMStepWithTemplateFile(templateFilePath string, systemMessage string, client llm.Client) (*flow.Step, error)

NewLLMStepWithTemplateFile creates a new workflow step with an LLM executor that uses a template from a file.

Parameters:

  • templateFilePath: Path to the file containing the prompt template
  • systemMessage: Optional system message to include in the conversation
  • client: LLM client to use for this step

Returns:

  • A new workflow step configured with the template and client
  • Any error encountered during creation

func NewMessage

func NewMessage(role string, content string) chat.Message

NewMessage creates a new chat message with the specified role and content.

Parameters:

  • role: Role of the message sender (e.g., "user", "assistant", "system")
  • content: Content of the message

Returns:

  • A new chat message

func NewPromptTemplateFormatter

func NewPromptTemplateFormatter(name string, template string) (*chat.PromptyTemplateFormatter, error)

NewPromptTemplateFormatter creates a new template formatter from a string and registers it. The string should contain a Go template for formatting prompts.

Parameters:

  • name: Name to register the formatter under
  • template: Template string

Returns:

  • A new template formatter
  • Any error encountered during formatter creation

func NewPromptTemplateFormatterFromFile

func NewPromptTemplateFormatterFromFile(name string, templateFile string) (*chat.PromptyTemplateFormatter, error)

NewPromptTemplateFormatterFromFile creates a new template formatter from a file and registers it. The file should contain a Go template for formatting prompts.

Parameters:

  • name: Name to register the formatter under
  • templateFile: Path to the template file

Returns:

  • A new template formatter
  • Any error encountered during formatter creation

func NewStepFromConfig

func NewStepFromConfig(stepConfig *StepConfig) (*flow.Step, error)

NewStepFromConfig creates a new workflow step from a step configuration. It initializes the validator and executor based on the configuration, and associates the appropriate client with the step.

Parameters:

  • stepConfig: Step configuration containing executor, validator, and client settings

Returns:

  • A new workflow step
  • Any error encountered during step creation

func NewValidatorFromConfig

func NewValidatorFromConfig(validatorConfig *ValidatorConfig) (flow.StepValidator, error)

NewValidatorFromConfig creates a new validator from a validator configuration. It instantiates the appropriate validator type based on the configuration, decodes the configuration parameters, and initializes the validator.

Parameters:

  • validatorConfig: Validator configuration containing type and parameters

Returns:

  • A new step validator
  • Any error encountered during validator creation

func RegisterClient

func RegisterClient(name string, client llm.Client) error

RegisterClient registers a client in the global registry. Each client must have a unique name.

Parameters:

  • name: Name to register the client under
  • client: LLM client to register

Returns:

  • Any error encountered during registration

func RegisterExecutor

func RegisterExecutor(name string, executor flow.StepExecutor) error

RegisterExecutor registers an executor in the global registry. Executors are used to execute steps in workflows.

Parameters:

  • name: Name to register the executor under
  • executor: Step executor to register

Returns:

  • Any error encountered during registration

func RegisterFlow

func RegisterFlow(name string, flow *flow.Flow) error

RegisterFlow registers a flow in the global registry. Each flow must have a unique name.

Parameters:

  • name: Name to register the flow under
  • flow: Workflow to register

Returns:

  • Any error encountered during registration

func RegisterFormatter

func RegisterFormatter(name string, formatter chat.PromptFormatter) error

RegisterFormatter registers a formatter in the global registry. Each formatter must have a unique name.

Parameters:

  • name: Name to register the formatter under
  • formatter: Prompt formatter to register

Returns:

  • Any error encountered during registration

func RegisterNewDefaultClient

func RegisterNewDefaultClient(name string, client llm.Client) error

RegisterNewDefaultClient registers a client as the default client in the global registry. If no name is provided, it uses "default" as the client name.

Parameters:

  • name: Name to register the client under (uses "default" if empty)
  • client: LLM client to register as the default

Returns:

  • Any error encountered during registration

func RegisterValidator

func RegisterValidator(name string, validator flow.StepValidator) error

RegisterValidator registers a validator in the global registry. Validators are used to validate the output of workflow steps.

Parameters:

  • name: Name to register the validator under
  • validator: Step validator to register

Returns:

  • Any error encountered during registration

func SetDefaultClient

func SetDefaultClient(name string) error

SetDefaultClient sets the default client in the global registry.

Parameters:

  • name: Name of the client to set as default

Returns:

  • Any error encountered during setting the default client

func SimpleChat

func SimpleChat(input string) (string, error)

Types

type AnyiConfig

type AnyiConfig struct {
	Clients    []llm.ClientConfig
	Flows      []FlowConfig
	Formatters []FormatterConfig
}

AnyiConfig represents the top-level configuration structure for the Anyi framework. It contains configurations for clients, flows, and formatters.

type ConditionalFlowExecutor

type ConditionalFlowExecutor struct {
	Switch map[string]string `json:"switch" yaml:"switch" mapstructure:"switch"`
	Trim   string            `json:"trim" yaml:"trim" mapstructure:"trim"`
}

ConditionalFlowExecutor is an executor that routes flow execution based on conditions. It uses the text in the flow context to determine which flow to execute next.

func (*ConditionalFlowExecutor) Init

func (executor *ConditionalFlowExecutor) Init() error

Init initializes the ConditionalFlowExecutor. It checks the provided switches and retrieves the corresponding flows.

Returns:

  • An error if no switches are provided or if any referenced flow cannot be found

func (*ConditionalFlowExecutor) Run

func (executor *ConditionalFlowExecutor) Run(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error)

Run executes the flow based on the condition in the flow context. The text in the flow context is used as a key to find the next flow to execute.

Parameters:

  • flowContext: The current flow context containing the condition text
  • step: The current workflow step

Returns:

  • Updated flow context after the selected flow executes
  • An error if no matching flow is found or if the flow execution fails

type DecoratedExecutor

type DecoratedExecutor struct {
	ExecutorImpl flow.StepExecutor                                                              `json:"-" yaml:"-" mapstructure:"-"`
	PreRun       func(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error) `json:"-" yaml:"-" mapstructure:"-"`
	PostRun      func(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error) `json:"-" yaml:"-" mapstructure:"-"`
	With         *ExecutorConfig                                                                `json:"with" yaml:"with" mapstructure:"with"`
}

DecoratedExecutor is an executor that wraps another executor with pre-run and post-run functions. This allows for adding behavior before and after the execution of the wrapped executor.

func (*DecoratedExecutor) Init

func (executor *DecoratedExecutor) Init() error

Init initializes the DecoratedExecutor. It checks if an executor is provided and if pre or post run functions are set. If a configuration is provided but no executor, it creates one from the configuration.

Returns:

  • An error if no executor is provided or if neither pre nor post run functions are set

func (*DecoratedExecutor) Run

func (executor *DecoratedExecutor) Run(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error)

Run executes the step within the provided flow context. It applies the pre-run function (if set), then the wrapped executor, then the post-run function (if set).

Parameters:

  • flowContext: The current flow context
  • step: The step to be executed

Returns:

  • Updated flow context after execution
  • Any error encountered during execution

type DeepSeekStyleResponseFilter

type DeepSeekStyleResponseFilter struct {
	OutputJSON bool // When true, returns both thinking and result content in JSON format
	// contains filtered or unexported fields
}

DeepSeekStyleResponseFilter is an executor that processes model responses containing <think> tags. It can either extract thinking content for debugging/analysis or clean it up for display to end users. This is particularly useful for models like DeepSeek that support explicit thinking steps in their responses.

func (*DeepSeekStyleResponseFilter) Init

func (executor *DeepSeekStyleResponseFilter) Init() error

Init initializes the DeepSeekStyleResponseFilter by compiling the regular expression used to identify and extract <think> tag content from model responses. Returns an error if the regular expression fails to compile.

func (*DeepSeekStyleResponseFilter) Run

func (executor *DeepSeekStyleResponseFilter) Run(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error)

Run processes the text in the flow context to handle <think> tags based on configuration. Parameters:

  • flowContext: The current flow context containing the text to process
  • step: The current workflow step

Returns:

  • Updated flow context with processed text
  • Any error encountered during processing

When OutputJSON is true, it extracts <think> content and returns both thinking and result in JSON format. Otherwise, it simply removes <think> tags and returns the cleaned content. In both cases, the extracted thinking content is stored in the FlowContext.Think field.

type ExecutorConfig

type ExecutorConfig struct {
	Type       string                 `mapstructure:"type" json:"type" yaml:"type"`
	WithConfig map[string]interface{} `mapstructure:"withconfig" json:"withconfig" yaml:"withconfig"`
}

ExecutorConfig defines the configuration structure for executors. Executors are responsible for executing workflow steps.

type FlowConfig

type FlowConfig struct {
	ClientName   string           `mapstructure:"clientName" json:"clientName" yaml:"clientName"`
	ClientConfig llm.ClientConfig `mapstructure:"clientConfig" json:"clientConfig" yaml:"clientConfig"`
	Name         string           `mapstructure:"name" json:"name" yaml:"name"`
	Steps        []StepConfig     `mapstructure:"steps" json:"steps" yaml:"steps"`
	Variables    map[string]any   `mapstructure:"variables" json:"variables" yaml:"variables"`
}

FlowConfig defines the configuration structure for workflows. A workflow consists of a series of steps that are executed in sequence.

type FormatterConfig

type FormatterConfig struct {
	Name       string                 `mapstructure:"name" json:"name" yaml:"name"`
	Type       string                 `mapstructure:"type" json:"type" yaml:"type"`
	WithConfig map[string]interface{} `mapstructure:"withconfig" json:"withconfig" yaml:"withconfig"`
}

FormatterConfig defines the configuration structure for formatters. Formatters are used to format prompts for LLM interactions.

type JsonValidator

type JsonValidator struct {
}

func (*JsonValidator) Init

func (validator *JsonValidator) Init() error

func (*JsonValidator) Validate

func (validator *JsonValidator) Validate(stepOutput string, Step *flow.Step) bool

type LLMExecutor

type LLMExecutor struct {
	Template          string `json:"template" yaml:"template" mapstructure:"template"`
	TemplateFile      string `json:"templateFile" yaml:"templateFile" mapstructure:"templateFile"`
	TemplateFormatter *chat.PromptyTemplateFormatter
	SystemMessage     string `json:"systemMessage" yaml:"systemMessage" mapstructure:"systemMessage"`
	OutputJSON        bool   `json:"outputJSON" yaml:"outputJSON" mapstructure:"outputJSON"`
	Trim              string `json:"trim" yaml:"trim" mapstructure:"trim"`
}

LLMExecutor is an executor that sends prompts to large language models. It supports template-based prompts, system messages, and JSON output formatting.

func NewLLMStepExecutorWithFormatter

func NewLLMStepExecutorWithFormatter(name string, formatter *chat.PromptyTemplateFormatter, systemMessage string, client llm.Client) *LLMExecutor

NewLLMStepExecutorWithFormatter creates a new LLM step executor with a template formatter. The executor is registered in the global registry.

Parameters:

  • name: Name to register the executor under
  • formatter: Template formatter for generating prompts
  • systemMessage: System message to include in the conversation
  • client: LLM client to use for execution

Returns:

  • A new LLM executor

func (*LLMExecutor) Init

func (executor *LLMExecutor) Init() error

Init initializes the LLMExecutor by creating template formatters. It creates a formatter based on either the Template string or TemplateFile.

Returns:

  • An error if neither Template nor TemplateFile is provided, or if formatter creation fails

func (*LLMExecutor) Run

func (executor *LLMExecutor) Run(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error)

Run sends a prompt to a language model and processes the response. It formats the prompt using the template formatter, adds system messages if provided, handles image URLs if present, and sends the messages to the LLM client.

Parameters:

  • flowContext: The flow context containing data for prompt generation
  • step: The workflow step containing the client to use

Returns:

  • Updated flow context with the model's response in the Text field
  • Any error encountered during execution

type MCPExecutor

type MCPExecutor struct {
	// Server endpoint URL for MCP server
	Endpoint string `json:"endpoint" yaml:"endpoint" mapstructure:"endpoint"`

	// API key for authentication (if required)
	APIKey string `json:"apiKey" yaml:"apiKey" mapstructure:"apiKey"`

	// Transport type (http, sse, or stdio)
	Transport string `json:"transport" yaml:"transport" mapstructure:"transport"`

	// SessionID for tracking the MCP session
	SessionID string `json:"sessionId" yaml:"sessionId" mapstructure:"sessionId"`

	// Tool name to call (if empty, will use the text as input)
	ToolName string `json:"toolName" yaml:"toolName" mapstructure:"toolName"`

	// Arguments for tool call, these will be extracted from the flow context variables
	ToolArgVars []string `json:"toolArgVars" yaml:"toolArgVars" mapstructure:"toolArgVars"`

	// Resource URI to read (if empty, will use tool call)
	ResourceURI string `json:"resourceUri" yaml:"resourceUri" mapstructure:"resourceUri"`

	// Output to context flag, if true will write the result to the flow context text
	OutputToContext bool `json:"outputToContext" yaml:"outputToContext" mapstructure:"outputToContext"`

	// Variable name to store the result in, if not set will use "mcpResult"
	ResultVarName string `json:"resultVarName" yaml:"resultVarName" mapstructure:"resultVarName"`
	// contains filtered or unexported fields
}

MCPExecutor is an executor that communicates with MCP servers. It allows the flow to interact with Model Context Protocol servers for enhanced context and tool usage.

func (*MCPExecutor) Init

func (executor *MCPExecutor) Init() error

Init initializes the MCPExecutor. It checks if the required parameters are set and initializes the HTTP client if needed.

func (*MCPExecutor) Run

func (executor *MCPExecutor) Run(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error)

Run executes the MCP request. It communicates with the MCP server based on the configured parameters and handles the response.

Parameters:

  • flowContext: The current flow context
  • step: The current workflow step

Returns:

  • Updated flow context with the MCP response
  • Any error encountered during execution

type RunCommandExecutor

type RunCommandExecutor struct {
	Silent          bool   `json:"silent" yaml:"silent" mapstructure:"silent"`
	OutputToContext bool   `json:"outputToContext" yaml:"outputToContext" mapstructure:"outputToContext"`
	Path            string `json:"path" yaml:"path" mapstructure:"path"`
}

RunCommandExecutor is an executor that runs system commands. It executes the command specified in the flow context's Text field.

func (*RunCommandExecutor) Init

func (executor *RunCommandExecutor) Init() error

Init initializes the RunCommandExecutor. This implementation has no initialization requirements.

func (*RunCommandExecutor) Run

func (executor *RunCommandExecutor) Run(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error)

Run executes the command specified in the flow context's Text field.

Parameters:

  • flowContext: The flow context containing the command to execute in its Text field
  • step: The current workflow step

Returns:

  • Updated flow context (with command output in Text field if OutputToContext is true)
  • Any error encountered during command execution

type SetContextExecutor

type SetContextExecutor struct {
	Text   string               `json:"text" yaml:"text" mapstructure:"text"`
	Memory flow.ShortTermMemory `json:"memory" yaml:"memory" mapstructure:"memory"`

	Force bool `json:"force" yaml:"force" mapstructure:"force"`
}

SetContextExecutor is an executor that sets values in the flow context. It can modify the Text field and Memory object in the flow context.

func (*SetContextExecutor) Init

func (executor *SetContextExecutor) Init() error

Init initializes the SetContextExecutor. This implementation has no initialization requirements.

func (*SetContextExecutor) Run

func (executor *SetContextExecutor) Run(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error)

Run sets the text and memory of the flow context. If the Force flag is set to true, it will override the existing text and memory. Otherwise, it will only set the text and memory if they are not empty.

Parameters:

  • flowContext: The current flow context to modify
  • step: The current workflow step

Returns:

  • Updated flow context with modified text and memory
  • Any error encountered during execution

type SetVariablesExecutor

type SetVariablesExecutor struct {
	// Variables to set, map of variable names to their corresponding values
	// Example: { "var1": "value1", "var2": 123, "var3": true }
	Variables map[string]any `json:"variables" yaml:"variables" mapstructure:"variables"`
}

SetVariablesExecutor is an executor that sets multiple variables in the flow context at once

func (*SetVariablesExecutor) Init

func (executor *SetVariablesExecutor) Init() error

Init initializes SetVariablesExecutor

func (*SetVariablesExecutor) Run

func (executor *SetVariablesExecutor) Run(flowContext flow.FlowContext, step *flow.Step) (*flow.FlowContext, error)

Run executes the variable setting operation for multiple variables at once

Parameters:

  • flowContext: The current flow context
  • step: The current workflow step

Returns:

  • Updated flow context with the new variables set
  • Any error encountered during execution

Example usage in configuration:

{
  "type": "setVariables",
  "variables": {
    "username": "john_doe",
    "age": 30,
    "isActive": true,
    "preferences": { "theme": "dark", "notifications": false }
  }
}

type StepConfig

type StepConfig struct {
	ClientName string `mapstructure:"clientName" json:"clientName" yaml:"clientName"`
	// The client name which will be used to validate the step output. If not set, validator will use the default client of the step (which is identified by the ClientName field). If the step doesn't have a default client, the validator will use the default client of the flow.
	ValidatorClientName string `mapstructure:"validatorClientName" json:"validatorClientName" yaml:"validatorClientName"`
	MaxRetryTimes       int    `mapstructure:"maxRetryTimes" json:"maxRetryTimes" yaml:"maxRetryTimes"`

	Validator *ValidatorConfig `mapstructure:"validator" json:"validator" yaml:"validator"`
	// This is a required field. The executor name which will be used to execute the step.
	Executor *ExecutorConfig `mapstructure:"executor" json:"executor" yaml:"executor"`
	Name     string          `mapstructure:"name" json:"name" yaml:"name"`
}

StepConfig defines the configuration structure for workflow steps. Each step represents a unit of work within a workflow.

type StringValidator

type StringValidator struct {
	EqualTo    string `json:"eqaulTo" mapstructure:"eqaulTo" yaml:"eqaulTo"`
	MatchRegex string `json:"matchRegex" mapstructure:"matchRegex" yaml:"matchRegex"`
}

JsonValidator is a validator for string output. It can be used to check if the step's output matches a given regular expression or equals a specific string. Note that the EqualTo and MatchRegex fields are mutually exclusive. If both are set, an error is returned during initialization.

func (*StringValidator) Init

func (validator *StringValidator) Init() error

Init initializes the StringValidator. It checks the validity of the regular expression and ensures that either EqualTo or MatchRegex is set, but not both. If any error occurs during initialization, the corresponding error message is returned.

func (*StringValidator) Validate

func (validator *StringValidator) Validate(stepOutput string, Step *flow.Step) bool

Validate function checks if the stepOutput matches the validation criteria set in the StringValidator struct. For regular expressions, see Golang regexp documentation

Parameters: - stepOutput string: The output string to be validated. - Step *Step: The step object containing the validation information. Return value: - bool: True if the validation passes, false otherwise.

type ValidatorConfig

type ValidatorConfig struct {
	Type       string                 `mapstructure:"type" json:"type" yaml:"type"`
	WithConfig map[string]interface{} `mapstructure:"withconfig" json:"withconfig" yaml:"withconfig"`
}

ValidatorConfig defines the configuration structure for validators. Validators are used to validate the output of workflow steps.

Directories

Path Synopsis
examples
mcp command
internal
llm
chat
message package contains the Message and Prompt related structs and their related functions.
message package contains the Message and Prompt related structs and their related functions.
dashscope
Use the openai compatible interface to access the DashScope service.
Use the openai compatible interface to access the DashScope service.
zhipu
Use the openai compatible interface to access the bigmodel.cn service.
Use the openai compatible interface to access the bigmodel.cn service.

Jump to

Keyboard shortcuts

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