🔍 Code Extractor

function api_collections_v1

Maturity: 41

Flask API endpoint that retrieves and returns a list of available data collections from the chat engine instance.

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
2248 - 2255
Complexity:
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

  • flask
  • logging

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

Related Versions

Other versions of this component:

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function api_collections 90.3% similar

    Flask API endpoint that retrieves and returns a list of available data collections from the RAG (Retrieval-Augmented Generation) engine.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_stats 74.6% similar

    Flask API endpoint that retrieves and returns statistics about a document collection from a RAG (Retrieval-Augmented Generation) system.

    From: /tf/active/vicechatdev/docchat/blueprint.py
  • function api_chat 67.8% similar

    Flask API endpoint that handles chat requests asynchronously, processing user queries through a RAG (Retrieval-Augmented Generation) engine with support for multiple modes, memory, web search, and custom configurations.

    From: /tf/active/vicechatdev/docchat/app.py
  • function api_send_chat_message 67.6% similar

    Flask API endpoint that handles sending a message in a chat session, processes it through a hybrid RAG engine with configurable search and memory settings, and returns an AI-generated response with references.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
← Back to Browse