Skip to main content

Generate Unified Lattices that show you how a set of functions are related to each other

Project description

UL - Unified Lattice 🔄

A Python library that automatically generates a directed acyclic graph (DAG) representing all possible input-output relationships among a set of functions.

Python Version License

Overview 📚

UL (Unified Lattice) enables developers to input a set of functions and automatically generate a directed acyclic graph (DAG), or lattice, representing all possible input-output relationships. Unlike traditional pipeline tools that assume a single, linear flow from inputs to outputs, UL produces a comprehensive graph where users can extract specific subgraphs (sub-DAGs) to perform targeted tasks.

Key Features 🌟

  • Flexible Input Formats: Accept functions as code, signatures, docstrings, or natural language descriptions
  • Intelligent Relationship Inference: Uses OpenAI's GPT models to identify potential connections between functions
  • Comprehensive Visualization: Generate Mermaid diagrams to visualize the lattice
  • Subgraph Extraction: Extract specific parts of the lattice for targeted tasks
  • Type Compatibility Checking: Validate input-output relationships based on type annotations

Installation 💾

pip install ul

Quick Start 🚀

import ul

# Define functions as code strings
functions = [
    """
    def load_csv(file_path: str) -> pd.DataFrame:
        '''Loads a CSV file into a DataFrame.'''
        pass
    """,
    """
    def split_data(df: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
        '''Splits data into training and testing sets.'''
        pass
    """,
    """
    def train_model(train_df: pd.DataFrame) -> object:
        '''Trains a model on the given data.'''
        pass
    """,
    """
    def evaluate_model(model: object, test_df: pd.DataFrame) -> dict:
        '''Evaluates the model on test data.'''
        pass
    """
]

# Generate the lattice
lattice = ul.generate_lattice(functions, openai_api_key="your-api-key")

# Visualize the lattice
mermaid_code = ul.visualize_lattice(lattice)
print(mermaid_code)

# Extract a subgraph for a specific task
training_subgraph = ul.extract_subgraph(
    lattice, 
    start_functions=["load_csv"], 
    end_functions=["train_model"]
)

# Save the subgraph visualization to a file
ul.visualize_lattice(training_subgraph, output_file="training_workflow.md")

Input Formats 📝

UL supports multiple ways to define functions:

Code Snippets

def load_csv(file_path: str) -> pd.DataFrame:
    """Loads a CSV file into a DataFrame."""
    pass

Function Signatures

load_csv(file_path: str) -> pd.DataFrame

Docstrings

load_csv: Takes a file path (string) and returns a pandas DataFrame.

Natural Language Descriptions

A function that loads a CSV file into a data frame.

Output Formats 📊

Structured JSON

json_output = lattice.to_structured_output()

Example output:

{
  "functions": [
    {
      "name": "load_csv",
      "args": [{"name": "file_path", "type": "str"}],
      "return_type": "pd.DataFrame",
      "docstring": "Loads a CSV file into a DataFrame."
    },
    {
      "name": "split_data",
      "args": [{"name": "df", "type": "pd.DataFrame"}],
      "return_type": "tuple[pd.DataFrame, pd.DataFrame]",
      "docstring": "Splits data into training and testing sets."
    }
  ],
  "links": [
    {
      "from": {"function": "load_csv", "output": "return"},
      "to": {"function": "split_data", "arg": "df"}
    }
  ]
}

Mermaid Diagram

mermaid_code = lattice.to_mermaid_output()

Example output:

graph TD
    load_csv["load_csv(file_path: str) → pd.DataFrame"]
    split_data["split_data(df: pd.DataFrame) → tuple[pd.DataFrame, pd.DataFrame]"]
    train_model["train_model(train_df: pd.DataFrame) → object"]
    evaluate_model["evaluate_model(model: object, test_df: pd.DataFrame) → dict"]
    load_csv --> split_data
    split_data --> train_model
    split_data --> evaluate_model
    train_model --> evaluate_model

