Generate Domain Specific Language statements from Natural Language input using LLMs
Project description
dsllm - Domain Specific Language from LLM
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e98982ec330caa3a388366fd532654b16bcf8b31b5fd101bc318a2c78a5f2720
|
|
| MD5 |
7f1a65584b82c7c89f7d576340cfec93
|
|
| BLAKE2b-256 |
43f1194333bbb096139cbea14389d94caf613c2dc0a9ac2b27671df3a19e16cf
|
Provenance
The following attestation bundles were made for dsllm-0.1.0.tar.gz:
Publisher:
publish.yml on frenkd/dsllm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dsllm-0.1.0.tar.gz -
Subject digest:
e98982ec330caa3a388366fd532654b16bcf8b31b5fd101bc318a2c78a5f2720 - Sigstore transparency entry: 456381771
- Sigstore integration time:
-
Permalink:
frenkd/dsllm@606bc8204b465b884be1c013b64d86625fa52c56 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/frenkd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@606bc8204b465b884be1c013b64d86625fa52c56 -
Trigger Event:
workflow_dispatch
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f4c39425ab348ddd7244beb457037a75f352adbd3c6e121d19aca71dd80c2df
|
|
| MD5 |
13fec87ea8bed3cd57a45eab6636c1f9
|
|
| BLAKE2b-256 |
df8e39d26c7fbf993447dcee32cd092614ce7ed8be25177641e08058b45fec83
|
Provenance
The following attestation bundles were made for dsllm-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on frenkd/dsllm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dsllm-0.1.0-py3-none-any.whl -
Subject digest:
7f4c39425ab348ddd7244beb457037a75f352adbd3c6e121d19aca71dd80c2df - Sigstore transparency entry: 456381791
- Sigstore integration time:
-
Permalink:
frenkd/dsllm@606bc8204b465b884be1c013b64d86625fa52c56 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/frenkd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@606bc8204b465b884be1c013b64d86625fa52c56 -
Trigger Event:
workflow_dispatch
-
Statement type: