Skip to main content

Using ai_execute for Seamless AI-Agent Execution

The ai_execute feature in Fiscus enables developers to leverage a single AI agent to perform complex, multi-API tasks dynamically. With ai_execute, you can seamlessly chain workflows, manage dependencies, and incorporate customizable hooks, allowing for effortless orchestration and robust error handling. This section will cover how to use a single AI agent for multi-API execution, demonstrate workflow chaining, and explore hooks and callbacks for tracking and customization.

Single AI Agent, Multi-API Execution

With ai_execute, a single AI agent can interpret input and route tasks across multiple APIs. Whether it’s updating a CRM, sending emails, or adding events to a calendar, ai_execute enables automatic orchestration of APIs without requiring hardcoded logic.

Key Features:

  • Single AI Agent Managing Multiple APIs: Simplify your development with a single agent that can manage tasks across multiple APIs.
  • Automated API Selection: Let the AI agent determine the best APIs to call based on user input.
  • Simplified Codebase: Avoid the need for separate integration logic for each API; one agent can handle it all, reducing development time.

Example 1: Multi-API Execution

Scenario: A user requests to update their CRM contact information and send a follow-up email.

from fiscus import FiscusClient

client = FiscusClient(api_key="YOUR_FISCUS_API_KEY", user_id="user_123")

# AI agent handling CRM update and email sending
response = client.ai_execute("Update John Doe's CRM record and send a follow-up email")

print(response.result)

How It Works:

  1. The AI agent interprets the input and determines that it needs to:

    • Update John Doe’s CRM record.
    • Send a follow-up email using an email API.
  2. ai_execute automatically routes these tasks to the appropriate APIs.

  3. The developer writes one line of code for multiple API actions, significantly reducing complexity.

 

AI-Powered Workflow Chaining with ai_execute

Workflow chaining allows an AI agent to execute a series of tasks in sequence, where each API call depends on the success of the previous step. This is particularly useful for dependent tasks. With ai_execute, workflows are managed dynamically, and hooks allow tracking and fine-tuning of each step.

Key Features:

  • Sequential API Execution: Ensure that each task in the chain is completed successfully before moving on to the next.
  • Hooks for Execution Tracking: Utilize pre- and post-execution hooks for enhanced tracking and customization.
  • Error Management and Retry Options: Add automated error handling and retry logic for more robust workflows.

Example 2: Workflow Chaining with Hooks

Scenario: A user wants to create a new lead in the CRM, send an introductory email, and notify the sales team on Slack if the email is successfully sent.

from fiscus import FiscusClient

client = FiscusClient(api_key="YOUR_FISCUS_API_KEY", user_id="user_123")

# Define pre- and post-execution hooks
def pre_execution_hook(connector_name, operation_name, params):
print(f"Preparing to execute {operation_name} on {connector_name} with parameters: {params}")
return params

def post_execution_hook(response):
if response.success:
print(f"Successfully executed: {response.result}")
else:
print(f"Failed: {response.error_message}")
return response

client.set_pre_execution_hook(pre_execution_hook)
client.set_post_execution_hook(post_execution_hook)

# AI-driven workflow to create lead, send email, and notify via Slack
response = client.ai_execute("Create lead, send welcome email, and notify sales team on Slack")

print(response.result)

How It Works:

  1. The AI agent interprets this as a workflow chain:

    • Create a new lead in the CRM.
    • Send a welcome email using an email service.
    • If successful, notify the sales team via Slack.
  2. Hooks track and modify execution:

    • The pre-execution hook logs details before each API call.
    • The post-execution hook logs results after each task, facilitating error handling or additional logic.
  3. The AI agent executes each task in sequence, ensuring that the workflow advances only if each step completes successfully.

Example 3: Error Handling and Retry Logic in Workflow Chaining

For a more resilient solution, you can incorporate retry logic to ensure that if a task fails, it is retried automatically.

from fiscus import FiscusClient

client = FiscusClient(api_key="YOUR_FISCUS_API_KEY", user_id="user_123")

# Define post-execution hook with retry logic
def post_execution_hook_with_retry(response):
if not response.success and response.operation_name == "send_email":
print("Email sending failed, retrying...")
# Retry the operation
return client.execute("EmailService", "send_email", response.params, user=response.user)
return response

client.set_post_execution_hook(post_execution_hook_with_retry)

# AI agent workflow with error handling and retry logic
response = client.ai_execute("Create lead, send welcome email, notify sales team")

print(response.result)

How It Works:

  1. If the send_email operation fails, the post-execution hook identifies the failure and retries the email.
  2. This approach keeps the workflow on track by automatically retrying the task, increasing reliability.

 

Best Practices for Optimizing ai_execute Workflows

To maximize the efficiency and adaptability of your workflows, consider these best practices:

  • Use Execution Modes Strategically: Choose SEQUENTIAL for predictable workflows, or customize with decision_logic_override for branching logic.
  • Leverage Callbacks for Real-Time Adjustments: Set up specific callbacks to handle events like errors, streaming responses, or task-specific processes (e.g., AI_TASK_CREATION).
  • Optimize Memory Strategies: Use memory settings like SEMANTIC_SEARCH for recalling relevant context or APPEND for adding new data progressively.
  • Employ Custom Hooks for Robust Tracking: Use pre- and post-execution hooks to log or modify parameters, enhancing observability and control over each API call.
  • Asynchronous Execution: For tasks that require real-time responses, use ai_execute_async to handle operations without blocking other processes.

Wrapping Up ai_execute

By leveraging ai_execute, developers can efficiently construct powerful AI agents capable of handling complex, adaptive workflows that include multi-API execution, custom hooks, and dynamic error handling. This robust orchestration tool reduces manual coding and enables flexible, resilient API integrations.