-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (63 loc) · 2.12 KB
/
index.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
const express = require("express");
const { createChat } = require("completions");
const cors = require('cors');
require("dotenv").config({ path: ".env" });
const app = express();
const PORT = process.env.PORT;
const OPENAI_KEY = process.env.OPENAI_KEY;
const WEATHER_KEY = process.env.WEATHER_API_KEY;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
const chat = createChat({
apiKey: OPENAI_KEY,
model: "gpt-3.5-turbo-0613",
functions: [
{
name: "get_current_weather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["location"],
},
function: async ({ location }) => {
let res_single = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${WEATHER_KEY}&units=metric&sys=unix`
);
let data = await res_single.json();
return {
location: data.name, //weather api
temperature: data.main.temp, //weather api
information: data,
unit: "celsius",
type: data.weather[0].main,
description: data.weather[0].description,
humidity: data.main.humidity,
explain: "explain all these parameters in a news forcast way",
};
},
},
],
functionCall: "auto",
});
app.post("/chat", async (req, res) => {
const { message } = req.body;
try {
const response = await chat.sendMessage(message);
return res.send({ message: response.content, success: true });
} catch (error) {
const errorMessage =
"It appears that the response to your question is not available at this time. Make an effort to rephrase the question. I'm excited to continue helping you.";
return res.send({ message: errorMessage, success: false });
}
});
app.listen(PORT, () => {
console.log(`server started on port ${PORT}`);
});