Skip to content

Commit 1071b24

Browse files
chat
1 parent 733e144 commit 1071b24

8 files changed

+764
-1
lines changed

README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
1-
# openai-nodejs
1+
# openai-nodejs
2+
3+
# OpenAI Node.js API Usage
4+
This repository provides examples of using the OpenAI API with Node.js. It demonstrates how to use three different API endpoints: createEmbedding, createCompletion, and createChatCompletion. Each endpoint serves a specific purpose and has its own usage pattern.
5+
6+
# Prerequisites
7+
Before running the examples, make sure you have the following:
8+
9+
Node.js installed on your machine.
10+
OpenAI API key. You can obtain one from the OpenAI website.
11+
Installation
12+
Clone this repository: https://github.com/vinodnextcoder/openai-nodejs
13+
14+
# add api key
15+
16+
Open the config.js file.
17+
18+
Replace the YOUR_OPENAI_API_KEY placeholder with your actual OpenAI API key.
19+
20+
Usage
21+
The repository includes examples for three different API endpoints: createEmbedding, createCompletion, and createChatCompletion. Here's how you can use each of them:
22+
curl
23+
curl --location 'http://localhost:3000/createEmbedding' \
24+
--header 'Content-Type: application/json' \
25+
--data '{
26+
"text":"chat"
27+
}'
28+
29+
curl --location 'http://localhost:3000/chatCompletion' \
30+
--header 'Content-Type: application/json' \
31+
--data '{
32+
"text":"i am going to pune university"
33+
}'
34+
35+
curl --location 'http://localhost:3000/createCompletion' \
36+
--header 'Content-Type: application/json' \
37+
--data '{
38+
"text":"Write a tagline for an ice cream shop."
39+
}'

chatCompletion.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const {openai} =require('./config');
2+
3+
const chatCompletion = async(text)=>{
4+
let response;
5+
try {
6+
response = await openai.createChatCompletion({
7+
model: "gpt-3.5-turbo",
8+
messages: [{role: "user", content: text}],
9+
});
10+
return response.data.choices[0].message
11+
} catch (e) {
12+
return e.message
13+
}
14+
}
15+
16+
module.exports = {
17+
chatCompletion
18+
};

config.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { Configuration, OpenAIApi } = require("openai");
2+
3+
const configuration = new Configuration({
4+
apiKey: '',
5+
});
6+
const openai = new OpenAIApi(configuration);
7+
8+
module.exports = {
9+
openai
10+
};

createcompletion.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const {openai} =require('./config');
2+
3+
const createCompletion = async(text)=>{
4+
let response;
5+
try {
6+
response = await openai.createCompletion({
7+
"model": "text-davinci-003",
8+
"prompt": text
9+
});
10+
return response.data.choices[0].text;
11+
} catch (e) {
12+
console.log(e)
13+
return e.message
14+
}
15+
}
16+
17+
module.exports = {
18+
createCompletion
19+
};

embeding.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const {openai} =require('./config');
2+
3+
const createEmbedding = async(text)=>{
4+
let response;
5+
try {
6+
response = await openai.createEmbedding({
7+
model: "text-embedding-ada-002",
8+
input: text,
9+
});
10+
console.log(response.data)
11+
return response.data.data;
12+
} catch (e) {
13+
return e.message
14+
}
15+
}
16+
createEmbedding('joke')
17+
18+
module.exports = {
19+
createEmbedding
20+
};

index.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Import required modules
2+
const express = require('express');
3+
4+
// Create an Express application
5+
const app = express();
6+
const {createCompletion} = require('./createcompletion');
7+
const { chatCompletion } = require('./chatCompletion');
8+
const { createEmbedding } = require('./embeding')
9+
10+
// Parse JSON request bodies
11+
app.use(express.json());
12+
13+
// Define a GET route
14+
app.get('/', (req, res) => {
15+
res.send('Hello, World!');
16+
});
17+
18+
app.post('/createCompletion', async (req, res) => {
19+
const data = await createCompletion(req.body.text);
20+
res.json({ message: data });
21+
});
22+
23+
app.post('/chatCompletion', async (req, res) => {
24+
const data = await chatCompletion(req.body.text);
25+
res.json({ message: data });
26+
});
27+
28+
// createEmbedding
29+
30+
app.post('/createEmbedding', async (req, res) => {
31+
const data = await createEmbedding(req.body.text);
32+
res.json({ message: data });
33+
});
34+
35+
// Start the server
36+
const port = 3000;
37+
app.listen(port, () => {
38+
console.log(`Server is listening on port ${port}`);
39+
});

0 commit comments

Comments
 (0)