openai 0.0.9 → 1.0.0

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/README.md CHANGED
@@ -71,7 +71,7 @@ Make a completion and stream the response:
71
71
  ```js
72
72
  // Very experimental! Don't use on production!!!
73
73
  // This API may change at any time
74
- const stream = await openai.completionTextStream('curie', {
74
+ const stream = await openai.completeAndStream('curie', { // or completeFromModelAndStream
75
75
  prompt: 'Q: Hello\nA:',
76
76
  user: 'user-123'
77
77
  });
package/dist/index.d.ts CHANGED
@@ -10,7 +10,8 @@ export declare class OpenAI {
10
10
  getEngine(engine: EngineId): Promise<Engine>;
11
11
  complete(engine: EngineId, options: CompletionRequest): Promise<Completion>;
12
12
  completeFromModel(fineTunedModel: string, options: CompletionRequest): Promise<Completion>;
13
- completionTextStream(engine: EngineId, options: CompletionRequest): Promise<Readable>;
13
+ completeAndStream(engine: EngineId, options: CompletionRequest): Promise<Readable>;
14
+ completeFromModelAndStream(fineTunedModel: string, options: CompletionRequest): Promise<Readable>;
14
15
  contentFilter(content: string, user?: string): Promise<ContentLabel>;
15
16
  search(engine: EngineId, options: SearchRequest): Promise<SearchDocument[]>;
16
17
  classify(options: ClassificationRequest): Promise<Classification>;
@@ -26,4 +27,5 @@ export declare class OpenAI {
26
27
  getFinetuneEvents(finetuneId: string): Promise<FineTuneEvent[]>;
27
28
  private requestRaw;
28
29
  private request;
30
+ private eventStreamTransformer;
29
31
  }
package/dist/index.js CHANGED
@@ -42,25 +42,17 @@
42
42
  completeFromModel(fineTunedModel, options) {
43
43
  return this.request(`/completions`, 'POST', { ...options, model: fineTunedModel });
44
44
  }
45
- async completionTextStream(engine, options) {
45
+ async completeAndStream(engine, options) {
46
46
  const request = await this.requestRaw(`/engines/${engine}/completions`, 'POST', { ...options, stream: true });
47
- console.warn("Stream completion is an experimental feature, don't use on production");
48
- const transform = new stream_1.Transform({
49
- transform: (chunk, _, callback) => {
50
- const body = chunk.slice(6).toString().trim();
51
- if (body && body[0] !== '[') {
52
- try {
53
- const completion = JSON.parse(body);
54
- return callback(undefined, completion.choices[0].text);
55
- }
56
- catch (e) {
57
- throw new Error(`Faile to parse: "${chunk.toString()}"`);
58
- }
59
- }
60
- callback();
61
- },
47
+ return request.body.pipe(this.eventStreamTransformer());
48
+ }
49
+ async completeFromModelAndStream(fineTunedModel, options) {
50
+ const request = await this.requestRaw(`/completions`, 'POST', {
51
+ ...options,
52
+ model: fineTunedModel,
53
+ stream: true,
62
54
  });
63
- return request.body.pipe(transform);
55
+ return request.body.pipe(this.eventStreamTransformer());
64
56
  }
65
57
  async contentFilter(content, user) {
66
58
  const completion = await this.complete('content-filter-alpha-c4', {
@@ -159,10 +151,16 @@
159
151
  if (!response.ok) {
160
152
  let errorBody;
161
153
  try {
162
- errorBody = await response.text();
154
+ const { error: { message }, } = await response.json();
155
+ errorBody = message;
163
156
  }
164
157
  catch {
165
- errorBody = 'Failed to get body as text';
158
+ try {
159
+ errorBody = await response.text();
160
+ }
161
+ catch {
162
+ errorBody = 'Failed to get body as text';
163
+ }
166
164
  }
167
165
  throw new Error(`OpenAI did not return ok: ${response.status} ~ Error body: ${errorBody}`);
168
166
  }
@@ -172,6 +170,24 @@
172
170
  const response = await this.requestRaw(path, method, body);
173
171
  return response.json();
174
172
  }
173
+ eventStreamTransformer() {
174
+ const dataHeader = Buffer.from('data: ');
175
+ return new stream_1.Transform({
176
+ transform: function (chunk, _, callback) {
177
+ if (chunk.length >= dataHeader.length &&
178
+ dataHeader.compare(chunk, undefined, dataHeader.length) === 0) {
179
+ if (this.prevChunk) {
180
+ const completion = JSON.parse(this.prevChunk.toString());
181
+ this.push(completion.choices[0].text);
182
+ this.prevChunk = undefined;
183
+ }
184
+ chunk = chunk.slice(dataHeader.length);
185
+ }
186
+ this.prevChunk = this.prevChunk ? Buffer.concat([this.prevChunk, chunk]) : chunk;
187
+ callback();
188
+ },
189
+ });
190
+ }
175
191
  }
176
192
  exports.OpenAI = OpenAI;
177
193
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openai",
3
- "version": "0.0.9",
3
+ "version": "1.0.0",
4
4
  "description": "Tiny OpenAI API wrapper",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {