Skip to main content

A Python library for crafting structured prompts and parsing structured outputs with a focus on JSON.

Project description

Fluxon version 0.0.2

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fluxon-0.0.2.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

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

fluxon-0.0.2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file fluxon-0.0.2.tar.gz.

File metadata

  • Download URL: fluxon-0.0.2.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for fluxon-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c8540c33d33de69d896c9d767dcf05f8b55f95326ef5df057cffaf2a3c5d0cac
MD5 8ef14b2a24e38645e85fa20ebb0e80df
BLAKE2b-256 5c3dea0541a63a38de3779dd49101ee3dfbb8720b3eb2f983000cee74627c67a

See more details on using hashes here.

File details

Details for the file fluxon-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: fluxon-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for fluxon-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 916cc7451ebcdd5386c886c768656764b9ecdb200c9e7d0549451fbe666973e8
MD5 c12d1cbb731f371f5f13f8e52c3b5e00
BLAKE2b-256 0693303c3344223be864a2cbfc19b27673269be742718ef8464f3a4785073abc

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