Skip to main content

Python SDK for ExnestAI API service

Project description

ExnestAI Python SDK

PyPI version License: MIT

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, timeouts, and access to EBC (Exnest Brain Core) features, use the ExnestAI client.

import asyncio
import os
from exnestai import ExnestAI, ExnestMessage, EBCDecisionContext

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
    )

    # --- Standard Chat ---
    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 not chat_response.error:
        print(f"Chat Response: {chat_response.choices[0].message.content}")

    # --- EBC Deep Think Analysis ---
    # Performs advanced reasoning and decision making
    deep_think_response = await client.deep_think(
        messages=[ExnestMessage(role="user", content="Analyze the potential impact of quantum computing on cryptography")],
        model="deepseek-r1" # Optional: specify EBC model
    )
    if not deep_think_response.error:
        print(f"\nDeep Think Response: {deep_think_response.choices[0].message.content}")

    # --- EBC Structured Decision Making ---
    # Performs structured analysis based on decision context
    decision_context = EBCDecisionContext(
        decisionType="technical_architecture",
        criteria=["scalability", "maintainability", "cost"],
        constraints=["must be open source", "budget < $500/month"]
    )
    
    decision_response = await client.structured_decision(
        messages=[ExnestMessage(role="user", content="Should we migrate our database to NoSQL?")],
        context=decision_context
    )
    if not decision_response.error:
        print(f"\nStructured Decision: {decision_response.choices[0].message.content}")

    # --- EBC Task Delegation ---
    # Quick reasoning for task delegation and action dispatch
    delegate_response = await client.delegate(
        messages=[ExnestMessage(role="user", content="Email the team about the meeting delay")]
    )
    if not delegate_response.error:
        print(f"\nDelegation Response: {delegate_response.choices[0].message.content}")

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 ExnestWrapper for quick tasks and an advanced ExnestAI client for full control.
  • EBC Capabilities: Access Exnest Brain Core features like Deep Think, Structured Decision, and Task Delegation.
  • Async First: Fully asynchronous architecture using httpx for 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.

Currently Available Models

The services currently support the following AI models:

  • OpenAI: gpt-4.1, gpt-4.1-mini, gpt-4.1-nano
  • Google: gemini-2.0-flash, gemini-2.0-flash-lite, gemini-2.5-flash, gemini-2.5-pro
  • Moonshot: kimi-k2-0711-preview, kimi-k2-0905-preview, kimi-k2-turbo-preview, moonshot-v1-8k, moonshot-v1-32k, moonshot-v1-128k
  • LongCat: LongCat-Flash-Chat, LongCat-Flash-Thinking

Note: More models will be added in the future

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 to https://api.exnest.app/v1.
  • timeout (int): Optional. Request timeout in milliseconds. Defaults to 30000.
  • retries (int): Optional. Number of times to retry a failed request. Defaults to 3.
  • retry_delay (int): Optional. Delay between retries in milliseconds. Defaults to 1000.
  • debug (bool): Optional. Set to True to enable debug printing. Defaults to False.

Requirements

  • Python 3.8+
  • 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

exnest_ai-1.1.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

exnest_ai-1.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file exnest_ai-1.1.0.tar.gz.

File metadata

  • Download URL: exnest_ai-1.1.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for exnest_ai-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f5eac409db69bc5120b6afdaac3ba1fadef5a7064b465669fd592f07e4519498
MD5 8d3c3b6961c687a6a5c86509df19873f
BLAKE2b-256 35b9a31e535d7d44b63c4cc7f698eba202a233baa168ef2aacce0c828380eb21

See more details on using hashes here.

File details

Details for the file exnest_ai-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: exnest_ai-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for exnest_ai-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 124f7817625cad99208ea9d0b58d10c9dd6272add632d4fc61ce57f8ce7f5daa
MD5 c15245469a49aedb4219da34bc3a6b63
BLAKE2b-256 5fdbc3f43bc9cf87f08229af0d75be6abe9b4d063f7f7a6b2aad8e604124432f

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