Effortlessly Build Real-Time AI Agents with GenAI and Serverless Functions
Want to build powerful AI agents that access live data? This tutorial demonstrates how to leverage GenAI and serverless functions to create intelligent applications. Learn how to create an AI agent to answer questions about your DigitalOcean account in real-time.
Why Build AI Agents with Real-Time Data Access?
In today's fast-paced environment, making informed decisions depends on having access to up-to-the-minute information. Real-time AI agents fill this need, offering immediate insights and driving better outcomes across industries:
- Finance: Get the edge with instant market analysis.
- Healthcare: Improve patient care using real-time data.
- E-commerce: Deliver personalized experiences that boost sales.
Unlock the Power of DigitalOcean's GenAI Platform and Serverless Functions
DigitalOcean's GenAI platform streamlines the creation of intelligent agents without the complexities of managing infrastructure. Combine this with serverless functions to build event-driven apps.
- Focus on AI: Concentrate on your AI models, not infrastructure.
- Streamlined Solutions: Build intelligent agents quickly and efficiently.
Use Cases: Real-Time AI Agents in Action
Real-time data transforms AI agents into indispensable tools across various roles:
- Developers: Monitor cloud infrastructure with ease and manage resources effectively.
- Support Teams: Automate responses to customer inquiries, improving service and reducing response times.
- System Administrators: Get real-time monitoring and alerts for DigitalOcean resources.
API integration Pros and Cons: Is It Worth the Effort?
Integrating real-time data via APIs into your AI agents provides:
- Increased accuracy: Provides up-to-date data for improved information.
- Automation: Reduces manual tasks and enables smarter decision-making.
- Scalability: Handle multiple requests effortlessly.
- Better User Experiences: Delivers intelligent and fast responses.
However, you should also be aware of:
- Potential API limits: Implement strategies to handle request limitations.
- Security: Protect sensitive data.
- Debugging complexity: Tackle real-time errors effectively.
Step-by-Step: Building Your AI Agent
Let's dive into the process of building an AI agent capable of fetching real-time data from the DigitalOcean API:
Prerequisites
- DigitalOcean GenAI Platform account (sign up at DigitalOcean GenAI Platform).
- DigitalOcean Functions enabled (more info at DigitalOcean Functions).
Step 1: Function Preparation
-
Navigate to Functions in the DigitalOcean control panel and create a Namespace.
-
Choose a data center location (e.g., Toronto).
-
Connect using the
doctl
command-line tool:doctl serverless connect
-
Initialize a Python project:
doctl serverless init --language python example-project
Step 2: Function Configuration
-
Modify your project to reflect a Python runtime.
-
Set security headers where needed.
-
Create an environment file for your DigitalOcean API token to make sure Functions retrieve data from your account securely (find your API Token here).
-
Replace the default sample with API function code to retrieve droplet information.
-
Create a build script to import dependencies.
-
Deploy your function:
doctl serverless deploy
Step 3: AI Agent Creation
You can create your AI agent through two convenient methods:
Option 1: Web Interface
- In the GenAI platform, click "Create Agent".
- Name your agent (e.g., "Droplet Agent").
- Define its purpose through agent 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 preferred language model (e.g.,
Llama 3.3 Instruct-70B
). - Create the agent.
Option 2: API
The API provides a programmatic approach. Try creating an agent 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"
}'
Step 4: Connecting Function to Agent
Choose either of these options:
Option 1: Web Interface
- Go to the Resources tab in the agent playground.
- Add a function route.
- Select your namespace and function.
- Write function instructions to guide the agent:
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.
- 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)"
}
}
}'
Testing and Debugging Your AI Agent
Pose questions to your agent such as:
- "What droplets do I have in my account?"
- "Tell me more about droplet [ID]"
If you encounter issues:
- API responses: Verify your API token.
- Function errors: Use
doctl serverless logs
to debug runtime issues. - Agent responsiveness: Double-check function links and schemas.
Elevate Your Applications With Real-Time AI Agents
By integrating AI agents with real-time data access using GenAI and serverless functions, you unlock new possibilities. Start building today and transform your applications into intelligent, responsive solutions.