generated from joyqi/typescript-module-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,90 @@ | ||
# node-openai | ||
|
||
Yet another Node.js wrapper for OpenAI's API. | ||
An elegant Node.js library written in TypeScript for the OpenAI API. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install node-openai | ||
``` | ||
|
||
## Usage | ||
## Example | ||
|
||
```js | ||
const OpenAI = require('node-openai'); | ||
For Node.js (CommonJS): | ||
|
||
```javascript | ||
const { OpenAI } = require('node-openai'); | ||
``` | ||
|
||
For ES Modules: | ||
|
||
```javascript | ||
import { OpenAI } from 'node-openai'; | ||
``` | ||
|
||
For TypeScript: | ||
|
||
```typescript | ||
import { OpenAI } from 'node-openai'; | ||
``` | ||
|
||
Create an instance of the OpenAI class: | ||
|
||
```javascript | ||
const client = new OpenAI({ | ||
apiKey: 'YOUR_API_KEY', | ||
// organization: 'YOUR_ORGANIZATION_ID', | ||
// options: { /* Axios Request Options */ }, | ||
}); | ||
``` | ||
|
||
## V1 API | ||
|
||
To use the OpenAI V1 API, you must call the `v1()` method on the client instance: | ||
|
||
```javascript | ||
const v1 = client.v1(); | ||
``` | ||
|
||
### Models | ||
|
||
List all available models: | ||
|
||
```javascript | ||
const models = await v1.models.list(); | ||
``` | ||
|
||
Retrieve a model: | ||
|
||
```javascript | ||
const model = await v1.models.retrieve('davinci'); | ||
``` | ||
|
||
### Completions | ||
|
||
Create a completion: | ||
|
||
```javascript | ||
const completion = await v1.completions.create({ | ||
model: 'davinci', | ||
prompt: 'This is a test', | ||
max_tokens: 5, | ||
temperature: 0.9 | ||
}); | ||
``` | ||
|
||
### Chat | ||
|
||
(async () => { | ||
const response = await client.v1().completions.create({ | ||
engine: 'davinci', | ||
prompt: 'This is a test', | ||
maxTokens: 5, | ||
temperature: 0.9, | ||
topP: 1, | ||
presencePenalty: 0, | ||
frequencyPenalty: 0, | ||
bestOf: 1, | ||
n: 1, | ||
stream: false, | ||
stop: ['\n', ' '], | ||
}); | ||
})(); | ||
``` | ||
Create a chat: | ||
|
||
```javascript | ||
const chat = await v1.chat.create({ | ||
model: 'gpt-3.5-turbo', | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: 'Hello, how are you?' | ||
} | ||
] | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters