Skip to main content

SHADAI Client

Project description

Shadai Client - Official Python SDK

Python 3.8+ License: MIT

Beautiful, Pythonic client for Shadai AI services. Query your knowledge base, orchestrate custom tools with intelligent agents, and more with an intuitive, production-ready API.

๐Ÿ†• What's New in v0.1.29

  • ๐Ÿง  Memory enabled by default - All tools now use conversation memory by default for better context continuity
  • ๐Ÿ’ฌ Chat history management - New methods to retrieve and clear session history with pagination
  • ๐Ÿ“– Enhanced documentation - Updated examples and API reference with new features

โœจ Features

  • ๐Ÿš€ Easy to use - Clean, intuitive API design with one-step async patterns
  • โšก Async streaming - Real-time streaming responses for all tools
  • ๐Ÿ› ๏ธ Multiple tools - Query, summarize, search, engine, and intelligent agent
  • ๐Ÿค– Intelligent agent - Automatic plan โ†’ execute โ†’ synthesize workflow
  • ๐Ÿง  Conversation memory - Built-in memory enabled by default for context continuity
  • ๐Ÿ’ฌ Chat history management - Get and clear session history with pagination
  • ๐Ÿ”’ Type-safe - Full type hints and Pydantic models for better IDE support
  • ๐Ÿ“ฆ Minimal dependencies - Only requires aiohttp and pydantic
  • ๐ŸŽฏ Production-ready - Comprehensive error handling

๐Ÿ“ฆ Installation

pip install shadai-client

Or install from source:

cd client
pip install -e .

๐Ÿš€ Quick Start

import asyncio
from shadai import Shadai

async def main():
    # Create session and query
    async with Shadai(name="my-session") as shadai:
        # Ingest documents
        await shadai.ingest(folder_path="./documents")

        # Query knowledge base
        async for chunk in shadai.query("What is machine learning?"):
            print(chunk, end="", flush=True)

asyncio.run(main())

๐Ÿ“š Usage Examples

Knowledge Base Query

Query your uploaded documents using RAG (Retrieval-Augmented Generation):

async with Shadai(name="research") as shadai:
    # Ingest documents
    await shadai.ingest(folder_path="./documents")

    # Ask questions about your documents
    async for chunk in shadai.query("What are the key findings?"):
        print(chunk, end="", flush=True)

    # Memory is enabled by default for context continuity
    async for chunk in shadai.query("Tell me more about that"):
        print(chunk, end="", flush=True)

    # Disable memory if needed
    async for chunk in shadai.query("Independent question", use_memory=False):
        print(chunk, end="", flush=True)

Document Summarization

Generate comprehensive summaries of all documents in a session:

async with Shadai(name="research") as shadai:
    await shadai.ingest(folder_path="./documents")

    async for chunk in shadai.summarize():
        print(chunk, end="", flush=True)

Web Search

Search the internet for current information:

async with Shadai(name="news") as shadai:
    async for chunk in shadai.web_search("Latest AI developments 2024"):
        print(chunk, end="", flush=True)

Chat History Management

Retrieve and manage conversation history for sessions:

async with Shadai(name="chat") as shadai:
    # Get chat history with pagination
    history = await shadai.get_session_history(
        page=1,
        page_size=5  # Default: 5, Max: 10
    )

    print(f"Total messages: {history['count']}")
    for message in history["results"]:
        print(f"{message['role']}: {message['content']}")

    # Clear all chat history
    result = await shadai.clear_session_history()
    print(result["message"])  # "Session history cleared successfully"

Unified Engine

Orchestrate multiple tools for comprehensive answers:

async with Shadai(name="analysis") as shadai:
    await shadai.ingest(folder_path="./documents")

    async for chunk in shadai.engine(
        prompt="Compare my docs with current trends",
        use_knowledge_base=True,
        use_web_search=True
    ):
        print(chunk, end="", flush=True)

Intelligent Agent

Orchestrate plan โ†’ execute โ†’ synthesize workflow with custom tools. The planner automatically infers tool arguments from your prompt!

from shadai import Shadai, tool

# Define tools using @tool decorator
@tool
def search_database(query: str, limit: int = 10) -> str:
    """Search database for user information.

    Args:
        query: Search query string
        limit: Maximum number of results
    """
    # Your implementation
    return "Search results..."

@tool
def generate_report(data: str, format: str = "text") -> str:
    """Generate a formatted report.

    Args:
        data: Data to include in report
        format: Report format (text, pdf, etc.)
    """
    # Your implementation
    return "Generated report..."

# Agent automatically infers arguments from your prompt!
async with Shadai(name="agent") as shadai:
    async for chunk in shadai.agent(
        prompt="Find the top 5 users and create a PDF report",
        tools=[search_database, generate_report]
    ):
        print(chunk, end="", flush=True)

The agent automatically:

  1. Plans which tools to use based on your prompt
  2. Infers the appropriate arguments for each tool from your prompt
  3. Executes the selected tools locally with inferred arguments
  4. Synthesizes all outputs into a unified, coherent answer

๐Ÿ”ง Configuration

Environment Variables

export SHADAI_API_KEY="your-api-key"
export SHADAI_BASE_URL="http://localhost"  # Optional

Client Initialization

# Named session (persistent)
async with Shadai(name="my-project") as shadai:
    await shadai.ingest(folder_path="./docs")

# Temporal session (auto-deleted)
async with Shadai(temporal=True) as shadai:
    async for chunk in shadai.query("Quick question"):
        print(chunk, end="")

# Custom configuration
async with Shadai(
    name="custom",
    api_key="your-api-key",
    base_url="https://api.shadai.com",
    timeout=60  # seconds
) as shadai:
    pass

