2025-03-08 22:23:50 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
|
import options from "../../services/options.js";
|
|
|
|
|
import log from "../../services/log.js";
|
|
|
|
|
import type { Request, Response } from "express";
|
|
|
|
|
|
|
|
|
|
/**
|
2025-03-26 19:19:19 +00:00
|
|
|
* @swagger
|
2025-04-01 10:55:20 -07:00
|
|
|
* /api/llm/providers/ollama/models:
|
|
|
|
|
* get:
|
2025-03-26 19:19:19 +00:00
|
|
|
* summary: List available models from Ollama
|
|
|
|
|
* operationId: ollama-list-models
|
2025-04-01 10:55:20 -07:00
|
|
|
* parameters:
|
|
|
|
|
* - name: baseUrl
|
|
|
|
|
* in: query
|
|
|
|
|
* required: false
|
|
|
|
|
* schema:
|
|
|
|
|
* type: string
|
|
|
|
|
* description: Optional custom Ollama API base URL
|
2025-03-26 19:19:19 +00:00
|
|
|
* responses:
|
|
|
|
|
* '200':
|
|
|
|
|
* description: List of available Ollama models
|
|
|
|
|
* content:
|
|
|
|
|
* application/json:
|
|
|
|
|
* schema:
|
|
|
|
|
* type: object
|
|
|
|
|
* properties:
|
|
|
|
|
* success:
|
|
|
|
|
* type: boolean
|
|
|
|
|
* models:
|
|
|
|
|
* type: array
|
|
|
|
|
* items:
|
|
|
|
|
* type: object
|
|
|
|
|
* '500':
|
|
|
|
|
* description: Error listing models
|
|
|
|
|
* security:
|
|
|
|
|
* - session: []
|
|
|
|
|
* tags: ["llm"]
|
2025-03-08 22:23:50 +00:00
|
|
|
*/
|
|
|
|
|
async function listModels(req: Request, res: Response) {
|
|
|
|
|
try {
|
2025-04-01 10:55:20 -07:00
|
|
|
const baseUrl = req.query.baseUrl as string || await options.getOption('ollamaBaseUrl') || 'http://localhost:11434';
|
2025-03-08 22:23:50 +00:00
|
|
|
|
|
|
|
|
// Call Ollama API to get models
|
2025-04-01 10:55:20 -07:00
|
|
|
const response = await axios.get(`${baseUrl}/api/tags?format=json`, {
|
2025-03-08 22:23:50 +00:00
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
timeout: 10000
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Return the models list
|
2025-03-08 22:28:14 +00:00
|
|
|
const models = response.data.models || [];
|
|
|
|
|
|
|
|
|
|
// Important: don't use "return res.send()" - just return the data
|
|
|
|
|
return {
|
2025-03-08 22:23:50 +00:00
|
|
|
success: true,
|
2025-03-08 22:28:14 +00:00
|
|
|
models: models
|
|
|
|
|
};
|
2025-03-08 22:23:50 +00:00
|
|
|
} catch (error: any) {
|
|
|
|
|
log.error(`Error listing Ollama models: ${error.message || 'Unknown error'}`);
|
|
|
|
|
|
2025-03-08 22:28:14 +00:00
|
|
|
// Properly throw the error to be handled by the global error handler
|
|
|
|
|
throw new Error(`Failed to list Ollama models: ${error.message || 'Unknown error'}`);
|
2025-03-08 22:23:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
listModels
|
|
|
|
|
};
|