-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai-city-search.js
60 lines (55 loc) · 1.62 KB
/
openai-city-search.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
import { Configuration, OpenAIApi } from "openai";
const examplesOfPartialMatch = [
"Mumbai in India",
"Mumbwa in Zambia",
"Mumbil in Australia",
"Mumbwa District in Zambia",
"Muminabad in Tajikistan",
];
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
export async function getOpenAiCities({ query, numOfCities = 5 } = {}) {
try {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: `You suggest ${numOfCities} ${
numOfCities > 1 ? "cities" : "city"
} along with ${
numOfCities > 1 ? "their full country names" : "its country name"
}.`,
},
{ role: "user", content: "mum" },
{
role: "assistant",
content: `['Partial match', '${examplesOfPartialMatch
.slice(0, numOfCities)
.join("', '")}'].`,
},
{ role: "user", content: "new delhi" },
{
role: "assistant",
content: "['Exact match','New Delhi in India'].",
},
{ role: "user", content: "fdsmfbjkhfd" },
{
role: "assistant",
content: "['No match'].",
},
{ role: "user", content: query },
],
});
return completion.data.choices[0].message.content;
} catch (error) {
if (error.response) {
console.error(error.response.status, error.response.data);
} else {
console.error(`Error with OpenAI API request: ${error.message}`);
}
return [];
}
}