๐Ÿ› ๏ธ Available Tools

Tool Description Streaming
query() Query knowledge base with RAG โœ…
summarize() Summarize all session documents โœ…
web_search() Search web for current information โœ…
engine() Unified multi-tool orchestration โœ…
agent() Intelligent agent (plan โ†’ execute โ†’ synthesize) โœ…
get_session_history() Retrieve chat history with pagination โŒ
clear_session_history() Clear all messages in a session โŒ

๐Ÿ“– API Reference

Shadai

Main entry point for the client.

Shadai(
    name: str = None,
    temporal: bool = False,
    api_key: str = None,
    base_url: str = "http://localhost",
    timeout: int = 30
)

Methods:

  • ingest(folder_path) โ†’ Dict - Ingest documents from folder
  • query(query, use_memory=True) โ†’ AsyncIterator[str] - Query knowledge base
  • summarize(use_memory=True) โ†’ AsyncIterator[str] - Summarize documents
  • web_search(prompt, use_web_search=True, use_memory=True) โ†’ AsyncIterator[str] - Search web
  • engine(prompt, **options) โ†’ AsyncIterator[str] - Unified engine
  • agent(prompt, tools) โ†’ AsyncIterator[str] - Intelligent agent
  • get_session_history(page=1, page_size=5) โ†’ Dict[str, Any] - Get chat history
  • clear_session_history() โ†’ Dict[str, str] - Clear chat history

query()

Query your knowledge base with streaming responses. Memory enabled by default.

async with Shadai(name="docs") as shadai:
    async for chunk in shadai.query("What is AI?", use_memory=True):
        print(chunk, end="")

summarize()

Summarize all documents in a session. Memory enabled by default.

async with Shadai(name="docs") as shadai:
    async for chunk in shadai.summarize(use_memory=True):
        print(chunk, end="")

web_search()

Search the web for current information. Memory enabled by default.

async with Shadai(name="search") as shadai:
    async for chunk in shadai.web_search("Latest AI news"):
        print(chunk, end="")

engine()

Unified engine with multiple tool capabilities. Memory enabled by default.

async with Shadai(name="engine") as shadai:
    async for chunk in shadai.engine(
        prompt="Analyze my documents",
        use_knowledge_base=True,
        use_web_search=True
    ):
        print(chunk, end="")

get_session_history()

Retrieve chat history with pagination support.

async with Shadai(name="chat") as shadai:
    history = await shadai.get_session_history(page=1, page_size=5)

    # Response includes: count, next, previous, results
    for msg in history["results"]:
        print(f"{msg['role']}: {msg['content']}")

clear_session_history()

Clear all messages in a session.

async with Shadai(name="chat") as shadai:
    result = await shadai.clear_session_history()
    # Returns: {"message": "Session history cleared successfully"}

agent()

Intelligent agent that orchestrates custom tools.

from shadai import tool

@tool
def my_tool(param: str) -> str:
    """Tool description."""
    return "result"

async with Shadai(name="agent") as shadai:
    async for chunk in shadai.agent(
        prompt="Execute task",
        tools=[my_tool]
    ):
        print(chunk, end="")

๐Ÿšจ Error Handling

from shadai import (
    Shadai,
    AuthenticationError,
    ConnectionError,
    ServerError
)

try:
    shadai = Shadai(api_key="invalid-key")
    await shadai.health()
except AuthenticationError:
    print("Invalid API key")
except ConnectionError:
    print("Cannot connect to server")
except ServerError as e:
    print(f"Server error: {e}")

๐Ÿ“ Examples

Check the examples/ directory for complete examples:

  • query_example.py - Knowledge base queries
  • summary_example.py - Document summarization
  • websearch_example.py - Web search
  • agent_example.py - Intelligent agent workflow
  • agent_synthesis_example.py - Advanced synthesis with multiple data sources
  • complete_workflow.py - Full agent workflow with multiple examples

Run examples:

cd client/
python examples/query_example.py
python examples/summary_example.py
python examples/websearch_example.py
python examples/agent_example.py
python examples/agent_synthesis_example.py
python examples/complete_workflow.py

๐Ÿ”’ Security

  • Always use environment variables for API keys
  • Never commit API keys to version control
  • Use HTTPS in production (base_url="https://...")

๐Ÿค Contributing

Contributions welcome! Please read our contributing guidelines.

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿ”— Links

๐Ÿ’ก Support


Made with โค๏ธ by the Shadai Team

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

shadai-0.1.31.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

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

shadai-0.1.31-py3-none-any.whl (29.6 kB view details)

Uploaded Python 3

File details

Details for the file shadai-0.1.31.tar.gz.

File metadata

  • Download URL: shadai-0.1.31.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shadai-0.1.31.tar.gz
Algorithm Hash digest
SHA256 eb863eb7f0c0e2e5905adf5993c1e313c847d3db4b96fe226ad6898f535c8c90
MD5 5c5b2d39bb900b11a6a9ff7ea497281c
BLAKE2b-256 9b900fda916ae115f36c563f8e20a7ab3a0077c7a24595da4ce4dc5d562974db

See more details on using hashes here.

File details

Details for the file shadai-0.1.31-py3-none-any.whl.

File metadata

  • Download URL: shadai-0.1.31-py3-none-any.whl
  • Upload date:
  • Size: 29.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shadai-0.1.31-py3-none-any.whl
Algorithm Hash digest
SHA256 4586abd084d427ff1c42d12070271f271a381ec19ea948986e918476f0fc3581
MD5 da6baf807f31c010f180de16fd0fb2ce
BLAKE2b-256 2c903a207dbf476c10ad0432cd7037a0155710d171ec79333bd4c54e470b2480

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