openai 1.1.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/lib.js DELETED
@@ -1,179 +0,0 @@
1
- import { ContentLabel, } from './types.js';
2
- import { Transform } from 'stream';
3
- import FormData from 'form-data';
4
- import fetch from 'node-fetch';
5
- const baseUrl = 'https://api.openai.com';
6
- const defaultVersion = 'v1';
7
- export class OpenAI {
8
- constructor(apiKey, organizationId, version = defaultVersion) {
9
- this.headers = {
10
- 'authorization': `Bearer ${apiKey}`,
11
- 'content-type': 'application/json',
12
- };
13
- if (organizationId) {
14
- this.headers['openai-organization'] = organizationId;
15
- }
16
- this.url = `${baseUrl}/${version}`;
17
- }
18
- getEngines() {
19
- return this.request('/engines', 'GET').then((r) => r.data);
20
- }
21
- getEngine(engine) {
22
- return this.request(`/engines/${engine}`, 'GET');
23
- }
24
- complete(engine, options) {
25
- return this.request(`/engines/${engine}/completions`, 'POST', options);
26
- }
27
- completeFromModel(fineTunedModel, options) {
28
- return this.request(`/completions`, 'POST', { ...options, model: fineTunedModel });
29
- }
30
- async completeAndStream(engine, options) {
31
- const request = await this.requestRaw(`/engines/${engine}/completions`, 'POST', { ...options, stream: true });
32
- return request.body.pipe(this.eventStreamTransformer());
33
- }
34
- async completeFromModelAndStream(fineTunedModel, options) {
35
- const request = await this.requestRaw(`/completions`, 'POST', {
36
- ...options,
37
- model: fineTunedModel,
38
- stream: true,
39
- });
40
- return request.body.pipe(this.eventStreamTransformer());
41
- }
42
- async contentFilter(content, user) {
43
- const completion = await this.complete('content-filter-alpha-c4', {
44
- prompt: `<|endoftext|>${content}\n--\nLabel:`,
45
- temperature: 0,
46
- max_tokens: 1,
47
- top_p: 1,
48
- frequency_penalty: 0,
49
- presence_penalty: 0,
50
- logprobs: 10,
51
- user,
52
- });
53
- let label = Number(completion.choices[0].text);
54
- if (label === ContentLabel.Unsafe) {
55
- const logprobs = completion.choices[0].logprobs?.top_logprobs[0];
56
- if (logprobs && logprobs['2'] < -0.355) {
57
- if (logprobs['0'] && logprobs['1']) {
58
- label = logprobs['0'] >= logprobs['1'] ? ContentLabel.Safe : ContentLabel.Sensitive;
59
- }
60
- else if (logprobs['0']) {
61
- label = ContentLabel.Safe;
62
- }
63
- else if (logprobs['1']) {
64
- label = ContentLabel.Sensitive;
65
- }
66
- }
67
- }
68
- if (![0, 1, 2].includes(label)) {
69
- label = ContentLabel.Unsafe;
70
- }
71
- return label;
72
- }
73
- search(engine, options) {
74
- return this.request(`/engines/${engine}/search`, 'POST', options).then((r) => r.data);
75
- }
76
- classify(options) {
77
- return this.request('/classifications', 'POST', options);
78
- }
79
- answer(options) {
80
- return this.request('/answers', 'POST', options);
81
- }
82
- getFiles() {
83
- return this.request('/files', 'GET').then((r) => r.data);
84
- }
85
- uploadFile(file, jsonlines, purpose) {
86
- const data = new FormData();
87
- let fileJsonlines;
88
- if (Array.isArray(jsonlines)) {
89
- if (typeof jsonlines[0] === 'object') {
90
- jsonlines = jsonlines.map((j) => JSON.stringify(j));
91
- }
92
- fileJsonlines = jsonlines.join('\n');
93
- }
94
- else {
95
- fileJsonlines = jsonlines;
96
- }
97
- data.append('file', fileJsonlines, file);
98
- data.append('purpose', purpose);
99
- return this.request('/files', 'POST', data);
100
- }
101
- getFile(fileId) {
102
- return this.request(`/files/${fileId}`, 'GET');
103
- }
104
- deleteFile(fileId) {
105
- return this.request(`/files/${fileId}`, 'DELETE');
106
- }
107
- finetune(options) {
108
- return this.request(`/fine-tunes`, 'POST', options);
109
- }
110
- getFinetunes() {
111
- return this.request('/fine-tunes', 'GET').then((r) => r.data);
112
- }
113
- getFinetune(finetuneId) {
114
- return this.request(`/fine-tunes/${finetuneId}`, 'GET');
115
- }
116
- cancelFinetune(finetuneId) {
117
- return this.request(`/fine-tunes/${finetuneId}/cancel`, 'POST');
118
- }
119
- getFinetuneEvents(finetuneId) {
120
- return this.request(`/fine-tunes/${finetuneId}/events`, 'GET').then((r) => r.data);
121
- }
122
- createEmbedding(engine, options) {
123
- return this.request(`/engines/${engine}/embeddings`, 'POST', options);
124
- }
125
- async requestRaw(path, method, body) {
126
- let headers = { ...this.headers };
127
- if (body instanceof FormData) {
128
- delete headers['content-type'];
129
- headers = body.getHeaders(headers);
130
- }
131
- else if (!['string', 'undefined'].includes(typeof body)) {
132
- body = JSON.stringify(body);
133
- }
134
- const response = await fetch(this.url + path, {
135
- headers,
136
- method,
137
- body: body,
138
- });
139
- if (!response.ok) {
140
- let errorBody;
141
- try {
142
- const { error: { message }, } = (await response.json());
143
- errorBody = message;
144
- }
145
- catch {
146
- try {
147
- errorBody = await response.text();
148
- }
149
- catch {
150
- errorBody = 'Failed to get body as text';
151
- }
152
- }
153
- throw new Error(`OpenAI did not return ok: ${response.status} ~ Error body: ${errorBody}`);
154
- }
155
- return response;
156
- }
157
- async request(path, method, body) {
158
- const response = await this.requestRaw(path, method, body);
159
- return response.json();
160
- }
161
- eventStreamTransformer() {
162
- const dataHeader = Buffer.from('data: ');
163
- return new Transform({
164
- transform: function (chunk, _, callback) {
165
- if (chunk.length >= dataHeader.length &&
166
- dataHeader.compare(chunk, undefined, dataHeader.length) === 0) {
167
- if (this.prevChunk) {
168
- const completion = JSON.parse(this.prevChunk.toString());
169
- this.push(completion.choices[0].text);
170
- this.prevChunk = undefined;
171
- }
172
- chunk = chunk.slice(dataHeader.length);
173
- }
174
- this.prevChunk = this.prevChunk ? Buffer.concat([this.prevChunk, chunk]) : chunk;
175
- callback();
176
- },
177
- });
178
- }
179
- }
package/dist/types.d.ts DELETED
@@ -1,186 +0,0 @@
1
- declare type LiteralUnion<T extends U, U = string> = T | (U & {});
2
- export declare type EngineId = LiteralUnion<'davinci' | 'curie' | 'babbage' | 'ada'>;
3
- export interface Engine {
4
- id: EngineId;
5
- object: 'engine';
6
- created?: Date;
7
- max_replicas?: number;
8
- owner: string;
9
- permissions: unknown;
10
- ready: boolean;
11
- ready_replicas: unknown;
12
- replicas: unknown;
13
- }
14
- export interface List<T> {
15
- object: 'list';
16
- data: T[];
17
- model?: string;
18
- }
19
- export interface CompletionRequest {
20
- prompt?: string | string[];
21
- max_tokens?: number;
22
- temperature?: number;
23
- top_p?: number;
24
- n?: number;
25
- logprobs?: number;
26
- echo?: boolean;
27
- stop?: string | string[];
28
- presence_penalty?: number;
29
- frequency_penalty?: number;
30
- best_of?: number;
31
- logit_bias?: Record<string, number>;
32
- user?: string;
33
- }
34
- export interface LogProbs {
35
- tokens: string[];
36
- token_logprobs: number[];
37
- top_logprobs: Array<Record<string, number>>;
38
- text_offset: number[];
39
- }
40
- export interface Choice {
41
- text: string;
42
- index: number;
43
- logprobs: LogProbs | null;
44
- finish_reason: string | null;
45
- }
46
- export interface Completion {
47
- id: string;
48
- object: 'text_completion';
49
- created: number;
50
- model: string;
51
- choices: Choice[];
52
- }
53
- export declare enum ContentLabel {
54
- Safe = 0,
55
- Sensitive = 1,
56
- Unsafe = 2
57
- }
58
- export interface SearchRequest {
59
- query: string;
60
- documents?: string[];
61
- file?: string;
62
- max_rerank?: number;
63
- return_metadata?: boolean;
64
- }
65
- export interface SearchDocument {
66
- document: number;
67
- object: 'search_result';
68
- score: number;
69
- }
70
- export interface ClassificationRequest {
71
- model: string;
72
- query: string;
73
- examples?: string[];
74
- file?: string;
75
- labels?: string[] | null;
76
- search_model?: string;
77
- temperature?: number;
78
- logprobs?: number;
79
- max_examples?: number;
80
- logit_bias?: Record<string, number>;
81
- return_prompt?: boolean;
82
- return_metadata?: boolean;
83
- expand?: string[];
84
- }
85
- export interface ClassificationExample {
86
- document: number;
87
- label: string;
88
- text: string;
89
- }
90
- export interface Classification {
91
- completion: string;
92
- label: string;
93
- model: string;
94
- object: 'classification';
95
- search_model: string;
96
- selected_examples: ClassificationExample[];
97
- }
98
- export interface AnswerRequest {
99
- model: string;
100
- question: string;
101
- examples: Array<[string, string]>;
102
- examples_context: string;
103
- documents?: string[];
104
- file?: string;
105
- search_model?: string;
106
- max_rerank?: number;
107
- temperature?: number;
108
- logprobs?: number;
109
- max_tokens?: number;
110
- stop?: string | string[];
111
- n?: number;
112
- logit_bias?: Record<string, number>;
113
- return_metadata?: boolean;
114
- return_prompt?: boolean;
115
- expand?: string[];
116
- }
117
- export interface AnswerDocument {
118
- document: number;
119
- text: string;
120
- }
121
- export interface Answer {
122
- answers: string[];
123
- completion: string | Completion;
124
- model: string;
125
- object: 'answer';
126
- search_model: string;
127
- prompt: string;
128
- selected_documents: AnswerDocument[];
129
- }
130
- export declare type FilePurpose = 'search' | 'answers' | 'classifications' | 'fine-tune';
131
- export declare type JsonLines = string | string[] | unknown[];
132
- export interface File {
133
- id: string;
134
- object: string;
135
- bytes: number;
136
- created_at: number;
137
- filename: string;
138
- purpose: FilePurpose;
139
- }
140
- export interface Hyperparams {
141
- n_epochs?: number;
142
- batch_size?: number;
143
- learning_rate_multiplier?: number;
144
- use_packing?: boolean;
145
- prompt_loss_weight?: number;
146
- }
147
- export interface FineTuneRequest extends Hyperparams {
148
- training_file: string;
149
- validation_file?: string;
150
- model?: string;
151
- compute_classification_metrics?: boolean;
152
- classification_n_classes?: number;
153
- classification_positive_class?: string;
154
- classification_betas?: number[];
155
- }
156
- export interface FineTuneEvent {
157
- object: 'fine-tune-event';
158
- created_at: number;
159
- level: string;
160
- message: string;
161
- }
162
- export interface FineTune {
163
- id: string;
164
- object: 'fine-tune';
165
- model: string;
166
- created_at: number;
167
- events: FineTuneEvent[];
168
- fine_tuned_model: string;
169
- hyperparams: Hyperparams;
170
- organization_id: string;
171
- result_files: File[];
172
- status: string;
173
- validation_files: File[];
174
- training_files: File[];
175
- updated_at: number;
176
- user_id: string;
177
- }
178
- export interface EmbeddingRequest {
179
- input: string | string[];
180
- }
181
- export interface Embedding {
182
- object: 'embedding';
183
- embedding: number[];
184
- index: number;
185
- }
186
- export {};
package/dist/types.js DELETED
@@ -1,6 +0,0 @@
1
- export var ContentLabel;
2
- (function (ContentLabel) {
3
- ContentLabel[ContentLabel["Safe"] = 0] = "Safe";
4
- ContentLabel[ContentLabel["Sensitive"] = 1] = "Sensitive";
5
- ContentLabel[ContentLabel["Unsafe"] = 2] = "Unsafe";
6
- })(ContentLabel || (ContentLabel = {}));