node-openai
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -64,2 +64,16 @@ "use strict"; | ||
createVariation: v1.createImageVariation(client) | ||
}, | ||
embeddings: { | ||
create: v1.createEmbedding(client) | ||
}, | ||
audio: { | ||
createTranscription: v1.createAudioTranscription(client), | ||
createTranslation: v1.createAudioTranslation(client) | ||
}, | ||
files: { | ||
list: v1.listFiles(client), | ||
retrieve: v1.retrieveFile(client), | ||
upload: v1.uploadFile(client), | ||
delete: v1.deleteFile(client), | ||
retrieveContent: v1.retrieveFileContent(client) | ||
} | ||
@@ -69,10 +83,10 @@ }; | ||
makeClient(version) { | ||
return async (path, options) => { | ||
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 (response.headers["content-type"] !== "application/json") { | ||
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(response.data.error.message); | ||
throw new Error(direct ? response.statusText : response.data.error.message); | ||
} | ||
@@ -79,0 +93,0 @@ else { |
@@ -17,11 +17,11 @@ "use strict"; | ||
return async (request, image, mask) => { | ||
const form = new form_data_1.default(); | ||
const data = new form_data_1.default(); | ||
for (const key in request) { | ||
form.append(key, '' + request[key]); | ||
data.append(key, '' + request[key]); | ||
} | ||
form.append('image', (0, fs_1.createReadStream)(image)); | ||
data.append('image', (0, fs_1.createReadStream)(image)); | ||
if (mask) { | ||
form.append('mask', (0, fs_1.createReadStream)(mask)); | ||
data.append('mask', (0, fs_1.createReadStream)(mask)); | ||
} | ||
return await client("images/edits", { method: "POST", data: form }); | ||
return await client("images/edits", { method: "POST", data }); | ||
}; | ||
@@ -32,8 +32,8 @@ } | ||
return async (request, image) => { | ||
const form = new form_data_1.default(); | ||
const data = new form_data_1.default(); | ||
for (const key in request) { | ||
form.append(key, '' + request[key]); | ||
data.append(key, '' + request[key]); | ||
} | ||
form.append('image', (0, fs_1.createReadStream)(image)); | ||
return await client("images/variations", { method: "POST", data: form }); | ||
data.append('image', (0, fs_1.createReadStream)(image)); | ||
return await client("images/variations", { method: "POST", data }); | ||
}; | ||
@@ -40,0 +40,0 @@ } |
@@ -22,2 +22,5 @@ "use strict"; | ||
__exportStar(require("./images"), exports); | ||
__exportStar(require("./embeddings"), exports); | ||
__exportStar(require("./audio"), exports); | ||
__exportStar(require("./files"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -36,2 +36,16 @@ import axios from "axios"; | ||
createVariation: v1.createImageVariation(client) | ||
}, | ||
embeddings: { | ||
create: v1.createEmbedding(client) | ||
}, | ||
audio: { | ||
createTranscription: v1.createAudioTranscription(client), | ||
createTranslation: v1.createAudioTranslation(client) | ||
}, | ||
files: { | ||
list: v1.listFiles(client), | ||
retrieve: v1.retrieveFile(client), | ||
upload: v1.uploadFile(client), | ||
delete: v1.deleteFile(client), | ||
retrieveContent: v1.retrieveFileContent(client) | ||
} | ||
@@ -41,10 +55,10 @@ }; | ||
makeClient(version) { | ||
return async (path, options) => { | ||
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 (response.headers["content-type"] !== "application/json") { | ||
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(response.data.error.message); | ||
throw new Error(direct ? response.statusText : response.data.error.message); | ||
} | ||
@@ -51,0 +65,0 @@ else { |
@@ -10,11 +10,11 @@ import { createReadStream } from "fs"; | ||
return async (request, image, mask) => { | ||
const form = new FormData(); | ||
const data = new FormData(); | ||
for (const key in request) { | ||
form.append(key, '' + request[key]); | ||
data.append(key, '' + request[key]); | ||
} | ||
form.append('image', createReadStream(image)); | ||
data.append('image', createReadStream(image)); | ||
if (mask) { | ||
form.append('mask', createReadStream(mask)); | ||
data.append('mask', createReadStream(mask)); | ||
} | ||
return await client("images/edits", { method: "POST", data: form }); | ||
return await client("images/edits", { method: "POST", data }); | ||
}; | ||
@@ -24,10 +24,10 @@ } | ||
return async (request, image) => { | ||
const form = new FormData(); | ||
const data = new FormData(); | ||
for (const key in request) { | ||
form.append(key, '' + request[key]); | ||
data.append(key, '' + request[key]); | ||
} | ||
form.append('image', createReadStream(image)); | ||
return await client("images/variations", { method: "POST", data: form }); | ||
data.append('image', createReadStream(image)); | ||
return await client("images/variations", { method: "POST", data }); | ||
}; | ||
} | ||
//# sourceMappingURL=images.js.map |
@@ -6,2 +6,5 @@ export * from "./models.js"; | ||
export * from "./images.js"; | ||
export * from "./embeddings.js"; | ||
export * from "./audio.js"; | ||
export * from "./files.js"; | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
import { AxiosRequestConfig } from "axios"; | ||
@@ -10,3 +11,3 @@ import * as v1 from "./v1"; | ||
export type ApiVersion = "v1" | "v2"; | ||
export type ApiClient = (path: string, options: AxiosRequestConfig) => Promise<any>; | ||
export type ApiClient = (path: string, options: AxiosRequestConfig, direct?: boolean) => Promise<any>; | ||
export declare class OpenAI { | ||
@@ -157,7 +158,6 @@ private config; | ||
created: number; | ||
data: ({ | ||
data: Partial<{ | ||
url: string; | ||
} | { | ||
b64_json: string; | ||
})[]; | ||
}>[]; | ||
}>; | ||
@@ -172,7 +172,6 @@ edit: (request: { | ||
created: number; | ||
data: ({ | ||
data: Partial<{ | ||
url: string; | ||
} | { | ||
b64_json: string; | ||
})[]; | ||
}>[]; | ||
}>; | ||
@@ -186,11 +185,88 @@ createVariation: (request: { | ||
created: number; | ||
data: ({ | ||
data: Partial<{ | ||
url: string; | ||
} | { | ||
b64_json: string; | ||
})[]; | ||
}>[]; | ||
}>; | ||
}; | ||
embeddings: { | ||
create: (request: { | ||
model: string; | ||
input: string; | ||
user?: string | undefined; | ||
}) => Promise<{ | ||
object: string; | ||
data: { | ||
object: string; | ||
embedding: number[]; | ||
index: number; | ||
}[]; | ||
model: string; | ||
usage: Omit<v1.Usage, "completion_tokens">; | ||
}>; | ||
}; | ||
audio: { | ||
createTranscription: (request: { | ||
model: string; | ||
prompt?: string | undefined; | ||
response_format?: ("json" | "text" | "srt" | "verbose_json" | "vtt") | undefined; | ||
temperature?: number | undefined; | ||
language?: string | undefined; | ||
}, file: string) => Promise<Partial<{ | ||
json: string; | ||
text: string; | ||
srt: string; | ||
verbose_json: string; | ||
vtt: string; | ||
}>>; | ||
createTranslation: (request: { | ||
model: string; | ||
prompt?: string | undefined; | ||
response_format?: ("json" | "text" | "srt" | "verbose_json" | "vtt") | undefined; | ||
temperature?: number | undefined; | ||
}, file: string) => Promise<Partial<{ | ||
json: string; | ||
text: string; | ||
srt: string; | ||
verbose_json: string; | ||
vtt: string; | ||
}>>; | ||
}; | ||
files: { | ||
list: () => Promise<{ | ||
data: { | ||
id: string; | ||
object: string; | ||
bytes: number; | ||
created_at: number; | ||
filename: string; | ||
purpose: string; | ||
}[]; | ||
object: string; | ||
}>; | ||
retrieve: (id: string) => Promise<{ | ||
id: string; | ||
object: string; | ||
bytes: number; | ||
created_at: number; | ||
filename: string; | ||
purpose: string; | ||
}>; | ||
upload: (file: string, purpose: string) => Promise<{ | ||
id: string; | ||
object: string; | ||
bytes: number; | ||
created_at: number; | ||
filename: string; | ||
purpose: string; | ||
}>; | ||
delete: (id: string) => Promise<{ | ||
id: string; | ||
object: string; | ||
deleted: boolean; | ||
}>; | ||
retrieveContent: (id: string) => Promise<Buffer>; | ||
}; | ||
}; | ||
private makeClient; | ||
} |
@@ -24,9 +24,5 @@ import { ApiClient } from ".."; | ||
}; | ||
type UrlImage = { | ||
url: string; | ||
}; | ||
type B64JsonImage = { | ||
b64_json: string; | ||
}; | ||
type ImageData = UrlImage | B64JsonImage; | ||
type ImageData = Partial<{ | ||
[key in ImageResponseFormat]: string; | ||
}>; | ||
type Image = { | ||
@@ -33,0 +29,0 @@ created: number; |
@@ -11,1 +11,4 @@ export type Usage = { | ||
export * from "./images"; | ||
export * from "./embeddings"; | ||
export * from "./audio"; | ||
export * from "./files"; |
@@ -15,3 +15,3 @@ { | ||
}, | ||
"version": "v0.2.0", | ||
"version": "v0.3.0", | ||
"exports": { | ||
@@ -18,0 +18,0 @@ ".": { |
@@ -13,2 +13,5 @@ # node-openai | ||
- [Images](#images) | ||
- [Embeddings](#embeddings) | ||
- [Audio](#audio) | ||
- [Files](#files) | ||
@@ -145,2 +148,65 @@ ## Installation | ||
}, '/path/to/image.png'); | ||
``` | ||
### Embeddings | ||
Create an embedding: | ||
```javascript | ||
const embedding = await v1.embeddings.create({ | ||
model: 'text-embedding-ada-002', | ||
input: 'This is a test', | ||
}); | ||
``` | ||
### Audio | ||
Create transcription: | ||
```javascript | ||
const transcription = await v1.audio.createTranscription({ | ||
model: 'whisper-1', | ||
prompt: 'This is a test', | ||
}, '/path/to/audio.mp3'); | ||
``` | ||
Create translation: | ||
```javascript | ||
const translation = await v1.audio.createTranslation({ | ||
model: 'whisper-1', | ||
prompt: 'This is a test', | ||
}, '/path/to/audio.mp3'); | ||
``` | ||
### Files | ||
List all available files: | ||
```javascript | ||
const files = await v1.files.list(); | ||
``` | ||
Retrieve a file: | ||
```javascript | ||
const file = await v1.files.retrieve('file-123'); | ||
``` | ||
Upload a file: | ||
```javascript | ||
const file = await v1.files.upload('/path/to/file.txt', 'fine-tune'); | ||
``` | ||
Delete a file: | ||
```javascript | ||
const file = await v1.files.delete('file-123'); | ||
``` | ||
Retrieve a file's contents: | ||
```javascript | ||
const content = await v1.files.retrieveContents('file-123'); | ||
``` |
@@ -15,3 +15,3 @@ import axios, { AxiosRequestConfig } from "axios"; | ||
export type ApiClient = (path: string, options: AxiosRequestConfig) => Promise<any>; | ||
export type ApiClient = (path: string, options: AxiosRequestConfig, direct?: boolean) => Promise<any>; | ||
@@ -55,2 +55,16 @@ // OpenAI is the main class for the OpenAI API | ||
createVariation: v1.createImageVariation(client) | ||
}, | ||
embeddings: { | ||
create: v1.createEmbedding(client) | ||
}, | ||
audio: { | ||
createTranscription: v1.createAudioTranscription(client), | ||
createTranslation: v1.createAudioTranslation(client) | ||
}, | ||
files: { | ||
list: v1.listFiles(client), | ||
retrieve: v1.retrieveFile(client), | ||
upload: v1.uploadFile(client), | ||
delete: v1.deleteFile(client), | ||
retrieveContent: v1.retrieveFileContent(client) | ||
} | ||
@@ -62,10 +76,10 @@ }; | ||
private makeClient(version: ApiVersion): ApiClient { | ||
return async (path: string, options: AxiosRequestConfig) => { | ||
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)); | ||
if (response.headers["content-type"] !== "application/json") { | ||
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(response.data.error.message); | ||
throw new Error(direct ? response.statusText : response.data.error.message); | ||
} else { | ||
@@ -72,0 +86,0 @@ return response.data; |
@@ -24,3 +24,3 @@ import { ApiClient } from ".."; | ||
user?: string; | ||
} | ||
}; | ||
@@ -27,0 +27,0 @@ type Choice = { |
@@ -16,3 +16,3 @@ import { Usage } from "."; | ||
text: string; | ||
} | ||
}; | ||
@@ -24,3 +24,3 @@ type Edit = { | ||
usage: Usage; | ||
} | ||
}; | ||
@@ -27,0 +27,0 @@ export function createEdit(client: ApiClient) { |
@@ -32,12 +32,6 @@ import { createReadStream } from "fs"; | ||
type UrlImage = { | ||
url: string; | ||
}; | ||
type ImageData = Partial<{ | ||
[key in ImageResponseFormat]: string; | ||
}>; | ||
type B64JsonImage = { | ||
b64_json: string; | ||
}; | ||
type ImageData = UrlImage | B64JsonImage; | ||
type Image = { | ||
@@ -56,15 +50,15 @@ created: number; | ||
return async (request: EditImageRequest, image: string, mask?: string): Promise<Image> => { | ||
const form = new FormData(); | ||
const data = new FormData(); | ||
for (const key in request) { | ||
form.append(key, '' + request[key as keyof EditImageRequest]); | ||
data.append(key, '' + request[key as keyof EditImageRequest]); | ||
} | ||
form.append('image', createReadStream(image)); | ||
data.append('image', createReadStream(image)); | ||
if (mask) { | ||
form.append('mask', createReadStream(mask)); | ||
data.append('mask', createReadStream(mask)); | ||
} | ||
return await client("images/edits", { method: "POST", data: form }); | ||
return await client("images/edits", { method: "POST", data }); | ||
} | ||
@@ -75,12 +69,12 @@ } | ||
return async (request: CreateImageVariationRequest, image: string): Promise<Image> => { | ||
const form = new FormData(); | ||
const data = new FormData(); | ||
for (const key in request) { | ||
form.append(key, '' + request[key as keyof CreateImageVariationRequest]); | ||
data.append(key, '' + request[key as keyof CreateImageVariationRequest]); | ||
} | ||
form.append('image', createReadStream(image)); | ||
data.append('image', createReadStream(image)); | ||
return await client("images/variations", { method: "POST", data: form }); | ||
return await client("images/variations", { method: "POST", data }); | ||
} | ||
} |
@@ -11,2 +11,5 @@ export type Usage = { | ||
export * from "./edits"; | ||
export * from "./images"; | ||
export * from "./images"; | ||
export * from "./embeddings"; | ||
export * from "./audio"; | ||
export * from "./files"; |
@@ -16,3 +16,3 @@ import { ApiClient } from ".."; | ||
is_blocking: boolean; | ||
} | ||
}; | ||
@@ -19,0 +19,0 @@ type Model = { |
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
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
70285
75
1414
210
6