Build Real-Time AI Agents: GenAI Platform & Serverless Functions
Harness the power of AI agents with real-time data access using GenAI and serverless functions. Learn how to create intelligent agents that can provide up-to-the-minute answers from your DigitalOcean account. This tutorial provides a step-by-step guide to building AI agents that offer valuable insights and improve decision-making.
The Rise of Real-Time AI Agents
In today's fast-paced world, accessing and processing information in real-time is a game-changer. AI-powered agents that can retrieve live data are invaluable across diverse industries. Think finance with instant market analysis, healthcare with immediate patient data, and e-commerce with dynamic inventory updates. These real-time insights are transforming how businesses operate.
Why GenAI Platform for Building AI Agents?
DigitalOcean's GenAI Platform is your streamlined solution for building intelligent agents. No more wrestling with complex infrastructure. Focus on crafting cutting-edge AI models that drive insightful interactions, leaving the underlying technicalities to DigitalOcean.
Building an AI Agent: A Practical Example
Let’s walk through building an AI agent that answers questions about your DigitalOcean account. This agent will pull data from the DigitalOcean API to provide live information about your Droplets (virtual machines) like IDs, status, and other key details. This demonstrates the power of AI agent with API integration.
Prerequisites to Getting Started
Here's what you need to follow this tutorial:
- DigitalOcean GenAI Platform: This is the core tool for building your AI agents.
- DigitalOcean Functions: A serverless environment to run your code (Python, Node.js, PHP, Go).
Use Cases for AI Agents with Live Data
AI agents are incredibly versatile, offering benefits across various roles:
- Developers: Instant access to cloud infrastructure insights, making resource management easier.
- Support Teams: Automated answers to frequent questions, leading to faster customer service.
- System Administrators: Real-time resource monitoring and alerts for enhanced efficiency.
Pros & Cons of API-Integrated AI Agents
Combining AI agents with APIs brings significant advantages, but it's crucial to consider the potential drawbacks.
The Upsides:
- Real-time data access: Always have the latest information at your fingertips.
- Automation of repetitive tasks: Free up valuable time for more strategic work.
- Scalability: Handle a growing number of requests effortlessly.
- Improved user experience: Provide quick and intelligent responses.
The Downsides:
- Potential API rate limits: Frequent requests might be restricted.
- Security concerns: Handling sensitive data requires robust security measures.
- Debugging complexity: Troubleshooting real-time errors can be challenging.
Step 1: Prepare Your Serverless Function
We need a function that the AI can call to fetch data from the DigitalOcean API:
-
In the DigitalOcean control panel, go to Functions and click Create Namespace.
-
Choose a data center location.
-
Connect to your namespace using the
doctl
command-line tool:doctl serverless connect
-
Initialize a sample Python project:
doctl serverless init --language python example-project
Step 2: Configure Your Function for API Access
With your project initialized, follow these configuration steps:
-
Modify your project file to define the Python runtime and configure security headers.
-
Create an environment file to store your DigitalOcean API token securely.
-
Replace the "Hello, world!" sample code with your API function to fetch Droplet information.
-
Create a build script for importing Python dependencies.
-
Deploy the function:
doctl serverless deploy
After deployment, test your function to verify that it returns the expected Droplet details. Refer to this GitHub repository for a complete coded example.
Step 3: Create Your AI Agent
You can create agents using the web interface or the API through the GenAI platform.
Option 1: Web Interface
A user-friendly way to create your AI agent!
-
In the GenAI platform, click Create Agent.
-
Name it (e.g., "Droplet Agent").
-
Give the agent clear instructions:
You are a helpful assistant that provides information about DigitalOcean customer accounts. Your role is to help users understand their account details, team information, resource usage, and account status.
-
Select your language model (e.g., Llama 3.3 Instruct-70B).
Option 2: Using the API
Do the same programmatically:
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
"https://api.digitalocean.com/v2/gen-ai/agents" \
-d '{
"name": "Droplet Agent",
"model_uuid": "d754f2d7-d1f0-11ef-bf8f-4e013e2ddde4",
"instruction": "You are a helpful assistant that provides information about DigitalOcean customer accounts. Your role is to help users understand their account details, team information, resource usage, and account status.",
"description": "AI agent for retrieving and explaining DigitalOcean account information",
"project_id": "YOUR_PROJECT_ID",
"tags": ["droplet-info"],
"region": "tor1"
}'
Replace $API_TOKEN
and YOUR_PROJECT_ID
.
Step 4: Link Function to AI Agent
This connection enables real-time data retrieval:
Option 1: Web Interface
-
Go to the "Resources" tab in the agent playground.
-
Add a function route.
-
Select your deployed function.
-
Provide instructions:
Call this function when a user asks about their DigitalOcean droplets, virtual machines, instances or servers. Use this function to retrieve information about one or more droplets in a DigitalOcean account.
-
Define input and output schemas.
Option 2: API
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
"https://api.digitalocean.com/v2/gen-ai/agents/1b418231-b7d6-11ef-bf8f-4e013e2ddde4/functions" \
-d '{
"agent_uuid": "1b418231-b7d6-11ef-bf8f-4e013e2ddde4",
"function_name": "droplet",
"description": "Call this function when the user asks about their DigitalOcean droplets, virtual machines, instances, or servers. Use this function to retrieve information about one or more droplets in a DigitalOcean account.",
"input_schema": {
"droplet_id": {
"required": false,
"description": "Specific droplet ID to retrieve information for. If not provided, returns a list of droplets",
"type": "string"
},
"tag": {
"description": "Filter droplets by tag",
"type": "string",
"required": false
},
"limit": {
"type": "number",
"required": false,
"description": "Maximum number of droplets to return (default: 10)"
}
},
"output_schema": {
"droplets": {
"type": "string",
"description": "JSON string containing list of droplet information"
},
"count": {
"type": "number",
"description": "Total number of droplets returned"
},
"error": {
"type": "string",
"required": false,
"description": "Error message if the request failed"
},
"status": {
"type": "string",
"description": "Status of the API request (success or error)"
}
}'
Example Input Schema
This defines parameters the agent sends:
{
"droplet_id": {
"required": false,
"description": "Specific droplet ID to retrieve information for. If not provided, returns a list of droplets",
"type": "string"
},
"tag": {
"description": "Filter droplets by tag",
"type": "string",
"required": false
},
"limit": {
"type": "number",
"required": false,
"description": "Maximum number of droplets to return (default: 10)"
}
}
Example Output Schema
{
"droplets": {
"type": "string",
"description": "JSON string containing list of droplet information"
},
"count": {
"type": "number",
"description": "Total number of droplets returned"
},
"error": {
"type": "string",
"required": false,
"description": "Error message if the request failed"
},
"status": {
"type": "string",
"description": "Status of the API request (success or error)"
}
}
Testing Your AI Agent
Time to put your AI agent to the test! Here are a few example prompts:
- "What droplets do I have in my account?"
- "Tell me more about droplet [ID]"
The agent will call the function, get the real-time data from the DigitalOcean API, and deliver an intelligent response.
Troubleshooting for AI Agents
While testing, you might encounter these common issues:
- Incorrect API Responses:
- Double-check your DigitalOcean API token and permissions.
- Function Execution Errors:
- Inspect function logs using
doctl serverless logs
to identify runtime issues.
- Inspect function logs using
- Agent Not Responding Correctly:
- Confirm that the function is linked correctly to the agent and verify the schemas.
By combining the DigitalOcean GenAI Platform with serverless functions, you can create powerful, intelligent agents that provide real-time insights and transform your workflows. Enjoy!