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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pinai_agent_sdk-0.1.7.tar.gz.
File metadata
- Download URL: pinai_agent_sdk-0.1.7.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39811163a6f714363dc34b230e89e2af95b33115c98b343ecc1660dbe4975faf
|
|
| MD5 |
c227dd4d1aa8fa0e36599c6b22ae7f41
|
|
| BLAKE2b-256 |
75989036ecccade5c7183e40c444213dafdf5253a86758f3f5c1ffb74b952106
|
File details
Details for the file pinai_agent_sdk-0.1.7-py3-none-any.whl.
File metadata
- Download URL: pinai_agent_sdk-0.1.7-py3-none-any.whl
- Upload date:
- Size: 16.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2e56cb97267353372534a1427d8899c636c1738a0efd68eb11f65b0e53f8f4a
|
|
| MD5 |
ceaae350a10e017cb26b1ed75b3d66e0
|
|
| BLAKE2b-256 |
27df391e6945babc38c5864c9994eae48515bc73b19dc635ee8b29830c734bcf
|