Skip to content

Commit

Permalink
use whatwg-fetch to allow default export
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceMacD authored Jan 21, 2024
2 parents 8a1885a + 1a1c720 commit 6ff57b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
"devDependencies": {
"@swc/core": "^1.3.14",
"@types/jest": "^29.2.2",
"@types/whatwg-fetch": "^0.0.33",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"eslint": "^8.29.0",
"eslint-plugin-jest": "^27.1.4",
"jest": "^29.3.0",
"ts-jest": "^29.0.3",
"typescript": "^4.8.4"
},
"dependencies": {
"whatwg-fetch": "^3.6.20"
}
}
34 changes: 13 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as utils from "./utils.js";
import 'whatwg-fetch';
import { promises, createReadStream } from 'fs';
import { join, resolve, dirname } from 'path';
import { createHash } from 'crypto';
Expand Down Expand Up @@ -31,27 +32,16 @@ export class Ollama {
private readonly config: Config;
private readonly fetch: Fetch;

constructor (config?: Partial<Config>) {
this.config = {
address: config?.address ?? "http://127.0.0.1:11434"
};

let f: Fetch | null = null;

if (config?.fetch != null) {
f = config.fetch;
} else if (typeof fetch !== "undefined") {
f = fetch;
} else if (typeof window !== "undefined") {
f = window.fetch;
}

if (f == null) {
throw new Error("unable to find fetch - please define it via 'config.fetch'");
}

this.fetch = f;
}
constructor (config?: Partial<Config>) {
this.config = {
address: config?.address ?? "http://127.0.0.1:11434"
};

this.fetch = fetch;
if (config?.fetch != null) {
this.fetch = config.fetch;
}
}

private async processStreamableRequest<T extends object>(endpoint: string, request: { stream?: boolean } & Record<string, any>): Promise<T | AsyncGenerator<T>> {
request.stream = request.stream ?? false;
Expand Down Expand Up @@ -287,3 +277,5 @@ export class Ollama {
return embeddingsResponse;
}
}

export default new Ollama();

0 comments on commit 6ff57b3

Please sign in to comment.