### Complete Callback Setup Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/callbacks-reference.md A comprehensive example demonstrating the implementation of a `SimpleLoggingHandler` and its integration into a runnable's invocation. ```go package main import ( "context" "log" "time" "github.com/cloudwego/eino/callbacks" "github.com/cloudwego/eino/compose" ) type SimpleLoggingHandler struct{} func (h *SimpleLoggingHandler) OnStart(ctx context.Context, info *callbacks.StartInfo) context.Context { log.Printf("[%s] START: %s", time.Now().Format("15:04:05"), info.ComponentName) return ctx } func (h *SimpleLoggingHandler) OnEnd(ctx context.Context, info *callbacks.EndInfo) { duration := info.EndTime.Sub(info.StartTime).Milliseconds() log.Printf("[%s] END: %s (took %dms)", time.Now().Format("15:04:05"), info.ComponentName, duration) } func (h *SimpleLoggingHandler) OnError(ctx context.Context, info *callbacks.ErrorInfo) { log.Printf("[%s] ERROR: %s: %v", time.Now().Format("15:04:05"), info.ComponentName, info.Error) } func (h *SimpleLoggingHandler) OnStartWithStreamInput(ctx context.Context, info *callbacks.StartInfo) context.Context { return h.OnStart(ctx, info) } func (h *SimpleLoggingHandler) OnEndWithStreamOutput(ctx context.Context, info *callbacks.StreamOutputInfo) { log.Printf("[%s] STREAM: %s produced chunk", time.Now().Format("15:04:05"), info.ComponentName) } func main() { ctx := context.Background() graph := compose.NewGraph[string, string]() // ... setup graph ... runnable, _ := graph.Compile(ctx) handler := &SimpleLoggingHandler{} output, err := runnable.Invoke(ctx, "input", compose.WithCallbacks(handler), ) if err != nil { log.Printf("Execution failed: %v", err) } else { log.Printf("Result: %v", output) } } ``` -------------------------------- ### OnStart Callback Implementation Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/callbacks-reference.md An example implementation of the OnStart method for a custom handler, logging the start of a component. ```go func (h *MyHandler) OnStart(ctx context.Context, info *StartInfo) context.Context { log.Printf("Starting %s.%s", info.ComponentType, info.ComponentName) return ctx } ``` -------------------------------- ### Complete Agent Setup Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Demonstrates the complete setup and execution of a chat model agent with tools. Ensure necessary API keys and tool configurations are provided. ```go package main import ( "context" "fmt" "os" "github.com/cloudwego/eino/adk" "github.com/cloudwego/eino/compose" "github.com/cloudwego/eino-ext/components/model/openai" "github.com/cloudwego/eino-ext/components/tool" ) func main() { ctx := context.Background() // Create chat model chatModel, _ := openai.NewChatModel(ctx, &openai.ChatModelConfig{ Model: "gpt-4o", APIKey: os.Getenv("OPENAI_API_KEY"), }) // Create tools weatherTool := /* ... */ // Create agent agent, _ := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{ Model: chatModel, Instruction: "You are a weather assistant", ToolsConfig: adk.ToolsConfig{ ToolsNodeConfig: compose.ToolsNodeConfig{ Tools: []tool.BaseTool{weatherTool}, }, }, }) // Create runner and execute runner := adk.NewRunner(ctx, adk.RunnerConfig{Agent: agent}) iter := runner.Query(ctx, "What is the weather in SF?") for { event, ok := iter.Next() if !ok { break } if event.Output != nil { fmt.Println(event.Output.Content) } } } ``` -------------------------------- ### Document Creation Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md An example demonstrating how to create and initialize a Document object. ```go doc := &schema.Document{ Content: "Eino is an LLM framework...", MetaData: map[string]any{ "source": "README", "page": 1, }, } ``` -------------------------------- ### Component Configuration Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md An example showing how to use functional options to configure a component during generation. ```go response, err := model.Generate(ctx, messages, WithTemperature(0.7), WithMaxTokens(2048), ) ``` -------------------------------- ### ChatModelAgent Configuration Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Example of how to configure a ChatModelAgent, specifying the model, instruction, and tools. ```go agent, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{ Model: chatModel, Instruction: "You are a helpful assistant", ToolsConfig: adk.ToolsConfig{ ToolsNodeConfig: compose.ToolsNodeConfig{ Tools: []tool.BaseTool{weatherTool, calculatorTool}, }, }, }) ``` -------------------------------- ### Complete Graph Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Demonstrates the creation, compilation, and invocation of a complete graph. This example shows how to add validation and processing nodes, define edges, and execute the graph with input. ```go package main import ( "context" "fmt" "github.com/cloudwego/eino/compose" ) type Input struct { Query string } type Output struct { Result string } func main() { ctx := context.Background() graph := compose.NewGraph[*Input, *Output]() // Add validation node graph.AddLambdaNode("validate", func(ctx context.Context, input *Input) (*string, error) { if input.Query == "" { return nil, fmt.Errorf("query required") } return &input.Query, nil }) // Add processing node graph.AddLambdaNode("process", func(ctx context.Context, query *string) (*Output, error) { return &Output{Result: "Processed: " + *query}, nil }) // Connect nodes graph.AddEdge(compose.START, "validate") graph.AddEdge("validate", "process") graph.AddEdge("process", compose.END) // Compile runnable, err := graph.Compile(ctx) if err != nil { panic(err) } // Execute input := &Input{Query: "test"} output, err := runnable.Invoke(ctx, input) if err != nil { panic(err) } fmt.Println(output.Result) } ``` -------------------------------- ### ToolInputSchema Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md An example demonstrating the creation of a ToolInputSchema for a weather tool. ```go schema := &schema.ToolInputSchema{ Type: "object", Description: "Get weather for a city", Properties: map[string]*schema.ToolProperty{ "city": { Type: "string", Description: "City name", }, "units": { Type: "string", Enum: []any{"celsius", "fahrenheit"}, Default: "celsius", }, }, Required: []string{"city"}, } ``` -------------------------------- ### ToolNode Input Format Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Provides an example of the expected input format for a ToolNode, including the tool name and its specific input parameters. ```go map[string]any{ "tool_name": "weather", "tool_input": map[string]any{ "city": "San Francisco", }, } ``` -------------------------------- ### Example: Invoking a Runnable with Streaming Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Demonstrates how to consume output and errors from a streaming Runnable execution. ```go outputs, errs, err := runnable.InvokeStream(ctx, input) for { select { case output := <-outputs: fmt.Println(output) case err := <-errs: return err } } ``` -------------------------------- ### Example: Compiling a Graph Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Demonstrates how to compile a graph into a Runnable and handle potential errors during compilation. ```go runnable, err := graph.Compile(ctx) if err != nil { return err } ``` -------------------------------- ### Example: Creating a New Chain Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Instantiates a new Chain with specified input and output types. ```go chain := compose.NewChain[*Input, *Output]() ``` -------------------------------- ### Example: Invoking a Runnable Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Shows how to invoke a compiled Runnable with input and process the output or error. ```go input := &Input{Query: "test"} output, err := runnable.Invoke(ctx, input) if err != nil { return err } fmt.Println(output.Result) ``` -------------------------------- ### Typer Interface Implementation Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md An example implementation of the Typer interface for a specific component. ```go type OpenAIChatModel struct{} func (m *OpenAIChatModel) GetType() string { return "OpenAI" } // Display name: "OpenAIChatModel" ``` -------------------------------- ### Format Message Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/schema-reference.md Demonstrates how to format a message using template variables and a specified format type. ```go msg := schema.UserMessage("What is Eino?") msgs, err := msg.Format(ctx, map[string]any{"query": "Eino"}, schema.FString) ``` -------------------------------- ### Agent Run Option Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/configuration.md Demonstrates how to configure agent execution with options like setting a name, providing session values, and specifying a timeout. ```go iter := runner.Query(ctx, "query", adk.WithName("my-run"), adk.WithSessionValues(map[string]any{"key": "value"}), adk.WithTimeout(30 * time.Second), ) ``` -------------------------------- ### StreamReader Usage Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/schema-reference.md Example demonstrating how to iterate through a stream using the Next() method of a StreamReader until an error occurs. ```go reader := /* some stream reader */ for { item, err := reader.Next() if err != nil { break } // Process item } ``` -------------------------------- ### Agent Run Option Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Demonstrates how to use functional options to configure agent execution, such as setting a name and passing session values. ```go iter := runner.Query(ctx, "query", adk.WithName("my-agent"), adk.WithSessionValues(map[string]any{"key": "value"}), ) ``` -------------------------------- ### NewGraph Creation Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/configuration.md Illustrates creating a new graph with default options. The graph is typed with input and output types. ```go graph := compose.NewGraph[*Input, *Output]() // Graph created with default options ``` -------------------------------- ### MessagesPlaceholder Usage Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/schema-reference.md Example demonstrating how to use MessagesPlaceholder to format messages from context, specifying the format type. ```go placeholder := schema.MessagesPlaceholder("history", false) params := map[string]any{ "history": []*schema.Message{...}, } msgs, err := placeholder.Format(ctx, params, schema.FString) ``` -------------------------------- ### Example: Creating a FieldMapping Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Illustrates the creation of a FieldMapping object to define a data flow from one field to another. ```go mapping := &compose.FieldMapping{ From: "output.text", To: "input.query", } ``` -------------------------------- ### Example: Creating a New Workflow Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Defines a WorkflowState struct and then creates a new Workflow, providing a function to initialize the state. ```go type WorkflowState struct { Messages []*Message Count int } workflow := compose.NewWorkflow[*Input, *Output, *WorkflowState]( func(ctx context.Context) *WorkflowState { return &WorkflowState{ Messages: make([]*Message, 0), Count: 0, } }, ) ``` -------------------------------- ### AgenticMessage Creation Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/schema-reference.md Shows how to create an AgenticMessage and append an image content block to it. ```go msg := schema.UserAgenticMessage("Analyze this image") msg.ContentBlocks = append(msg.ContentBlocks, &schema.ContentBlock{ Type: schema.ContentBlockTypeUserInputImage, UserInputImage: &schema.UserInputImage{ URL: "https://example.com/image.png", MIMEType: "image/png", }, }) ``` -------------------------------- ### StartInfo Structure Source: https://github.com/cloudwego/eino/blob/main/_autodocs/callbacks-reference.md Provides detailed information about a component's execution start, including identity, run context, input details, and metadata. ```go type StartInfo struct { // Component identity ComponentName string ComponentType string ComponentKind string // Run context RunID string Message string // Input details Input any InputType reflect.Type // Path and metadata RunPath string Extra map[string]any } ``` -------------------------------- ### Schema Package Import Source: https://github.com/cloudwego/eino/blob/main/_autodocs/schema-reference.md Example import statement for accessing all types within the schema package. ```go import "github.com/cloudwego/eino/schema" ``` -------------------------------- ### AddNode Example with Options Source: https://github.com/cloudwego/eino/blob/main/_autodocs/configuration.md Demonstrates adding a lambda node to a graph with custom options, including node key, retry configuration, and timeout. ```go graph.AddLambdaNode("process", fn, compose.WithNodeKey("Data Processing"), compose.WithNodeRetryConfig(&adk.RetryConfig{MaxRetries: 3}), ) ``` -------------------------------- ### PromptTemplate Format Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md Renders a chat template using a map of variables, substituting placeholders like '{domain}' and '{history}'. ```Go template := prompt.FromMessages(schema.FString, schema.SystemMessage("You are an expert in {domain}"), schema.MessagesPlaceholder("history", false), ) msgs, err := template.Format(ctx, map[string]any{ "domain": "Go programming", "history": []*schema.Message{...}, }) ``` -------------------------------- ### Graph Compilation Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/configuration.md Shows how to compile a graph with specific compilation options, such as enabling schema check and setting maximum concurrency. ```go runnable, err := graph.Compile(ctx, compose.WithCompileCheckSchema(true), compose.WithMaxConcurrency(4), ) ``` -------------------------------- ### StartInfo Struct for Callbacks Source: https://github.com/cloudwego/eino/blob/main/_autodocs/types.md Represents information provided when a component starts execution, including details about the component, run, message, and input. ```go type StartInfo struct { ComponentName string ComponentType string ComponentKind string RunID string Message string Input any InputType reflect.Type RunPath string Extra map[string]any } ``` -------------------------------- ### Tool Invoke Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md Invokes a tool with a map of string-to-any input parameters. The tool's input schema must be respected. ```Go result, err := tool.Invoke(ctx, map[string]any{ "city": "San Francisco", }) ``` -------------------------------- ### Usage Example for Filtered Handler Source: https://github.com/cloudwego/eino/blob/main/_autodocs/callbacks-reference.md Demonstrates how to use the FilteredHandler to selectively apply callbacks, such as logging only tool executions. ```go // Only log tool executions toolLogger := &FilteredHandler{ inner: loggingHandler, componentKey: "Tool", } output, err := runnable.Invoke(ctx, input, compose.WithCallbacks(toolLogger), ) ``` -------------------------------- ### Implement Custom Calculator Tool Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md Example of a custom tool implementation for basic arithmetic operations. Defines name, description, input schema, and invocation logic for both direct and streaming calls. ```go package main import ( "context" "github.com/cloudwego/eino/components/tool" "github.com/cloudwego/eino/schema" ) type CalculatorTool struct{} func (t *CalculatorTool) GetName() string { return "calculator" } func (t *CalculatorTool) GetDescription() string { return "Perform basic arithmetic" } func (t *CalculatorTool) GetInputSchema() *schema.ToolInputSchema { return &schema.ToolInputSchema{ Type: "object", Properties: map[string]*schema.ToolProperty{ "operation": {Type: "string", Enum: []any{'+', '-', '*', '/'}}, "a": {Type: "number"}, "b": {Type: "number"}, }, Required: []string{"operation", "a", "b"}, } } func (t *CalculatorTool) Invoke(ctx context.Context, input map[string]any) (string, error) { op := input["operation"].(string) a := input["a"].(float64) b := input["b"].(float64) var result float64 switch op { case "+": result = a + b case "-": result = a - b case "*": result = a * b case "/": result = a / b } return fmt.Sprintf("%.2f", result), nil } func (t *CalculatorTool) InvokeStream(ctx context.Context, input map[string]any) (*schema.StreamReader[string], error) { // Streaming not applicable for calculator result, err := t.Invoke(ctx, input) if err != nil { return nil, err } // Return result wrapped in stream // Implementation details depend on StreamReader construction return nil, nil } ``` -------------------------------- ### Get Session from Context Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Retrieves the session object from the provided context. ```go func GetSession(ctx context.Context) *Session ``` -------------------------------- ### ChatModel Generate Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md Generates a response from a language model using a list of messages. Ensure the model and context are properly initialized. ```Go messages := []*schema.Message{ schema.UserMessage("What is Eino?"), } response, err := model.Generate(ctx, messages) if err != nil { return err } fmt.Println(response.Content) ``` -------------------------------- ### Implement File-Based Checkpoint Store Source: https://github.com/cloudwego/eino/blob/main/_autodocs/configuration.md An example implementation of the CheckPointStore interface using local files for persistence. It serializes and deserializes data using gob encoding. ```go type FileStore struct { basePath string } func (s *FileStore) Save(ctx context.Context, id string, data any) error { // Serialize and save to file path := filepath.Join(s.basePath, id+".gob") f, _ := os.Create(path) defer f.Close() return gob.NewEncoder(f).Encode(data) } func (s *FileStore) Load(ctx context.Context, id string) (any, error) { // Load and deserialize from file path := filepath.Join(s.basePath, id+".gob") f, _ := os.Open(path) defer f.Close() var data any return data, gob.NewDecoder(f).Decode(&data) } // Use in runner runner := adk.NewRunner(ctx, adk.RunnerConfig{ Agent: agent, CheckPointStore: &FileStore{basePath: "./checkpoints"}, }) ``` -------------------------------- ### Get and Add Session Values Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Retrieve all session values from the context or add new values to the session. ```go // Get all session values func GetSessionValues(ctx context.Context) map[string]any // Add session values func AddSessionValues(ctx context.Context, values map[string]any) ``` ```go values := adk.GetSessionValues(ctx) userID := values["user_id"].(string) ``` -------------------------------- ### Example: Adding a Mapping to a Graph Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Adds a field mapping to a graph, specifying the source and target nodes and the fields to be mapped. ```go graph.AddMapping("extract", "prompt", []*compose.FieldMapping{ {From: "content", To: "context"}, }) ``` -------------------------------- ### Run Runner with Message List Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Starts agent execution using a list of messages. Returns an AsyncIterator for agent events. ```go func (r *Runner) Run(ctx context.Context, messages []Message, opts ...AgentRunOption) *AsyncIterator[*TypedAgentEvent[*Message]] ``` -------------------------------- ### StartInfo Struct Source: https://github.com/cloudwego/eino/blob/main/_autodocs/types.md Contains information about the start of a component's execution, including its name, type, run ID, message, input, and other relevant details. ```APIDOC ## StartInfo Struct ### Description Provides information about the start of a component's execution. ### Fields - **ComponentName string**: The name of the component. - **ComponentType string**: The type of the component. - **ComponentKind string**: The kind of the component. - **RunID string**: The unique identifier for the run. - **Message string**: A message associated with the start event. - **Input any**: The input provided to the component. - **InputType reflect.Type**: The type of the input. - **RunPath string**: The path of the run. - **Extra map[string]any**: Additional key-value pairs. ``` -------------------------------- ### Session Methods Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Provides methods to get and add session-level values that are shared across agents. ```go func GetSessionValues(ctx context.Context) map[string]any ``` ```go func AddSessionValues(ctx context.Context, values map[string]any) ``` -------------------------------- ### Implement a Custom Tool Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md Define a custom tool by implementing the Tool interface. This includes methods for getting the tool's name, description, input schema, and invocation logic (both synchronous and streaming). ```go type MyTool struct{} func (t *MyTool) GetName() string { return "my_tool" } func (t *MyTool) GetDescription() string { return "Does something useful" } func (t *MyTool) GetInputSchema() *schema.ToolInputSchema { return &schema.ToolInputSchema{ Type: "object", Properties: map[string]*schema.ToolProperty{ "param": {Type: "string", Description: "Input param"}, }, Required: []string{"param"}, } } func (t *MyTool) Invoke(ctx context.Context, input map[string]any) (string, error) { // Execute tool return "result", nil } func (t *MyTool) InvokeStream(ctx context.Context, input map[string]any) (*schema.StreamReader[string], error) { // Streaming execution return stream, nil } ``` -------------------------------- ### Runner.Query Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Starts agent execution with a single user query string. Returns an asynchronous iterator for agent events. ```APIDOC ## Runner.Query ### Description Starts execution with a single user query string. ### Signature ```go func (r *Runner) Query(ctx context.Context, query string, opts ...AgentRunOption) *AsyncIterator[*TypedAgentEvent[*Message]] ``` ### Parameters #### Path Parameters - **ctx** (context.Context) - Required - Execution context. - **query** (string) - Required - User query string. - **opts** (...AgentRunOption) - Optional - Optional run-time options. ### Returns - **AsyncIterator[*TypedAgentEvent[*Message]]** - Asynchronous iterator of agent events. ### Example ```go iter := runner.Query(ctx, "What is Eino?") for { event, ok := iter.Next() if !ok { break } if event.Err != nil { // Handle error } if event.Output != nil { fmt.Println(event.Output.Content) } } ``` ``` -------------------------------- ### Retriever Retrieve Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md Retrieves documents based on a query string. Iterates through the returned documents and prints their content. ```Go docs, err := retriever.Retrieve(ctx, "What is Eino?") for _, doc := range docs { fmt.Println(doc.Content) } ``` -------------------------------- ### Configure Workflow Initial State Source: https://github.com/cloudwego/eino/blob/main/_autodocs/configuration.md Define the initial state for workflows, including messages, context, and counters. This sets up the starting environment for a workflow. ```go type WorkflowState struct { Messages []*Message Context map[string]any Count int } workflow := compose.NewWorkflow[*Input, *Output, *WorkflowState]( func(ctx context.Context) *WorkflowState { return &WorkflowState{ Messages: make([]*Message, 0), Context: make(map[string]any), Count: 0, } }, ) ``` -------------------------------- ### Interrupt and Resume Pattern Source: https://github.com/cloudwego/eino/blob/main/_autodocs/errors.md Illustrates the interrupt and resume pattern for long-running operations. It shows how to capture an interrupt event, get user input, and resume the process with provided parameters. ```Go iter := runner.Query(ctx, query) for { event, ok := iter.Next() if !ok { break } if event.Action != nil && event.Action.Interrupted != nil { // Save checkpoint cpID := event.Action.Interrupted.CheckpointID // Get user input userInput := getUserInput() // Resume with user data iter, _ := runner.ResumeWithParams(ctx, cpID, &adk.ResumeParams{ Targets: map[string]any{ "interrupt_addr": userInput, }, }) // Continue processing } } ``` -------------------------------- ### State Management Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Utilities for managing state within the execution context. Includes functions to get, update, and set initial state. ```APIDOC ## State Management ### GetState Get state from execution context. ### UpdateState Update state in execution context. ### WithInitialState Set initial state. ``` -------------------------------- ### Catching Invalid Node Name Error Source: https://github.com/cloudwego/eino/blob/main/_autodocs/errors.md Demonstrates catching an error when trying to add a node using reserved names like 'start' or 'end'. Use unique names as START and END are added automatically. ```go err := graph.AddLambdaNode("start", fn) // Error: reserved ``` -------------------------------- ### Implement Fallback/Retry Logic in Graph Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md Add fallback or retry mechanisms to a graph by defining conditional edges. This example shows how to route execution to a fallback node if the primary node returns nil. ```go graph.AddLambdaNode("primary", primaryFn) graph.AddLambdaNode("fallback", fallbackFn) graph.AddConditionalEdges("primary", func(ctx context.Context, output any) (string, error) { if output == nil { return "fallback", nil } return "end", nil }, map[string]string{"fallback": "fallback", "end": compose.END}, ) graph.AddEdge("fallback", compose.END) ``` -------------------------------- ### Get Run Context Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Retrieves the run context from the current execution context. ```go func GetRunContext(ctx context.Context) *TypedRunContext[Message] ``` -------------------------------- ### AsyncIterator Operations Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Operations for asynchronous iterators, including getting the next item. ```APIDOC ## AsyncIterator Operations ### AsyncIterator.Next Get next item from iterator. ``` -------------------------------- ### Create and Run a ChatModelAgent Source: https://github.com/cloudwego/eino/blob/main/README.zh_CN.md Initialize a ChatModel with configuration and create an agent. Use a runner to query the agent and iterate through the events. ```Go chatModel, _ := openai.NewChatModel(ctx, &openai.ChatModelConfig{ Model: "gpt-4o", APIKey: os.Getenv("OPENAI_API_KEY"), }) agent, _ := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{ Model: chatModel, }) runner := adk.NewRunner(ctx, adk.RunnerConfig{Agent: agent}) iter := runner.Query(ctx, "Hello, who are you?") for { event, ok := iter.Next() if !ok { break } fmt.Println(event.Message.Content) } ``` -------------------------------- ### CancelError Methods Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Provides methods to check if an error is a CancelError and to get its string representation. ```go func (e *CancelError) Error() string ``` ```go func IsCancelError(err error) bool ``` -------------------------------- ### Session Values Helpers Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Functions to get and add session values within a context. ```APIDOC ## Session Values ### Description Helpers for managing session values. ### Functions - `GetSessionValues(ctx context.Context) map[string]any` - `AddSessionValues(ctx context.Context, values map[string]any)` ### Example ```go values := adk.GetSessionValues(ctx) userID := values["user_id"].(string) ``` ``` -------------------------------- ### Typer Interface Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md Provides a method to get the display name for a component, typically in CamelCase. ```go type Typer interface { GetType() string } ``` -------------------------------- ### Basic Agent Execution Source: https://github.com/cloudwego/eino/blob/main/_autodocs/index.md Demonstrates how to initialize an agent runner and iterate through query events. This pattern is useful for basic agent interactions. ```go runner := adk.NewRunner(ctx, adk.RunnerConfig{Agent: agent}) iter := runner.Query(ctx, "What is Eino?") for { event, ok := iter.Next() if !ok { break } // Handle event } ``` -------------------------------- ### Initialize and Run a DeepAgent Source: https://github.com/cloudwego/eino/blob/main/README.zh_CN.md Create a DeepAgent with a chat model, sub-agents, and tools. Use a runner to execute a complex query that the DeepAgent will break down and delegate. ```Go deepAgent, _ := deep.New(ctx, &deep.Config{ ChatModel: chatModel, SubAgents: []adk.Agent{researchAgent, codeAgent}, ToolsConfig: adk.ToolsConfig{ ToolsNodeConfig: compose.ToolsNodeConfig{ Tools: []tool.BaseTool{shellTool, pythonTool, webSearchTool}, }, }, }) runner := adk.NewRunner(ctx, adk.RunnerConfig{Agent: deepAgent}) iter := runner.Query(ctx, "Analyze the sales data in report.csv and generate a summary chart") ``` -------------------------------- ### Runner.Run Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Starts agent execution with a list of messages. Returns an asynchronous iterator for agent events. ```APIDOC ## Runner.Run ### Description Starts execution with a message list. ### Signature ```go func (r *Runner) Run(ctx context.Context, messages []Message, opts ...AgentRunOption) *AsyncIterator[*TypedAgentEvent[*Message]] ``` ### Parameters #### Path Parameters - **ctx** (context.Context) - Required - Execution context. - **messages** ([]Message) - Required - List of messages to process. - **opts** (...AgentRunOption) - Optional - Optional run-time options. ### Returns - **AsyncIterator[*TypedAgentEvent[*Message]]** - Asynchronous iterator of agent events. ``` -------------------------------- ### Connect Graph Nodes Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md Defines the edges between nodes in the graph, specifying the flow from start to end. ```go graph.AddEdge(compose.START, "validate") graph.AddEdge("validate", "generate") graph.AddEdge("generate", compose.END) ``` -------------------------------- ### Run Linting Source: https://github.com/cloudwego/eino/blob/main/README.md Execute golangci-lint to verify code style and quality. ```bash golangci-lint run ./... ``` -------------------------------- ### Create Agent Runner Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md Sets up a runner for an agent, configuring streaming behavior and checkpoint storage. ```go runner := adk.NewRunner(ctx, adk.RunnerConfig{ Agent: agent, EnableStreaming: false, CheckPointStore: store, }) ``` -------------------------------- ### Graph Composition and Execution Source: https://github.com/cloudwego/eino/blob/main/_autodocs/index.md Illustrates how to build a graph with nodes and edges, compile it, and invoke it with input. Use this for orchestrating complex workflows. ```go graph := compose.NewGraph[*Input, *Output]() graph.AddLambdaNode("validate", validateFn) graph.AddChatModelNode("generate", chatModel) graph.AddEdge(compose.START, "validate") graph.AddEdge("validate", "generate") graph.AddEdge("generate", compose.END) runnable, _ := graph.Compile(ctx) result, _ := runnable.Invoke(ctx, input) ``` -------------------------------- ### Embedding EmbedQuery Example Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md Embeds a query string into a vector representation. The resulting vector can be used for similarity searches. ```Go query := "What is Eino?" vector, err := embedding.EmbedQuery(ctx, query) if err != nil { return err } // Use vector for similarity search ``` -------------------------------- ### Registering Callbacks with Options Source: https://github.com/cloudwego/eino/blob/main/_autodocs/callbacks-reference.md Illustrates how callbacks are registered for components like chat models, typically via options during execution rather than at construction time. ```go type ChatModel interface { Generate(ctx context.Context, messages []Message, opts ...Option) (Message, error) } // Option may include callbacks Generate(ctx, messages, WithCallbacks(handler)) ``` -------------------------------- ### Configure Model Generation Options Source: https://github.com/cloudwego/eino/blob/main/_autodocs/configuration.md Customize model generation behavior using options like temperature, max tokens, and sampling parameters. Use WithSeed for reproducible results. ```go response, err := model.Generate(ctx, messages, model.WithTemperature(0.7), model.WithMaxTokens(1024), model.WithSeed(42), ) ``` -------------------------------- ### Tool Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md Interface for callable tools/functions. It defines methods to get tool information, input schema, and invoke the tool. ```APIDOC ## Tool (BaseTool) ### Description Interface for callable tools/functions. Defines methods to get tool information, input schema, and invoke the tool. ### Methods - **GetName** `func() string`: Tool name (must be unique in agent). - **GetDescription** `func() string`: Tool purpose and usage. - **GetInputSchema** `func() *schema.ToolInputSchema`: JSON schema of inputs. - **Invoke** `func(ctx context.Context, input map[string]any) (string, error)`: Execute tool. - **InvokeStream** `func(ctx context.Context, input map[string]any) (*StreamReader[string], error)`: Execute with streaming. ### Input Format ```go map[string]any{ "param1": "value1", "param2": 123, } ``` ### Output String result or error ### Example ```go result, err := tool.Invoke(ctx, map[string]any{ "city": "San Francisco", }) ``` ``` -------------------------------- ### Create New Runner Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Instantiates a new Runner with the provided context and configuration. Ensure Agent is provided; EnableStreaming and CheckPointStore are optional. ```go func NewRunner(ctx context.Context, conf RunnerConfig) *Runner ``` ```go runner := adk.NewRunner(ctx, adk.RunnerConfig{ Agent: agent, EnableStreaming: true, CheckPointStore: store, }) ``` -------------------------------- ### Configure Chat Model Agent with Tools Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md Initialize a `ChatModelAgent` with a list of tools and configure tool-specific behaviors like `ReturnDirectly`. This allows the agent to use tools for task execution. ```go agent, _ := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{ Model: model, ToolsConfig: adk.ToolsConfig{ ToolsNodeConfig: compose.ToolsNodeConfig{ Tools: tools, }, ReturnDirectly: map[string]bool{ "final_answer": true, // Return immediately after this tool }, }, }) ``` -------------------------------- ### Create Graph Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md Initializes a new graph for composing nodes with specified input and output types. ```go graph := compose.NewGraph[*Input, *Output]() ``` -------------------------------- ### Providing Valid Model Configuration Source: https://github.com/cloudwego/eino/blob/main/_autodocs/errors.md Ensure model creation succeeds by providing a valid configuration, including necessary credentials and model names. ```go model, err := openai.NewChatModel(ctx, &openai.ChatModelConfig{ Model: "gpt-4o", APIKey: os.Getenv("OPENAI_API_KEY"), }) ``` -------------------------------- ### Logging Handler Implementation Source: https://github.com/cloudwego/eino/blob/main/_autodocs/callbacks-reference.md A handler that logs the start, end, error, and stream events of a component. It requires a logger instance. ```go type LoggingHandler struct { logger *log.Logger } func (h *LoggingHandler) OnStart(ctx context.Context, info *StartInfo) context.Context { h.logger.Printf("START: %s/%s", info.ComponentType, info.ComponentName) return ctx } func (h *LoggingHandler) OnEnd(ctx context.Context, info *EndInfo) { h.logger.Printf("END: %s/%s (duration: %v)", info.ComponentType, info.ComponentName, info.EndTime.Sub(info.StartTime)) } func (h *LoggingHandler) OnError(ctx context.Context, info *ErrorInfo) { h.logger.Printf("ERROR: %s/%s: %v", info.ComponentType, info.ComponentName, info.Error) } func (h *LoggingHandler) OnStartWithStreamInput(ctx context.Context, info *StartInfo) context.Context { return h.OnStart(ctx, info) } func (h *LoggingHandler) OnEndWithStreamOutput(ctx context.Context, info *StreamOutputInfo) { h.logger.Printf("STREAM_CHUNK: %s/%s", info.ComponentType, info.ComponentName) } ``` -------------------------------- ### Agent Execution with Callbacks Source: https://github.com/cloudwego/eino/blob/main/_autodocs/callbacks-reference.md Shows how to attach a callback handler during agent execution using `adk.WithCallbacks`. ```go iter := runner.Query(ctx, "query", adk.WithCallbacks(handler), ) ``` -------------------------------- ### Format Prompt Template with Placeholders Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md Create and format prompt templates using `prompt.FromMessages`. This allows dynamic insertion of variables like system roles, chat history, and current queries into messages. ```go template := prompt.FromMessages(schema.FString, schema.SystemMessage("You are {system_role}"), schema.MessagesPlaceholder("chat_history", false), schema.UserMessage("{current_query}"), ) msgs, _ := template.Format(ctx, map[string]any{ "system_role": "helpful assistant", "chat_history": previousMessages, "current_query": "What's the weather?", }) ``` -------------------------------- ### Create Chat Model Agent Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md Initializes a new chat model agent with configuration including the model, instructions, and tools. ```go agent, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{ Model: chatModel, Instruction: "You are a helpful assistant", ToolsConfig: adk.ToolsConfig{ ToolsNodeConfig: compose.ToolsNodeConfig{ Tools: []tool.BaseTool{tool1, tool2}, }, }, }) ``` -------------------------------- ### Get Handler from Context Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Use FromContext to retrieve a registered handler from the context. This is typically used by components implementing callbacks directly. ```go // Get handler from context func FromContext(ctx context.Context) Handler ``` -------------------------------- ### Use Message Constructors Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Prefer using message constructors like schema.UserMessage for creating messages. Avoid manual construction of message objects. ```go // Good: Use constructors msg := schema.UserMessage("Hello") // Avoid: Manual construction msg := &schema.Message{ Role: schema.User, Content: "Hello", } ``` -------------------------------- ### Template Rendering with Helpers Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Constructs a message template using predefined schema types and placeholders. Use this to dynamically format messages based on context variables. ```go // With helpers template := prompt.FromMessages(schema.FString, schema.SystemMessage("You are {role}"), schema.MessagesPlaceholder("history", false), ) msgs, err := template.Format(ctx, map[string]any{ "role": "expert", "history": history, }) ``` -------------------------------- ### Graph Invocation with Callbacks Source: https://github.com/cloudwego/eino/blob/main/_autodocs/callbacks-reference.md Demonstrates how to pass multiple callback handlers during graph invocation using `compose.WithCallbacks`. ```go runnable, err := graph.Compile(ctx) output, err := runnable.Invoke(ctx, input, compose.WithCallbacks(handler1, handler2), ) ``` -------------------------------- ### Runnable.InvokeStream Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Executes the Runnable with streaming output. It returns channels for receiving output values and errors, as well as an initial error if the stream setup fails. ```APIDOC ## Runnable.InvokeStream Execute with streaming output. ### Signature ```go func (r Runnable[I, O]) InvokeStream(ctx context.Context, input I) (<-chan O, <-chan error, error) ``` ### Parameters - `ctx` (context.Context): The context for the streaming invocation. - `input` (I): The input data for the runnable. ### Returns - `<-chan O`: A channel that streams output values. - `<-chan error`: A channel that streams errors encountered during execution. - `error`: An error if the stream initialization fails. ``` -------------------------------- ### Expose Graph as an Agent Tool Source: https://github.com/cloudwego/eino/blob/main/README.md Wrap a compiled graph into a tool that can be utilized by an agent. ```Go tool, _ := graphtool.NewInvokableGraphTool(graph, "data_pipeline", "Process and validate data") agent, _ := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{ Model: chatModel, ToolsConfig: adk.ToolsConfig{ Tools: []tool.BaseTool{tool}, }, }) ``` -------------------------------- ### Handler Interface Definition for Callbacks Source: https://github.com/cloudwego/eino/blob/main/_autodocs/types.md Defines the interface for handling various events during component execution, including start, end, error, and stream-related events. ```go type Handler interface { OnStart(ctx context.Context, info *StartInfo) context.Context OnEnd(ctx context.Context, info *EndInfo) OnError(ctx context.Context, info *ErrorInfo) OnStartWithStreamInput(ctx context.Context, info *StartInfo) context.Context OnEndWithStreamOutput(ctx context.Context, info *StreamOutputInfo) } ``` -------------------------------- ### Eino Context Helpers Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md These helpers provide access to session values, run context, and graph state within the Eino framework. Use them to retrieve or update contextual information. ```go // Get session values values := adk.GetSessionValues(ctx) // Get run context runCtx := adk.GetRunContext(ctx) // Get graph state state := compose.GetState(ctx) // Update state compose.UpdateState(ctx, map[string]any{"key": "value"}) ``` -------------------------------- ### Eino Imports Source: https://github.com/cloudwego/eino/blob/main/_autodocs/adk-core.md Standard import statement for the Eino ADK. ```go import "github.com/cloudwego/eino/adk" ``` -------------------------------- ### StreamReader Interface and Methods Source: https://github.com/cloudwego/eino/blob/main/_autodocs/types.md Defines the interface for a stream reader, including methods to get the next item, receive an item with status, and close the stream. ```go type StreamReader[T any] struct { // Private fields } func (s *StreamReader[T]) Next() (T, error) func (s *StreamReader[T]) Recv() (T, bool, error) func (s *StreamReader[T]) Close() error ``` -------------------------------- ### Get Component Type Name Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Use GetType to retrieve the type name of a component. The second return value indicates if the type was successfully retrieved. ```go // Get component type name func GetType(component any) (string, bool) // Check if callbacks are enabled func IsCallbacksEnabled(component any) bool ``` ```go typeName, ok := components.GetType(model) if ok { fmt.Printf("Model type: %s\n", typeName) } ``` -------------------------------- ### Compile and Execute Graph Source: https://github.com/cloudwego/eino/blob/main/_autodocs/quick-reference.md Compiles the graph into a runnable object and then invokes it with input, handling potential errors. ```go runnable, err := graph.Compile(ctx) if err != nil { return err } output, err := runnable.Invoke(ctx, input) if err != nil { return err } ``` -------------------------------- ### Message Creation Helpers Source: https://github.com/cloudwego/eino/blob/main/_autodocs/schema-reference.md Provides helper functions to easily create messages with predefined roles. ```go func UserMessage(content string) *Message func AssistantMessage(content string) *Message func SystemMessage(content string) *Message func ToolMessage(toolCallID string, toolName string, content string) *Message ``` -------------------------------- ### Define Graph Entry and Exit Points Source: https://github.com/cloudwego/eino/blob/main/_autodocs/compose-reference.md Constants for marking the start and end points of a graph. These are used when defining edges to indicate the graph's flow. ```go const ( START = "start" // Graph entry point END = "end" // Graph exit point ) ``` -------------------------------- ### Create Tool Input Schema Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Construct a tool input schema using NewToolInputSchema, defining its type, description, properties, and required fields. Use NewToolProperty to define individual properties. ```go // Create tool input schema func NewToolInputSchema( typ string, description string, properties map[string]*ToolProperty, required []string, ) *ToolInputSchema // Create tool property func NewToolProperty( typ string, description string, defaultVal any, enum []any, ) *ToolProperty ``` ```go schema := schema.NewToolInputSchema( "object", "Search parameters", map[string]*schema.ToolProperty{ "query": schema.NewToolProperty("string", "Search term", nil, nil), "limit": schema.NewToolProperty("integer", "Result limit", 10, nil), }, []string{"query"}, ) ``` -------------------------------- ### Get and Set Run Context Source: https://github.com/cloudwego/eino/blob/main/_autodocs/api-utilities.md Access and modify the run context within an execution context. Supports generic message types for typed run contexts. ```go // Get run context from execution context func GetRunContext(ctx context.Context) *TypedRunContext[Message] // Get typed run context func GetTypedRunContext[M MessageType](ctx context.Context) *TypedRunContext[M] // Set run context func WithRunContext(ctx context.Context, rc *TypedRunContext[Message]) context.Context // Set typed run context func WithTypedRunContext[M MessageType](ctx context.Context, rc *TypedRunContext[M]) context.Context ``` ```go runCtx := adk.GetRunContext(ctx) fmt.Printf("Run ID: %s\n", runCtx.RunID) ``` -------------------------------- ### Providing Valid Tool Input Source: https://github.com/cloudwego/eino/blob/main/_autodocs/errors.md Ensure tool inputs conform to the schema by checking properties and required fields before invocation. ```go schema := tool.GetInputSchema() // Check schema.Properties and schema.Required result, err := tool.Invoke(ctx, map[string]any{ "city": "San Francisco", // Required field "units": "celsius", // Optional with default }) ``` -------------------------------- ### Typer Interface Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md The Typer interface provides a method to get the display name of a component. This name is typically in CamelCase and is used by DevOps tools and debug interfaces. ```APIDOC ## Typer Interface ### Description Provides display name for components. ### Methods #### GetType - **Signature**: `func() string` - **Description**: Returns CamelCase type name, used by DevOps tools and debug interfaces. ``` -------------------------------- ### Eino Component Imports Source: https://github.com/cloudwego/eino/blob/main/_autodocs/components-reference.md Essential imports for using Eino components, including core components, model, tool, retriever, embedding, prompt, and schema modules. ```go import ( "github.com/cloudwego/eino/components" "github.com/cloudwego/eino/components/model" "github.com/cloudwego/eino/components/tool" "github.com/cloudwego/eino/components/retriever" "github.com/cloudwego/eino/components/embedding" "github.com/cloudwego/eino/components/prompt" "github.com/cloudwego/eino/schema" ) ``` -------------------------------- ### BaseTool Interface Definition Source: https://github.com/cloudwego/eino/blob/main/_autodocs/types.md Defines the contract for a base tool component, including methods for getting its name, description, input schema, and invoking it synchronously or with streaming output. ```go type BaseTool interface { GetName() string GetDescription() string GetInputSchema() *schema.ToolInputSchema Invoke(ctx context.Context, input map[string]any) (string, error) InvokeStream(ctx context.Context, input map[string]any) (*StreamReader[string], error) } ``` -------------------------------- ### Handler Interface Source: https://github.com/cloudwego/eino/blob/main/_autodocs/types.md The Handler interface defines callback methods for various stages of a component's lifecycle, including starting, ending, error handling, and handling stream inputs/outputs. ```APIDOC ## Handler Interface ### Description Defines callback methods for component lifecycle events. ### Methods - **OnStart(ctx context.Context, info *StartInfo) context.Context**: Called when a component starts. - **OnEnd(ctx context.Context, info *EndInfo)**: Called when a component ends. - **OnError(ctx context.Context, info *ErrorInfo)**: Called when a component encounters an error. - **OnStartWithStreamInput(ctx context.Context, info *StartInfo) context.Context**: Called when a component starts with stream input. - **OnEndWithStreamOutput(ctx context.Context, info *StreamOutputInfo)**: Called when a component ends with stream output. ```