Skip to main content

Collaborative Agent Reasoning Library

Project description

MMAR CARL - Collaborative Agent Reasoning Library

A Python library for building chain-of-thought reasoning systems with DAG-based parallel execution and mmar-llm integration.

Overview

CARL provides a structured framework for creating complex reasoning chains that can execute steps in parallel where dependencies allow. It's designed to work seamlessly with the mmar-llm library for production LLM integration and supports multi-language reasoning (Russian/English).

Key Features

  • DAG-based Execution: Automatically parallelizes reasoning steps based on dependencies
  • Direct mmar-llm Integration: Uses EntrypointsAccessor directly without unnecessary abstractions
  • Multi-language Support: Built-in support for Russian and English languages
  • Clean Architecture: Simple and straightforward usage pattern
  • Production Ready: Async/sync compatibility, error handling, and retry logic
  • Parallel Processing: Optimized execution with configurable worker pools
  • Financial Focus: Designed for financial analysis with specialized prompts

Quick Start

import asyncio
from mmar_carl import (
    ReasoningChain, StepDescription, ReasoningContext,
    Language
)
from mmar_llm import EntrypointsAccessor, EntrypointsConfig

# Define a reasoning chain
EBITDA_ANALYSIS = [
    StepDescription(
        number=1,
        title="Рост EBITDA и стабильность",
        aim="Выяснить, стабильно ли растет EBITDA и насколько выросла",
        reasoning_questions="Насколько изменилась (выросла/уменьшилась) EBITDA за рассматриваемый период?",
        dependencies=[],
        entities=["EBITDA"],
        stage_action="Рассчитать темп прироста EBITDA за 12 последних месяцев год к году",
        example_reasoning="1) если темп > 0%, то положительный сигнал; если темп < 0%, то отрицательный сигнал"
    ),
    StepDescription(
        number=2,
        title="Маржинальность EBITDA",
        aim="Выяснить, сколько процентов выручки забирают операционные расходы",
        reasoning_questions="Какая маржинальность по EBITDA за рассматриваемый период?",
        dependencies=[1],  # Depends on step 1
        entities=["EBITDA_MARGIN", "EBITDA", "SALES_REVENUE"],
        stage_action="EBITDA маржа = EBITDA / SALES_REVENUE",
        example_reasoning="1) если маржа > 0%, то положительный сигнал: компания зарабатывает больше своих операционных расходов"
    )
]

# Create entrypoints accessor from configuration file
def create_entrypoints(entrypoints_path: str):
    """Create EntrypointsAccessor from configuration file."""
    import json
    with open(entrypoints_path, encoding="utf-8") as f:
        config_data = json.load(f)

    entrypoints_config = EntrypointsConfig.model_validate(config_data)
    return EntrypointsAccessor(entrypoints_config)

# Create and execute the reasoning chain
entrypoints = create_entrypoints("entrypoints.json")
chain = ReasoningChain(
    steps=EBITDA_ANALYSIS,
    max_workers=2,
    enable_progress=True
)

context = ReasoningContext(
    outer_context="Период,EBITDA,SALES_REVENUE\n2023-Q1,1000000,5000000\n2023-Q2,1200000,5500000",
    entrypoints=entrypoints,
    entrypoint_key="my_entrypoint",
    language=Language.RUSSIAN,
    retry_max=3
)

result = chain.execute(context)
print(result.get_final_output())

Installation

# For production use
pip install mmar-carl

# For development with mmar-llm integration
pip install mmar-carl mmar-llm>=1.0.3

# Development version with all dependencies
pip install mmar-carl[dev]

Requirements

  • Python 3.12+
  • mmar-llm>=1.0.3 (for LLM integration)
  • Pydantic for data models
  • asyncio for parallel execution

Documentation

Architecture

CARL is built around several key components:

  • StepDescription: Defines individual reasoning steps with metadata and dependencies
  • ReasoningChain: Orchestrates the execution of reasoning steps with DAG optimization
  • DAGExecutor: Handles parallel execution based on dependencies with configurable workers
  • ReasoningContext: Manages execution state, history, and multi-language support
  • EntrypointsAccessor: Direct integration with mmar-llm EntrypointsAccessor
  • Language: Built-in support for Russian and English languages
  • PromptTemplate: Multi-language prompt templates for different analysis types

Key Concepts

DAG-Based Parallel Execution

CARL automatically analyzes step dependencies and creates execution batches for maximum parallelization:

# Steps 1 and 2 execute in parallel
StepDescription(number=1, title="Revenue Analysis", dependencies=[])
StepDescription(number=2, title="Cost Analysis", dependencies=[])
# Step 3 waits for both to complete
StepDescription(number=3, title="Profitability Analysis", dependencies=[1, 2])

Multi-language Support

Built-in support for Russian and English with appropriate prompt templates:

# Russian language reasoning
context = ReasoningContext(
    outer_context=data,
    entrypoints=entrypoints,
    entrypoint_key="my_entrypoint",
    language=Language.RUSSIAN
)

# English language reasoning
context = ReasoningContext(
    outer_context=data,
    entrypoints=entrypoints,
    entrypoint_key="my_entrypoint",
    language=Language.ENGLISH
)

Direct mmar-llm Integration

Simple and straightforward usage without unnecessary abstractions:

from mmar_llm import EntrypointsAccessor

# Direct usage pattern
context = ReasoningContext(
    outer_context=data,
    entrypoints=entrypoints,
    entrypoint_key="my_entrypoint"
)

Example Usage

See the example.py file for a complete end-to-end demonstration with:

  • Direct mmar-llm EntrypointsAccessor integration
  • Multi-language support (Russian/English)
  • Parallel execution demonstration
  • Error handling and retry logic
  • Performance metrics

Run it with:

# Set entrypoints configuration
export ENTRYPOINTS_PATH=/path/to/your/entrypoints.json

# Run the demonstration
python example.py entrypoints.json my_entrypoint_key

# Or run with environment variable
ENTRYPOINTS_PATH=entrypoints.json python example.py

License

MIT License - see LICENSE file for details.

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

mmar_carl-0.0.5.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

mmar_carl-0.0.5-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file mmar_carl-0.0.5.tar.gz.

File metadata

  • Download URL: mmar_carl-0.0.5.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.31

File hashes

Hashes for mmar_carl-0.0.5.tar.gz
Algorithm Hash digest
SHA256 80bc9b16c3977707448c23d71ce8f2a41351d8a46695b99a71f9bd437cff8ae4
MD5 0119522422c2082e2bc0f4cb65c7b4b9
BLAKE2b-256 7e4f56d850cb8671640341ef1abdc4658fdd399ff31b7b39b6faef637116eb89

See more details on using hashes here.

File details

Details for the file mmar_carl-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: mmar_carl-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.31

File hashes

Hashes for mmar_carl-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1158ad3b17cc48fdcb4e00e01cb9db0f73da964fcd7e797743b6e1fe7e462552
MD5 4ab9b03e2dfeabdd62cb4c62805a2fa4
BLAKE2b-256 a02e1a568fc3e86c8b8b0d7724c27ee02e9ed2253f69200a1edd35c354954a83

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