Skip to content

Commit 3151a33

Browse files
committed
feat: /models endpoint
1 parent d40b5bc commit 3151a33

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

optillm.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ def proxy():
9797
break
9898
else:
9999
# If no known approach is found in the model name, default to 'bon'
100-
approach = 'bon'
101-
100+
approach = "bon"
102101

103102
logger.info(f'Using approach {approach}, with {model}')
104103

@@ -166,6 +165,27 @@ def proxy():
166165
logger.debug(f'API response: {response_data}')
167166
return jsonify(response_data), 200
168167

168+
169+
@app.route('/v1/models', methods=['GET'])
170+
def proxy_models():
171+
logger.info('Received request to /v1/models')
172+
173+
try:
174+
if server_config['base_url']:
175+
client = OpenAI(api_key=API_KEY, base_url=server_config['base_url'])
176+
else:
177+
client = default_client
178+
179+
# Fetch models using the OpenAI client and return the raw response
180+
models_response = client.models.list()
181+
182+
logger.debug('Models retrieved successfully')
183+
return models_response.model_dump(), 200
184+
except Exception as e:
185+
logger.error(f"Error fetching models: {str(e)}")
186+
return jsonify({"error": f"Error fetching models: {str(e)}"}), 500
187+
188+
169189
@app.route('/health', methods=['GET'])
170190
def health():
171191
return jsonify({"status": "ok"}), 200

0 commit comments

Comments
 (0)