Cache

The cache functionality in DataBridge allows you to create and manage specialized caches for efficient querying of your documents.

POST Create Cache

Create a new cache with specified configuration. The cache can be created using metadata filters, specific document IDs, or both.

Parameters:

  • name: Name of the cache to create

  • model: Name of the model to use (e.g. "llama2")

  • gguf_file: Name of the GGUF file to use for the model

  • filters: Optional metadata filters to determine which documents to include

  • docs: Optional list of specific document IDs to include

Returns: Cache configuration object with success status.

from databridge import DataBridge

# Create client instance
db = DataBridge(uri="your-databridge-uri")

# Create cache using filters
cache = db.create_cache(
    name="tech_docs",
    model="llama2",
    gguf_file="llama-2-7b-chat.Q4_K_M.gguf",
    filters={"category": "tech", "topic": "ml"}
)

# Create cache using specific documents
cache = db.create_cache(
    name="research_docs",
    model="llama2",
    gguf_file="llama-2-7b-chat.Q4_K_M.gguf",
    docs=["doc_123", "doc_456"]
)

# Create cache using both filters and specific documents
cache = db.create_cache(
    name="combined_cache",
    model="llama2",
    gguf_file="llama-2-7b-chat.Q4_K_M.gguf",
    filters={"category": "tech"},
    docs=["doc_789"]
)

Response:

{
    "name": "tech_docs",
    "model": "llama2",
    "gguf_file": "llama-2-7b-chat.Q4_K_M.gguf",
    "filters": {
        "category": "tech",
        "topic": "ml"
    },
    "doc_count": 5,
    "success": true
}

GET Get Cache

Get cache configuration by name.

Parameters:

  • name: Name of the cache to retrieve

Returns: Cache existence status.

from databridge import DataBridge

# Create client instance
db = DataBridge(uri="your-databridge-uri")

# Get cache
cache = db.get_cache("tech_docs")

Response:

{
    "exists": true
}

POST Update Cache

Update cache with new documents matching its filter.

Parameters:

  • name: Name of the cache to update

Returns: Success status of the update operation.

from databridge import DataBridge

# Create client instance
db = DataBridge(uri="your-databridge-uri")

# Get and update cache
cache = db.get_cache("tech_docs")
success = cache.update()

Response:

{
    "success": true
}

POST Add Documents to Cache

Add specific documents to an existing cache.

Parameters:

  • name: Name of the cache

  • docs: List of document IDs to add

Returns: Success status of the add operation.

from databridge import DataBridge

# Create client instance
db = DataBridge(uri="your-databridge-uri")

# Get cache and add documents
cache = db.get_cache("tech_docs")
success = cache.add_docs(["doc_123", "doc_456"])

Response:

{
    "success": true
}

POST Query Cache

Query the cache with a prompt to generate a completion.

Parameters:

  • name: Name of the cache

  • query: Query text

  • max_tokens: Optional maximum number of tokens to generate

  • temperature: Optional temperature parameter for controlling randomness

Returns: Completion response with generated text.

from databridge import DataBridge

# Create client instance
db = DataBridge(uri="your-databridge-uri")

# Get cache and query
cache = db.get_cache("tech_docs")

# Basic query
response = cache.query("What are the key concepts in machine learning?")
print(response.completion)

# Query with parameters
response = cache.query(
    "Explain machine learning concepts",
    max_tokens=500,
    temperature=0.7
)

Response:

{
    "completion": "Machine learning is a field that focuses on...",
    "tokens_used": 150,
    "finish_reason": "length"
}

Best Practices

  1. Cache Naming: Use descriptive names for your caches that reflect their content or purpose

  2. Document Organization: Use consistent metadata when ingesting documents to make filtering easier

  3. Cache Updates: Regularly update your caches if you frequently add new documents

  4. Query Parameters:

    • Use lower temperature (0.0-0.3) for more focused, deterministic responses

    • Use higher temperature (0.7-1.0) for more creative responses

    • Adjust max_tokens based on your needed response length

Error Handling

Always wrap cache operations in try-except blocks in production code:

try:
    cache = db.get_cache("ml_cache")
    response = cache.query("What is machine learning?")
except ValueError as e:
    print(f"Cache error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Last updated