function api_collections_v1
Flask API endpoint that retrieves and returns a list of available data collections from the chat engine instance.
/tf/active/vicechatdev/vice_ai/complex_app.py
2248 - 2255
simple
Purpose
This endpoint provides a REST API interface to query available data collections that can be used for RAG (Retrieval-Augmented Generation) operations. It safely accesses the chat_engine's available_collections attribute and returns it as JSON, with error handling for cases where the chat engine is unavailable or the operation fails.
Source Code
def api_collections():
"""Get available data collections"""
try:
collections = getattr(chat_engine, 'available_collections', []) if chat_engine 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 may be empty if no collections are available or if chat_engine is None.
Dependencies
flasklogging
Required Imports
from flask import jsonify
import logging
Usage Example
# Assuming Flask app setup with authentication
# GET request to /api/collections
import requests
# Client-side usage
response = requests.get('http://localhost:5000/api/collections',
headers={'Authorization': 'Bearer <token>'})
if response.status_code == 200:
data = response.json()
collections = data.get('collections', [])
print(f"Available collections: {collections}")
else:
error = response.json().get('error')
print(f"Error: {error}")
# Server-side context (within Flask app):
# The function expects:
# - chat_engine: global variable with available_collections attribute
# - logger: configured logging instance
# - require_auth: decorator for authentication
Best Practices
- Ensure chat_engine is properly initialized before the Flask app starts accepting requests
- The function uses getattr with a default empty list to safely handle missing attributes
- Always check for None on chat_engine to prevent AttributeError
- Error logging is implemented for debugging failed collection retrievals
- Returns appropriate HTTP status codes (200 for success, 500 for server errors)
- The require_auth decorator should be implemented to protect this endpoint from unauthorized access
- Consider implementing caching if collections list is large or frequently accessed
- The available_collections attribute should be maintained and updated by the chat_engine instance
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function api_collections 90.3% similar
-
function get_stats 74.6% similar
-
function api_chat 67.8% similar
-
function api_send_chat_message 67.6% similar