-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (90 loc) · 2.94 KB
/
index.js
File metadata and controls
105 lines (90 loc) · 2.94 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import express from 'express';
import bodyParser from 'body-parser';
import fetch from 'node-fetch';
import path from 'path';
import { chatData, chatData2 } from './data.js';
const app = express();
const __dirname = path.resolve();
const API_KEY = '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';
app.use(bodyParser.json());
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'chat-gpt.html'));
});
const fetchChatData = async (messages, parameters) => {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages,
...parameters
}),
});
if (!response.ok) {
throw new Error(`API request failed: ${response.statusText}`);
}
return response.json();
};
const fetchImageData = async (chatData) => {
const response = await fetch('https://api.openai.com/v1/images/generations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`
},
body: JSON.stringify({
model: "dall-e-3",
prompt: chatData,
n: 1,
size: "1024x1024",
}),
});
if (!response.ok) {
throw new Error(`API request failed: ${response.statusText}`);
}
return response.json();
};
app.post('/generate/chat/data', async (req, res) => {
const messages = [
{
role: 'system',
content: `The assistant's job is to generate conversation data including personal information provided by the user. It should return data in the JSON format used for conversation datasets.`,
},
{
role: 'user', content: `context = ["철수와 짱구가 퇴근 후 게임을 하는 상황"]`
},
{
role: 'assistant', content: chatData
},
{
role: 'user', content: `context = ["훈이와 유리가 서울로 쇼핑을 간 상황"]`
},
{
role: 'assistant', content: chatData2
},
{
role: 'user', content: `context = ["${req.body.context}"]`
},
];
const parameters = { temperature: 0.7 };
try {
const chatResponse = await fetchChatData(messages, parameters);
const chatData = chatResponse.choices[0].message.content.trim();
const imageResponse = await fetchImageData(chatData);
const imageData = imageResponse.data[0].url;
res.json({
chat_data: chatData,
image: imageData,
});
} catch (e) {
console.error(e);
res.status(500).send(e.message);
}
});
app.listen(5000, () => {
console.log('Server is running on port 5000');
});