gpt4 book ai didi

javascript - wit.ai 和 Node.js 入门

转载 作者:行者123 更新时间:2023-12-03 05:14:10 25 4
gpt4 key购买 nike

我正在尝试使用wit.ai quickstart example 。该示例适用于硬编码值,但当我使用第三方天气 API 并尝试向用户提供响应时,它不起作用。

工作代码:

 const actions = {
send(request, response) {
const {sessionId, context, entities} = request;
const {text, quickreplies} = response;
console.log('sending...', JSON.stringify(response));
},
getForecast({context, entities}) {
var location = firstEntityValue(entities, 'location');
if (location) {
context.forecast = 'sunny in ' + location; // we should call a weather API here
delete context.missingLocation;

} else {
context.missingLocation = true;
delete context.forecast;
}
return context;
},
};

现在我编写了函数 getWeather({ context,Entity }, location) 来调用第三方天气 API 并获取用户给定位置的天气信息。

非工作代码:

var getWeather = function ({ context, entities }, location) {
console.log('Entities: ',entities)
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=myAppID';
request.get(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(typeof body)
var weatherObj = JSON.parse(body);
console.log('weather api res: ', weatherObj.weather[0].description);
context.forecast = weatherObj.weather[0].description + ' ' + location; // we should call a weather API here
delete context.missingLocation;
}
})
}

const actions = {
send(request, response) {
const {sessionId, context, entities} = request;
const {text, quickreplies} = response;
console.log('sending...', JSON.stringify(response));
},
getForecast({context, entities}) {
var location = firstEntityValue(entities, 'location');
if (location) {
//Call a function which calls the third party weather API and then handles the response message.
getWeather({ context, entities }, location);
} else {
context.missingLocation = true;
delete context.forecast;
}
return context;
},
};

此外,如果我稍微更改 getWeather() 函数并移动 context.forecast = 'sunny in ' + location;删除 context.missingLocation; 在 request.get() 调用的回调函数之外,它将再次工作,但此时我没有来自第 3 方 api 的天气信息:

工作代码:

var getWeather = function ({ context, entities }, location) {
//Keeping context.forecast outside the request.get() callback function is useless as we yet to get the weather info from the API
context.forecast = 'sunny in ' + location;
delete context.missingLocation;
console.log('Entities: ',entities)
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=myAppID';
request.get(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(typeof body)
var weatherObj = JSON.parse(body);
console.log('weather api res: ', weatherObj.weather[0].description);
}
})
}

那么如何使 context.forecast = apiRes + location; 线路在 http 调用的回调中工作?我在这里做错了什么?

注意:我从 wit.ai 得到的错误响应:

Error: Oops, I don't know what to do.

at F:\..\node-wit\lib\wit.js:87:15
at process._tickCallback (internal/process/next_tick.js:103:7)

我正在使用 npm 包 request在 Node 内进行 http 调用。

最佳答案

通过正确解决 promise 解决了问题。完整代码如下:

'use strict';

let Wit = null;
let interactive = null;

var getWeather = function ( location) {
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=myAppID';
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
})
.then(rsp => {
var res = rsp.json();
return res;
})
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
}

try {
// if running from repo
Wit = require('../').Wit;
interactive = require('../').interactive;
} catch (e) {
Wit = require('node-wit').Wit;
interactive = require('node-wit').interactive;
}

const accessToken = (() => {
if (process.argv.length !== 3) {
console.log('usage: node examples/quickstart.js <wit-access-token>');
process.exit(1);
}
return process.argv[2];
})();

// Quickstart example
// See https://wit.ai/ar7hur/quickstart

const firstEntityValue = (entities, entity) => {
const val = entities && entities[entity] &&
Array.isArray(entities[entity]) &&
entities[entity].length > 0 &&
entities[entity][0].value;
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};

const actions = {
send(request, response) {
const {sessionId, context, entities} = request;
const {text, quickreplies} = response;
return new Promise(function (resolve, reject) {
console.log('sending...', JSON.stringify(response));
return resolve();
});
},
getForecast({context, entities}) {
var location = firstEntityValue(entities, 'location');
if (location) {
return new Promise(function (resolve, reject) {
return getWeather(location).then(weatherJson => {
context.forecast = weatherJson.weather[0].description + ' in the ' + location;
delete context.missingLocation;
return resolve(context);
})
});
} else {
context.missingLocation = true;
delete context.forecast;
return Promise.reject(context);
}
return context;
},
};

const client = new Wit({ accessToken, actions });
interactive(client);

关于javascript - wit.ai 和 Node.js 入门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41674303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com