# AI Agent

### Introduction

The AI Agent State in xFlow is a powerful component designed to integrate AI-driven decision-making and automation within business workflows. Leveraging large language models (LLMs), this state enables the creation of intelligent agents that can understand, process, and respond to complex queries, thereby enhancing the efficiency and effectiveness of business processes.

This documentation explains the key concepts and configurations of the AI Agent State.

### Key Concepts

#### System Message

The `systemMessage` is a crucial configuration that sets the context for the AI agent. It acts as a guideline for the AI, instructing it on how to behave and what role to assume during the interaction. This message is typically used to define the AI agent's identity, tone, and the type of assistance it is expected to provide.

* **Example**: "You are a helpful assistant specialized in customer support queries. Provide concise and accurate answers based on the given data."

#### User Message

The `userMessage` represents the input from the user that the AI agent needs to process. It captures the user's query or command, which the AI will analyze to generate an appropriate response. This message is dynamically constructed based on the user's interaction with the system.

* **Example**: '${ "User: " + .request.question }'

#### Max Tool Executions

The `maxToolExecutions` parameter defines the maximum number of tools the AI agent can invoke during its operation. This is important to limit the scope of actions an AI can take, preventing excessive resource usage and ensuring focused execution.

* **Example**: 5 (limits the AI agent to execute up to 5 different tools during its task).

#### Memory

The `memory` concept allows the AI agent to retain information across different interactions. This is essential for maintaining context, making the AI's responses more coherent and relevant over multiple exchanges. Memory ensures that the agent can recall past interactions and use this information to enhance future responses.

* **Example**:

  ```yaml
  memory: 
    memoryId: "session123"
    memoryType: "message_window"
    maxMessages: 10
  ```

#### Output

The `output` configuration defines the structure of the data the AI agent will return after processing a user query. It ensures that the AI's responses are formatted correctly and contain all necessary information required by the subsequent workflow steps.

* **Example**:

  ```yaml
  {
      "type": "object",
      "properties": {
          "answer": {
              "type": "string",
              "description": "The answer to the user question"
          },
          "images": {
              "type": "array",
              "description": "List of related images",
              "items": {
                  "type": "string",
                  "format": "uri"
              }
          }
      },
      "required": ["answer"]
  }
  ```

#### Tools

`Tools` are external functions or services that the AI agent can invoke to perform specific tasks. These tools extend the capabilities of the AI, allowing it to fetch data, perform calculations, or interact with other systems to provide comprehensive responses.

* **Example**:

  ```yaml
  - name: FIND_RELEVANT_DOCUMENTS
    description: "This tool searches for documents related to the user's query."
    parameters: 
      {
        "type": "object",
        "properties": {
            "input": {
                "type": "string",
                "description": "The search query"
            }
        },
        "required": ["input"]
      }
    output: 
      {
          "type": "object",
          "properties": {
              "documents": {
                  "type": "array",
                  "items": {
                      "type": "string",
                      "format": "uri"
                  }
              }
          },
          "required": ["documents"]
      }
  ```

#### Agent Outcomes

`AgentOutcomes` define the possible results of the AI agent's operations. These outcomes are based on conditions evaluated after the AI processes a user query. Depending on the outcome, the workflow can transition to different states, ensuring flexible and adaptive process flows.

* **Example**:

  ```yaml
  - condition: '${ $agentOutcome.returnValues.selectedAgent == "RAG_REACT" }'
    transition: RagAgentProxy
  - condition: '${ $agentOutcome.returnValues.selectedAgent == "TRANSACTION_PROCESSING" }'
    transition: ProductRetriver
  ```

#### LLM Config

The `llmConfig` parameter specifies the configuration settings for the large language model that powers the AI agent. This includes details like the model type, version, and any specific tuning parameters that optimize the model's performance for the given task.

* **Example**:

  ```yaml
  llmConfig: 
    provider: "openai"
    apiKey: "your-api-key"
    overrideParams: 
      modelType: "gpt-4"
      temperature: 0.7
      maxTokens: 1500
  ```

### Detailed Explanation of AI Agent State Configuration

#### State Definition

The AI Agent State is defined within a workflow using specific properties. These properties configure how the AI agent behaves, what messages it processes, the tools it can use, and the outcomes it generates.

**Example Definition**

```yaml
- name: AgentSelector
  type: aiagent
  agentName: AgentSelector
  aiModel: gpt-4
  systemMessage: "You are an assistant for selecting which tool is the most useful to use. Based on the tool's description, you have to return the name of the selected tool."
  userMessage: '${ "User: " + .request.question }'
  output: 
    {
      "type": "object",
      "properties": {
          "selectedAgent": {
              "type": "string",
              "description": "The exact name of agent to be selected."
          }
      },
      "required": [
          "selectedAgent"
      ]
    }
  maxToolExecutions: 5
  memory: 
    memoryId: "session123"
    memoryType: "message_window"
    maxMessages: 10
  tools:
    - name: FIND_RELEVANT_DOCUMENTS
      description: "This tool searches for documents related to the user's query."
      parameters: 
        {
          "type": "object",
          "properties": {
              "input": {
                  "type": "string",
                  "description": "The search query"
              }
          },
          "required": ["input"]
        }
      output: 
        {
            "type": "object",
            "properties": {
                "documents": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "format": "uri"
                    }
                }
            },
            "required": ["documents"]
        }
  agentOutcomes:
    - condition: '${ $agentOutcome.returnValues.selectedAgent == "RAG_REACT" }'
      transition: RagAgentProxy
    - condition: '${ $agentOutcome.returnValues.selectedAgent == "TRANSACTION_PROCESSING" }'
      transition: ProductRetriver
  llmConfig: 
    provider: "OpenAI"
    apiKey: "your-api-key"
    overrideParams: 
      modelType: "gpt-4"
      temperature: 0.7
      maxTokens: 1500
    multiLLMsConfig: 
      strategy: "fallback"
      models: 
        - provider: "OpenAI"
          apiKey: "your-openai-api-key"
        - provider: "Anthropic"
          apiKey: "your-anthropic-api-key"
```

#### Workflow Integration

The AI Agent State is integrated into the overall workflow to handle specific tasks that require intelligent processing. The state interacts with other states, such as user tasks and operational states, to ensure a seamless flow of operations.

**Example Workflow Integration**

```yaml
states:
  - name: Init
    type: operation
    actions:
      - name: StartWorkflow
        functionRef:
          refName: InitFunction
    transition: AgentSelector
  - name: AgentSelector
    type: aiagent
    ... # Configuration as shown in the previous section
  - name: RagAgentProxy
    type: aiagentproxy
    ... # Configuration for proxy state
  - name: ProductRetriver
    type: operation
    actions:
      - name: RetrieveProductInfo
        functionRef:
          refName: RetrieveProduct


    transition: CompleteRequest
  - name: CompleteRequest
    type: end
```

### Conclusion

The AI Agent State in xFlow offers a robust framework for integrating AI capabilities into business workflows. By configuring system messages, user messages, memory, tools, and agent outcomes, developers can create intelligent agents that enhance process automation, decision-making, and user interaction. Understanding these key concepts allows for the effective deployment of AI-driven solutions, driving digital transformation and operational efficiency.

For more detailed information and advanced configurations, refer to the [AI Agent State spec](/xflow/developer-guide/workflow-states-reference/aiagent-state.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.a4b.vn/xflow/developer-guide/ai-agent-development/ai-agent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
