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"]
)
# Create cache with filters
curl -X POST "http://localhost:8000/cache/create" \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{
"name": "tech_docs",
"model": "llama2",
"gguf_file": "llama-2-7b-chat.Q4_K_M.gguf",
"filters": {
"category": "tech",
"topic": "ml"
}
}'
# Create cache with specific documents
curl -X POST "http://localhost:8000/cache/create" \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{
"name": "research_docs",
"model": "llama2",
"gguf_file": "llama-2-7b-chat.Q4_K_M.gguf",
"docs": ["doc_123", "doc_456"]
}'
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
}
{
"detail": "No documents to add to cache"
}
{
"detail": "Invalid authentication credentials"
}
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")
curl -X GET "http://localhost:8000/cache/tech_docs" \
-H "Authorization: Bearer your_token"
Response:
{
"detail": "Invalid authentication credentials"
}
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()
curl -X POST "http://localhost:8000/cache/tech_docs/update" \
-H "Authorization: Bearer your_token"
Response:
{
"detail": "Cache 'tech_docs' not found"
}
POST
Add Documents to Cache
Add specific documents to an existing cache.
Parameters:
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"])
curl -X POST "http://localhost:8000/cache/tech_docs/add_docs" \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{
"docs": ["doc_123", "doc_456"]
}'
Response:
{
"detail": "Invalid authentication credentials"
}
POST
Query Cache
Query the cache with a prompt to generate a completion.
Parameters:
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
)
curl -X POST "http://localhost:8000/cache/tech_docs/query" \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{
"query": "What are the key concepts in machine learning?",
"max_tokens": 500,
"temperature": 0.7
}'
Response:
{
"completion": "Machine learning is a field that focuses on...",
"tokens_used": 150,
"finish_reason": "length"
}
{
"detail": "Invalid authentication credentials"
}
Best Practices
Cache Naming: Use descriptive names for your caches that reflect their content or purpose
Document Organization: Use consistent metadata when ingesting documents to make filtering easier
Cache Updates: Regularly update your caches if you frequently add new documents
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