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
client.register_agent(
    name="My Agent",
    category="general",
    description="A general purpose agent",
    logo="https://example.com/logo.png",  # Optional
    metadata={"version": "1.0"}  # Optional additional metadata
)

# Define message handler function
def handle_message(message):
    """
    Handle messages received from the server
    
    Message format:
    {
        "sessionId": "session-id",
        "timestamp": 12345678,  # Timestamp in milliseconds
        "content": "message content"
    }
    """
    print(f"Message received: {message['content']}")
    
    # Reply to message
    client.send_message(
        content="This is a reply message"
    )
    
    # You can also reply with an image
    # client.send_message(
    #     content="This is a reply with an image",
    #     image_url="https://example.com/image.jpg"
    # )

# 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",
    category="general",
    description="Agent description",
    logo="https://example.com/logo.png",  # Optional
    metadata={"version": "1.0", "author": "Your Name"}  # Optional
)

Listening for Messages

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

# 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")

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

Stopping the Listener

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

Unregistering an Agent

client.unregister_agent(name="My Agent")

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", category="general", 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.6.tar.gz (10.7 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.6-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pinai_agent_sdk-0.1.6.tar.gz
  • Upload date:
  • Size: 10.7 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.6.tar.gz
Algorithm Hash digest
SHA256 f65ec5d76dc4eaeb51b79141dc922f82e25d170c5685c8442efcddc823c96930
MD5 5eadf87712ec5294a10ace87f1191b5e
BLAKE2b-256 0a6cc04c8459f523889c19288db9c2f118df67c1d790fceb1baef0f2b3d12e37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pinai_agent_sdk-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 bf7fc7af84e6578aa74f96c83d71f96b1b38679ea18ca8c9a2f9a68c5ebd7626
MD5 b242c8fb947be105911b07930e7c61cf
BLAKE2b-256 60206aeb353732e98edc8801f31f61138252a2342d33f42a46e89f47b23ca5c3

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