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.
@@ -0,0 +1,6 @@
1
+ .npmignore
2
+ api.ts
3
+ base.ts
4
+ common.ts
5
+ configuration.ts
6
+ index.ts
@@ -0,0 +1 @@
1
+ 5.3.1
@@ -0,0 +1,5 @@
1
+ .gitignore
2
+ README.md
3
+ git_push.sh
4
+ package.json
5
+ package-lock.json
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
- MIT License
1
+ The MIT License
2
2
 
3
- Copyright (c) 2021 Gabriel Francisco
3
+ Copyright (c) OpenAI (https://openai.com)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
9
  copies of the Software, and to permit persons to whom the Software is
10
10
  furnished to do so, subject to the following conditions:
11
11
 
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
14
 
15
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,204 +1,75 @@
1
- [![Build Status](https://github.com/ceifa/openai/actions/workflows/publish.yml/badge.svg)](https://github.com/ceifa/openai/actions/workflows/publish.yml)
2
- [![npm](https://img.shields.io/npm/v/openai.svg)](https://npmjs.com/package/openai)
3
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
1
+ # OpenAI Node.js Library
4
2
 
5
- # OpenAI
3
+ The OpenAI Node.js library provides convenient access to the OpenAI API from Node.js applications. Most of the code in this library is generated from our [OpenAPI specification](https://github.com/openai/openai-openapi).
6
4
 
7
- A tiny async production-ready wrapper for [OpenAI GPT-3 API](https://beta.openai.com/docs/api-reference/introduction).
8
-
9
- **This is an unofficial library and has no affiliations with OpenAI**
5
+ **Important note: this library is meant for server-side usage only, as using it in client-side browser code will expose your secret API key. [See here](https://beta.openai.com/docs/api-reference/authentication) for more details.**
10
6
 
11
7
  ## Installation
12
8
 
13
- ### Via npm
14
-
15
- ```sh
16
- npm install openai
17
- ```
18
-
19
- ### Via yarn
20
-
21
- ```sh
22
- yarn add openai
9
+ ```bash
10
+ $ npm install openai
23
11
  ```
24
12
 
25
13
  ## Usage
26
14
 
27
- ### Initialize OpenAI
28
-
29
- ```js
30
- import { OpenAI } from 'openai';
31
- // or the commonJS way:
32
- const { OpenAI } = require('openai');
33
-
34
- // new OpenAI(apikey: string, organization?: string, version?: string)
35
- const openai = new OpenAI(process.env.API_KEY, 'my-organization');
36
- ```
37
-
38
- ### Engine
39
-
40
- Get all engines:
41
-
42
- ```js
43
- const engines = await openai.getEngines();
44
- ```
45
-
46
- Get specific engine:
47
-
48
- ```js
49
- const engine = await openai.getEngine('curie');
50
- ```
51
-
52
- ### Completion
53
-
54
- Make a completion:
55
-
56
- ```js
57
- const completion = await openai.complete('curie', {
58
- prompt: 'Q: Hello\nA:',
59
- user: 'user-123'
60
- });
61
- ```
62
-
63
- The options argument(2nd) properties follow the exactly same names as shown on official docs.
64
-
65
- Make a completion from a fine-tuned model:
66
-
67
- ```js
68
- const completion = await openai.completeFromModel('FINE_TUNED_MODEL', {
69
- prompt: 'Q: Hello\nA:'
70
- });
71
- ```
72
-
73
- Make a completion and stream the response:
74
-
75
- ```js
76
- const stream = await openai.completeAndStream('curie', { // or completeFromModelAndStream
77
- prompt: 'Q: Hello\nA:',
78
- user: 'user-123'
79
- });
80
-
81
- stream.pipe(response)
82
- ```
83
-
84
- Make a content filter:
85
-
86
- ```js
87
- const isSafe = (await openai.contentFilter('hi I am cool')) === 0;
88
- ```
89
-
90
- ### Search
91
-
92
- Make a search:
93
-
94
- ```js
95
- const search = await openai.search('curie', {
96
- query: 'the president',
97
- documents: [
98
- 'whitehouse',
99
- 'school',
100
- 'hospital'
101
- ]
102
- });
103
- ```
104
-
105
- The options argument(2nd) properties follow the exactly same names as shown on official docs.
106
-
107
- ### Classification
15
+ The library needs to be configured with your account's secret key, which is available on the [website](https://beta.openai.com/account/api-keys). We recommend setting it as an environment variable. Here's an example of initializing the library with the API key loaded from an environment variable and creating a completion:
108
16
 
109
- Classify a document:
17
+ ```javascript
18
+ const { Configuration, OpenAIApi } = require("openai");
110
19
 
111
- ```js
112
- const classification = await openai.classify({
113
- examples: [
114
- ['A happy moment', 'Positive'],
115
- ['I am sad.', 'Negative'],
116
- ['I am feeling awesome', 'Positive']
117
- ],
118
- labels: ['Positive', 'Negative', 'Neutral'],
119
- query: 'It is a raining day :(',
120
- search_model: 'ada',
121
- model: 'curie'
20
+ const configuration = new Configuration({
21
+ apiKey: process.env.OPENAI_API_KEY,
122
22
  });
123
- ```
124
-
125
- The argument properties follow the exactly same names as shown on official docs.
126
-
127
- ### Answer
128
-
129
- Answer a question:
23
+ const openai = new OpenAIApi(configuration);
130
24
 
131
- ```js
132
- const answer = await openai.answer({
133
- documents: ['Puppy A is happy.', 'Puppy B is sad.'],
134
- question: 'which puppy is happy?',
135
- search_model: 'ada',
136
- model: 'curie',
137
- examples_context: 'In 2017, U.S. life expectancy was 78.6 years.',
138
- examples: [['What is human life expectancy in the United States?','78 years.']],
25
+ const completion = await openai.createCompletion("text-davinci-001", {
26
+ prompt: "Hello world",
139
27
  });
28
+ console.log(completion.data.choices[0].text);
140
29
  ```
141
30
 
142
- The argument properties follow the exactly same names as shown on official docs.
143
-
144
- ### File
145
-
146
- Get all files:
147
-
148
- ```js
149
- const files = await openai.getFiles();
150
- ```
151
-
152
- Upload a single file:
153
-
154
- ```js
155
- const result = await openai.uploadFile('filename.json', await fs.readFileSync('somefile.json'), 'fine-tune');
156
- ```
157
-
158
- Get a single file by id:
159
-
160
- ```js
161
- const file = await openai.getFile('file-29u89djwq');
162
- ```
163
-
164
- Delete a single file by id:
165
-
166
- ```js
167
- await openai.deleteFile('file-29u89djwq');
168
- ```
169
-
170
- ### Fine-tuning
31
+ Check out the [full API documentation](https://beta.openai.com/docs/api-reference?lang=node.js) for examples of all the available functions.
171
32
 
172
- Fine-tune from a file:
173
-
174
- ```js
175
- const result = await openai.finetune({
176
- training_file: 'file-29u89djwq'
177
- });
178
- ```
33
+ ### Request options
179
34
 
180
- The argument properties follow the exactly same names as shown on official docs.
35
+ All of the available API request functions additionally contain an optional final parameter where you can pass custom [axios request options](https://axios-http.com/docs/req_config), for example:
181
36
 
182
- Get all fine-tunes:
183
37
 
184
- ```js
185
- const finetunes = await openai.getFinetunes();
38
+ ```javascript
39
+ const completion = await openai.createCompletion(
40
+ "text-davinci-001",
41
+ {
42
+ prompt: "Hello world",
43
+ },
44
+ {
45
+ timeout: 1000,
46
+ headers: {
47
+ "Example-Header": "example",
48
+ },
49
+ }
50
+ );
186
51
  ```
187
52
 
188
- Get a specific fine-tune:
189
-
190
- ```js
191
- const finetune = await openai.getFinetune('ftjob-AF1WoRqd3aJ');
192
- ```
53
+ ### Error handling
193
54
 
194
- Cancel a fine-tune:
55
+ API requests can potentially return errors due to invalid inputs or other issues. These errors can be handled with a `try...catch` statement, and the error details can be found in either `error.response` or `error.message`:
195
56
 
196
- ```js
197
- await openai.cancelFinetune('ftjob-AF1WoRqd3aJ');
57
+ ```javascript
58
+ try {
59
+ const completion = await openai.createCompletion("text-davinci-001", {
60
+ prompt: "Hello world",
61
+ });
62
+ console.log(completion.data.choices[0].text);
63
+ } catch (error) {
64
+ if (error.response) {
65
+ console.log(error.response.status);
66
+ console.log(error.response.data);
67
+ } else {
68
+ console.log(error.message);
69
+ }
70
+ }
198
71
  ```
199
72
 
200
- Get fine-tune events of a fine-tune:
73
+ ## Thanks
201
74
 
202
- ```js
203
- const events = await openai.getFinetuneEvents('ftjob-AF1WoRqd3aJ');
204
- ```
75
+ Thank you to [ceifa](https://github.com/ceifa) for creating and maintaining the original unofficial `openai` npm package before we released this official library! ceifa's original package has been renamed to [gpt-x](https://www.npmjs.com/package/gpt-x).