Skip to content

renep86/mainframe-llm-inference

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

11 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Mainframe LLM Inference

A direct inference application that transforms legacy mainframe programs (COBOL, etc.) into executable code using Large Language Models. This tool provides instant program analysis, translation, and verification without requiring complex parsing or compilation infrastructure.

Key Features

๐Ÿ”„ Direct Code Translation

  • COBOL to Python: Precise translation preserving business logic and mathematical accuracy
  • Decimal Precision: Uses Python's Decimal for monetary calculations matching COBOL behavior
  • Control Flow: Exact translation of IF/ELSE, EVALUATE, and conditional logic

โšก Instant Execution & Verification

  • Live Execution: Run generated Python code in isolated environment
  • Output Comparison: Verify LLM predictions against actual execution results
  • Error Detection: Catch calculation errors and logic bugs automatically

๐ŸŽฏ Interactive Input Handling

  • CLI Inputs: JSON parameter passing via command line
  • Interactive Mode: Prompt users for variable values with type detection
  • Flexible Defaults: Support for default values and optional parameters

๐Ÿ“Š Performance Monitoring

  • Token Usage: Track prompt/completion tokens and costs
  • Execution Time: Monitor LLM response and code execution performance
  • Success Metrics: Validation of prediction accuracy

Installation & Setup

# 1. Create virtual environment
python -m venv .venv && source .venv/bin/activate

# 2. Install package
pip install -e .

# 3. Configure API key
cp .env.example .env
# Edit .env and set GROQ_API_KEY=your_key_here

Usage Examples

Basic Translation

# Analyze and translate COBOL program
mainframe-llm run --path examples/cobol/premium.cob

# Execute generated Python and compare outputs
mainframe-llm run --path examples/cobol/premium.cob --execute

Interactive Input

# Prompt user for input values (POL-SUM-INSURED, POL-AGE available)
mainframe-llm run --path examples/cobol/premium.cob --interactive --execute

# Use CLI inputs
mainframe-llm run --path examples/cobol/premium.cob --inputs '{"POL-SUM-INSURED" : 2500, "POL-AGE": 25}' --execute

Development & Testing

# Dry run (no API call)
mainframe-llm run --path examples/cobol/premium.cob --dry-run

# Use specific model
mainframe-llm run --path examples/cobol/premium.cob --model llama-3.1-70b-versatile

Example Output

> mainframe-llm run --path examples/cobol/premium.cob --inputs '{"POL-SUM-INSURED" : 2500, "POL-AGE": 21}' --execute          
Using COBOL-specific prompt
Using CLI input values
Processing premium.cob with model llama-3.1-8b-instant...
**Program Analysis**

Input Variables:
- POL-SUM-INSURED: 2500
- POL-AGE: 21

Working Variables:
- WS-BASE-RATE
- WS-AGE-LOAD
- WS-GROSS

Output Variables:
- POL-SUM-INSURED
- WS-GROSS

**Translated Python Program**

from decimal import Decimal, getcontext

# Set precision for decimal calculations
getcontext().prec = 28

def main():
    # Input values from JSON
    pol_sum_insured = Decimal('2500.00')
    pol_age = 21

    # Working variables
    ws_base_rate = Decimal('0.0100')  # .010 with V9(4) = 0.0100
    ws_age_load = Decimal('0.200')  # POL-AGE = 21 < 25? TRUE

    # Calculate WS-GROSS
    ws_gross = pol_sum_insured * ws_base_rate * (1 + ws_age_load)

    # Output variables
    print("Sum Insured: ", pol_sum_insured)
    print("Gross Premium: ", ws_gross)

if __name__ == "__main__":
    main()

**Execution and Output**

1. Evaluate all IF/ELSE conditions with actual values:
   - POL-AGE = 21
   - Condition: 21 < 25? TRUE
   - Therefore: IF branch executes
   - Result: WS-AGE-LOAD = 0.200 (not 0.100)

2. Calculate intermediate variables in order:
   - WS-GROSS = POL-SUM-INSURED * WS-BASE-RATE * (1 + WS-AGE-LOAD)
   - WS-GROSS = 2500.00 * 0.0100 * (1 + 0.200)
   - WS-GROSS = 2500.00 * 0.0100 * 1.200
   - WS-GROSS = 30.00

3. Compute final results using the determined values:
   - WS-GROSS = 30.00

Running the Python program produces the following output:

Sum Insured:  2500
Gross Premium:  30.0

**Final Variable Values**

- POL-SUM-INSURED: 2500.00
- WS-GROSS: 30.00

**Step-by-Step Calculation Verification**
1. Input values: POL-SUM-INSURED = 2500, POL-AGE = 21
2. Conditional logic: POL-AGE = 21 < 25? TRUE, so WS-AGE-LOAD = 0.200
3. Final calculation: WS-GROSS = 2500.00 * 0.0100 * (1 + 0.200) = 30.00

