Python SDK for ExnestAI API service
Project description
ExnestAI Python SDK
This is the Python SDK for the ExnestAI API service. It provides both a simple wrapper and an advanced, fully-featured client for interacting with the ExnestAI API, with a response format compatible with OpenAI.
Installation
pip install exnest-ai
Quick Start
Make sure to set your API key as an environment variable:
export EXNEST_API_KEY="your-api-key-here"
Simple Wrapper Usage
For straightforward use cases, the ExnestWrapper provides a simple interface.
import asyncio
import os
from exnestai import ExnestWrapper, ExnestMessage
async def main():
# Initialize the wrapper
api_key = os.getenv("EXNEST_API_KEY")
exnest = ExnestWrapper(api_key=api_key)
# Perform a chat completion
chat_response = await exnest.chat(
model="openai:gpt-4o-mini",
messages=[ExnestMessage(role="user", content="Hello, how are you?")]
)
if not chat_response.error:
print(f"Wrapper Chat Response: {chat_response.choices[0].message.content}")
# Perform a text completion
completion_response = await exnest.completion(
model="openai:gpt-4o-mini",
prompt="What is the capital of France?",
max_tokens=50
)
if not completion_response.error:
print(f"Wrapper Completion Response: {completion_response.choices[0].text}")
if __name__ == "__main__":
asyncio.run(main())
Advanced Client Usage
For full control over configuration, retries, and timeouts, use the ExnestAI client.
import asyncio
import os
from exnestai import ExnestAI, ExnestMessage
async def main():
api_key = os.getenv("EXNEST_API_KEY")
# Initialize the advanced client
client = ExnestAI(
api_key=api_key,
timeout=60000, # 60 seconds
retries=2,
debug=True
)
# Perform a chat completion with Exnest metadata
chat_response = await client.chat(
model="openai:gpt-4o-mini",
messages=[ExnestMessage(role="user", content="Hello! What can you tell me about ExnestAI?")],
exnest_metadata=True
)
if chat_response.error:
print(f"API Error: {chat_response.error.message}")
else:
print(f"Advanced Chat Response: {chat_response.choices[0].message.content}")
if chat_response.exnest and chat_response.exnest.billing:
print(f"Cost: {chat_response.exnest.billing.actual_cost_usd} USD")
if __name__ == "__main__":
asyncio.run(main())
Streaming Responses
Both the client and wrapper support streaming for real-time data processing.
import asyncio
import os
from exnestai import ExnestAI, ExnestMessage
async def stream_demo():
api_key = os.getenv("EXNEST_API_KEY")
client = ExnestAI(api_key=api_key)
print("\n--- Streaming Chat Completion ---")
print("Streaming response: ", end="")
try:
async for chunk in client.stream(
model="openai:gpt-4o-mini",
messages=[ExnestMessage(role="user", content="Tell me a fun fact about Python programming.")]
):
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print("\nStreaming complete.")
except Exception as e:
print(f"\nAn error occurred during streaming: {e}")
if __name__ == "__main__":
asyncio.run(stream_demo())
Features
- OpenAI-Compatible: Response formats are compatible with OpenAI's, allowing for easy integration.
- Dual Clients: Choose between a simple
ExnestWrapperfor quick tasks and an advancedExnestAIclient for full control. - Async First: Fully asynchronous architecture using
httpxfor high performance. - Streaming Support: Built-in support for Server-Sent Events (SSE) for real-time responses.
- Retry Logic: The advanced client includes automatic retries for transient network errors.
- Model Management: Fetch lists of available models.
- Billing Metadata: Option to receive detailed billing and transaction information with each request.
Authentication
The SDK uses Bearer Token authentication by passing your API key to the client constructor. It will be sent in the Authorization header.
Configuration
The ExnestAI client can be configured during initialization:
api_key(str): Required. Your ExnestAI API key.base_url(str): Optional. The base URL for the API. Defaults tohttps://api.exnest.app/v1.timeout(int): Optional. Request timeout in milliseconds. Defaults to30000.retries(int): Optional. Number of times to retry a failed request. Defaults to3.retry_delay(int): Optional. Delay between retries in milliseconds. Defaults to1000.debug(bool): Optional. Set toTrueto enable debug printing. Defaults toFalse.
Requirements
- Python 3.7+
httpx
Development
To set up the development environment:
# Clone the repository
git clone https://github.com/fazza-abiyyu/exnest-ai-sdk-py.git
cd exnest-ai-sdk-py
# Install dependencies
pip install -r requirements.txt
# Install for local development
pip install -e .
Testing
To run tests:
pytest
Contributing
Contributions are welcome! Please feel free to fork the repository, make changes, and submit a pull request.
License
This project is licensed under the MIT License.
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
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 exnest_ai-1.0.4.tar.gz.
File metadata
- Download URL: exnest_ai-1.0.4.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0675df0fcfbddcee5289522097389e0725df2210508bb0f0c7e9c7e821fad82d
|
|
| MD5 |
2c25f00f11718cfe1ad049b5d2f44b66
|
|
| BLAKE2b-256 |
a33eb6416f0a9f7554f9d480d7e0aa45bdc63558dd83d8d8a94f0af1bf0cdd62
|
File details
Details for the file exnest_ai-1.0.4-py3-none-any.whl.
File metadata
- Download URL: exnest_ai-1.0.4-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
601e6ddc619863fa6151a0cf246d721113f0f11049ec5e05a64e459d16a89c35
|
|
| MD5 |
289e6ec439f9a17ee19d7c3e59b1a71b
|
|
| BLAKE2b-256 |
869587fe538d1d376fa831e6fea4a53146fa376ae2f97acdbf79f78d7e64d682
|