A simple Python SDK for interacting with the optycode API.
Project description
optycode SDK
A lightweight Python SDK to interact with the optycode API for logging model interactions and analytics.
Installation
pip install optycode-sdk
Getting Started
Authentication
To use the optycode SDK, you'll need an authentication token and model ID. Both can be obtained from your optycode account dashboard.
- Auth Token: Your API authentication token (available in your optycode account)
- Model ID: The identifier for your model (available in your optycode account)
Basic Usage
from optycode_sdk import OptycodeAPI
# Initialize the client with your auth token
client = OptycodeAPI(auth_token="your-auth-token-here")
# Log a simple interaction
client.log_data(
user_question="What is machine learning?",
model_answer="Machine learning is a subset of artificial intelligence...",
model_id="your-model-id-here"
)
Parameters
The log_data and log_data_async methods accept the following parameters:
user_question(required): The user's question or inputmodel_answer(required): The model's responsemodel_id(required): Your model identifier from optycode accountsession_id(optional): Session identifier for grouping related interactionsmodel_input(optional): The actual input sent to the model (defaults touser_questionif not provided)question_id(optional): Unique identifier for the questionrag_elements(optional): RAG (Retrieval-Augmented Generation) elements usedattachment(optional): Binary attachment data (e.g., images, documents)signed_url(optional): only if attachement is not present, it's retourned from the upload_attachement function
Production Usage
Using log_data_async for Production
For production environments, always use log_data_async instead of log_data. The async method:
- Uses a very short timeout to avoid blocking your application
- Doesn't wait for the server response, improving performance
- Is designed for high-throughput scenarios where you don't need immediate confirmation
We also recommend always logging a session_id and a question_id, for better visualizations in the dashboard
from optycode_sdk import OptycodeAPI
client = OptycodeAPI(auth_token="your-auth-token-here")
# Production-ready async logging
client.log_data_async(
user_question="What is the weather today?",
model_answer="The weather is sunny with a high of 75°F.",
model_id="your-model-id-here",
session_id="session-123",
question_id=42
)
Handling Attachments Separately
If your attachment processing happens in a different part of your codebase than your model processing, use upload_attachment to upload the file first, then pass the returned signed_url to log_data_async.
Use case: You process attachments in one service/module, and log model interactions in another.
from optycode_sdk import OptycodeAPI
client = OptycodeAPI(auth_token="your-auth-token-here")
# Upload attachment separately (e.g., in a different service/module)
with open("document.pdf", "rb") as f:
attachment_data = f.read()
signed_url = client.upload_attachement(
attachment=attachment_data,
model_id="your-model-id-here",
question_id=42,
session_id="session-123"
)
# Later, in a different part of your code, log the interaction with the signed URL
client.log_data_async(
user_question="Analyze this document",
model_answer="The document contains...",
model_id="your-model-id-here",
session_id="session-123",
question_id=42,
signed_url=signed_url # Use the pre-uploaded attachment
)
Complete Example with Attachment
If you have the attachment available at the same time as logging:
from optycode_sdk import OptycodeAPI
client = OptycodeAPI(auth_token="your-auth-token-here")
# Read attachment
with open("image.png", "rb") as f:
image_data = f.read()
# Log with attachment (handled automatically)
client.log_data_async(
user_question="What's in this image?",
model_answer="The image shows a cat sitting on a mat.",
model_id="your-model-id-here",
session_id="session-123",
question_id=42,
attachment=image_data
)
Error Handling
The SDK will raise exceptions if:
- The authentication token is invalid
- The API request fails
- Required parameters are missing
Always wrap your calls in try-except blocks for production code:
try:
client.log_data_async(
user_question="Test question",
model_answer="Test answer",
model_id="your-model-id-here"
)
except Exception as e:
# Handle error appropriately
print(f"Failed to log data: {e}")
Notes
- The SDK automatically verifies your token upon initialization
- All timestamps are automatically generated
- The
log_data_asyncmethod is fire-and-forget, so it won't block your application - For best performance in production, always use
log_data_async
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 optycode_sdk-1.1.1.tar.gz.
File metadata
- Download URL: optycode_sdk-1.1.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa18f71800057b88058124029368e493cd40740e033492ce0642ba19c4297889
|
|
| MD5 |
ed1622cb28b41dcbd8418b4647170da6
|
|
| BLAKE2b-256 |
56c0710385bf4d2e1f237d8f0b322c5e8ae48d0861ba80db01d2f8dd987afd6f
|
File details
Details for the file optycode_sdk-1.1.1-py3-none-any.whl.
File metadata
- Download URL: optycode_sdk-1.1.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88eb076a085607a097064b9fbb38ef2682be1fbecb2140f75615f8c31e59480f
|
|
| MD5 |
453fb3c0f3a596d7995ddf562cab9397
|
|
| BLAKE2b-256 |
70f403877de8102756212c6b9f78f539094c711affd208869423b1d2da28cb3d
|