function api_get_models
Flask API endpoint that returns a list of available LLM models and the default model configuration.
/tf/active/vicechatdev/docchat/app.py
830 - 835
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
flaskconfig
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
-
function get_llm_instance 55.5% similar
-
class Config_v1 51.5% similar
-
function api_templates_v2 50.6% similar
-
function api_templates 50.4% similar