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.12.tar.gz (19.5 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.12-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pinai_agent_sdk-0.1.12.tar.gz
  • Upload date:
  • Size: 19.5 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.12.tar.gz
Algorithm Hash digest
SHA256 c3ba3c11c3c9367ed227396ac79effae8a044ee1eded018aa06329de88d6a039
MD5 6e8ac1b237c490f400b12d4d2c8eabe4
BLAKE2b-256 4462f425563456029f510d1fa2ba167c663a5cc6d4e8a17c4faa29de8995bbaf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pinai_agent_sdk-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 0de9413e29682f865c65266bc7d2b5167a3959b6880fff3c4d998905f32e6598
MD5 10ebc148e3923dc3bb8cadbfdbd3fe4e
BLAKE2b-256 5e6d58c142ebacff89c65c8ad40ee695fd77c76b4c97573940400c62f3aa7b79

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