Dhenara AI
website·Indexed 31 days ago
https://docs.dhenara.comAn 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.
What's inside Dhenara
- 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.
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.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.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.
Manage provider credentials with ResourceConfig
ResourceConfigserves 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.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.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:
- API Providers: Services exposing the AI APIs (e.g., OpenAI, Anthropic, Google Vertex AI, Amazon Bedrock).
- Models: Specific AI models with defined capabilities, token limits, and context windows.
- Endpoints: The binding of a specific model to a specific API provider.
- Clients: The primary interface used to interact with endpoints to generate content.
Create a custom foundation model
If a required model is not in the packaged list, you can instantiate aFoundationModelobject. 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, ), )Manage multi-turn conversations with Dhenara
Dhenara uses a direct approach to multi-turn chat by maintaining an explicit list ofMessageItemobjects. To maintain conversation state, append the assistant's message back into the message list using theto_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)Install and configure Dhenara AI credentials
Install thedhenara-aipackage via pip. Dhenara AI uses adai_credentials.yamlfile to manage API keys. You can specify the location of this file by setting theDAI_SECRET_CONFIG_DIRenvironment variable, or by passing the file path directly toResourceConfig.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>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 theusage_chargeobject 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}")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_apiprovider 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_openaiprovider block) - Amazon Bedrock: AWS credentials with Bedrock access