Skip to main content

An intelligent optimization problem solver using LLMs and Answer Set Programming

Project description

Savanty: Intelligent Optimization Problem Solver

PyPI version License: MIT

Savanty is an intelligent Python library that leverages the power of Large Language Models (LLMs) and DSPy to parse, solve, and visualize optimization problems. It provides both a command-line interface and a web interface for users to input problem descriptions in natural language and automatically generates solutions using Answer Set Programming (ASP).

๐ŸŒŸ Features

  • Natural Language Processing: Utilizes GPT-4 and DSPy for robust optimization problem parsing
  • Code Generation: Automatically generates Clorm predicates and ASP programs
  • Problem Validation: Validates if a problem can be solved with ASP before attempting to solve it
  • Interactive Gap Filling: Asks for additional information when problem descriptions are incomplete
  • Problem Solving: Implements optimization problem solving using Clingo
  • Dual Interface: Command-line interface for direct usage and web interface for interactive solving
  • Modern Python Package: Installable via pip with proper CLI integration

๐Ÿš€ Quick Start

# Install Savanty
pip install savanty

# Set your OpenAI API key
export OPENAI_API_KEY=your_openai_api_key_here

# Solve a problem directly from the command line
savanty -p "Minimize x+y subject to x>=0, y>=0, x+y<=10"

# Or run the web interface
savanty --web --port 5000

๐Ÿ“‹ Prerequisites

  • Python 3.8+
  • An OpenAI API key

๐Ÿ“ฆ Installation

From PyPI (Recommended)

pip install savanty

From Source

git clone https://github.com/terraprompt/savanty.git
cd savanty
pip install .

โš™๏ธ Configuration

Savanty can be configured using environment variables:

# Required: Set your OpenAI API key
export OPENAI_API_KEY=your_openai_api_key_here

# Optional: Configure the LLM model (default: gpt-4o)
export SAVANTY_LLM_MODEL=gpt-4-turbo

# Optional: Set a custom Flask secret key
export FLASK_SECRET_KEY=your_flask_secret_key_here

๐Ÿ–ฅ๏ธ Usage

Command Line Interface

Solve optimization problems directly from the command line:

# Solve a problem directly
savanty -p "We have a task scheduling problem. Each task has a name, duration, and priority. We need to schedule tasks within a maximum time of 10 units. Tasks cannot overlap. We want to maximize the total priority of scheduled tasks. Available tasks are: Task1: duration 3, priority 5; Task2: duration 2, priority 3; Task3: duration 4, priority 7; Task4: duration 1, priority 2."

# Run the web interface on a custom port
savanty --web --port 8080

Interactive Problem Solving

When a problem description is incomplete, Savanty will ask for additional information:

savanty -p "Solve a scheduling problem"
# Savanty will ask: "What are the tasks and their properties?"
# You can then provide: "Task1: duration 3, priority 5; Task2: duration 2, priority 3"

Web Interface

Run the web interface to solve problems interactively:

# Run on default port 5000
savanty --web

# Run on a custom port
savanty --web --port 8080

Then open your browser to http://localhost:5000 (or your custom port) to access the web interface. The web interface also supports interactive gap filling - if your problem description is incomplete, Savanty will ask for additional information directly in the web interface.

Python API

Use Savanty directly in your Python code:

from savanty.solver import solve_optimization_problem

problem_description = """
We have a task scheduling problem. Each task has a name, duration, and priority.
We need to schedule tasks within a maximum time of 10 units.
Tasks cannot overlap.
We want to maximize the total priority of scheduled tasks.

Available tasks are:
Task1: duration 3, priority 5
Task2: duration 2, priority 3
Task3: duration 4, priority 7
Task4: duration 1, priority 2
"""

result = solve_optimization_problem(problem_description)

if result.needs_more_info:
    print("Please provide more information:")
    for question in result.questions:
        print(f"- {question}")
elif result.error:
    print(f"Error: {result.error}")
