fix: update LMStudio endpoint from /health to /v1/models

- Replace health check endpoint with proper model listing endpoint
- Remove workaround that returned 200 for unexpected GET /v1/health
This commit is contained in:
haddadrm
2025-02-16 15:21:28 +04:00
parent b20ea70089
commit 76ed952aa2
2 changed files with 42 additions and 6 deletions

View File

@ -19,7 +19,7 @@ API_KEY = ""
API_URL = "" # Ollama API URL - http://host.docker.internal:11434
[MODELS.LMSTUDIO]
API_URL = "" # LM STUDIO API URL - http://host.docker.internal:1234/v1
API_URL = "" # LM STUDIO API URL - http://host.docker.internal:1234
[MODELS.CUSTOM_OPENAI]
API_KEY = ""

View File

@ -4,6 +4,10 @@ import { getKeepAlive, getLMStudioApiEndpoint } from '../../config';
import logger from '../../utils/logger';
import axios from 'axios';
const ensureV1Endpoint = (endpoint: string): string => {
return endpoint.endsWith('/v1') ? endpoint : `${endpoint}/v1`;
};
interface LMStudioModel {
id: string;
// add other properties if LM Studio API provides them
@ -14,6 +18,22 @@ interface ChatModelConfig {
model: ChatOpenAI;
}
const checkLMStudioAvailability = async (endpoint: string): Promise<boolean> => {
const v1Endpoint = ensureV1Endpoint(endpoint);
try {
await axios.get(`${v1Endpoint}/models`, {
timeout: 1000, // 1 second timeout
headers: {
'Content-Type': 'application/json',
},
});
return true;
} catch (err) {
logger.debug(`LM Studio server not available at ${endpoint}`);
return false;
}
};
export const loadLMStudioChatModels = async (): Promise<Record<string, ChatModelConfig>> => {
const lmStudioEndpoint = getLMStudioApiEndpoint();
@ -22,8 +42,16 @@ export const loadLMStudioChatModels = async (): Promise<Record<string, ChatModel
return {};
}
// Check if server is available before attempting to load models
const isAvailable = await checkLMStudioAvailability(lmStudioEndpoint);
if (!isAvailable) {
return {};
}
try {
const response = await axios.get<{ data: LMStudioModel[] }>(`${lmStudioEndpoint}/models`, {
const v1Endpoint = ensureV1Endpoint(lmStudioEndpoint);
const response = await axios.get<{ data: LMStudioModel[] }>(`${v1Endpoint}/models`, {
timeout: 5000, // 5 second timeout for model loading
headers: {
'Content-Type': 'application/json',
},
@ -37,7 +65,7 @@ export const loadLMStudioChatModels = async (): Promise<Record<string, ChatModel
model: new ChatOpenAI({
openAIApiKey: 'lm-studio',
configuration: {
baseURL: lmStudioEndpoint,
baseURL: ensureV1Endpoint(lmStudioEndpoint),
},
modelName: model.id,
temperature: 0.7,
@ -58,8 +86,16 @@ export const loadLMStudioEmbeddingsModels = async () => {
if (!lmStudioEndpoint) return {};
// Check if server is available before attempting to load models
const isAvailable = await checkLMStudioAvailability(lmStudioEndpoint);
if (!isAvailable) {
return {};
}
try {
const response = await axios.get(`${lmStudioEndpoint}/models`, {
const v1Endpoint = ensureV1Endpoint(lmStudioEndpoint);
const response = await axios.get(`${v1Endpoint}/models`, {
timeout: 5000, // 5 second timeout for model loading
headers: {
'Content-Type': 'application/json',
},
@ -73,7 +109,7 @@ export const loadLMStudioEmbeddingsModels = async () => {
model: new OpenAIEmbeddings({
openAIApiKey: 'lm-studio', // Dummy key required by LangChain
configuration: {
baseURL: lmStudioEndpoint,
baseURL: ensureV1Endpoint(lmStudioEndpoint),
},
modelName: model.id,
}),
@ -86,4 +122,4 @@ export const loadLMStudioEmbeddingsModels = async () => {
logger.error(`Error loading LM Studio embeddings model: ${err}`);
return {};
}
};
};