Skip to content

Commit

Permalink
feat: allow changing the API base URL (#9)
Browse files Browse the repository at this point in the history
* feat: allow changing the API base URL

* chore: specify url_join version and fix formatting

* chore: remove external dependencies

* fix: base url feature feedback changes
  • Loading branch information
djmuted committed May 3, 2023
1 parent 7831e2c commit f5c5130
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ import type {
} from "./types.ts";
import { basename } from "https://deno.land/std@0.185.0/path/mod.ts";

const baseUrl = "https://api.openai.com/v1";
const defaultBaseUrl = "https://api.openai.com/v1";

export class OpenAI {
#privateKey: string;
#baseUrl: string;

constructor(privateKey: string) {
constructor(privateKey: string, options?: { baseUrl?: string }) {
this.#privateKey = privateKey;
this.#baseUrl = options?.baseUrl ?? defaultBaseUrl;
}

async #request(
Expand All @@ -46,20 +48,23 @@ export class OpenAI {
body: any,
options?: { method?: string; noContentType?: boolean },
) {
const response = await fetch(`${baseUrl}${url}`, {
body: options?.noContentType
? body
: (body ? JSON.stringify(body) : undefined),
headers: {
Authorization: `Bearer ${this.#privateKey}`,
...(
options?.noContentType ? {} : {
"Content-Type": "application/json",
}
),
const response = await fetch(
`${this.#baseUrl}${url}`,
{
body: options?.noContentType
? body
: (body ? JSON.stringify(body) : undefined),
headers: {
Authorization: `Bearer ${this.#privateKey}`,
...(
options?.noContentType ? {} : {
"Content-Type": "application/json",
}
),
},
method: options?.method ?? "POST",
},
method: options?.method ?? "POST",
});
);

return await response.json();
}
Expand Down Expand Up @@ -362,12 +367,15 @@ export class OpenAI {
* https://platform.openai.com/docs/api-reference/files/retrieve-content
*/
async retrieveFileContent(fileId: string) {
const response = await fetch(`${baseUrl}/files/${fileId}/content`, {
headers: {
Authorization: `Bearer ${this.#privateKey}`,
"Content-Type": "application/json",
const response = await fetch(
`${this.#baseUrl}/files/${fileId}/content`,
{
headers: {
Authorization: `Bearer ${this.#privateKey}`,
"Content-Type": "application/json",
},
},
});
);
return response.body;
}

Expand Down

0 comments on commit f5c5130

Please sign in to comment.