🔍 Code Extractor

function configure_docchat

Maturity: 54

Configures DocChat module settings by overriding default configuration values from a Flask application instance and optional keyword arguments.

File:
/tf/active/vicechatdev/docchat/integration.py
Lines:
108 - 153
Complexity:
simple

Purpose

This function serves as a configuration bridge between a Flask application (PlanningTool) and the DocChat module. It allows dynamic configuration of ChromaDB connection settings, data directories, and API keys for OpenAI and Anthropic services. The function imports the DocChat config module and updates its settings based on provided parameters, then logs the final configuration for verification.

Source Code

def configure_docchat(app, **kwargs):
    """
    Configure DocChat settings from PlanningTool app config.
    
    Args:
        app: Flask application instance
        **kwargs: Override configuration options
            - chroma_host: ChromaDB host (default: pp_chroma)
            - chroma_port: ChromaDB port (default: 8000)
            - chroma_collection: Collection name
            - data_dir: Path to data directory
            - openai_api_key: OpenAI API key
            - anthropic_api_key: Anthropic API key
    """
    try:
        from . import config as docchat_config
        
        # Override config from kwargs
        if 'chroma_host' in kwargs:
            docchat_config.CHROMA_HOST = kwargs['chroma_host']
        
        if 'chroma_port' in kwargs:
            docchat_config.CHROMA_PORT = kwargs['chroma_port']
        
        if 'chroma_collection' in kwargs:
            docchat_config.CHROMA_COLLECTION_NAME = kwargs['chroma_collection']
        
        if 'data_dir' in kwargs:
            docchat_config.DATA_DIR = Path(kwargs['data_dir'])
            docchat_config.DOCUMENTS_DIR = docchat_config.DATA_DIR / "qa_docs"
        
        if 'openai_api_key' in kwargs:
            docchat_config.OPENAI_API_KEY = kwargs['openai_api_key']
        
        if 'anthropic_api_key' in kwargs:
            docchat_config.ANTHROPIC_API_KEY = kwargs['anthropic_api_key']
        
        # Log configuration
        logger.info("📝 DocChat Configuration:")
        logger.info(f"   ChromaDB: {docchat_config.CHROMA_HOST}:{docchat_config.CHROMA_PORT}")
        logger.info(f"   Collection: {docchat_config.CHROMA_COLLECTION_NAME}")
        logger.info(f"   Data Dir: {docchat_config.DATA_DIR}")
        logger.info(f"   Default Model: {docchat_config.DEFAULT_MODEL}")
        
    except Exception as e:
        logger.error(f"❌ Failed to configure DocChat: {e}")

Parameters

Name Type Default Kind
app - - positional_or_keyword
**kwargs - - var_keyword

Parameter Details

app: Flask application instance. While passed as a parameter, it's not actively used in the current implementation but likely reserved for future Flask-specific configuration access.

**kwargs: Variable keyword arguments for overriding configuration. Supported keys: 'chroma_host' (str, ChromaDB server hostname, default: 'pp_chroma'), 'chroma_port' (int, ChromaDB server port, default: 8000), 'chroma_collection' (str, ChromaDB collection name), 'data_dir' (str or Path, base directory for data storage), 'openai_api_key' (str, OpenAI API authentication key), 'anthropic_api_key' (str, Anthropic API authentication key)

Return Value

This function does not return any value (implicitly returns None). It performs side effects by modifying the docchat_config module's global configuration variables and logging configuration details.

Dependencies

  • flask
  • pathlib

Required Imports

import logging
from pathlib import Path

Conditional/Optional Imports

These imports are only needed under specific conditions:

from . import config as docchat_config

Condition: Always required - imports the DocChat configuration module from the same package

Required (conditional)

Usage Example

from flask import Flask
from pathlib import Path
import logging

# Setup logger
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

# Create Flask app
app = Flask(__name__)

# Configure DocChat with custom settings
configure_docchat(
    app,
    chroma_host='localhost',
    chroma_port=8000,
    chroma_collection='my_documents',
    data_dir='/path/to/data',
    openai_api_key='sk-...',
    anthropic_api_key='sk-ant-...'
)

# Or with minimal configuration
configure_docchat(app, data_dir='/path/to/data')

Best Practices

  • Always call this function during Flask application initialization before using DocChat features
  • Store API keys securely using environment variables rather than hardcoding them in kwargs
  • Ensure the data_dir path exists and has appropriate read/write permissions before configuration
  • Verify ChromaDB server is running and accessible at the specified host:port before configuration
  • Handle the logged configuration output to verify settings are applied correctly
  • Wrap the function call in try-except to handle configuration failures gracefully
  • The function modifies global configuration state, so call it only once during application startup
  • Consider using Flask's app.config dictionary to pass configuration values instead of individual kwargs

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function integrate_docchat 68.6% similar

    One-line integration function that configures and registers the DocChat component into a Flask application, handling dependency checking, configuration, and blueprint registration.

    From: /tf/active/vicechatdev/docchat/integration.py
  • function register_docchat 64.9% similar

    Registers a DocChat Flask blueprint with a Flask application instance, handling path configuration and error logging.

    From: /tf/active/vicechatdev/docchat/integration.py
  • function api_update_chat_config 57.4% similar

    Flask API endpoint that updates the configuration settings for a specific chat session by accepting JSON data, converting it to a ChatConfiguration object, and persisting the changes.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_integration_status 55.8% similar

    Retrieves the integration status of the DocChat blueprint within a Flask application, including registration status, configuration details, and dependency checks.

    From: /tf/active/vicechatdev/docchat/integration.py
  • class Config 55.6% similar

    Configuration class that manages application-wide settings, directory structures, API keys, and operational parameters for a statistical analysis application.

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