Search API
Perform semantic searches across your vector stores to find relevant document content
The Search API enables you to perform semantic searches across your vector stores to find relevant document content. This API is at the core of the RAG system, allowing you to retrieve information based on meaning rather than just keywords.
The official documentation for the Search API is available here.
Key Benefits
Semantic Understanding
Find content based on meaning, not just exact keyword matches
Contextual Results
Retrieve document chunks with surrounding context preserved
Flexible Filtering
Narrow results by document attributes and metadata
AI Integration
Connect search results directly to AI responses
API Endpoints
Search a Vector Store
Search for relevant content within a vector store.
Endpoint: POST /v1/vector_stores/{vector_store_id}/search
Content-Type: application/json
Path Parameters:
vector_store_id
: The ID of the vector store to search (required)
Request Body:
query
: The search query string (required)max_num_results
: Maximum number of results to return (default: 10)filters
: Optional filters to apply based on file attributesrewrite_query
: Whether to rewrite the query for better vector search (default: false)ranking_options
: Options for ranking/scoring results
Example Response:
Request Parameters in Detail
Query
The query
parameter is a natural language question or statement that describes the information you’re looking for. The system converts this query into a vector embedding and finds document chunks with similar embeddings.
Straightforward queries work well for specific information needs.
Straightforward queries work well for specific information needs.
More detailed queries can help refine the search when looking for procedural information.
Conceptual queries help find explanatory content about broader topics.
Max Number of Results
The max_num_results
parameter controls how many results are returned. The default is 10, but you can adjust this based on your needs:
For applications that need more context, increase this value, but be aware that higher values may include less relevant results.
Filters
The filters
parameter lets you narrow search results based on file attributes. Filters follow a structured format that enables both simple and complex filtering conditions.
Filter Types
The API supports two types of filters:
Comparison Filters
Compare a property value against a specific value
Compound Filters
Combine multiple filters using logical operators
Comparison Operators
Operator | Description | Example |
---|---|---|
eq | Equal to | Match where language is “en” |
ne | Not equal to | Match where status is not “archived” |
gt | Greater than | Match where version > 1.0 |
gte | Greater than or equal to | Match where created_at ≥ 1672531200 |
lt | Less than | Match where priority < 3 |
lte | Less than or equal to | Match where size ≤ 1024 |
Basic Filter Examples
Find documents where the language attribute equals “en”.
Find documents where the language attribute equals “en”.
Find documents with version greater than 2.0.
Find documents where status is not “archived”.
Compound Filters
Combine multiple conditions using logical operators:
Advanced Use Cases
Date Range Filtering
Filter documents created in 2023 (using Unix timestamps).
Boolean Filters
Find approved public documents.
When designing your document attributes, consider what filtering capabilities you’ll need and ensure your attributes are structured accordingly.
Ranking Options
The ranking_options
parameter configures how results are scored and ranked:
score_threshold
: Minimum similarity score (0.0-1.0) for results to be included
Response Format in Detail
The search response includes:
Search Query
The original query string used for the search:
Data Array
An array of search results, each containing:
file_id
: ID of the file containing the resultfilename
: Name of the filescore
: Similarity score (0.0-1.0)attributes
: File attributes for filteringcontent
: Array of content segments
The results are ordered by descending score (most relevant first).
Pagination Information
has_more
: Boolean indicating if there are more results availablenext_page
: Token for retrieving the next page of results (if available)
Using the file_search Tool
The file_search
tool provides an alternative way to search vector stores through the AI assistant API. This is especially useful for integrating search within conversational AI flows.
Tool Usage with OpenAI Models
The file_search tool automatically performs the search and provides the results to the AI model in the same request. This creates a seamless RAG experience.
Advanced Search Techniques
Query Rewriting (Coming Soon)
For improved search results, you can enable query rewriting:
When enabled, the system will automatically reformulate the query to improve vector similarity matching. This is particularly helpful for:
- Queries with ambiguous terms
- Questions with implicit context
- Queries that could benefit from expansion with related terms
Best Practices
Query Optimization
Be specific and clear
Craft precise queries rather than broad ones. “How do I reset a user password?” is better than “password reset.”
Use natural language
Frame queries as complete questions or statements rather than keyword lists.
Include context
If searching for domain-specific information, include relevant context in the query.
Use filters effectively
Combine query text with appropriate filters to narrow down the search space.
Performance Considerations
- Keep
max_num_results
reasonable (5-20) for better performance - Use filters to reduce the search space when applicable
- Consider implementing client-side caching for frequent queries
- For very large vector stores, use pagination to handle results efficiently
Integration Patterns
Direct API Integration
Integrate the Search API directly into your application’s backend:
- Capture user query from your application UI
- Send search request to the Search API
- Process and display results in your application
- Optionally use results to enhance subsequent AI interactions
AI-Driven Search with file_search Tool
Let the AI assistant drive the search interaction:
- Configure AI with the file_search tool
- User asks a question to the AI
- AI determines when to use the search tool and formulates appropriate queries
- AI incorporates search results into its response
- User receives a cohesive answer combining search results with AI capabilities