Completions

This guide explains how to generate AI completions using your documents as context in DataBridge. The completions feature allows you to ask questions about your documents and get accurate, contextual responses.

Basic Setup

First, ensure you have the DataBridge client initialized:

from databridge import DataBridge

# Initialize client with your DataBridge URI
db = DataBridge("databridge://owner_id:token@api.databridge.ai")

Generating Completions

The query method combines semantic search with language model completion:

# Generate a completion with context
response = db.query(
    query="What are the key findings about customer satisfaction?",
    filters={"department": "research"},
    k=4,  # Number of chunks to use as context
    min_score=0.7,  # Minimum similarity threshold
    max_tokens=500,  # Maximum length of completion
    temperature=0.7  # Controls randomness (0.0-1.0)
)

print(response.completion)  # The generated response
print(response.usage)  # Token usage statistics

How It Works

  1. Your query is used to search for relevant chunks in your documents

  2. The most relevant chunks are selected based on semantic similarity

  3. These chunks are used as context for the language model

  4. The model generates a completion that answers your query using the provided context

Advanced Usage

1. Controlling Context

Adjust how much context is used:

2. Temperature Control

Adjust response creativity:

3. Token Management

Control response length and manage token usage:

The usage dictionary in CompletionResponse provides detailed token consumption:

  • prompt_tokens: Number of tokens in the context (retrieved chunks) and query

  • completion_tokens: Number of tokens in the generated response

  • total_tokens: Total tokens used in the operation

Monitor token usage to:

  • Optimize costs

  • Stay within rate limits

  • Track usage patterns

  • Budget appropriately

Common Use Cases

1. Question Answering

2. Summarization

3. Analysis and Insights

Next Steps

After mastering completions:

  • Implement caching for frequent queries

  • Set up monitoring for token usage

  • Create templates for common query types

  • Integrate with your application's error handling and logging systems

Last updated