Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 49 additions & 21 deletions core/llm/llms/Ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,17 @@ class Ollama extends BaseLLM {
if (options.model === "AUTODETECT") {
return;
}
const headers: Record<string, string> = {
"Content-Type": "application/json",
};

if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}

this.fetch(this.getEndpoint("api/show"), {
method: "POST",
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
},
headers: headers,
body: JSON.stringify({ name: this._getModel() }),
})
.then(async (response) => {
Expand Down Expand Up @@ -323,12 +328,16 @@ class Ollama extends BaseLLM {
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<string> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};

if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
const response = await this.fetch(this.getEndpoint("api/generate"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
headers: headers,
body: JSON.stringify(this._getGenerateOptions(options, prompt)),
signal,
});
Expand Down Expand Up @@ -384,13 +393,16 @@ class Ollama extends BaseLLM {
}));
chatOptions.stream = false; // Cannot set stream = true for tools calls
}

const headers: Record<string, string> = {
"Content-Type": "application/json",
};

if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
const response = await this.fetch(this.getEndpoint("api/chat"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
headers: headers,
body: JSON.stringify(chatOptions),
signal,
});
Expand Down Expand Up @@ -467,12 +479,16 @@ class Ollama extends BaseLLM {
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<string> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};

if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
const response = await this.fetch(this.getEndpoint("api/generate"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
headers: headers,
body: JSON.stringify(this._getGenerateOptions(options, prefix, suffix)),
signal,
});
Expand Down Expand Up @@ -504,11 +520,19 @@ class Ollama extends BaseLLM {
}

async listModels(): Promise<string[]> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};

if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
const response = await this.fetch(
// localhost was causing fetch failed in pkg binary only for this Ollama endpoint
this.getEndpoint("api/tags"),
{
method: "GET",
headers: headers,
},
);
const data = await response.json();
Expand All @@ -522,16 +546,20 @@ class Ollama extends BaseLLM {
}

protected async _embed(chunks: string[]): Promise<number[][]> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};

if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
const resp = await this.fetch(new URL("api/embed", this.apiBase), {
method: "POST",
body: JSON.stringify({
model: this.model,
input: chunks,
}),
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
headers: headers
});

if (!resp.ok) {
Expand Down
Loading