A Python library for crafting structured prompts and parsing structured outputs with a focus on JSON.
Project description
Fluxon version 0.0.3"
Pronunciation: Fluhk-sawn
Fluxon is a Python library designed for crafting structured prompts and parsing structured outputs, with a primary focus on JSON. Fluxon bridges the gap between human-readable prompts and machine-readable structured data, enabling seamless interaction with large language models (LLMs). It ensures robust error recovery, validation, and the generation of well-structured prompts tailored to LLMs.
Features
- Prompt Formatting: Guides LLMs to generate structured outputs using schemas and custom tags.
- Schema-Driven Parsing: Validates and parses outputs with tools like YAML, JSON Schema, or Pydantic classes.
- Error-Tolerant Parsing: Repairs common JSON issues such as missing commas, invalid formatting, and unquoted keys.
- Comprehensive Validation: Ensures outputs conform to expected structures using schema validation.
- Preprocessing Tools: Cleans LLM-generated outputs by removing comments and isolating JSON content.
- Lightweight and Modular Design: Optimized for Python-based workflows.
Installation
To install Fluxon, use pip:
pip install fluxon
Usage
1. Prompt Formatting
Create structured prompts to guide LLMs:
from fluxon.prompter import format_prompt
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["name", "age"]
}
prompt = "Provide user details in JSON format."
formatted_prompt = format_prompt(prompt, schema)
print("Prompt to LLM:", formatted_prompt)
Output:
Provide user details in JSON format.
Output the JSON object between the tags:
BEGIN_JSON
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"required": [
"name",
"age"
]
}
END_JSON
2. Parsing and Error Recovery
Use Fluxon to clean and parse LLM outputs:
from fluxon.parser import clean_llm_output, parse_json_with_recovery
llm_output = """
BEGIN_JSON
{
"name": "Alice",
"age": 25
"city": "New York"
}
END_JSON
"""
# Step 1: Clean the LLM output
cleaned_output = clean_llm_output(llm_output)
# Step 2: Parse and recover JSON
parsed_json = parse_json_with_recovery(cleaned_output)
print("Parsed JSON:", parsed_json)
Output:
{
"name": "Alice",
"age": 25,
"city": "New York"
}
3. Validation and Repair
Validate or repair JSON outputs using schemas:
from fluxon.validator import validate_with_schema, repair_with_schema
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "default": 30}
},
"required": ["name", "age"]
}
json_obj = {"name": "Alice"}
# Validate the JSON object
is_valid = validate_with_schema(json_obj, schema)
print("Is valid:", is_valid)
# Repair the JSON object by filling defaults
repaired_json = repair_with_schema(json_obj, schema)
print("Repaired JSON:", repaired_json)
Output:
Is valid: False
Repaired JSON: {"name": "Alice", "age": 30}
Contributing
Contributions are welcome! Please submit pull requests or report issues on the GitHub repository.
License
Fluxon is licensed under the Apache License 2.0.
Start building error-resilient, structured workflows with Fluxon today!
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 fluxon-0.0.3.tar.gz.
File metadata
- Download URL: fluxon-0.0.3.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e55492a2ed2c528e1b4dee9fc614f047b5b841f5382a7d0ee731ee1a27ee75fd
|
|
| MD5 |
8fd94a94fda3e1f679ddc4e797db53ba
|
|
| BLAKE2b-256 |
1ac1cf67651dbcb15e8684a83057eec1451e2a3bd8903b4b4866c40ad68768f3
|
File details
Details for the file fluxon-0.0.3-py3-none-any.whl.
File metadata
- Download URL: fluxon-0.0.3-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8863efb3a541e7d024e522f19d0dbdee8ef0517857005709302b27dfe7b6d035
|
|
| MD5 |
f13726ce2c4ee46e1d477a75e4e22a38
|
|
| BLAKE2b-256 |
34105271e3500d290c1e4cbce2290b8bbf2c17dedc82b715afe896fbe8fa2fb8
|