VALIDATION CHECKLIST:
โœ“ All monetary values use Decimal type
โœ“ Mathematical operations preserve COBOL precision
โœ“ Control flow matches COBOL logic exactly
โœ“ DISPLAY statements converted to appropriate print() calls
โœ“ Predicted output matches what Python code will actually produce
โœ“ Variable names follow Python conventions (lowercase with underscores)

============================================================
CODE EXECUTION AND COMPARISON
============================================================
โœ… Code executed successfully

๐Ÿ“ค Code Execution Output:
Sum Insured:  2500.00
Gross Premium:  30.000000000

๐Ÿ”ฎ LLM Inference Output:
Sum Insured:  2500
Gross Premium:  30.0

๐Ÿ“Š Comparison Results:
โœ… Numeric values match (formatting differences)
============================================================

============================================================
SYSTEM PERFORMANCE REPORT
============================================================
EXECUTION TIME:      4.484 seconds
PROMPT TOKENS:       1,537
COMPLETION TOKENS:   669
TOTAL TOKENS:        2,206
ESTIMATED COST:      $0.028750
VARIABLES PROCESSED: 0
LINES OF CODE:       20
TOKENS/SECOND:       492.0
============================================================

Use Cases & Applications

๐Ÿฆ Financial Services

  • Insurance Premium Calculations: Convert COBOL actuarial programs to Python for modern systems
  • Banking Transaction Processing: Translate mainframe batch processing logic
  • Risk Assessment Models: Modernize legacy risk calculation algorithms
  • Regulatory Compliance: Verify calculations match original COBOL implementations

๐Ÿข Enterprise Modernization

  • Legacy System Migration: Understand business logic before full system rewrites
  • Code Documentation: Generate clear explanations of complex legacy algorithms
  • Business Rule Extraction: Identify and document embedded business logic
  • Prototype Development: Quickly test legacy logic in modern environments

๐Ÿ”ฌ Development & Testing

  • Algorithm Verification: Validate that modernized code produces identical results
  • Regression Testing: Compare outputs between legacy and modern implementations
  • Impact Analysis: Test how input changes affect program behavior
  • Performance Benchmarking: Compare execution characteristics across platforms

๐Ÿ“š Education & Training

  • COBOL Learning: Understand legacy code through Python translations
  • Mainframe Concepts: Visualize how mainframe logic works in familiar languages
  • Code Review: Analyze legacy programs for optimization opportunities
  • Knowledge Transfer: Bridge knowledge gaps between legacy and modern developers

Rationale & Benefits

Why Direct LLM Inference?

Traditional Approach Challenges:

  • Complex parsers and compilers required
  • Extensive domain knowledge needed
  • Time-intensive development cycles
  • Limited flexibility for edge cases

LLM Inference Advantages:

  • Instant Results: No compilation or setup required
  • Context Awareness: Understands business logic and intent
  • Flexible Input: Handles various legacy language dialects
  • Self-Documenting: Provides explanations alongside translations

Mathematical Precision

The tool ensures financial-grade accuracy by:

  • Using Python's Decimal for monetary calculations
  • Preserving COBOL's implied decimal positioning
  • Maintaining exact operator precedence
  • Validating results through execution comparison

Verification Through Execution

Unlike static translation tools, this application:

  • Executes generated code to verify correctness
  • Compares predicted vs actual outputs to catch errors
  • Provides immediate feedback on translation quality
  • Builds confidence in LLM-generated results

Future Applications

  • Financial calculators and pricing prototypes (interest, premiums, fees, accruals) where you can validate results against known formulas.
  • Rule engines drafted by the model but executed deterministically (journal templates, eligibility checks, underwriting rules).
  • Data mapping/ETL sketches (field mappings, unit conversions, basic validations) before building production pipelines.
  • Audit prep and narrative: produce plain-language explanations and test evidence from deterministic outputs.

Technical Architecture

The application follows a clean, modular design:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Legacy Code   โ”‚โ”€โ”€โ”€โ–ถโ”‚   LLM Inference  โ”‚โ”€โ”€โ”€โ–ถโ”‚  Python Code    โ”‚
โ”‚   (COBOL/etc)   โ”‚    โ”‚   (Groq API)     โ”‚    โ”‚  + Predictions  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                         โ”‚
                                                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Comparison    โ”‚โ—€โ”€โ”€โ”€โ”‚   Safe Execution โ”‚โ—€โ”€โ”€โ”€โ”‚   Input Values  โ”‚
โ”‚   & Validation  โ”‚    โ”‚   Environment    โ”‚    โ”‚   (Interactive) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

This architecture enables rapid iteration, reliable verification, and seamless integration into existing development workflows.

Important Disclaimers

โš ๏ธ This tool is designed for analysis, prototyping, and verification purposes only. For production use, thoroughly test all generated code, validate calculations against original systems, and ensure compliance with security/privacy policies. Legacy code is sent to Groq's API for processing, so verify data confidentiality requirements. LLM results may vary between runs and complex COBOL constructs may require manual review. Start with pilot testing on non-critical programs, maintain backup access to original systems, and ensure proper code ownership rights and regulatory compliance before deployment.

About

Direct inference of legacy mainframe code through LLMs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages