node-openai
Advanced tools
Comparing version 0.4.2 to 0.5.0
@@ -25,20 +25,9 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OpenAI = void 0; | ||
const axios_1 = __importDefault(require("axios")); | ||
const v1 = __importStar(require("./v1")); | ||
const request_1 = require("./request"); | ||
class OpenAI { | ||
constructor(cfg) { | ||
this.config = Object.assign({ | ||
endpoint: "https://api.openai.com", | ||
options: { | ||
headers: { | ||
Authorization: `Bearer ${cfg.apiKey}`, | ||
}, | ||
validateStatus: () => true | ||
} | ||
}, cfg); | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
@@ -95,13 +84,19 @@ v1() { | ||
return async (path, options, direct = false) => { | ||
const url = `${this.config.endpoint}/${version}/${path}`; | ||
const response = await (0, axios_1.default)(Object.assign({ url }, this.config.options, options)); | ||
if (!direct && response.headers["content-type"] !== "application/json") { | ||
throw new Error(`Unexpected Content-Type: ${response.headers["content-type"]}`); | ||
if (this.config.options) { | ||
options = Object.assign(this.config.options, options); | ||
} | ||
else if (response.status !== 200) { | ||
throw new Error(direct ? response.statusText : response.data.error.message); | ||
const headers = { | ||
Authorization: `Bearer ${this.config.apiKey}`, | ||
}; | ||
if (this.config.organization) { | ||
headers["OpenAI-Organization"] = this.config.organization; | ||
} | ||
else { | ||
return response.data; | ||
options.headers = Object.assign(headers, options.headers || {}); | ||
const endpoint = this.config.endpoint || "https://api.openai.com"; | ||
const url = `${endpoint}/${version}/${path}`; | ||
const response = await (0, request_1.request)(url, options, direct ? "original" : "json"); | ||
if (response.status !== 200) { | ||
throw new Error(direct ? response.statusText : response.body.error.message); | ||
} | ||
return response.body; | ||
}; | ||
@@ -108,0 +103,0 @@ } |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createAudioTranslation = exports.createAudioTranscription = void 0; | ||
const fs_1 = require("fs"); | ||
const form_data_1 = __importDefault(require("form-data")); | ||
const request_1 = require("../request"); | ||
function createAudioTranscription(client) { | ||
return async (request, file) => { | ||
const data = new form_data_1.default(); | ||
const form = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key]); | ||
form[key] = request[key]; | ||
} | ||
data.append('file', (0, fs_1.createReadStream)(file)); | ||
return await client("audio/transcriptions", { method: "POST", data }); | ||
form['file'] = (0, request_1.readFile)(file); | ||
return await client("audio/transcriptions", { method: "POST", form }); | ||
}; | ||
@@ -22,8 +18,8 @@ } | ||
return async (request, file) => { | ||
const data = new form_data_1.default(); | ||
const form = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key]); | ||
form[key] = request[key]; | ||
} | ||
data.append('file', (0, fs_1.createReadStream)(file)); | ||
return await client("audio/translations", { method: "POST", data }); | ||
form['file'] = (0, request_1.readFile)(file); | ||
return await client("audio/translations", { method: "POST", form }); | ||
}; | ||
@@ -30,0 +26,0 @@ } |
@@ -5,4 +5,4 @@ "use strict"; | ||
function createChat(client) { | ||
return async (request) => { | ||
return await client("chat/completions", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("chat/completions", { method: "POST", data }, !!data.stream); | ||
}; | ||
@@ -9,0 +9,0 @@ } |
@@ -5,4 +5,4 @@ "use strict"; | ||
function createCompletion(client) { | ||
return async (request) => { | ||
return await client("completions", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("completions", { method: "POST", data }, !!data.stream); | ||
}; | ||
@@ -9,0 +9,0 @@ } |
@@ -5,4 +5,4 @@ "use strict"; | ||
function createEdit(client) { | ||
return async (request) => { | ||
return await client("edits", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("edits", { method: "POST", data }); | ||
}; | ||
@@ -9,0 +9,0 @@ } |
@@ -5,4 +5,4 @@ "use strict"; | ||
function createEmbedding(client) { | ||
return async (request) => { | ||
return await client("embeddings", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("embeddings", { method: "POST", data }); | ||
}; | ||
@@ -9,0 +9,0 @@ } |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.retrieveFileContent = exports.retrieveFile = exports.deleteFile = exports.uploadFile = exports.listFiles = void 0; | ||
const fs_1 = require("fs"); | ||
const form_data_1 = __importDefault(require("form-data")); | ||
const request_1 = require("../request"); | ||
function listFiles(client) { | ||
@@ -17,6 +13,7 @@ return async () => { | ||
return async (file, purpose) => { | ||
const data = new form_data_1.default(); | ||
data.append('purpose', purpose); | ||
data.append('file', (0, fs_1.createReadStream)(file)); | ||
return await client("files", { method: "POST", data }); | ||
const form = { | ||
'purpose': purpose, | ||
'file': (0, request_1.readFile)(file) | ||
}; | ||
return await client("files", { method: "POST", form }); | ||
}; | ||
@@ -23,0 +20,0 @@ } |
@@ -5,4 +5,4 @@ "use strict"; | ||
function createFineTune(client) { | ||
return async (fineTuneRequest) => { | ||
return await client("fine-tunes", { method: "POST", data: fineTuneRequest }); | ||
return async (data) => { | ||
return await client("fine-tunes", { method: "POST", data }); | ||
}; | ||
@@ -9,0 +9,0 @@ } |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createImageVariation = exports.editImage = exports.createImage = void 0; | ||
const fs_1 = require("fs"); | ||
const form_data_1 = __importDefault(require("form-data")); | ||
const request_1 = require("../request"); | ||
function createImage(client) { | ||
return async (request) => { | ||
return await client("images/generations", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("images/generations", { method: "POST", data }); | ||
}; | ||
@@ -17,11 +13,11 @@ } | ||
return async (request, image, mask) => { | ||
const data = new form_data_1.default(); | ||
const form = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key]); | ||
form[key] = request[key]; | ||
} | ||
data.append('image', (0, fs_1.createReadStream)(image)); | ||
form['image'] = (0, request_1.readFile)(image); | ||
if (mask) { | ||
data.append('mask', (0, fs_1.createReadStream)(mask)); | ||
form['mask'] = (0, request_1.readFile)(mask); | ||
} | ||
return await client("images/edits", { method: "POST", data }); | ||
return await client("images/edits", { method: "POST", form }); | ||
}; | ||
@@ -32,8 +28,8 @@ } | ||
return async (request, image) => { | ||
const data = new form_data_1.default(); | ||
const form = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key]); | ||
form[key] = request[key]; | ||
} | ||
data.append('image', (0, fs_1.createReadStream)(image)); | ||
return await client("images/variations", { method: "POST", data }); | ||
form['image'] = (0, request_1.readFile)(image); | ||
return await client("images/variations", { method: "POST", form }); | ||
}; | ||
@@ -40,0 +36,0 @@ } |
@@ -1,15 +0,7 @@ | ||
import axios from "axios"; | ||
import * as v1 from "./v1/index.js"; | ||
import { request } from "./request/index.js"; | ||
export class OpenAI { | ||
config; | ||
constructor(cfg) { | ||
this.config = Object.assign({ | ||
endpoint: "https://api.openai.com", | ||
options: { | ||
headers: { | ||
Authorization: `Bearer ${cfg.apiKey}`, | ||
}, | ||
validateStatus: () => true | ||
} | ||
}, cfg); | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
@@ -66,13 +58,19 @@ v1() { | ||
return async (path, options, direct = false) => { | ||
const url = `${this.config.endpoint}/${version}/${path}`; | ||
const response = await axios(Object.assign({ url }, this.config.options, options)); | ||
if (!direct && response.headers["content-type"] !== "application/json") { | ||
throw new Error(`Unexpected Content-Type: ${response.headers["content-type"]}`); | ||
if (this.config.options) { | ||
options = Object.assign(this.config.options, options); | ||
} | ||
else if (response.status !== 200) { | ||
throw new Error(direct ? response.statusText : response.data.error.message); | ||
const headers = { | ||
Authorization: `Bearer ${this.config.apiKey}`, | ||
}; | ||
if (this.config.organization) { | ||
headers["OpenAI-Organization"] = this.config.organization; | ||
} | ||
else { | ||
return response.data; | ||
options.headers = Object.assign(headers, options.headers || {}); | ||
const endpoint = this.config.endpoint || "https://api.openai.com"; | ||
const url = `${endpoint}/${version}/${path}`; | ||
const response = await request(url, options, direct ? "original" : "json"); | ||
if (response.status !== 200) { | ||
throw new Error(direct ? response.statusText : response.body.error.message); | ||
} | ||
return response.body; | ||
}; | ||
@@ -79,0 +77,0 @@ } |
@@ -1,11 +0,10 @@ | ||
import { createReadStream } from "fs"; | ||
import FormData from "form-data"; | ||
import { readFile } from "../request/index.js"; | ||
export function createAudioTranscription(client) { | ||
return async (request, file) => { | ||
const data = new FormData(); | ||
const form = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key]); | ||
form[key] = request[key]; | ||
} | ||
data.append('file', createReadStream(file)); | ||
return await client("audio/transcriptions", { method: "POST", data }); | ||
form['file'] = readFile(file); | ||
return await client("audio/transcriptions", { method: "POST", form }); | ||
}; | ||
@@ -15,10 +14,10 @@ } | ||
return async (request, file) => { | ||
const data = new FormData(); | ||
const form = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key]); | ||
form[key] = request[key]; | ||
} | ||
data.append('file', createReadStream(file)); | ||
return await client("audio/translations", { method: "POST", data }); | ||
form['file'] = readFile(file); | ||
return await client("audio/translations", { method: "POST", form }); | ||
}; | ||
} | ||
//# sourceMappingURL=audio.js.map |
export function createChat(client) { | ||
return async (request) => { | ||
return await client("chat/completions", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("chat/completions", { method: "POST", data }, !!data.stream); | ||
}; | ||
} | ||
//# sourceMappingURL=chat.js.map |
export function createCompletion(client) { | ||
return async (request) => { | ||
return await client("completions", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("completions", { method: "POST", data }, !!data.stream); | ||
}; | ||
} | ||
//# sourceMappingURL=completions.js.map |
export function createEdit(client) { | ||
return async (request) => { | ||
return await client("edits", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("edits", { method: "POST", data }); | ||
}; | ||
} | ||
//# sourceMappingURL=edits.js.map |
export function createEmbedding(client) { | ||
return async (request) => { | ||
return await client("embeddings", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("embeddings", { method: "POST", data }); | ||
}; | ||
} | ||
//# sourceMappingURL=embeddings.js.map |
@@ -1,3 +0,2 @@ | ||
import { createReadStream } from "fs"; | ||
import FormData from "form-data"; | ||
import { readFile } from "../request/index.js"; | ||
export function listFiles(client) { | ||
@@ -10,6 +9,7 @@ return async () => { | ||
return async (file, purpose) => { | ||
const data = new FormData(); | ||
data.append('purpose', purpose); | ||
data.append('file', createReadStream(file)); | ||
return await client("files", { method: "POST", data }); | ||
const form = { | ||
'purpose': purpose, | ||
'file': readFile(file) | ||
}; | ||
return await client("files", { method: "POST", form }); | ||
}; | ||
@@ -16,0 +16,0 @@ } |
export function createFineTune(client) { | ||
return async (fineTuneRequest) => { | ||
return await client("fine-tunes", { method: "POST", data: fineTuneRequest }); | ||
return async (data) => { | ||
return await client("fine-tunes", { method: "POST", data }); | ||
}; | ||
@@ -5,0 +5,0 @@ } |
@@ -1,6 +0,5 @@ | ||
import { createReadStream } from "fs"; | ||
import FormData from "form-data"; | ||
import { readFile } from "../request/index.js"; | ||
export function createImage(client) { | ||
return async (request) => { | ||
return await client("images/generations", { method: "POST", data: request }); | ||
return async (data) => { | ||
return await client("images/generations", { method: "POST", data }); | ||
}; | ||
@@ -10,11 +9,11 @@ } | ||
return async (request, image, mask) => { | ||
const data = new FormData(); | ||
const form = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key]); | ||
form[key] = request[key]; | ||
} | ||
data.append('image', createReadStream(image)); | ||
form['image'] = readFile(image); | ||
if (mask) { | ||
data.append('mask', createReadStream(mask)); | ||
form['mask'] = readFile(mask); | ||
} | ||
return await client("images/edits", { method: "POST", data }); | ||
return await client("images/edits", { method: "POST", form }); | ||
}; | ||
@@ -24,10 +23,10 @@ } | ||
return async (request, image) => { | ||
const data = new FormData(); | ||
const form = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key]); | ||
form[key] = request[key]; | ||
} | ||
data.append('image', createReadStream(image)); | ||
return await client("images/variations", { method: "POST", data }); | ||
form['image'] = readFile(image); | ||
return await client("images/variations", { method: "POST", form }); | ||
}; | ||
} | ||
//# sourceMappingURL=images.js.map |
/// <reference types="node" /> | ||
import { AxiosRequestConfig } from "axios"; | ||
import * as v1 from "./v1"; | ||
import { Init } from "./request"; | ||
export type ApiConfig = { | ||
@@ -8,9 +8,10 @@ apiKey: string; | ||
endpoint?: string; | ||
options?: AxiosRequestConfig; | ||
options?: ApiInit; | ||
}; | ||
export type ApiInit = Omit<Init, "signal">; | ||
export type ApiVersion = "v1" | "v2"; | ||
export type ApiClient = (path: string, options: AxiosRequestConfig, direct?: boolean) => Promise<any>; | ||
export type ApiClient = (path: string, options: Init, direct?: boolean) => Promise<any>; | ||
export declare class OpenAI { | ||
private config; | ||
constructor(cfg: ApiConfig); | ||
constructor(config: ApiConfig); | ||
v1(): { | ||
@@ -67,3 +68,3 @@ models: { | ||
completions: { | ||
create: (request: { | ||
create: (data: { | ||
model: string; | ||
@@ -104,3 +105,3 @@ prompt: string; | ||
chat: { | ||
create: (request: { | ||
create: (data: { | ||
model: string; | ||
@@ -139,3 +140,3 @@ messages: { | ||
edits: { | ||
create: (request: { | ||
create: (data: { | ||
model: string; | ||
@@ -158,3 +159,3 @@ input?: string | undefined; | ||
images: { | ||
create: (request: { | ||
create: (data: { | ||
prompt: string; | ||
@@ -178,3 +179,3 @@ n?: number | undefined; | ||
user?: string | undefined; | ||
}, image: string, mask?: string | undefined) => Promise<{ | ||
}, image: string | File, mask?: string | File | undefined) => Promise<{ | ||
created: number; | ||
@@ -191,3 +192,3 @@ data: Partial<{ | ||
user?: string | undefined; | ||
}, image: string) => Promise<{ | ||
}, image: string | File) => Promise<{ | ||
created: number; | ||
@@ -201,3 +202,3 @@ data: Partial<{ | ||
embeddings: { | ||
create: (request: { | ||
create: (data: { | ||
model: string; | ||
@@ -224,3 +225,3 @@ input: string; | ||
language?: string | undefined; | ||
}, file: string) => Promise<Partial<{ | ||
}, file: string | File) => Promise<Partial<{ | ||
json: string; | ||
@@ -237,3 +238,3 @@ text: string; | ||
temperature?: number | undefined; | ||
}, file: string) => Promise<Partial<{ | ||
}, file: string | File) => Promise<Partial<{ | ||
json: string; | ||
@@ -248,7 +249,7 @@ text: string; | ||
list: () => Promise<{ | ||
data: v1.File[]; | ||
data: v1.FileObject[]; | ||
object: "list"; | ||
}>; | ||
retrieve: (id: string) => Promise<v1.File>; | ||
upload: (file: string, purpose: string) => Promise<v1.File>; | ||
retrieve: (id: string) => Promise<v1.FileObject>; | ||
upload: (file: string | File, purpose: string) => Promise<v1.FileObject>; | ||
delete: (id: string) => Promise<{ | ||
@@ -262,3 +263,3 @@ id: string; | ||
fineTunes: { | ||
create: (fineTuneRequest: { | ||
create: (data: { | ||
training_file: string; | ||
@@ -290,6 +291,6 @@ validation_file?: string | undefined; | ||
organization_id: string; | ||
result_files: v1.File[]; | ||
result_files: v1.FileObject[]; | ||
status: string; | ||
validation_files: v1.File[]; | ||
training_files: v1.File[]; | ||
validation_files: v1.FileObject[]; | ||
training_files: v1.FileObject[]; | ||
updated_at: number; | ||
@@ -318,6 +319,6 @@ events: { | ||
organization_id: string; | ||
result_files: v1.File[]; | ||
result_files: v1.FileObject[]; | ||
status: string; | ||
validation_files: v1.File[]; | ||
training_files: v1.File[]; | ||
validation_files: v1.FileObject[]; | ||
training_files: v1.FileObject[]; | ||
updated_at: number; | ||
@@ -345,6 +346,6 @@ events: { | ||
organization_id: string; | ||
result_files: v1.File[]; | ||
result_files: v1.FileObject[]; | ||
status: string; | ||
validation_files: v1.File[]; | ||
training_files: v1.File[]; | ||
validation_files: v1.FileObject[]; | ||
training_files: v1.FileObject[]; | ||
updated_at: number; | ||
@@ -371,6 +372,6 @@ events: { | ||
organization_id: string; | ||
result_files: v1.File[]; | ||
result_files: v1.FileObject[]; | ||
status: string; | ||
validation_files: v1.File[]; | ||
training_files: v1.File[]; | ||
validation_files: v1.FileObject[]; | ||
training_files: v1.FileObject[]; | ||
updated_at: number; | ||
@@ -377,0 +378,0 @@ events: { |
@@ -19,4 +19,4 @@ import { ApiClient } from ".."; | ||
}>; | ||
export declare function createAudioTranscription(client: ApiClient): (request: CreateAudioTranscriptionRequest, file: string) => Promise<Audio>; | ||
export declare function createAudioTranslation(client: ApiClient): (request: CreateAudioTranslationRequest, file: string) => Promise<Audio>; | ||
export declare function createAudioTranscription(client: ApiClient): (request: CreateAudioTranscriptionRequest, file: string | File) => Promise<Audio>; | ||
export declare function createAudioTranslation(client: ApiClient): (request: CreateAudioTranslationRequest, file: string | File) => Promise<Audio>; | ||
export {}; |
@@ -36,3 +36,3 @@ import { ApiClient } from ".."; | ||
}; | ||
export declare function createChat(client: ApiClient): (request: ChatRequest) => Promise<Chat>; | ||
export declare function createChat(client: ApiClient): (data: ChatRequest) => Promise<Chat>; | ||
export {}; |
@@ -39,3 +39,3 @@ import { Usage } from "."; | ||
}; | ||
export declare function createCompletion(client: ApiClient): (request: CompletionRequest) => Promise<Completion>; | ||
export declare function createCompletion(client: ApiClient): (data: CompletionRequest) => Promise<Completion>; | ||
export {}; |
@@ -21,3 +21,3 @@ import { Usage } from "."; | ||
}; | ||
export declare function createEdit(client: ApiClient): (request: EditRequest) => Promise<Edit>; | ||
export declare function createEdit(client: ApiClient): (data: EditRequest) => Promise<Edit>; | ||
export {}; |
@@ -19,3 +19,3 @@ import { Usage } from "."; | ||
}; | ||
export declare function createEmbedding(client: ApiClient): (request: EmbeddingRequest) => Promise<EmbeddingList>; | ||
export declare function createEmbedding(client: ApiClient): (data: EmbeddingRequest) => Promise<EmbeddingList>; | ||
export {}; |
/// <reference types="node" /> | ||
import { ApiClient } from ".."; | ||
export type File = { | ||
export type FileObject = { | ||
id: string; | ||
@@ -17,10 +17,10 @@ object: "file"; | ||
type FileList = { | ||
data: File[]; | ||
data: FileObject[]; | ||
object: "list"; | ||
}; | ||
export declare function listFiles(client: ApiClient): () => Promise<FileList>; | ||
export declare function uploadFile(client: ApiClient): (file: string, purpose: string) => Promise<File>; | ||
export declare function uploadFile(client: ApiClient): (file: string | File, purpose: string) => Promise<FileObject>; | ||
export declare function deleteFile(client: ApiClient): (id: string) => Promise<DeletedFile>; | ||
export declare function retrieveFile(client: ApiClient): (id: string) => Promise<File>; | ||
export declare function retrieveFile(client: ApiClient): (id: string) => Promise<FileObject>; | ||
export declare function retrieveFileContent(client: ApiClient): (id: string) => Promise<Buffer>; | ||
export {}; |
import { ApiClient } from ".."; | ||
import { File } from "./files"; | ||
import { FileObject } from "./files"; | ||
type FineTuneHyperparameters = { | ||
@@ -33,6 +33,6 @@ learning_rate_multiplier: number; | ||
organization_id: string; | ||
result_files: File[]; | ||
result_files: FileObject[]; | ||
status: string; | ||
validation_files: File[]; | ||
training_files: File[]; | ||
validation_files: FileObject[]; | ||
training_files: FileObject[]; | ||
updated_at: number; | ||
@@ -49,3 +49,3 @@ events: FineTuneEvent[]; | ||
}; | ||
export declare function createFineTune(client: ApiClient): (fineTuneRequest: FineTuneRequest) => Promise<FineTune>; | ||
export declare function createFineTune(client: ApiClient): (data: FineTuneRequest) => Promise<FineTune>; | ||
export declare function retrieveFineTune(client: ApiClient): (id: string) => Promise<FineTune>; | ||
@@ -52,0 +52,0 @@ export declare function listFineTunes(client: ApiClient): () => Promise<FineTuneList>; |
@@ -31,5 +31,5 @@ import { ApiClient } from ".."; | ||
}; | ||
export declare function createImage(client: ApiClient): (request: CreateImageRequest) => Promise<Image>; | ||
export declare function editImage(client: ApiClient): (request: EditImageRequest, image: string, mask?: string) => Promise<Image>; | ||
export declare function createImageVariation(client: ApiClient): (request: CreateImageVariationRequest, image: string) => Promise<Image>; | ||
export declare function createImage(client: ApiClient): (data: CreateImageRequest) => Promise<Image>; | ||
export declare function editImage(client: ApiClient): (request: EditImageRequest, image: string | File, mask?: string | File) => Promise<Image>; | ||
export declare function createImageVariation(client: ApiClient): (request: CreateImageVariationRequest, image: string | File) => Promise<Image>; | ||
export {}; |
@@ -20,3 +20,3 @@ { | ||
}, | ||
"version": "v0.4.2", | ||
"version": "v0.5.0", | ||
"exports": { | ||
@@ -50,7 +50,3 @@ ".": { | ||
"typescript": "latest" | ||
}, | ||
"dependencies": { | ||
"axios": "^1.3.4", | ||
"form-data": "^4.0.0" | ||
} | ||
} |
# node-openai | ||
An elegant Node.js library written in TypeScript for the OpenAI API. | ||
**An elegant Node.js library written in TypeScript for the OpenAI API. Pure JavaScript, no dependencies. Works in Node.js and the browser.** | ||
[![npm](https://img.shields.io/npm/v/node-openai.svg)](https://www.npmjs.com/package/node-openai) | ||
[![npm](https://img.shields.io/npm/dt/node-openai.svg)](https://www.npmjs.com/package/node-openai) | ||
[![GitHub](https://img.shields.io/github/license/joyqi/node-openai.svg)](https://github.com/joyqi/node-openai/blob/master/LICENSE) | ||
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/joyqi/node-openai/ci.yml) | ||
![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/joyqi/node-openai) | ||
- [Installation](#installation) | ||
@@ -67,3 +73,3 @@ - [Features](#features) | ||
// organization: 'YOUR_ORGANIZATION_ID', | ||
// options: { /* Axios Request Options */ }, | ||
// endpoint: 'https://api.openai.com', | ||
}); | ||
@@ -111,6 +117,22 @@ ``` | ||
max_tokens: 5, | ||
temperature: 0.9 | ||
temperature: 0.9, | ||
stream: false, | ||
}); | ||
``` | ||
If the `stream` option is set to `true`, the completion will be streamed: | ||
```javascript | ||
const stream = await api.completions.create({ | ||
model: 'davinci', | ||
prompt: 'This is a test', | ||
max_tokens: 5, | ||
temperature: 0.9, | ||
stream: true, | ||
}); | ||
const reader = stream.pipeThrough(new TextDecoderStream()).getReader(); | ||
// Read the stream | ||
``` | ||
### Chat | ||
@@ -117,0 +139,0 @@ |
@@ -1,3 +0,3 @@ | ||
import axios, { AxiosRequestConfig } from "axios"; | ||
import * as v1 from "./v1"; | ||
import { Init, request } from "./request"; | ||
@@ -9,25 +9,16 @@ // ApiConfig defines the configuration options for the OpenAI API | ||
endpoint?: string; | ||
options?: AxiosRequestConfig; | ||
options?: ApiInit; | ||
}; | ||
export type ApiInit = Omit<Init, "signal">; | ||
// ApiVersion defines the version of the OpenAI API | ||
export type ApiVersion = "v1" | "v2"; | ||
export type ApiClient = (path: string, options: AxiosRequestConfig, direct?: boolean) => Promise<any>; | ||
export type ApiClient = (path: string, options: Init, direct?: boolean) => Promise<any>; | ||
// OpenAI is the main class for the OpenAI API | ||
export class OpenAI { | ||
private config: ApiConfig; | ||
constructor(cfg: ApiConfig) { | ||
this.config = Object.assign({ | ||
endpoint: "https://api.openai.com", | ||
options: { | ||
headers: { | ||
Authorization: `Bearer ${cfg.apiKey}`, | ||
}, | ||
validateStatus: () => true | ||
} | ||
}, cfg); | ||
} | ||
constructor(private config: ApiConfig) {} | ||
@@ -86,15 +77,28 @@ v1() { | ||
private makeClient(version: ApiVersion): ApiClient { | ||
return async (path: string, options: AxiosRequestConfig, direct = false) => { | ||
const url = `${this.config.endpoint}/${version}/${path}`; | ||
const response = await axios(Object.assign({ url }, this.config.options, options)); | ||
return async (path: string, options: Init, direct = false) => { | ||
if (this.config.options) { | ||
options = Object.assign(this.config.options, options); | ||
} | ||
if (!direct && response.headers["content-type"] !== "application/json") { | ||
throw new Error(`Unexpected Content-Type: ${response.headers["content-type"]}`); | ||
} else if (response.status !== 200) { | ||
throw new Error(direct ? response.statusText : response.data.error.message); | ||
} else { | ||
return response.data; | ||
const headers: Record<string, string> = { | ||
Authorization: `Bearer ${this.config.apiKey}`, | ||
}; | ||
if (this.config.organization) { | ||
headers["OpenAI-Organization"] = this.config.organization; | ||
} | ||
options.headers = Object.assign(headers, options.headers || {}); | ||
const endpoint = this.config.endpoint || "https://api.openai.com"; | ||
const url = `${endpoint}/${version}/${path}`; | ||
const response = await request(url, options, direct ? "original" : "json"); | ||
if (response.status !== 200) { | ||
throw new Error(direct ? response.statusText : response.body.error.message); | ||
} | ||
return response.body; | ||
} | ||
} | ||
} |
@@ -1,4 +0,3 @@ | ||
import { createReadStream } from "fs"; | ||
import { ApiClient } from ".."; | ||
import FormData from "form-data"; | ||
import { readFile } from "../request"; | ||
@@ -27,12 +26,11 @@ type AudioResponseFormat = 'json' | 'text' | 'srt' | 'verbose_json' | 'vtt'; | ||
export function createAudioTranscription(client: ApiClient) { | ||
return async (request: CreateAudioTranscriptionRequest, file: string): Promise<Audio> => { | ||
const data = new FormData(); | ||
return async (request: CreateAudioTranscriptionRequest, file: string | File): Promise<Audio> => { | ||
const form: Record<string, any> = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key as keyof CreateAudioTranscriptionRequest]); | ||
form[key] = request[key as keyof CreateAudioTranscriptionRequest]; | ||
} | ||
data.append('file', createReadStream(file)); | ||
return await client("audio/transcriptions", { method: "POST", data }); | ||
form['file'] = readFile(file); | ||
return await client("audio/transcriptions", { method: "POST", form }); | ||
} | ||
@@ -42,13 +40,12 @@ } | ||
export function createAudioTranslation(client: ApiClient) { | ||
return async (request: CreateAudioTranslationRequest, file: string): Promise<Audio> => { | ||
const data = new FormData(); | ||
return async (request: CreateAudioTranslationRequest, file: string | File): Promise<Audio> => { | ||
const form: Record<string, any> = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key as keyof CreateAudioTranslationRequest]); | ||
form[key] = request[key as keyof CreateAudioTranslationRequest]; | ||
} | ||
data.append('file', createReadStream(file)); | ||
return await client("audio/translations", { method: "POST", data }); | ||
form['file'] = readFile(file); | ||
return await client("audio/translations", { method: "POST", form }); | ||
} | ||
} |
@@ -41,5 +41,5 @@ import { ApiClient } from ".."; | ||
export function createChat(client: ApiClient) { | ||
return async (request: ChatRequest): Promise<Chat> => { | ||
return await client("chat/completions", { method: "POST", data: request }); | ||
return async (data: ChatRequest): Promise<Chat> => { | ||
return await client("chat/completions", { method: "POST", data }, !!data.stream); | ||
} | ||
} |
@@ -40,5 +40,5 @@ import { Usage } from "."; | ||
export function createCompletion(client: ApiClient) { | ||
return async (request: CompletionRequest): Promise<Completion> => { | ||
return await client("completions", { method: "POST", data: request }); | ||
return async (data: CompletionRequest): Promise<Completion> => { | ||
return await client("completions", { method: "POST", data }, !!data.stream); | ||
} | ||
} |
@@ -26,5 +26,5 @@ import { Usage } from "."; | ||
export function createEdit(client: ApiClient) { | ||
return async (request: EditRequest): Promise<Edit> => { | ||
return await client("edits", { method: "POST", data: request }); | ||
return async (data: EditRequest): Promise<Edit> => { | ||
return await client("edits", { method: "POST", data }); | ||
} | ||
} |
@@ -24,5 +24,5 @@ import { Usage } from "."; | ||
export function createEmbedding(client: ApiClient) { | ||
return async (request: EmbeddingRequest): Promise<EmbeddingList> => { | ||
return await client("embeddings", { method: "POST", data: request }); | ||
return async (data: EmbeddingRequest): Promise<EmbeddingList> => { | ||
return await client("embeddings", { method: "POST", data }); | ||
} | ||
} |
@@ -1,6 +0,5 @@ | ||
import { createReadStream } from "fs"; | ||
import { ApiClient } from ".."; | ||
import FormData from "form-data"; | ||
import { readFile } from "../request"; | ||
export type File = { | ||
export type FileObject = { | ||
id: string; | ||
@@ -21,3 +20,3 @@ object: "file"; | ||
type FileList = { | ||
data: File[]; | ||
data: FileObject[]; | ||
object: "list"; | ||
@@ -33,9 +32,9 @@ }; | ||
export function uploadFile(client: ApiClient) { | ||
return async (file: string, purpose: string): Promise<File> => { | ||
const data = new FormData(); | ||
return async (file: string | File, purpose: string): Promise<FileObject> => { | ||
const form: Record<string, any> = { | ||
'purpose': purpose, | ||
'file': readFile(file) | ||
}; | ||
data.append('purpose', purpose); | ||
data.append('file', createReadStream(file)); | ||
return await client("files", { method: "POST", data }); | ||
return await client("files", { method: "POST", form }); | ||
} | ||
@@ -51,3 +50,3 @@ } | ||
export function retrieveFile(client: ApiClient) { | ||
return async (id: string): Promise<File> => { | ||
return async (id: string): Promise<FileObject> => { | ||
return await client(`files/${id}`, { method: "GET" }); | ||
@@ -54,0 +53,0 @@ } |
import { ApiClient } from ".."; | ||
import { File } from "./files"; | ||
import { FileObject } from "./files"; | ||
@@ -37,6 +37,6 @@ type FineTuneHyperparameters = { | ||
organization_id: string; | ||
result_files: File[]; | ||
result_files: FileObject[]; | ||
status: string; | ||
validation_files: File[]; | ||
training_files: File[]; | ||
validation_files: FileObject[]; | ||
training_files: FileObject[]; | ||
updated_at: number; | ||
@@ -57,4 +57,4 @@ events: FineTuneEvent[]; | ||
export function createFineTune(client: ApiClient) { | ||
return async (fineTuneRequest: FineTuneRequest): Promise<FineTune> => { | ||
return await client("fine-tunes", { method: "POST", data: fineTuneRequest }); | ||
return async (data: FineTuneRequest): Promise<FineTune> => { | ||
return await client("fine-tunes", { method: "POST", data }); | ||
} | ||
@@ -61,0 +61,0 @@ } |
@@ -1,4 +0,3 @@ | ||
import { createReadStream } from "fs"; | ||
import { ApiClient } from ".."; | ||
import FormData from "form-data"; | ||
import { readFile } from "../request"; | ||
@@ -42,4 +41,4 @@ type ImageSize = '256x256' | '512x512' | '1024x1024'; | ||
export function createImage(client: ApiClient) { | ||
return async (request: CreateImageRequest): Promise<Image> => { | ||
return await client("images/generations", { method: "POST", data: request }); | ||
return async (data: CreateImageRequest): Promise<Image> => { | ||
return await client("images/generations", { method: "POST", data }); | ||
} | ||
@@ -49,16 +48,16 @@ } | ||
export function editImage(client: ApiClient) { | ||
return async (request: EditImageRequest, image: string, mask?: string): Promise<Image> => { | ||
const data = new FormData(); | ||
return async (request: EditImageRequest, image: string | File, mask?: string | File): Promise<Image> => { | ||
const form: Record<string, any> = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key as keyof EditImageRequest]); | ||
form[key] = request[key as keyof EditImageRequest]; | ||
} | ||
data.append('image', createReadStream(image)); | ||
form['image'] = readFile(image); | ||
if (mask) { | ||
data.append('mask', createReadStream(mask)); | ||
form['mask'] = readFile(mask); | ||
} | ||
return await client("images/edits", { method: "POST", data }); | ||
return await client("images/edits", { method: "POST", form }); | ||
} | ||
@@ -68,13 +67,13 @@ } | ||
export function createImageVariation(client: ApiClient) { | ||
return async (request: CreateImageVariationRequest, image: string): Promise<Image> => { | ||
const data = new FormData(); | ||
return async (request: CreateImageVariationRequest, image: string | File): Promise<Image> => { | ||
const form: Record<string, any> = {}; | ||
for (const key in request) { | ||
data.append(key, '' + request[key as keyof CreateImageVariationRequest]); | ||
form[key] = request[key as keyof CreateImageVariationRequest]; | ||
} | ||
data.append('image', createReadStream(image)); | ||
form['image'] = readFile(image); | ||
return await client("images/variations", { method: "POST", data }); | ||
return await client("images/variations", { method: "POST", form }); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 3 instances in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
139921
0
124
2718
301
2
7
- Removedaxios@^1.3.4
- Removedform-data@^4.0.0
- Removedasynckit@0.4.0(transitive)
- Removedaxios@1.7.9(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedfollow-redirects@1.15.9(transitive)
- Removedform-data@4.0.1(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedproxy-from-env@1.1.0(transitive)