Skip to main content

SDK for PINAI Agent API

Project description

PINAI Agent SDK

PINAI Agent SDK is the official Python SDK for the PINAI platform, enabling developers to easily build, register, and manage PINAI Agents with seamless platform integration.

Installation

Install PINAI Agent SDK using pip:

pip install pinai-agent-sdk

Basic Usage

Here's a basic example of using the PINAI Agent SDK:

from pinai_agent_sdk import PINAIAgentSDK

# Initialize SDK
client = PINAIAgentSDK(
    api_key="your-pinai-api-key"  # Replace with your PINAI API Key
)

# Register a new agent
agent_info = client.register_agent(
    name="My Agent",
    ticker="MYAG",
    description="A general purpose agent",
    cover="https://example.com/cover.png",  # Optional
    metadata={"version": "1.0"}  # Optional additional metadata
)

# Print agent ID
print(f"Agent registered with ID: {agent_info['id']}")

# Define message handler function
def handle_message(message):
    """
    Handle messages received from the server
    
    Message format:
    {
        "session_id": "session-id",
        "id": 12345,  # Message ID
        "content": "message content",
        "created_at": "2025-03-05T12:30:00"  # ISO 8601 timestamp
    }
    """
    print(f"Message received: {message['content']}")
    session_id = message["session_id"]
    
    # Reply to message
    client.send_message(
        content="This is a reply message",
        session_id=session_id
    )
    
    # You can also reply with an image
    # First upload the image
    # media_result = client.upload_media("path/to/image.jpg", "image")
    # Then send a message with the image
    # client.send_message(
    #     content="This is a reply with an image",
    #     session_id=session_id,
    #     media_type="image",
    #     media_url=media_result["media_url"]
    # )

# Start listening for new messages (non-blocking by default)
client.start(on_message_callback=handle_message)

# Keep the application running until interrupted
# Option 1: Use run_forever() method (recommended)
client.run_forever()

# Option 2: Use blocking mode
# client.start(on_message_callback=handle_message, blocking=True)

Key Features

Initializing the SDK

client = PINAIAgentSDK(
    api_key="your-pinai-api-key",
    base_url="https://dev-web.pinai.tech/",  # Optional, defaults to https://dev-web.pinai.tech/
    timeout=30,  # Optional, request timeout in seconds, defaults to 30
    polling_interval=1.0  # Optional, interval in seconds between message polls, defaults to 1.0
)

Registering an Agent

response = client.register_agent(
    name="My Agent",
    ticker="MYAG",  # Usually 4 uppercase letters
    description="Agent description",
    cover="https://example.com/cover.png",  # Optional, cover image URL
    metadata={"version": "1.0", "author": "Your Name"}  # Optional
)

# Response contains the agent_id
agent_id = response["id"]

Listening for Messages

def handle_message(message):
    # Process received message
    print(f"Message received: {message}")
    session_id = message["session_id"]
    
    # Reply to message
    client.send_message(content="Reply content", session_id=session_id)

# Start listening for new messages in the background
client.start(on_message_callback=handle_message)

# To start in blocking mode (will not return until stopped)
# client.start(on_message_callback=handle_message, blocking=True)

# Keep the application running until interrupted
client.run_forever()  # This method will block until KeyboardInterrupt

Sending Messages

# Send text-only message
client.send_message(
    content="This is a message",
    session_id="session_12345"
)

# Send message with image
client.send_message(
    content="This is a message with an image",
    session_id="session_12345",
    media_type="image",
    media_url="https://example.com/image.jpg"
)

Uploading Media

# Upload an image
media_result = client.upload_media("path/to/image.jpg", "image")
image_url = media_result["media_url"]

# Upload other types of media
# Supported media types: "image", "video", "audio", "file"
video_result = client.upload_media("path/to/video.mp4", "video")

Getting Persona Information

# Get persona information associated with a session
persona = client.get_persona(session_id="session_12345")
print(f"Persona name: {persona['name']}")

Stopping the Listener

# Stop listening for messages and clean up resources
client.stop()

Unregistering an Agent

# Using the registered agent
client.unregister_agent()

# Or specify an agent_id
client.unregister_agent(agent_id=123)

Exception Handling

The SDK will raise exceptions when errors occur. It's recommended to use try-except blocks to handle potential exceptions:

try:
    client.register_agent(name="My Agent", ticker="MYAG", description="Agent description")
except Exception as e:
    print(f"Error registering agent: {e}")

Thread Safety

The SDK uses threading internally for message polling, ensure proper usage in multi-threaded environments.

Logging

The SDK uses the Python standard library's logging module. To customize the log level:

import logging
logging.getLogger("PINAIAgentSDK").setLevel(logging.DEBUG)

License

This SDK is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pinai_agent_sdk-0.1.8.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pinai_agent_sdk-0.1.8-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file pinai_agent_sdk-0.1.8.tar.gz.

File metadata

  • Download URL: pinai_agent_sdk-0.1.8.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for pinai_agent_sdk-0.1.8.tar.gz
Algorithm Hash digest
SHA256 d57f76f37b12500b40cb69dad46b1afc575906aed7e0ac326990014b0c49ffa3
MD5 721696ed3d5b0781f46077b990b58bf1
BLAKE2b-256 86309948962bb3e4a02e7d85f7d154ef5493a6893b372a767844ffb3a48bea44

See more details on using hashes here.

File details

Details for the file pinai_agent_sdk-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for pinai_agent_sdk-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 0d4d268246efeead9a302fd970aee33b333a56e71421f8c3a1638f00cfc7ddc4
MD5 383e0442a2c9bfa9aeeb324c520776e5
BLAKE2b-256 ba1cc4ea9f0594cccf5dae547bbf39ae647841472b96ffe2521542e548f1b3a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page