Upload 3 files
Browse files- README.md +127 -12
- requirements.txt +17 -0
- run.py +17 -0
README.md
CHANGED
|
@@ -1,12 +1,127 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# JSON Logic Rule Generator (with RAG & Embeddings)
|
| 2 |
+
|
| 3 |
+
An AI-powered FastAPI service that converts natural-language prompts into valid JSON Logic rules using embeddings, hybrid key mapping, and a lightweight RAG layer over domain policies. The API also returns a plain-English explanation, the keys used, and a confidence score for each generated rule.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- Natural language → JSON Logic rule generation
|
| 8 |
+
- Uses only predefined `SAMPLE_STORE_KEYS` in `{"var": "key"}` format
|
| 9 |
+
- Hybrid key mapping (embeddings + BM25 + Reciprocal Rank Fusion)
|
| 10 |
+
- Lightweight RAG over hard-coded policy documents (FAISS + CRAG-style refinement)
|
| 11 |
+
- Self-consistency: multiple rule variants with voting + mock-data validation
|
| 12 |
+
- Confidence score based on key similarity, policy relevance, and generation quality
|
| 13 |
+
- Utility endpoints to inspect keys and policies
|
| 14 |
+
- Ready for local dev, UV, or Docker usage
|
| 15 |
+
|
| 16 |
+
## Prerequisites
|
| 17 |
+
|
| 18 |
+
- Python 3.10 or higher
|
| 19 |
+
- pip or uv for dependency management
|
| 20 |
+
- An OpenAI API key (for GPT-4o-mini)
|
| 21 |
+
|
| 22 |
+
## Installation & Setup
|
| 23 |
+
|
| 24 |
+
### 1. Clone the Repository
|
| 25 |
+
|
| 26 |
+
```
|
| 27 |
+
git clone https://github.com/<your-username>/<your-repo>.git
|
| 28 |
+
cd jsonlogic-rag-api
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
### 2. Create a Python Environment
|
| 33 |
+
|
| 34 |
+
```
|
| 35 |
+
python -m venv venv
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
### 3. Activate the Environment and Install Requirements
|
| 40 |
+
|
| 41 |
+
#### On Windows
|
| 42 |
+
|
| 43 |
+
```
|
| 44 |
+
venv\Scripts\activate
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
#### On macOS / Linux
|
| 49 |
+
|
| 50 |
+
```
|
| 51 |
+
source venv/bin/activate
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
#### Install Requirements
|
| 56 |
+
|
| 57 |
+
```
|
| 58 |
+
pip install -r requirements.txt
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
### 4. Configure Environment Variables
|
| 63 |
+
|
| 64 |
+
Create a `.env` file in the project root:
|
| 65 |
+
|
| 66 |
+
```
|
| 67 |
+
OPENAI_API_KEY="sk-your-key-here"
|
| 68 |
+
EMBED_MODEL="all-MiniLM-L6-v2"
|
| 69 |
+
RRF_K="60"
|
| 70 |
+
SIM_THRESHOLD="0.7"
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
### 5. Run the API with Uvicorn
|
| 75 |
+
|
| 76 |
+
```
|
| 77 |
+
python run.py
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
The API will start at:
|
| 82 |
+
|
| 83 |
+
- Swagger UI: http://127.0.0.1:8000/docs
|
| 84 |
+
- Health check: http://127.0.0.1:8000/health
|
| 85 |
+
|
| 86 |
+
### Or you can use Uvicorn directly
|
| 87 |
+
|
| 88 |
+
```
|
| 89 |
+
uvicorn app.main:app --host 127.0.0.1 --port 8000 --reload
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
### 6. Use below endpoint to get results. Here is the example input
|
| 93 |
+
|
| 94 |
+
```
|
| 95 |
+
http://127.0.0.1:8000/generate-rule
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
Example input
|
| 99 |
+
|
| 100 |
+
```
|
| 101 |
+
{
|
| 102 |
+
"context_docs": [
|
| 103 |
+
"Custom policy: Minimum age 25"
|
| 104 |
+
],
|
| 105 |
+
"prompt": "Approve if bureau score > 700 and business vintage at least 3 years"
|
| 106 |
+
}
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
The response will include:
|
| 112 |
+
|
| 113 |
+
- `json_logic`: the generated JSON Logic rule
|
| 114 |
+
- `explanation`: short natural-language explanation
|
| 115 |
+
- `used_keys`: which fields were used
|
| 116 |
+
- `key_mappings`: how user phrases mapped to keys
|
| 117 |
+
- `confidence_score`: overall confidence (0–1)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
### Test
|
| 122 |
+
|
| 123 |
+
To test it with some examples there are tests written in "tests" folder . To test just run below command
|
| 124 |
+
|
| 125 |
+
```
|
| 126 |
+
python tests\test_api.py
|
| 127 |
+
```
|
requirements.txt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
sentence-transformers
|
| 4 |
+
faiss-cpu
|
| 5 |
+
openai
|
| 6 |
+
pydantic-settings
|
| 7 |
+
rank-bm25
|
| 8 |
+
scikit-learn
|
| 9 |
+
pytest
|
| 10 |
+
pytest-asyncio
|
| 11 |
+
pytest-cov
|
| 12 |
+
python-dotenv
|
| 13 |
+
numpy
|
| 14 |
+
structlog
|
| 15 |
+
json-logic-qubit
|
| 16 |
+
httpx
|
| 17 |
+
loguru
|
run.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# run.py
|
| 2 |
+
import uvicorn
|
| 3 |
+
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
# Add project root to path
|
| 7 |
+
ROOT_DIR = Path(__file__).parent
|
| 8 |
+
sys.path.insert(0, str(ROOT_DIR))
|
| 9 |
+
|
| 10 |
+
if __name__ == "__main__":
|
| 11 |
+
uvicorn.run(
|
| 12 |
+
"app.main:app",
|
| 13 |
+
host="127.0.0.1", # Changed to localhost for local testing
|
| 14 |
+
port=8000,
|
| 15 |
+
reload=True,
|
| 16 |
+
log_level="info"
|
| 17 |
+
)
|