Skip to main content

Generate Domain Specific Language statements from Natural Language input using LLMs

Project description

dsllm - Domain Specific Language from LLM

Python 3.9+ License: MIT

Generate Domain Specific Language (DSL) statements from Natural Language input using Large Language Models (LLMs).

Overview

dsllm is a Python library that converts natural language descriptions into structured DSL statements. Currently supports SQL generation with OpenAI models, designed to be extensible to other DSLs and LLM providers.

Features

  • SQL Generation: Convert natural language to SQL queries
  • OpenAI Integration: Built-in support for GPT-3.5/4 models
  • Context Injection: Support for database schemas and constraints
  • Validation: Pre-flight and post-flight validation checks
  • Safety Features: Read-only mode, dangerous operation detection
  • Retry Mechanism: Automatic retries with exponential backoff

Installation

pip install dsllm

Quick Start

import asyncio
import os
from dsllm import DSLLMGenerator, OpenAIProvider, SQLGenerator

async def main():
    # Initialize components
    provider = OpenAIProvider(api_key=os.getenv("OPENAI_API_KEY"))
    sql_generator = SQLGenerator()
    generator = DSLLMGenerator(provider=provider, dsl_generator=sql_generator)
    
    # Generate SQL from natural language
    result = await generator.generate(
        natural_language="Find all active users who registered this month",
        context={
            "schema": '''
                CREATE TABLE users (
                    id SERIAL PRIMARY KEY,
                    email VARCHAR(255),
                    is_active BOOLEAN DEFAULT true,
                    created_at TIMESTAMP
                );
            '''
        }
    )
    
    print("Generated SQL:")
    print(result.dsl_statement)

# Run the example
asyncio.run(main())

Basic Usage

Simple Query Generation

result = await generator.generate("Show me all users")
print(result.dsl_statement)  # SELECT * FROM users;

With Context and Constraints

result = await generator.generate(
    "Find recent orders with high values",
    context={"tables": ["orders", "customers"]},
    constraints={"max_rows": 50}
)

Read-Only Mode

# Enable read-only mode for safety
sql_generator = SQLGenerator(enforce_read_only=True)
generator = DSLLMGenerator(provider=provider, dsl_generator=sql_generator)

# Only SELECT statements will be generated
result = await generator.generate("Get user data")  # ✅ Works
result = await generator.generate("Delete old records")  # ❌ Fails validation

Configuration

Environment Variables

  • OPENAI_API_KEY: Your OpenAI API key

Provider Options

provider = OpenAIProvider(
    model="gpt-4",           # Model to use
    temperature=0.1,         # Generation temperature
    max_tokens=500          # Maximum tokens
)

Roadmap

  • Additional DSLs: GraphQL, MongoDB queries, Elasticsearch DSL
  • More LLM Providers: Anthropic Claude, Google PaLM, Azure OpenAI
  • Advanced Features: Agentic loops, automated testing, observability
  • Enhanced Validation: Semantic validation, performance analysis

Contributing

Contributions are welcome! Please see our Contributing Guide for details.

License

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

Author

Frenk Dragar (@frenkd)

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

dsllm-0.1.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

dsllm-0.1.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file dsllm-0.1.0.tar.gz.

File metadata

  • Download URL: dsllm-0.1.0.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dsllm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e98982ec330caa3a388366fd532654b16bcf8b31b5fd101bc318a2c78a5f2720
MD5 7f1a65584b82c7c89f7d576340cfec93
BLAKE2b-256 43f1194333bbb096139cbea14389d94caf613c2dc0a9ac2b27671df3a19e16cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for dsllm-0.1.0.tar.gz:

Publisher: publish.yml on frenkd/dsllm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dsllm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dsllm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dsllm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f4c39425ab348ddd7244beb457037a75f352adbd3c6e121d19aca71dd80c2df
MD5 13fec87ea8bed3cd57a45eab6636c1f9
BLAKE2b-256 df8e39d26c7fbf993447dcee32cd092614ce7ed8be25177641e08058b45fec83

See more details on using hashes here.

Provenance

The following attestation bundles were made for dsllm-0.1.0-py3-none-any.whl:

Publisher: publish.yml on frenkd/dsllm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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