Create Intelligent AI Agents: Real-Time Data from Serverless Functions
Harness the power of AI with real-time data! Learn how to build intelligent AI Agents using serverless functions for instant insights.
In today's fast-paced world, instant access to relevant information is crucial. This tutorial demonstrates how to leverage DigitalOcean's GenAI Platform and serverless functions to create AI powered agents that access and process real-time data.
Why Build AI Agents with Real-Time Data?
AI agents with live data access offer incredible possibilities, delivering up-to-the-minute answers and interactive solutions across diverse applications. Imagine an AI assistant that can instantly provide the status of your cloud infrastructure or automatically respond to customer inquiries with real-time data.
Who Benefits from Real-Time AI Agents?
- Developers: Gain instant insights into cloud infrastructure, improving resource management.
- Support Teams: Automate answers to common questions, enhancing customer service.
- System Administrators: Monitor DigitalOcean resources in real-time, receiving valuable alerts.
Benefits of integrating AI Agents with APIs:
- Guaranteed up-to-date information
- Automated repetitive tasks
- High degree of scalability
- Improved user experience
Prerequisites: Get Ready to Build
Before diving in, make sure you have these ready:
- DigitalOcean GenAI Platform: The core for building production-ready AI Agents.
- DigitalOcean Functions: Serverless environment for executing code (Python, Node.js, PHP, Go).
Step 1: Prepare Your Serverless Function
Your function will be the bridge to the DigitalOcean API, retrieving real-time data for your AI Agent.
-
Navigate to Functions in the DigitalOcean control panel and click “Create Namespace”.
-
Select a data center location (e.g., Toronto).
-
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
Now configure the function to retrieve droplet information from the DigitalOcean API.
-
Modify the project file to define the Python runtime and set security headers.
-
Create an environment file for your DigitalOcean API token.
-
Replace the "hello world" sample with your API function code that retrieves droplet information.
-
Create a build script for importing Python dependencies.
-
Deploy the function to make it available in the cloud:
doctl serverless deploy
After deployment, test your function through the web interface. For a complete example, check out this repository.
Step 3: Create Your AI Agent
Create your AI Agent, the intelligent brain that will utilize the real-time data. Choose between the web interface or a programmatic approach using the API.
Option 1: Create AI agent Using the Web Interface
An intuitive method ideal for users who prefer a visual approach.
- In the GenAI platform, click “Create Agent”.
- Name your agent (e.g., “Droplet Agent”).
- Provide agent instructions (system prompt) to define its purpose.
- Choose your preferred language model (e.g.,
Llama 3.3 Instruct-70B
). - Create the agent.
Option 2: Create AI agent Using the API
Programmatic creation providing flexibility and automation. Useful for custom integrations and large deployments.
Use the DigitalOcean API create an agent programmatically using curl
:
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"
}'
Remember to replace $API_TOKEN
and YOUR_PROJECT_ID
with your actual values.
Step 4: Connect Your Function to the Agent
Link your function for real-time access. This ensures your agent uses up-to-date information.
Option 1: Web Interface
Link the agent through the web interface
- Navigate to the Resources tab in the agent playground
- Add a function route
- Select your namespace and function
- Provide function instructions the agent called functions
- Define input and out put schemas
Option 2: API
Programmatically creating the agent through the API.
Connect functions programmatically using the DigitalOcean API. Here’s an example using curl
:
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 for Droplet Function
Specifies parameters the agent can send to your function (like droplet ID).
{
"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
Helps the agent interpret the returned data
{
"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)"
}
}
Step 5: Test Your Intelligent AI Agent
Now it's time to see your real–time AI agent in action!
Ask questions like:
- “What droplets do I have in my account?”
- “Tell me more about droplet [ID]”
The agent will call your function, retrieve real-time information from the DigitalOcean API, and provide you with an intelligent response.
By following these steps you can build a useful AI agent, and use serverless functions to retrieve real-time information on DigitalOcean account.