-
Notifications
You must be signed in to change notification settings - Fork 3
/
example-node.js
44 lines (39 loc) · 1.2 KB
/
example-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Baseline example of OpenAI Node.js/TypeScript library use.
* @link https://github.com/openai/openai-node
*
* OpenAI Python library documentation: https://github.com/openai/openai-node/blob/master/api.md
* - OpenAI API documentation: https://platform.openai.com/docs
* - OpenAI API reference for chat creation: https://platform.openai.com/docs/api-reference/chat/create
*
*/
import OpenAI from 'openai';
import { config } from 'dotenv';
config();
// Create an instance of the OpenAI class with your secret API key
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// Call the OpenAI API with the parameters you want to use
async function main() {
const chatCompletion = await openai.chat.completions.create({
model: "gpt-4",
temperature: 0.9,
max_tokens: 150,
messages: [
{
"role": "system",
"content": "You are an eloquent linguist. All your answers paint pictures with words.",
},
{
"role": "user",
"content": "Why is the sky blue?",
}
],
});
// Print the result
console.log(chatCompletion)
console.log("===")
console.log("The chat response: ", chatCompletion.choices[0].message.content)
}
main();