Dhenara AI

website·Indexed 31 days ago

https://docs.dhenara.com

An open-source Python package for calling multiple LLM providers through a single typed interface. Dhenara provides a unified API for text and image generation, featuring strong type safety with Pydantic, structured output, simplified streaming, unified usage tracking, and normalized tool/function calling across different AI model providers.

Tokens
17.6K
Snippets
78
Records
101
Agent score
53%

What's inside Dhenara

  1. Overview of Dhenara AI

    Dhenara AI is an open-source Python package designed to call multiple LLM providers through a single typed interface. It abstracts provider-specific details, allowing developers to manage credentials, model selection, prompt formatting, streaming, tool use, and structured output through a consistent core client. Key capabilities include support for both synchronous and asynchronous interfaces, Pydantic-based type safety, and the ability to switch between models and providers at runtime.
  2. Overview of Dhenara Foundation Models

    Foundation models in Dhenara are pre-configured representations of AI models from various providers. They abstract away provider-specific implementation details by standardizing model identification, context/token limits, cost data, and configuration options. This allows developers to maintain consistent interactions regardless of the underlying API.
  3. Overview of Dhenara AI Framework

    Dhenara is a Python framework designed for integrating AI into production applications. It prioritizes maintainability, robustness, and type safety over experimentation. Key features include a unified provider interface (supporting OpenAI, Anthropic, Google, etc.), built-in cost tracking, monitoring, and comprehensive error handling.
  4. Dhenara AI Core Features Overview

    Dhenara AI is an open-source Python package that simplifies LLM integration through the following capabilities:

    • Unified API: Switch between AI providers without changing code.
    • Real-time Streaming: Built-in support for streaming responses with accumulated results.
    • Cost & Usage Tracking: Integrated token usage and cost analytics across providers.
    • Structured Output & Tools: Provider-agnostic tool calling and typed, validated outputs using Pydantic.
    • Debugging Artifacts: Capture normalized requests, responses, and Python logs per call to diagnose provider differences.
  5. Manage provider credentials with ResourceConfig

    ResourceConfig serves as the central management point for provider credentials, API definitions, and model endpoints. It allows developers to maintain a single public configuration surface for multiple AI providers and models, ensuring credentials remain separate from application code.
  6. Understand Dhenara's type safety and Pydantic integration

    Dhenara uses Pydantic models for all requests and responses to ensure strong runtime validation, IDE autocompletion, and consistent response shapes. Provider and model configurations use real enumeration types rather than string constants to prevent configuration errors.
  7. Dhenara AI Architecture Overview

    Dhenara uses a decoupled architecture to allow developers to switch models or API providers without changing the core code structure. The architecture consists of four primary layers:

    1. API Providers: Services exposing the AI APIs (e.g., OpenAI, Anthropic, Google Vertex AI, Amazon Bedrock).
    2. Models: Specific AI models with defined capabilities, token limits, and context windows.
    3. Endpoints: The binding of a specific model to a specific API provider.
    4. Clients: The primary interface used to interact with endpoints to generate content.
  8. Create a custom foundation model

    If a required model is not in the packaged list, you can instantiate a FoundationModel object. You must define the model name, provider, functional type, settings, and cost data.
    from dhenara.ai.types.genai.ai_model import (
        AIModelFunctionalTypeEnum,
        AIModelProviderEnum,
        ChatModelCostData,
        ChatModelSettings,
        FoundationModel,
    )
    
    # Create a custom foundation model
    custom_model = FoundationModel(
        model_name="your-custom-model",
        display_name="Your Custom Model",
        provider=AIModelProviderEnum.CUSTOM,
        functional_type=AIModelFunctionalTypeEnum.TEXT_GENERATION,
        settings=ChatModelSettings(
            max_context_window_tokens=100000,
            max_output_tokens=8000,
        ),
        valid_options={
            # Define your model's valid options here
        },
        metadata={
            "details": "Your custom model's description",
        },
        cost_data=ChatModelCostData(
            input_token_cost_per_million=1.0,
            output_token_cost_per_million=5.0,
        ),
    )
  9. Manage multi-turn conversations with Dhenara

    Dhenara uses a direct approach to multi-turn chat by maintaining an explicit list of MessageItem objects. To maintain conversation state, append the assistant's message back into the message list using the to_message_item() method from the chat response.
    from dhenara.ai.types.genai.dhenara.request import MessageItem, Prompt
    
    messages: list[MessageItem] = []
    messages.append(Prompt.with_text("Hello! What can you help me with?"))
    
    response = client.generate(messages=messages)
    chat = response.chat_response
    if not chat:
        raise RuntimeError("No chat_response returned")
    
    assistant_message = chat.to_message_item()
    if assistant_message:
        messages.append(assistant_message)
  10. Install and configure Dhenara AI credentials

    Install the dhenara-ai package via pip. Dhenara AI uses a dai_credentials.yaml file to manage API keys. You can specify the location of this file by setting the DAI_SECRET_CONFIG_DIR environment variable, or by passing the file path directly to ResourceConfig.load_from_file().
    # Install package
    pip install dhenara-ai
    
    # Setup credentials directory
    mkdir -p /path/to/secrets
    export DAI_SECRET_CONFIG_DIR=/path/to/secrets

    /path/to/secrets/dai_credentials.yaml

    anthropic:
      api_key: <YOUR_ANTHROPIC_API_KEY>
  11. Access automated cost calculations for AI responses

    Dhenara automatically calculates the cost of each AI response based on provider pricing. The cost is available via the usage_charge object in the response. For text generation, the cost is calculated as: (prompt_tokens × input_cost_per_token) + (completion_tokens × output_cost_per_token). For image generation, it is based on the number of images and model-specific parameters like size and quality.
    # The cost is automatically calculated with each response
    if response.chat_response and response.chat_response.usage_charge:
        print(f"Cost for this call: ${response.chat_response.usage_charge.cost}")
        print(f"Charge (optional): {response.chat_response.usage_charge.charge}")
  12. Dhenara AI system and provider requirements

    Dhenara AI requires Python 3.13 to ensure consistent typing, provider, and structured-output support. While provider dependencies are installed automatically, specific credentials are required based on the AI provider used:

    • OpenAI: OpenAI API key
    • Google Gemini Developer API: Gemini API key (configured under the google_gemini_api provider block)
    • Google Vertex AI: Service account credentials
    • Anthropic: Anthropic API key
    • Microsoft OpenAI: API key and an Azure OpenAI or Microsoft Foundry OpenAI v1 endpoint (use the microsoft_openai provider block)
    • Amazon Bedrock: AWS credentials with Bedrock access