Skip to content

Commit e98393d

Browse files
fix: add error handling and proper Docker host mapping to Ollama provider
1 parent a3e5109 commit e98393d

1 file changed

Lines changed: 35 additions & 7 deletions

File tree

app/lib/modules/llm/providers/ollama.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,36 @@ export default class OllamaProvider extends BaseProvider {
8383
*/
8484
const isDocker = process?.env?.RUNNING_IN_DOCKER === 'true' || serverEnv?.RUNNING_IN_DOCKER === 'true';
8585

86-
baseUrl = isDocker ? baseUrl.replace('localhost', 'host.docker.internal') : baseUrl;
87-
baseUrl = isDocker ? baseUrl.replace('127.0.0.1', 'host.docker.internal') : baseUrl;
86+
if (isDocker) {
87+
try {
88+
const url = new URL(baseUrl);
89+
if (url.hostname === 'localhost' || url.hostname === '127.0.0.1') {
90+
url.hostname = 'host.docker.internal';
91+
baseUrl = url.toString().replace(/\/$/, '');
92+
}
93+
} catch (error) {
94+
logger.warn('Failed to parse Ollama baseUrl for Docker mapping:', error);
95+
}
96+
}
8897
}
8998

9099
const response = await fetch(`${baseUrl}/api/tags`);
100+
101+
if (!response.ok) {
102+
throw new Error(
103+
`Failed to fetch Ollama models: HTTP ${response.status} ${response.statusText}`,
104+
);
105+
}
106+
91107
const data = (await response.json()) as OllamaApiResponse;
92108

93-
// console.log({ ollamamodels: data.models });
109+
if (!data || !Array.isArray(data.models)) {
110+
throw new Error('Invalid response from Ollama API: missing models array');
111+
}
94112

95113
return data.models.map((model: OllamaModel) => ({
96114
name: model.name,
97-
label: `${model.name} (${model.details.parameter_size})`,
115+
label: `${model.name} (${model.details?.parameter_size || 'unknown'})`,
98116
provider: this.name,
99117
maxTokenAllowed: 8000,
100118
maxCompletionTokens: 8000,
@@ -119,14 +137,24 @@ export default class OllamaProvider extends BaseProvider {
119137
defaultApiTokenKey: '',
120138
});
121139

122-
// Backend: Check if we're running in Docker
123140
if (!baseUrl) {
124141
throw new Error('No baseUrl found for OLLAMA provider');
125142
}
126143

144+
// Backend: Check if we're running in Docker
127145
const isDocker = process?.env?.RUNNING_IN_DOCKER === 'true' || envRecord.RUNNING_IN_DOCKER === 'true';
128-
baseUrl = isDocker ? baseUrl.replace('localhost', 'host.docker.internal') : baseUrl;
129-
baseUrl = isDocker ? baseUrl.replace('127.0.0.1', 'host.docker.internal') : baseUrl;
146+
147+
if (isDocker) {
148+
try {
149+
const url = new URL(baseUrl);
150+
if (url.hostname === 'localhost' || url.hostname === '127.0.0.1') {
151+
url.hostname = 'host.docker.internal';
152+
baseUrl = url.toString().replace(/\/$/, '');
153+
}
154+
} catch (error) {
155+
logger.warn('Failed to parse Ollama baseUrl for Docker mapping:', error);
156+
}
157+
}
130158

131159
logger.debug('Ollama Base Url used: ', baseUrl);
132160

0 commit comments

Comments
 (0)