An intelligent optimization problem solver using LLMs and Answer Set Programming
Project description
Savanty: Intelligent Optimization Problem Solver
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:
- Fork the repository
- Create a new branch:
git checkout -b feature-branch-name - Make your changes and commit them:
git commit -m 'Add some feature' - Push to the original branch:
git push origin feature-branch-name - 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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a64fe60d55e7ec9b0ba45ac86d5345b361a35dda163c20883a0ab14a12fbb6a
|
|
| MD5 |
ae46a416001332779bf0fd9c853a72e6
|
|
| BLAKE2b-256 |
e2e2d05757467a98aa8e6c88d59079438bcf6f507056d1009556e294343acc38
|
Provenance
The following attestation bundles were made for savanty-0.2.1.tar.gz:
Publisher:
publish.yml on Skelf-Research/savanty
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
savanty-0.2.1.tar.gz -
Subject digest:
2a64fe60d55e7ec9b0ba45ac86d5345b361a35dda163c20883a0ab14a12fbb6a - Sigstore transparency entry: 421643208
- Sigstore integration time:
-
Permalink:
Skelf-Research/savanty@a253b821c0bd93c5fb39bc10f236ed27b58231c2 -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/Skelf-Research
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a253b821c0bd93c5fb39bc10f236ed27b58231c2 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
971d302e32307b8f868631335402987d28d204168c94185dbbef00c8e3394ad8
|
|
| MD5 |
ae1afa4d1c5d187e2c1d901d37e90fa1
|
|
| BLAKE2b-256 |
5c36a6ffdbcd32c1a60e39cb0dbf0481a81a7ef5ee4af08b8c03616ff94cedf4
|
Provenance
The following attestation bundles were made for savanty-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on Skelf-Research/savanty
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
savanty-0.2.1-py3-none-any.whl -
Subject digest:
971d302e32307b8f868631335402987d28d204168c94185dbbef00c8e3394ad8 - Sigstore transparency entry: 421643228
- Sigstore integration time:
-
Permalink:
Skelf-Research/savanty@a253b821c0bd93c5fb39bc10f236ed27b58231c2 -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/Skelf-Research
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a253b821c0bd93c5fb39bc10f236ed27b58231c2 -
Trigger Event:
release
-
Statement type: