🔍 Code Extractor

function api_get_models

Maturity: 39

Flask API endpoint that returns a list of available LLM models and the default model configuration.

File:
/tf/active/vicechatdev/docchat/app.py
Lines:
830 - 835
Complexity:
simple

Purpose

This endpoint serves as a configuration discovery mechanism for frontend clients to retrieve the list of available Large Language Models (LLMs) that can be used in the application, along with the default model selection. It enables dynamic model selection in the UI without hardcoding model names on the client side.

Source Code

def api_get_models():
    """Get available LLM models"""
    return jsonify({
        'models': config.AVAILABLE_MODELS,
        'default_model': config.DEFAULT_MODEL
    })

Return Value

Returns a Flask JSON response containing a dictionary with two keys: 'models' (a list/array of available LLM model identifiers from config.AVAILABLE_MODELS) and 'default_model' (a string representing the default model identifier from config.DEFAULT_MODEL). The response has Content-Type: application/json and HTTP status 200.

Dependencies

  • flask
  • config

Required Imports

from flask import Flask, jsonify
import config

Usage Example

# In your Flask application file:
from flask import Flask, jsonify
import config

app = Flask(__name__)

# Ensure config.py has:
# AVAILABLE_MODELS = ['gpt-4', 'gpt-3.5-turbo', 'claude-2']
# DEFAULT_MODEL = 'gpt-4'

@app.route('/api/models', methods=['GET'])
def api_get_models():
    """Get available LLM models"""
    return jsonify({
        'models': config.AVAILABLE_MODELS,
        'default_model': config.DEFAULT_MODEL
    })

if __name__ == '__main__':
    app.run()

# Client-side usage (JavaScript fetch example):
# fetch('/api/models')
#   .then(response => response.json())
#   .then(data => {
#     console.log('Available models:', data.models);
#     console.log('Default model:', data.default_model);
#   });

Best Practices

  • Ensure config.AVAILABLE_MODELS and config.DEFAULT_MODEL are properly defined before the Flask app starts to avoid AttributeError
  • Consider adding error handling to return a meaningful error response if config values are missing
  • This endpoint should be accessible without authentication if model information is not sensitive, or protected with authentication middleware if needed
  • Consider adding CORS headers if this API will be accessed from different domains
  • The endpoint uses GET method which is appropriate for read-only operations and allows caching
  • Consider adding response caching headers to reduce server load if model configuration changes infrequently
  • Validate that config.DEFAULT_MODEL exists in config.AVAILABLE_MODELS to maintain data consistency

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_available_models 93.8% similar

    Flask API endpoint that returns a JSON response containing the list of available LLM models and the default model configured in the application.

    From: /tf/active/vicechatdev/docchat/blueprint.py
  • function get_llm_instance 55.5% similar

    Factory function that creates and returns an appropriate LLM (Large Language Model) instance based on the specified model name, automatically detecting the provider (OpenAI, Azure OpenAI, or Anthropic) and configuring it with the given parameters.

    From: /tf/active/vicechatdev/docchat/llm_factory.py
  • class Config_v1 51.5% similar

    Configuration class that centralizes all application settings including Flask configuration, directory paths, API keys, LLM model settings, and statistical analysis parameters.

    From: /tf/active/vicechatdev/full_smartstat/config.py
  • function api_templates_v2 50.6% similar

    Flask API endpoint that retrieves and returns a list of available instruction templates from the chat engine.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function api_templates 50.4% similar

    Flask API endpoint that retrieves and returns a list of available instruction templates from the chat engine.

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