A Python SDK for PromptStudio
Project description
PromptStudio Python SDK
A Python SDK for interacting with PromptStudio API and AI platforms directly.
Installation
From PyPI (Coming Soon)
pip install promptstudio-sdk
From Source
git clone https://github.com/your-repo/promptstudio-sdk.git
cd promptstudio-sdk
pip install -e .
Development Setup
- Create a virtual environment:
python -m venv venv
- Activate the virtual environment:
# On Windows
venv\Scripts\activate
# On Unix or MacOS
source venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
Usage
Initializing the SDK
from promptstudio_sdk import PromptStudio
client = PromptStudio({
'api_key': 'YOUR_API_KEY',
'env': 'prod', # Use 'prod' for production environment
'bypass': True
})
Configuration Options
bypass
(default: False
)
The bypass
parameter determines whether to use the local AI provider directly or route requests through PromptStudio's API:
- When
bypass=True
: Requests go directly to the AI provider, bypassing PromptStudio's API - When
bypass=False
: Requests are routed through PromptStudio's API for additional processing and logging
is_session_enabled
(default: True
)
The is_session_enabled
parameter controls whether the chat maintains conversation history:
- When
True
: The conversation history is maintained between requests using the providedsession_id
- When
False
: Each request is treated independently with no conversation history
Example with explicit configuration:
client = PromptStudio({
'api_key': 'YOUR_API_KEY',
'env': 'prod',
'bypass': False # Route through PromptStudio API (default)
})
response = client.chat_with_prompt(
prompt_id="your_prompt_id",
user_message=[{"type": "text", "text": "Hello"}],
session_id="test_session",
is_logging=True # Enable conversation history (default)
)
Chatting with a Prompt
response = client.chat_with_prompt(
prompt_id="your_prompt_id",
user_message=[
{
"type": "text",
"text": "Hello, how are you?"
}
],
memory_type="fullMemory",
window_size=0,
session_id="test_session",
variables={},
is_session_enabled=True,
)
print(response)
Complete Example
from promptstudio_sdk import PromptStudio
def main():
# Initialize the client
client = PromptStudio({
'api_key': 'YOUR_API_KEY',
'env': 'test',
'bypass': True
})
try:
# Get all prompts
prompts = client.get_all_prompts("your_folder_id")
print("Available prompts:", prompts)
# Chat with a specific prompt
response = client.chat_with_prompt(
prompt_id="your_prompt_id",
user_message=[
{
"type": "text",
"text": "Hello, how are you?"
}
],
memory_type="windowMemory",
window_size=10,
session_id="test_session",
variables={},
is_session_enabled=True,
)
print("Chat response:", response)
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()
Testing
Setting Up Tests
- Install test dependencies:
pip install pytest pytest-cov
- Create a
.env
file in the root directory with your test credentials:
PROMPTSTUDIO_API_KEY=your_test_api_key
PROMPTSTUDIO_ENV=test
Running Tests
Run all tests:
pytest
Run tests with coverage:
pytest --cov=promptstudio_sdk
Writing Tests
Create test files in the tests
directory. Here's an example test:
import pytest
from promptstudio_sdk import PromptStudio
def test_chat_with_prompt():
client = PromptStudio({
'api_key': 'test_api_key',
'env': 'test',
'bypass': True
})
response = client.chat_with_prompt(
prompt_id: str,
user_message: List[Dict[str, Union[str, Dict[str, str]]]],
memory_type: str,
window_size: int,
session_id: str,
variables: Dict[str, str],
version: Optional[int] = None,
is_session_enabled: Optional[bool] = True,
)
assert isinstance(response, dict)
assert 'response' in response
Type Hints
The SDK uses Python type hints for better IDE support and code documentation. Here are some key types:
from typing import Dict, List, Union, Optional
# Message types
ImageMessage = Dict[str, Union[str, Dict[str, str]]] # {"type": "image_url", "image_url": {"url": "..."}}
TextMessage = Dict[str, str] # {"type": "text", "text": "..."}
UserMessage = List[Union[ImageMessage, TextMessage]]
# Memory types
Memory = Literal["fullMemory", "windowMemory"]
# Request payload
RequestPayload = Dict[str, Union[UserMessage, Memory, int, str, Dict[str, str], Optional[int]]]
Error Handling
The SDK raises exceptions for various error cases:
from promptstudio_sdk import PromptStudio
try:
client = PromptStudio({
'api_key': 'YOUR_API_KEY_from_promptstudio',
'env': 'test',
'bypass': True
})
response = client.chat_with_prompt(...)
except requests.exceptions.HTTPError as e:
print(f"HTTP error occurred: {e}")
except requests.exceptions.RequestException as e:
print(f"Network error occurred: {e}")
except Exception as e:
print(f"An error occurred: {e}")
Contributing
- Fork the repository
- Create a new branch for your feature
- Make your changes
- Run the tests to ensure everything works
- Submit a pull request
License
This SDK is released under the MIT License.
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
File details
Details for the file promptstudio_sdk-1.0.216.tar.gz
.
File metadata
- Download URL: promptstudio_sdk-1.0.216.tar.gz
- Upload date:
- Size: 35.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b521caf5ffe6413bea6c81cd43824cc0bc1e5f82632548c12a7ff0dce923b6af |
|
MD5 | 3253590688d5e463a63747dcf1ab3885 |
|
BLAKE2b-256 | 5e323f127769c5916d3aa7cca90648fcdee14ab4c06262be35c7ab2303b4624e |
File details
Details for the file promptstudio_sdk-1.0.216-py3-none-any.whl
.
File metadata
- Download URL: promptstudio_sdk-1.0.216-py3-none-any.whl
- Upload date:
- Size: 29.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae73e433d14fb0f5d42246559921f3c635224c83e9f740804bb1649bdbc38930 |
|
MD5 | 46d5171fc404dd52d55c1ec817f372e1 |
|
BLAKE2b-256 | e0be301310848d92ed806c295c59c3eac88bf607455498433be1b1edb249265e |