function api_collections
Flask API endpoint that retrieves and returns a list of available data collections from the RAG (Retrieval-Augmented Generation) engine.
/tf/active/vicechatdev/vice_ai/new_app.py
2268 - 2275
simple
Purpose
This endpoint provides a REST API interface to query available data collections in the RAG system. It checks if RAG functionality is available, retrieves the collections list from the rag_engine object, and returns it as JSON. Used by frontend applications to display available data sources for querying or analysis.
Source Code
def api_collections():
"""Get available data collections"""
try:
collections = getattr(rag_engine, 'available_collections', []) if RAG_AVAILABLE else []
return jsonify({'collections': collections})
except Exception as e:
logger.error(f"Collections API error: {e}")
return jsonify({'error': 'Failed to retrieve collections'}), 500
Return Value
Returns a Flask JSON response. On success: {'collections': [list of collection names]} with HTTP 200. On error: {'error': 'Failed to retrieve collections'} with HTTP 500. The collections list contains string identifiers of available data collections in the RAG system.
Dependencies
flasklogging
Required Imports
from flask import jsonify
import logging
Conditional/Optional Imports
These imports are only needed under specific conditions:
from hybrid_rag_engine import OneCo_hybrid_RAG
Condition: Required for the rag_engine object to exist and RAG_AVAILABLE flag to be set
Required (conditional)Usage Example
# In Flask application setup:
from flask import Flask, jsonify
import logging
app = Flask(__name__)
logger = logging.getLogger(__name__)
# Initialize RAG engine
RAG_AVAILABLE = True
rag_engine = OneCo_hybrid_RAG() # Assuming this has available_collections attribute
@app.route('/api/collections')
def api_collections():
"""Get available data collections"""
try:
collections = getattr(rag_engine, 'available_collections', []) if RAG_AVAILABLE else []
return jsonify({'collections': collections})
except Exception as e:
logger.error(f"Collections API error: {e}")
return jsonify({'error': 'Failed to retrieve collections'}), 500
# Client usage:
# GET request to /api/collections
# Response: {"collections": ["collection1", "collection2", ...]}
# Or on error: {"error": "Failed to retrieve collections"}
Best Practices
- Ensure RAG_AVAILABLE flag is properly set before the application starts to avoid runtime errors
- The rag_engine object must be initialized before this endpoint is called
- Use getattr with default value [] to safely handle cases where available_collections attribute doesn't exist
- Always log errors for debugging and monitoring purposes
- Return appropriate HTTP status codes (500 for server errors)
- Consider adding authentication/authorization if collections data is sensitive
- The endpoint uses GET method by default; ensure this aligns with REST conventions
- Consider adding caching if collections list doesn't change frequently
- Validate that rag_engine is not None before accessing its attributes in production
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function api_collections_v1 90.3% similar
-
function api_collections_v1 86.1% similar
-
function get_stats 76.6% similar
-
function api_templates_v1 71.5% similar
-
function api_load_template_v1 62.7% similar