Skip to content

AI Network Package

The azad.ainetwork package handles communication with language model providers, processes streaming responses, and manages context window exceeded errors with compression.

Core Components

Network

The AINetwork class is the main component of this package. It:

  • Sends requests to language models via LiteLLM
  • Processes streaming responses
  • Manages context window exceeded errors with compression
  • Emits standardized network events

Types

The Types module defines the data structures for network events and configuration. It includes:

  • Network event types for different stages of communication
  • Network configuration types
  • Event union types for type-safe event handling

Callback Manager

The Callback Manager provides utilities for managing callbacks during network operations. It:

  • Sets up callbacks for tracking usage statistics
  • Ensures callbacks are properly cleaned up

Compression Handler

The Compression Handler manages compression of context windows when they exceed the model's limits. It:

  • Detects context window exceeded errors
  • Applies compression strategies
  • Emits compression events

Errors

The Errors module provides error handling utilities for network operations. It:

  • Defines network-specific error types
  • Creates appropriate error instances from exceptions

AINetwork Utils

The AINetwork Utils module provides utility functions for the AI network. It:

  • Processes response streams
  • Creates network events
  • Handles usage statistics