AI Network Module¶
The AI Network module manages communication with language model providers, processes streaming responses, and handles context window exceeded errors with compression.
Overview¶
The AINetwork class is the main component of this module. It:
- Sends requests to language models via LiteLLM
- Processes streaming responses
- Manages context window exceeded errors with compression
- Emits standardized network events
This module is critical for the agent's ability to communicate with language models and handle their responses.
Key Concepts¶
Request/Response Flow¶
The AI Network follows a specific flow for making requests to language models:
- Format the prompt data using the specified dialect
- Send the request to the language model
- Process the streaming response
- Emit events based on the response
- Handle any errors that occur
Compression Handling¶
When a context window exceeded error occurs, the AI Network:
- Uses a compression handler to compress the message history
- Retries the request with the compressed messages
- Emits compression events to inform the client
Event Emission¶
The AI Network emits various events during the request/response cycle:
NetworkConnectionAttempt: When a connection is attemptedNetworkConnectionEstablished: When a connection is establishedNetworkConnectionFailed: When a connection failsNetworkConnectionInterrupted: When a connection is interruptedAINetworkEventContentComplete: When content is completeAINetworkConnectionEnded: When a connection ends
Dialect Integration¶
The AI Network works with the dialect system to:
- Format prompts for the language model
- Parse responses from the language model
- Extract tool calls and other content
Implementation Details¶
The make_request() method is the main entry point for the AI Network. It:
- Initializes a compression handler
- Formats the prompt data using the specified dialect
- Sends the request to the language model
- Processes the streaming response
- Emits events based on the response
- Handles any errors that occur
The method returns an async generator that yields network events.
Usage Example¶
Here's a simplified example of how to use the AINetwork:
# Create an AI Network
ai_network = AINetwork()
# Make a request
event_stream = ai_network.make_request(
prompt_data=prompt_data,
task=task,
dialect=dialect,
config=network_config,
assistant_id=assistant_id,
)
# Process the events
async for event in event_stream:
# Handle the event
handle_event(event)
Important Considerations¶
When working with the AINetwork:
-
Compression: The AI Network automatically handles context window exceeded errors with compression. Make sure to configure the compression strategy appropriately.
-
Event Handling: Implement proper event handling to process events emitted by the AI Network.
-
Error Handling: Handle errors appropriately to ensure the agent can recover and continue execution.
-
Resource Cleanup: Ensure that resources are properly cleaned up, especially when cancelling a request.
API Reference¶
azad.ainetwork.network ¶
AINetwork manages communication with language model providers.
This module provides the main AINetwork class that handles: 1. Communicating with language models via LiteLLM 2. Processing streaming responses 3. Managing context window exceeded errors with compression 4. Error handling and usage statistics
Attributes¶
Classes¶
AsyncCompletionCallback ¶
Callback class to handle async completion events. This class is used to track whetever the cache was hit or not. It is used to set the result of a future when the callback is called. It now includes a unique request ID to ensure it only processes events for its specific request.
Source code in azad/ainetwork/network.py
AINetwork ¶
AINetwork handles communication with language model providers, processes streaming responses, and emits standardized network events for consumption by other system components.
This class is responsible for: 1. Sending requests to language models via LiteLLM 2. Processing streaming responses 3. Managing context window exceeded errors with compression 4. Emitting properly sequenced events 5. Error management and reporting
Initialize the AINetwork.
This constructor initializes the logger and attempts to load custom model settings from a JSON file named "azad_model_cost_map.json" in the current directory.
The custom model settings allow overriding litellm's default model configurations, such as pricing, context window sizes, etc. This enables faster updates to model configurations than waiting for litellm's official updates.
If the file doesn't exist or there's an error loading it, the default litellm settings will be used.
Example JSON format: { "model_name": { "litellm_provider": "openai", "max_tokens": 8192, "input_cost_per_token": 0.0001, "output_cost_per_token": 0.0002 } }
Source code in azad/ainetwork/network.py
Attributes¶
Functions¶
make_request
async
¶
make_request(prompt_data: PromptData, task: Task, dialect: Dialect, config: NetworkConfig, assistant_id: str, tool_call_id: Optional[str] = None) -> AsyncGenerator[AINetworkEventUnion, None]
Make a request to the language model and yield network events from the response.
Parameters:
-
prompt_data(PromptData) –The prompt data to send
-
dialect(Dialect) –The dialect to use for formatting and parsing
-
config(NetworkConfig) –Network configuration
-
assistant_id(str) –ID for the assistant
-
tool_call_id(Optional[str], default:None) –Optional ID for the tool call
Yields:
-
AsyncGenerator[AINetworkEventUnion, None]–Network events from the processed response
Source code in azad/ainetwork/network.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
Functions¶
estimate_token_count ¶
Estimate the token count of messages using tiktoken.
Parameters:
-
messages(List[Dict[str, Any]]) –List of formatted messages to count tokens for
-
model(str) –The model to use for token counting
Returns:
-
int–Estimated token count