Skip to main content

A dictionary with fuzzy key matching using RAG and LLM

Project description

RagDict

RagDict is an enhanced Python dictionary with fuzzy key matching support using RAG (Retrieval-Augmented Generation) and LLM.

Features

  • Works like a regular Python dictionary for exact key matches
  • Automatically finds similar keys when an exact match is not found
  • Uses OpenAI embeddings to find semantically similar keys
  • Applies LLM to select the most appropriate key from candidates
  • Fully customizable similarity parameters and models

Installation

pip install ragdict

Or using uv:

uv pip install ragdict

Usage

Basic Example

from ragdict import RagDict
import os

# Set your OpenAI API key
os.environ["OPENAI_API_KEY"] = "your-api-key"

# Create a dictionary with car prices
car_prices = RagDict({
    "Toyota Camry": 25000,
    "Honda Accord": 27000,
    "Tesla Model 3": 40000,
    "Ford Mustang": 35000,
    "Chevrolet Corvette": 60000
})

# Exact match works like a normal dictionary
price = car_prices["Toyota Camry"]  # 25000

# Fuzzy matching when the key is not exact
price = car_prices["Toyot Camri"]  # Will find "Toyota Camry" and return 25000
price = car_prices["Tesla Model Three"]  # Will find "Tesla Model 3" and return 40000
price = car_prices["Chevy Corvette"]  # Will find "Chevrolet Corvette" and return 60000

Parameter Customization

# Creating a dictionary with customizable parameters
products = RagDict(
    {
        "iPhone 14 Pro": 999,
        "Samsung Galaxy S23": 799,
        "Google Pixel 7": 599
    },
    embedding_model="text-embedding-3-large",  # Embedding model
    llm_model="gpt-4o",  # LLM model for key selection
    similarity_threshold=0.6,  # Minimum similarity threshold (0-1)
    top_k=5,  # Number of top candidates to consider
    api_key="your-openai-api-key"  # You can pass the API key directly
)

# Fuzzy search
price = products["iPhone 14"]  # Will find "iPhone 14 Pro"
price = products["Samsung S23"]  # Will find "Samsung Galaxy S23"

How It Works

  1. When you request a key, RagDict first tries to find an exact match
  2. If an exact match is not found, RagDict:
    • Creates an embedding for the requested key
    • Finds semantically similar keys based on cosine similarity
    • Filters keys below the similarity threshold
    • Uses LLM to select the most appropriate key from candidates
    • Returns the value associated with the selected key

Requirements

  • Python 3.8+
  • OpenAI API key
  • Packages: openai, numpy

License

MIT

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

ragdict-0.2.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

ragdict-0.2.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ragdict-0.2.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for ragdict-0.2.1.tar.gz
Algorithm Hash digest
SHA256 670fe406630f641c1f73f161b9efd340d38771e161da048dce28b356ca6f8619
MD5 b395beb9cf60109fb88f3f26730338c5
BLAKE2b-256 a2c3c89abce7ab4a5b9dba5d8d53e8812811c4afbf43c9d9741324dc153c13be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ragdict-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for ragdict-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a2d09930d871398ee4478d22d1f3c2438671afb1d100603b18df5673cc0bd1f0
MD5 98e14d3d20d827ff2ab6cd797655bbad
BLAKE2b-256 191f79ee2acf0f2195ff05a12cbdfeba995d24b68e4a027efe042ea807773d46

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