-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
81 lines (66 loc) · 2.23 KB
/
app.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
const dotenv = require('dotenv');
const { AgentExecutor } = require("langchain/agents");
const { StructuredTool } = require("langchain/tools");
const { OpenAIAssistantRunnable } = require("langchain/experimental/openai_assistant");
const { z } = require("zod");
const weather = require('openweather-apis')
const readlineSync = require('readline-sync');
dotenv.config();
async function getTodaysWeather(city) {
return new Promise((resolve, reject) => {
weather.setLang('en');
weather.setCity(city);
weather.setAPPID(process.env.OPENWEATHER_APPID);
weather.getTemperature(function(err, temp) {
if (err) {
reject(err);
} else {
resolve(temp);
}
});
});
}
// getTodaysWeather(city);
class WeatherTool extends StructuredTool {
schema = z.object({
location: z.string().describe("The city, e.g. Mumbai"),
});
name = "get_current_weather";
description = "Get the current weather in a given location";
constructor() {
super(...arguments);
}
async _call(input) {
const { location } = input;
try {
const result = await getTodaysWeather(location);
return JSON.stringify({ location, temperature: result, unit: "celsius" });
} catch (error) {
return `I apologize, but it seems that I am unable to retrieve the current weather for ${location} at the moment.`;
}
}
}
const tools = [new WeatherTool()];
async function main(){
const agent = await OpenAIAssistantRunnable.createAssistant({
clientOptions: { apiKey: process.env.OPENAI_API_KEY },
model: "gpt-3.5-turbo",
instructions:
"You are a weather bot. Use the provided functions to answer questions.",
name: "Weather Assistant",
tools,
asAgent: true,
});
// console.log(agent);
const agentExecutor = AgentExecutor.fromAgentAndTools({
agent,
tools,
});
// console.log(agentExecutor)
const userInput = readlineSync.question("Enter the city: ")
const assistantResponse = await agentExecutor.invoke({
content: userInput,
});
console.log(assistantResponse);
}
main()