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)
}
Output:
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)
}
Output:
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)
}
Output:
Index ¶
- Variables
- func Config(config *AnyiConfig) error
- func ConfigFromFile(configFile string) error
- func ConfigFromString(configContent string, configType string) error
- func GetClient(name string) (llm.Client, error)
- func GetDefaultClient() (llm.Client, error)
- func GetExecutor(name string) (flow.StepExecutor, error)
- func GetFlow(name string) (*flow.Flow, error)
- func GetFormatter(name string) chat.PromptFormatter
- func GetValidator(name string) (flow.StepValidator, error)
- func Init()
- func NewClient(name string, model llm.ModelConfig) (llm.Client, error)
- func NewClientFromConfig(config *llm.ClientConfig) (llm.Client, error)
- func NewClientFromConfigFile(name string, configFile string) (llm.Client, error)
- func NewExecutorFromConfig(executorConfig *ExecutorConfig) (flow.StepExecutor, error)
- func NewFlow(name string, client llm.Client, steps ...flow.Step) (*flow.Flow, error)
- func NewFlowContext(text string, memory flow.ShortTermMemory) *flow.FlowContext
- func NewFlowContextWithMemory(memory flow.ShortTermMemory) *flow.FlowContext
- func NewFlowContextWithText(text string) *flow.FlowContext
- func NewFlowContextWithVariables(text string, memory flow.ShortTermMemory, variables map[string]any) *flow.FlowContext
- func NewFlowFromConfig(flowConfig *FlowConfig) (*flow.Flow, error)
- func NewLLMStep(tmplate string, systemMessage string, client llm.Client) (*flow.Step, error)
- func NewLLMStepWithTemplate(tmplate string, systemMessage string, client llm.Client) (*flow.Step, error)
- func NewLLMStepWithTemplateFile(templateFilePath string, systemMessage string, client llm.Client) (*flow.Step, error)
- func NewMessage(role string, content string) chat.Message
- func NewPromptTemplateFormatter(name string, template string) (*chat.PromptyTemplateFormatter, error)
- func NewPromptTemplateFormatterFromFile(name string, templateFile string) (*chat.PromptyTemplateFormatter, error)
- func NewStepFromConfig(stepConfig *StepConfig) (*flow.Step, error)
- func NewValidatorFromConfig(validatorConfig *ValidatorConfig) (flow.StepValidator, error)
- func RegisterClient(name string, client llm.Client) error
- func RegisterExecutor(name string, executor flow.StepExecutor) error
- func RegisterFlow(name string, flow *flow.Flow) error
- func RegisterFormatter(name string, formatter chat.PromptFormatter) error
- func RegisterNewDefaultClient(name string, client llm.Client) error
- func RegisterValidator(name string, validator flow.StepValidator) error
- func SetDefaultClient(name string) error
- func SimpleChat(input string) (string, error)
- type AnyiConfig
- type ConditionalFlowExecutor
- type DecoratedExecutor
- type DeepSeekStyleResponseFilter
- type ExecutorConfig
- type FlowConfig
- type FormatterConfig
- type JsonValidator
- type LLMExecutor
- type MCPExecutor
- type RunCommandExecutor
- type SetContextExecutor
- type SetVariablesExecutor
- type StepConfig
- type StringValidator
- type ValidatorConfig
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
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.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
mcp
command
|
|
|
internal
|
|
|
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. |