Documents (RAG)
Working with documents in Julep
Overview
Documents in Julep provide a way to store and retrieve information that can be used by agents. This section covers how to work with documents effectively.
Components
Documents in Julep consist of several key components that enable efficient storage and retrieval of information.
- Title: The title component helps identify and organize documents.
- Content: The textual content of the document.
- Embeddings (automatically generated): The vector representations of text that enable semantic search capabilities. Generated using the
text-embedding-3-large
model from OpenAI. - Metadata: Metadata provides additional context and filtering capabilities for documents.
Docs Configuration Options
When creating a doc, the following attributes can be specified:
Field | Type | Description | Default |
---|---|---|---|
title | string | The title of the document. | Required |
content | string | The content of the document. | Required |
metadata | object | Additional metadata for the document, such as preferences or settings. | null |
How to Use Docs
Creating a Doc
Documents are attached to either an agent or a user. This is how you can create a doc using Julep’s SDKs.
Example:
To create a user doc, replace client.agents.docs.create
with client.users.docs.create
, and the agent_id
argument with user_id
.
Check out the API reference or SDK reference (Python or JavaScript) for more details on different operations you can perform on docs.
Chunking
In Julep, documents are not automatically chunked. We recommend that developers handle chunking based on their specific use case requirements, as different applications may have unique needs for how documents should be divided.
For those who need assistance with chunking, we provide a utility function chunk_doc
that can be used directly in task steps. For implementation details, you can check the source code for this method in this file.
Search
Full Text Search
Our full-text search functionality leverages Timescale’s powerful indexing to efficiently match user-provided keywords and phrases against document content, delivering relevant results even with variations in text. Key features include prefix and infix searching, morphology processing (stemming and lemmatization), fuzzy searching to handle typos, and exact result counts.
Parameters:
Parameter | Type | Description | Default |
---|---|---|---|
text | str | The textual query to search within documents. | Required |
metadata_filter | object | Filters to apply based on document metadata. | None |
lang | str | The language to use for full-text search processing. | 'english' |
limit | int | The maximum number of documents to return. | 10 |
trigram_similarity_threshold | float | The threshold for trigram similarity matching (higher values require closer matches) | 0.6 |
The default parameters for full-text search are based on our internal benchmarking. These values provide a good starting point, but you may need to adjust them depending on your specific use case to achieve optimal results.
Example:
Check out the API reference or SDK reference (Python or JavaScript) for more details on different operations you can perform on docs.
Embedding (vector) Search
Our embedding (vector) search functionality leverages machine learning to convert search queries and documents into numerical vectors, enabling semantic matching based on vector similarity. It utilizes an embedding space where similar vectors indicate related content and employs algorithms like k-nearest neighbors (KNN) to retrieve the most relevant documents. Key features include context awareness, flexibility for natural language queries, multi-modal search across various content types, and effective handling of synonyms.
Parameters:
Parameter | Type | Description | Default |
---|---|---|---|
vector | number[] | The embedding vector representing the semantic meaning of the query. | Required |
limit | integer | The number of top results to return (must be between 1 and 50). | 10 |
lang | string | The language for the search query. | en-US |
metadata_filter | object | Filters to apply based on document metadata. | None |
mmr_strength | number | The strength of Maximum Marginal Relevance diversification (must be between 0 and 1). | 0.5 |
confidence | number | The confidence threshold for embedding similarity (must be between -1 and 1). | 0.5 |
The default parameters for embedding search are based on our internal benchmarking. These values provide a good starting point, but you may need to adjust them depending on your specific use case to achieve optimal results.
Check out the API reference or SDK reference (Python or JavaScript) for more details on different operations you can perform on docs.
Hybrid Search
Our hybrid search functionality combines multiple search techniques to deliver highly relevant and accurate search results. Julep’s hybrid search uses a three-pronged approach that leverages:
- Full-text search - Traditional keyword-based search using PostgreSQL’s tsquery/tsrank
- Vector search - Semantic search using embeddings for contextual understanding
- Trigram search - Fuzzy text matching using character n-grams for typo tolerance
By combining these approaches, hybrid search ensures that queries are matched not only on exact terms but also understood in context and tolerant of variations, providing more nuanced and precise results. This comprehensive approach enhances search performance, improves result relevance, and accommodates a wider range of search queries.
Trigram Search Features
Julep’s trigram search capabilities include:
- Fuzzy Matching - Handles typos, spelling variations, and morphological differences
- Similarity Scoring - Combines trigram similarity with Levenshtein distance for accurate matching
- Word-Level Analysis - Matches individual meaningful words against target content
- Adaptive Weighting - Adjusts fuzzy matching strength based on query length
- Performance Optimization - Uses PostgreSQL’s GIN indexes and materialized CTEs for efficient processing
How It Works
- When a search query is submitted, Julep runs both full-text search and trigram-based fuzzy search in parallel.
- Traditional full-text search results are prioritized (returned first).
- Trigram search then finds documents that full-text search might miss due to minor variations or typos.
- The system integrates these results with vector-based search results using Distribution-Based Score Fusion (DBSF).
- Results are ranked and returned based on a combination of all three search approaches.
Parameters:
Parameter | Type | Description | Default |
---|---|---|---|
text | str | The textual query to search within documents. | Required |
vector | List[float] | The embedding vector representing the semantic meaning of the query. | Required |
alpha | float | The weight assigned to embedding-based results versus text-based results (must be between 0 and 1). | 0.5 |
confidence | float | The confidence threshold for embedding similarity (must be between -1 and 1). | 0.5 |
metadata_filter | object | Filters to apply based on document metadata. | None |
limit | int | The number of top results to return. | 3 |
lang | str | The language to use for full-text search processing. | english_unaccent |
mmr_strength | float | The strength of Maximum Marginal Relevance diversification (must be between 0 and 1). | 0.5 |
trigram_similarity_threshold | float | The threshold for trigram similarity matching (must be between 0 and 1). | 0.6 |
k_multiplier | int | Controls how many intermediate results to fetch before final scoring. | 7 |
The default parameters for hybrid search are based on our internal benchmarking. These values provide a good starting point, but you may need to adjust them depending on your specific use case to achieve optimal results.
Example:
Check out the API reference or SDK reference (Python or JavaScript) for more details on different operations you can perform on docs.
Filtering
While search is carried on based on the textual and/or semantic content of the documents, you can also filter the documents based on their metadata.
Example:
Check out the API reference or SDK reference (Python or JavaScript) for more details on different operations you can perform on docs.
Relationship to Other Concepts
Sessions
Sessions have access to search, retrieve and reference agents and users documents inside chat conversations. Read more about it here.
Tasks
By leveraging System Tools, Julep Tasks have the ability to create, search, filter and read documents.
Example:
Checkout this cookbook that leverages Julep’s docs, system tools and tasks to create content-rich user personas.
Best Practices
Organize Metadata
- 1. Metadata: Use consistent and descriptive metadata to enhance document retrieval and filtering.
Version Control
- 1. Version Control: Maintain version control for documents to track changes and updates over time.
Security and Privacy
- 1. Access Control: Ensure sensitive information is protected and access to documents is properly managed.
Efficient Chunking
- 1. Chunking Strategies: Implement efficient chunking strategies to optimize document processing and retrieval.
Regular Updates
- 1. Update: Regularly update document content and metadata to keep information current and relevant.
Advanced Search Features
Trigram-Enhanced Fuzzy Matching
Julep’s advanced fuzzy matching capability is built on PostgreSQL’s pg_trgm
extension and enhanced with additional similarity techniques. This allows for resilient document retrieval that can handle variations in text, including:
- Typos and spelling errors
- Morphological variations
- Term order differences
- Incomplete terms
Similarity Mechanisms
Julep uses a multi-layered approach to determine text similarity:
-
Basic Trigram Similarity - Uses PostgreSQL’s built-in trigram functions to match documents based on character-level n-grams.
-
Enhanced Similarity - Combines trigram matching with Levenshtein distance calculations to provide better accuracy, especially for shorter text segments:
-
Word-Level Similarity - Breaks text into individual words and finds the best match for each meaningful word:
-
Comprehensive Similarity - Adaptively weights different similarity metrics based on query characteristics:
Tuning Search Behavior
You can customize Julep’s fuzzy search behavior through several parameters:
-
Similarity Threshold (
trigram_similarity_threshold
) - Controls the minimum similarity score required for a document to match:- Higher values (e.g., 0.8) require closer matches, reducing false positives but may miss relevant documents with variations
- Lower values (e.g., 0.3) are more lenient, catching more variations but potentially including less relevant results
- Default: 0.6 for hybrid search, 0.3 for text-only search
-
Alpha Weight (
alpha
) - Balances the importance of vector-based semantic search vs. text-based search:- Higher values prioritize semantic/embedding matches
- Lower values prioritize text and trigram matches
- Default: 0.7 (70% weight to embeddings)
-
Search Language (
lang
) - Affects tokenization, stemming, and other text processing operations:- Default: ‘english_unaccent’ which handles accent/diacritic-insensitive matching
Implementation Details
Julep’s trigram search is implemented using:
-
Database Indexes - GIN indexes on document title and content for efficient trigram operations:
-
Materialized CTEs - Improves performance for complex query operations:
-
Runtime Optimizations - Selective application of more expensive calculations:
-
Distribution-Based Score Fusion - Combines results from different search methods:
These technologies combine to provide a sophisticated fuzzy search capability that significantly improves document retrieval compared to traditional search methods.
Next Steps
- Sessions - Learn about sessions and how documents are used in chat conversations.
- Tools - Learn about tools and how they can be used to fill documents with content.
- Tasks - Learn about tasks and how to use documents inside tasks.
- Cookbooks - Check out cookbooks to see how Julep can be used in real-world scenarios.