-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
237 lines (223 loc) · 7.48 KB
/
app.js
File metadata and controls
237 lines (223 loc) · 7.48 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const express = require("express");
//const cors = require("cors");
const { Configuration, OpenAIApi } = require("openai");
const port = 3001;
const app = express();
//app.use(cors());
require("dotenv").config();
const configiration = new Configuration({
organization: "org-0uaQLaArF3m5pgctUePtq5OR",
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configiration);
const data_schema = {
$schema: "http://json-schema.org/draft-07/schema#",
title: "Bucket List Keywords",
description:
"A schema for defining main keywords and their associated details for a bucket list.",
type: "object",
properties: {
BucketList: {
type: "object",
description: "MainKeywords related to the bucket list",
properties: {
MainKeyword1: {
type: "object",
description: "Details related to MainKeyword1 for the bucket list",
properties: {
Value: {
type: "string",
description: "Main keyword1 to achieve bucket list",
},
Details: {
type: "object",
properties: {
Detail1: {
type: "string",
description: "The first detail for MainKeyword1.",
},
Detail2: {
type: "string",
description: "The second detail for MainKeyword1.",
},
Detail3: {
type: "string",
description: "The third detail for MainKeyword1.",
},
Detail4: {
type: "string",
description: "The fourth detail for MainKeyword1.",
},
},
required: ["Detail1", "Detail2", "Detail3", "Detail4"],
},
},
required: ["Value", "Details"],
},
MainKeyword2: {
type: "object",
description: "Details related to MainKeyword2 for the bucket list",
properties: {
Value: {
type: "string",
description: "Main keyword2 to achieve bucket list",
},
Details: {
type: "object",
properties: {
Detail1: {
type: "string",
description: "The first detail for MainKeyword2.",
},
Detail2: {
type: "string",
description: "The second detail for MainKeyword2.",
},
Detail3: {
type: "string",
description: "The third detail for MainKeyword2.",
},
Detail4: {
type: "string",
description: "The fourth detail for MainKeyword2.",
},
},
required: ["Detail1", "Detail2", "Detail3", "Detail4"],
},
},
required: ["Value", "Details"],
},
MainKeyword3: {
type: "object",
description: "Details related to MainKeyword3 for the bucket list",
properties: {
Value: {
type: "string",
description: "Main keyword3 to achieve bucket list",
},
Details: {
type: "object",
properties: {
Detail1: {
type: "string",
description: "The first detail for MainKeyword3.",
},
Detail2: {
type: "string",
description: "The second detail for MainKeyword3.",
},
Detail3: {
type: "string",
description: "The third detail for MainKeyword3.",
},
Detail4: {
type: "string",
description: "The fourth detail for MainKeyword3.",
},
},
required: ["Detail1", "Detail2", "Detail3", "Detail4"],
},
},
required: ["Value", "Details"],
},
MainKeyword4: {
type: "object",
description: "Details related to MainKeyword4 for the bucket list",
properties: {
Value: {
type: "string",
description: "Main keyword4 to achieve bucket list",
},
Details: {
type: "object",
properties: {
Detail1: {
type: "string",
description: "The first detail for MainKeyword4.",
},
Detail2: {
type: "string",
description: "The second detail for MainKeyword4.",
},
Detail3: {
type: "string",
description: "The third detail for MainKeyword4.",
},
Detail4: {
type: "string",
description: "The fourth detail for MainKeyword4.",
},
},
required: ["Detail1", "Detail2", "Detail3", "Detail4"],
},
},
required: ["Value", "Details"],
},
},
},
},
};
const runGPT35 = async (prompt) => {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content:
"Provide realistic solutions so you can achieve people's dreams one step closer",
},
{
role: "user",
content: prompt,
},
],
functions: [
{
name: "data_schema",
description:
"Using the given age, gender, occupation, and bucket list information, create four main keywords for achieving a bucket list that fits the situation, and four detailed keywords according to each main keyword",
parameters: data_schema,
},
],
function_call: { name: "data_schema" },
temperature: 0.7,
top_p: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
max_tokens: 1000,
});
var output_schema = response.data.choices[0].message.function_call.arguments;
return output_schema;
// console.log(response.data.choices[0].message.content);
//console.log(output_schema);
};
app.get("/ask/report", async (req, res) => {
var gender = req.gender;
var age = req.age;
var job = req.job;
var bucket = req.bucket;
var propmt_sentence = `직업: '${job}', 나이: '${age}',
성별:'${gender}' 버킷리스트: '${bucket}'버킷리스트를 이루기 위해
필요한 메인 키워드 4개와 각각의 메인 키워드를 이루기 위한
9글자 이하의 세부 목표를 4개씩 한글로 json형태로 생성해줘`;
// const allowedOrigins = ["http://localhost:3000, https://ggumtle.vercel.app"];
// const origin = req.headers.origin;
const response = await runGPT35(propmt_sentence);
if (response) {
const user = JSON.parse(response); // json.parse로 파싱
res.setHeader("Access-Control-Allow-origin", req.headers.origin);
res.setHeader("Content-Type", 'application/json; charset="utf-8"');
res.setHeader("Access-Control-Allow-Credentials", true);
res.json(user);
console.log(user.BucketList.MainKeyword1.Value);
console.log(user.BucketList.MainKeyword1.Details.Detail1);
} else {
res.status(500).json({ error: "fail......" });
}
});
app.get("/test", async (req, res) => {
res.send("hello world");
});
app.listen(port, () => {
console.log("Server on: " + port);
});