else:
    print(f"Solution: {result.solution}")

๐Ÿ’ก Example Use Cases

1. Task Scheduling Problem

savanty -p "Schedule tasks to maximize priority. TaskA (duration 3, priority 5), TaskB (duration 2, priority 4), TaskC (duration 4, priority 7). Time limit is 8 units. Tasks cannot overlap."

2. Resource Allocation

savanty -p "Allocate 3 projects to 2 teams. Project1 (value 100, requires 2 team members), Project2 (value 150, requires 3 team members), Project3 (value 80, requires 1 team member). Total team members available: 4. Maximize total value."

3. Knapsack Problem

savanty -p "Knapsack with capacity 10. Item1 (weight 5, value 10), Item2 (weight 4, value 7), Item3 (weight 6, value 12), Item4 (weight 2, value 3). Maximize value without exceeding capacity."

4. Graph Coloring

savanty -p "Color a graph with 4 nodes and edges: (1,2), (2,3), (3,4), (1,4). Use minimum number of colors such that adjacent nodes have different colors."

5. Sudoku Solver

savanty -p "Solve a 4x4 Sudoku puzzle. Given: position (1,1) = 1, position (1,3) = 2, position (2,2) = 3, position (3,1) = 4. Find values for all positions respecting Sudoku rules."

๐Ÿ“ Project Structure

savanty/
โ”œโ”€โ”€ savanty/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ cli.py          # Command-line interface
โ”‚   โ”œโ”€โ”€ solver.py       # Core solver logic
โ”‚   โ”œโ”€โ”€ dspy_modules.py # DSPy modules for LLM processing
โ”‚   โ””โ”€โ”€ templates/
โ”‚       โ””โ”€โ”€ index.html  # Web interface template
โ”œโ”€โ”€ tests/
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ LICENSE

๐Ÿค Contributing

Contributions to Savanty are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a new branch: git checkout -b feature-branch-name
  3. Make your changes and commit them: git commit -m 'Add some feature'
  4. Push to the original branch: git push origin feature-branch-name
  5. Create the pull request

Alternatively, see the GitHub documentation on creating a pull request.

๐Ÿ“„ License

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

๐Ÿ“ง Contact

If you have any questions or feedback, please contact the maintainer:

Dipankar Sarkar - me@dipankar.name

Project Link: https://github.com/terraprompt/savanty

๐Ÿ™ Acknowledgements

  • OpenAI for providing the GPT-4 API
  • DSPy for LLM prompting and optimization
  • Clingo for the ASP solver
  • Clorm for Object-Relational Mapping with Clingo
  • Flask for the web framework
  • HTMX for dynamic HTML capabilities
  • Tailwind CSS for styling
  • Click for CLI creation

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

savanty-0.2.1.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

savanty-0.2.1-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file savanty-0.2.1.tar.gz.

File metadata

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

File hashes

Hashes for savanty-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2a64fe60d55e7ec9b0ba45ac86d5345b361a35dda163c20883a0ab14a12fbb6a
MD5 ae46a416001332779bf0fd9c853a72e6
BLAKE2b-256 e2e2d05757467a98aa8e6c88d59079438bcf6f507056d1009556e294343acc38

See more details on using hashes here.

Provenance

The following attestation bundles were made for savanty-0.2.1.tar.gz:

Publisher: publish.yml on Skelf-Research/savanty

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

File details

Details for the file savanty-0.2.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for savanty-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 971d302e32307b8f868631335402987d28d204168c94185dbbef00c8e3394ad8
MD5 ae1afa4d1c5d187e2c1d901d37e90fa1
BLAKE2b-256 5c36a6ffdbcd32c1a60e39cb0dbf0481a81a7ef5ee4af08b8c03616ff94cedf4

See more details on using hashes here.

Provenance

The following attestation bundles were made for savanty-0.2.1-py3-none-any.whl:

Publisher: publish.yml on Skelf-Research/savanty

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