function api_get_reference_document
Flask API endpoint that retrieves a reference document from Neo4j database by UUID and stores it in the user's session for chat usage.
/tf/active/vicechatdev/vice_ai/app.py
1473 - 1523
moderate
Purpose
This endpoint serves as a bridge between Neo4j document storage and the chat interface. It fetches a document by UUID from Neo4j, validates its existence, creates metadata for it, stores it in the user's session storage, and returns document information including a preview. This allows users to reference specific documents from the knowledge base in their chat conversations.
Source Code
def api_get_reference_document(document_uuid):
"""Retrieve a reference document from Neo4j by UUID"""
try:
if not chat_engine:
return jsonify({'error': 'Chat engine not available'}), 500
# Check if chat engine has the extensive search manager for Neo4j access
if not hasattr(chat_engine, 'extensive_search_manager'):
return jsonify({'error': 'Neo4j access not available'}), 500
logger.info(f"🔍 Reference document request for UUID: {document_uuid}")
# Retrieve document from Neo4j
doc_info = chat_engine.extensive_search_manager.get_document_by_uuid(document_uuid)
if not doc_info:
logger.warning(f"⚠️ Reference document not found: {document_uuid}")
return jsonify({'error': 'Reference document not found'}), 404
# Store the document in user session for use in chat
user_email = session['user'].get('email', 'unknown')
reference_doc_id = f"ref_{document_uuid}"
# Create metadata for the reference document
metadata = {
'original_filename': doc_info['name'],
'source': 'Neo4j Reference',
'document_uuid': document_uuid,
'file_type': doc_info.get('type', 'reference'),
'size': doc_info.get('content_length', 0),
'chunk_count': doc_info.get('chunk_count', 0)
}
# Store in user document storage
store_document(user_email, reference_doc_id, None, doc_info['content'], metadata)
logger.info(f"✅ Reference document stored: {doc_info['name']} ({len(doc_info['content'])} chars)")
return jsonify({
'document_id': reference_doc_id,
'uuid': document_uuid,
'name': doc_info['name'],
'type': doc_info.get('type', 'reference'),
'content_length': len(doc_info['content']),
'chunk_count': doc_info.get('chunk_count', 0),
'preview': doc_info['content'][:500] + '...' if len(doc_info['content']) > 500 else doc_info['content']
})
except Exception as e:
logger.error(f"Reference document retrieval error: {e}")
return jsonify({'error': 'Failed to retrieve reference document'}), 500
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
document_uuid |
- | - | positional_or_keyword |
Parameter Details
document_uuid: String UUID identifier for the document to retrieve from Neo4j. This should be a valid UUID format that exists in the Neo4j database as a document identifier.
Return Value
Returns a Flask JSON response tuple. On success (200): JSON object containing 'document_id' (session storage ID), 'uuid' (original UUID), 'name' (document name), 'type' (document type), 'content_length' (character count), 'chunk_count' (number of chunks), and 'preview' (first 500 characters). On error: JSON object with 'error' message and HTTP status code (404 for not found, 500 for server errors).
Dependencies
flasklogginghybrid_rag_engineauth.azure_authdocument_processor
Required Imports
from flask import Flask, jsonify, session
import logging
Conditional/Optional Imports
These imports are only needed under specific conditions:
from hybrid_rag_engine import OneCo_hybrid_RAG
Condition: Required for chat_engine instance that provides extensive_search_manager
Required (conditional)from auth.azure_auth import AzureSSO
Condition: Required for require_auth decorator functionality
Required (conditional)Usage Example
# Assuming Flask app setup with authentication and chat engine
# Client-side request:
import requests
headers = {'Authorization': 'Bearer <auth_token>'}
document_uuid = '123e4567-e89b-12d3-a456-426614174000'
response = requests.get(
f'http://localhost:5000/api/reference-document/{document_uuid}',
headers=headers
)
if response.status_code == 200:
doc_data = response.json()
print(f"Document: {doc_data['name']}")
print(f"Preview: {doc_data['preview']}")
print(f"Session ID: {doc_data['document_id']}")
else:
print(f"Error: {response.json()['error']}")
Best Practices
- Ensure chat_engine is properly initialized before the Flask app starts accepting requests
- Implement proper error handling for Neo4j connection failures
- The function assumes store_document is available in module scope - ensure it's imported or defined
- User session must contain authenticated user data with 'email' field before calling this endpoint
- Consider implementing rate limiting to prevent abuse of document retrieval
- The preview is limited to 500 characters - adjust based on frontend requirements
- Document content is stored in session storage which may have size limitations
- Ensure proper cleanup of stored documents from session when no longer needed
- The extensive_search_manager.get_document_by_uuid method must return a dict with 'name', 'content', 'type', 'content_length', and 'chunk_count' keys
- Consider adding caching mechanism for frequently accessed documents to reduce Neo4j queries
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function api_add_reference_document 75.1% similar
-
function api_get_extensive_reference 71.7% similar
-
function api_get_reference_block 70.5% similar
-
function api_get_document 65.2% similar
-
function api_get_chat_uploaded_documents 64.4% similar