API Reference 📘

Core Functions

generate_lattice(functions, openai_api_key=None, model="gpt-4", input_type="auto")

Generate a lattice (DAG) based on function input-output relationships.

extract_subgraph(lattice, start_functions=None, end_functions=None, include_functions=None)

Extract a subgraph from the lattice for a specific task.

validate_lattice(lattice)

Ensure the lattice is a valid DAG (no cycles) and that input-output links are type-compatible.

visualize_lattice(lattice, output_file=None)

Renders the lattice as a Mermaid diagram or saves to a file.

Input Processing

process_code_input(code_strings)

Process a list of Python function code strings to extract metadata.

process_signature_input(signatures)

Process a list of function signatures to extract metadata.

process_docstring_input(docstrings, api_key, model="gpt-4")

Process a list of docstrings to extract function metadata using OpenAI.

process_natural_language_input(descriptions, api_key, model="gpt-4")

Process natural language descriptions to extract function metadata using OpenAI.

Lattice Class

The Lattice class represents a directed acyclic graph of functions and provides methods for output and analysis.

Methods

to_structured_output()

Convert the lattice to a structured JSON-like format.

to_mermaid_output()

Generate a Mermaid graph specification for visualization.

Use Cases 🧩

Data Science Pipelines

Create a lattice for data processing, model training, and evaluation:

lattice = ul.generate_lattice([
    "def load_data(path: str) -> pd.DataFrame: ...",
    "def preprocess(df: pd.DataFrame) -> pd.DataFrame: ...",
    "def train_model(df: pd.DataFrame) -> Model: ...",
    "def evaluate(model: Model, test_data: pd.DataFrame) -> dict: ...",
    "def save_model(model: Model, path: str) -> None: ...",
])

# Extract only the training pipeline
training = ul.extract_subgraph(lattice, end_functions=["train_model"])

API Integration

Connect API functions in a workflow:

lattice = ul.generate_lattice([
    "def fetch_user_data(user_id: str) -> dict: ...",
    "def validate_user(user_data: dict) -> bool: ...",
    "def process_payment(user_id: str, amount: float) -> dict: ...",
    "def generate_receipt(payment_data: dict) -> str: ...",
])

# Create a payment processing workflow
payment_flow = ul.extract_subgraph(
    lattice, 
    start_functions=["fetch_user_data"],
    end_functions=["generate_receipt"]
)

Environment Variables 🔐

Set your OpenAI API key as an environment variable:

export OPEN_API_KEY="your-api-key"

Requirements 📋

  • Python 3.8+
  • openai
  • networkx
  • matplotlib (optional, for custom visualizations)

Contributing 🤝

Contributions are welcome! Please feel free to submit a Pull Request.

License 📜

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments 🙏

  • This package uses OpenAI's GPT models for function relationship inference
  • NetworkX for graph operations
  • Mermaid for graph visualization

🔄 Built with UL - Connecting Your Functions in Harmony

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

ul-0.0.6.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

ul-0.0.6-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file ul-0.0.6.tar.gz.

File metadata

  • Download URL: ul-0.0.6.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for ul-0.0.6.tar.gz
Algorithm Hash digest
SHA256 de5c135687868604d1509bbde9c3e8ae0e1dbcfe774a9133f285328a73169ccd
MD5 b9fc8ad9fa72813f01854604cf7f7915
BLAKE2b-256 025a83d56165328c190c28f6e905b61b20e0491c724580ae03ccf74b5044ea80

See more details on using hashes here.

File details

Details for the file ul-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: ul-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for ul-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 087db32018862755d95c55baa7cd3264555f6456503b94e9eb23f9d02a863404
MD5 175985df380d6286850e3ed9e7307e98
BLAKE2b-256 a0c802ec6c3b2f2e79ea34149b17e7650272019702e48474c0931b7579dcd043

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