-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai_debug.js
161 lines (146 loc) · 6.71 KB
/
openai_debug.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
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
import { API_KEY } from "./API_key.json"
const OPENAI_API_KEY1 = "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";
const OPENAI_API_KEY2 = API_KEY.OPENAI;
let placeInfo = {
name: "The Lancaster Smokehouse",
rating: 4.6,
address: "574 Lancaster St W, Kitchener, ON N2K 1M3",
numReviews: 5050,
type: "Southern restaurant (US)"
};
let reviewInfo = {
text: `UPDATED June 18, 2022:
What has happened to this place ? We have patronized restaurant for years and last night was a poor experience
Server ( bartender ?? ) taking our table was rushed, not checking on us, did not offer food suggestions other than to tell us ( first thing out of her mouth) you know we're out of ribs and low on brisket right ). Not a hello, what can I get for you . Nothing. Poor start.
Brisket sandwich was mediocre. Meat dry. No garnish. Fries BARELY warm. Had to ask and remind staff of extra sides that were ordered but not brought. Had specifically asked for extra pickles. None brought. Asked twice. Ketchup was empty at table. No vinegar for fries and no one around to ask about them. No follow up on meal quality by server during meal. No offer of more drinks during meal. But those questions were asked when bringing the cheque ?? Server training 101 not practiced here. Could go on but point is made. Stay away and save your money.
Has ownership changed here ? Or new managers ? Gone WAY down from previous. Won't be back
Below you can see how much we enjoyed before. Such a shame …`,
rating: 1,
time: "8 months ago"
};
function generatePrompt(reviewInfo) {
// retrieve info
return `${placeInfo.name} is a ${placeInfo.type} located at ${placeInfo.address}.
The overall rating of the restaurant is ${placeInfo.rating} (lowest rating is 1 and highest rating is 5).
There are a total of ${placeInfo.numReviews} reviews for the restaurant.
The following is a review for the restaurant:
"${reviewInfo.text}"
The reviewer gave a rating of ${reviewInfo.rating} (lowest rating is 1 and highest rating is 5).
The review was written ${reviewInfo.time}.
Do you think the review is reliable?
Please return an answer between 0 to 10, where 0 stands for absolutely not reliable and 10 stands for absolutely reliable.
Your answer should include the number first and some explanation.
Your response should in strict JSON format that includes the opening and closing curly brackets.
The first key is called "rate" and should include a single number, which is the number you provided out of 10; the second key is called "explanation", which should include your explanation.
In the explanation you should only provide information about this review.
Please don't say that you can't say for sure or mention anything about considering multiple reviews.
Please be confident.`;
}
async function moderation(reviewText) {
const apiURL = "https://api.openai.com/v1/moderations";
const response = await fetch(apiURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${OPENAI_API_KEY2}`
},
body: JSON.stringify({
input: reviewText
})
});
const data = await response.json();
console.log(data);
if (data.results[0].flagged) {
let flaggedList = [];
const flagCategories = data.results[0].categories;
for (const cat in flagCategories) {
if (flagCategories[cat]) {
flaggedList.push(cat);
}
}
return { flagged: true, categories: flaggedList };
} else {
return { flagged: false };
}
}
async function analyzeReview(reviewInfo) {
const systemContent = `You are a bot that checks for reliability of restaurant reviews.
Check if the review is genuine and warn the users if the review is a spam.`;
const userPrompt = generatePrompt(reviewInfo);
try {
const moderationResult = await moderation(reviewInfo.text);
if (moderationResult.flagged) {
let flagMsg = "The review is flagged for the following inappropriate elements:";
for (const cat in moderationResult.categories) {
flagMsg += " " + String(cat);
}
return { success: false, result: flagMsg };
}
const apiURL = "https://api.openai.com/v1/chat/completions";
const response = await fetch(apiURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${OPENAI_API_KEY2}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{role: "system", content: systemContent},
{role: "user", content: userPrompt}
],
temperature: 0.53
})
});
const data = await response.json();
const gptResponse = data.choices[0].message.content;
let gptResponseJSON = {};
try {
gptResponseJSON = JSON.parse(gptResponse);
} catch {
gptResponse = `{${gptResponse}}`;
gptResponseJSON = JSON.parse(gptResponse);
}
return { success: true, result: gptResponseJSON };
} catch (err) {
return { success: false, result: err };
}
}
const disclaimers = [
"Since we do not have any information about the reviewer, we cannot be completely sure about their authenticity.",
"It is important to note that this is only one review and other reviews should also be considered for a more accurate assessment.",
"It is important to consider other reviews as well before making a final decision.",
"It should be taken with a grain of salt and considered alongside other reviews.",
"It is recommended to read other reviews as well to get a more accurate assessment."
];
async function compareDisclaimer(lastSentence) {
const systemContent = `You are a bot that checks if sentences have the same meaning.`;
let userPrompt = `You are given the following sentence: "${lastSentence}". Is the above sentence the same meaning as any one of the following sentences?\n`;
for (const idx in disclaimers) {
userPrompt += `\t"${disclaimers[idx]}"\n`;
}
console.log(userPrompt);
const apiURL = "https://api.openai.com/v1/chat/completions";
const response = await fetch(apiURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${OPENAI_API_KEY2}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{role: "system", content: systemContent},
{role: "user", content: userPrompt}
],
temperature: 0.1
})
});
const data = await response.json();
console.log(data);
const gptResponse = data.choices[0].message.content;
return gptResponse;
}
console.log(OPENAI_API_KEY2);
const testSentence = "The reviewer gave a rating of 2 stars and provided both positive and negative feedback, which suggests that the review is not a spam.";
compareDisclaimer(testSentence).then(console.log);