LogoLogo
DocsGitHubDiscordOpenServ.ai
  • OpenServ Docs
  • Getting Started
    • Quickstart Guide
    • Choosing Your Method
      • No Code Builder
      • TypeScript SDK
      • OpenServ API
  • Developing Your AI Agent For OpenServ
  • How to...
    • Add Integrations in OpenServ
    • Connect Zapier MCP to OpenServ Agents
    • Create An Agent API Key
    • Deploy Your Agent To Production
  • Demos and Tutorials
    • Agents Examples
    • DexScreener
    • GOAT Wallet
    • Perplexity Sonar Pro Agent
    • API Python Agent
    • API TypeScript Agent
    • Multi AI Agents Demo
  • Resources
    • Getting Started With AI Agent Development
    • Code of Conduct
    • Contribution Guide
    • Designing Specialized Multi-Agent Systems
      • Multi-Agents Example
    • Human Assistance Request
    • MCP (Model Context Protocol) in OpenServ
    • Memory Management
    • Mental Model Shift: From Software Engineering to AI Agent Development
    • Secret Management
    • Tools and Capabilities
    • TypeScript SDK Framework Architecture
Powered by GitBook
LogoLogo

Product

  • OpenServ.ai
  • YouTube

Community

  • Discord
  • X

© 2025 OpenServ Labs Ltd. All Rights Reserved.

On this page
  • Overview
  • Prerequisites and Setup
  • System Requirements
  • Setup Instructions
  • Running the Agent
  • Conceptual Understanding
  • What is the OpenServ API?
  • Key Components of This Implementation
  • Technical Architecture
  • Project Structure Explained
  • Key Implementation Highlights
  • Secure API Communication
  • Task Processing Example
  • Troubleshooting
  • Common Issues
  • Learning Resources

Was this helpful?

Edit on GitHub
Export as PDF
  1. Demos and Tutorials

API Python Agent

PreviousPerplexity Sonar Pro AgentNextAPI TypeScript Agent

Last updated 2 months ago

Was this helpful?

Overview

This repository provides a comprehensive example of building an AI agent for the OpenServ platform using OpenServ API, Python and FastAPI. Building upon the OpenServ API Development Guide, this implementation serves as a practical reference for developers looking to create intelligent agents that can handle tasks and respond to chat messages.

Need more details? Check our step-by-step guide on

Prerequisites and Setup

System Requirements

  • Python 3.8+

  • pip package manager

  • OpenAI API access

  • OpenServ Agent API Key

Setup Instructions

  1. Clone the repository

    git clone https://github.com/openserv-labs/agent-tutorial.git
    cd python-api-agent-example
  2. Virtual Environment Setup

    # Create virtual environment
    python3 -m venv venv
    
    # Activate virtual environment
    # Unix/macOS
    source venv/bin/activate
    
    # Windows
    venv\Scripts\activate
  3. Dependencies Installation

    # Install project dependencies
    pip install -r requirements.txt
    
    # Upgrade pip and key libraries
    pip install --upgrade pip
    pip install --upgrade openai
  4. Configuration Create a .env file in your project root:

    OPENAI_API_KEY=your_openai_api_key
    OPENSERV_API_KEY=your_openserv_api_key
    API_BASE_URL=https://api.openserv.ai
    PORT=7378  # Configurable port

Running the Agent

# Navigate to project directory
cd python-api-agent-example

# Start the FastAPI server
python src/main.py

Conceptual Understanding

What is the OpenServ API?

An OpenServ API is a RESTful API that allows you to create and manage agents on the OpenServ platform. It provides a standardized way to interact with the OpenServ platform through a HTTP request.

  • Receive and process tasks dynamically

  • Respond to chat messages

  • Interact with the OpenServ platform through a standardized API

  • Leverage AI capabilities to complete assigned objectives

Key Components of This Implementation

Our Python agent demonstrates several critical aspects of agent development:

  1. Task Handling: Ability to receive, process, and complete complex tasks

  2. Chat Interaction: Manage conversational interfaces

  3. File Management: Upload and handle file-based outputs

  4. Secure Communication: Implement SSL and API security

  5. Asynchronous Processing: Manage concurrent operations

Technical Architecture

Project Structure Explained

python-api-agent-example/
├── src/
│   ├── __init__.py          # Package initialization
│   ├── main.py              # FastAPI application entry point
│   ├── api.py               # API client configuration
│   ├── do_task.py           # Task handling implementation
│   └── respond_chat_message.py  # Chat message handling
└── requirements.txt         # Python dependencies

Deep Dive into Components

  1. main.py:

    • Serves as the primary application entry point

    • Configures FastAPI server

    • Sets up endpoint routes for task and chat interactions

  2. api.py:

    • Manages API client configuration

    • Handles secure communication with OpenServ platform

    • Implements SSL certificate management

    • Provides utility methods for API interactions

  3. do_task.py:

    • Implements core task processing logic

    • Handles task reception, processing, and completion

    • Uses OpenAI API for task resolution

  4. respond_chat_message.py:

    • Manages incoming chat messages

    • Generates contextually appropriate responses

    • Maintains conversation state and context

Key Implementation Highlights

Secure API Communication

The implementation emphasizes security through:

  • SSL certificate verification using certifi

  • Secure session management with aiohttp

  • Robust error handling mechanisms

import ssl
import certifi

# Create a secure SSL context
ssl_context = ssl.create_default_context(cafile=certifi.where())
connector = aiohttp.TCPConnector(ssl=ssl_context)
session = aiohttp.ClientSession(connector=connector)

Task Processing Example

# Upload task result
await api_client.upload_file(
    workspace_id=workspace_id,
    file_content=result,
    filename=f'task-{task_id}-output.txt',
    path='text-summary.txt',
    task_ids=[task_id],
    skip_summarizer=True
)

# Mark task as complete
await api_client.put(
    f"/workspaces/{workspace_id}/tasks/{task_id}/complete",
    {'output': "Task successfully processed"}
)

Troubleshooting

Common Issues

  • Ensure all environment variables are correctly set

  • Verify API key permissions

  • Check network connectivity

  • Validate SSL certificate configurations

Learning Resources

Built with ❤️ by

how to use OpenServ API
OpenServ API Documentation
FastAPI Official Documentation
aiohttp Async HTTP Client
OpenServ Labs