You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
7 months ago
|
'use strict';
|
||
|
|
||
|
const axios = require('axios');
|
||
|
const { logger, getUserLogger } = require('../../utils/logger.util');
|
||
|
|
||
|
const config = {
|
||
|
publicUrl: 'https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion',
|
||
|
apiKey: 'sk-7d059313bd8943318b91cf4c25d91470', // process.env.ALIYUN_API_KEY,
|
||
|
agent: {
|
||
|
id: '3abf8008c77b4ccabb6ed37c6f42a882',
|
||
|
name: 'sales-7',
|
||
|
},
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* ! system prompt 将会覆盖 agent的system prompt
|
||
|
*/
|
||
|
async function callAgentFirst(payload) {
|
||
|
const url = config.publicUrl.replace('{appId}', config.agent.id);
|
||
|
try {
|
||
|
if (!url) {
|
||
|
logger.error('no agent url provided\n', payload);
|
||
|
return;
|
||
|
}
|
||
|
const body = {
|
||
|
input: {
|
||
|
// prompt: payload.content,
|
||
|
messages: [
|
||
|
// { role: 'system', content: `Start contacting the guest named ${payload.customer_name}, here is the guest's reservation request information: ${payload.background}` },
|
||
|
// { role: 'assistant', content: payload.hello },
|
||
|
{ role: 'user', content: payload.content },
|
||
|
],
|
||
|
},
|
||
|
// parameters: {
|
||
|
// messages: '',
|
||
|
// },
|
||
|
};
|
||
|
const { output } = await axios.post(url, body, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${config.apiKey}` } });
|
||
|
return output;
|
||
|
} catch (error) {
|
||
|
logger.error(JSON.stringify({ url, payload, error: error.message }, undefined, 2), 'Error calling agent');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @returns {object} agent response { session_id, text }
|
||
|
*/
|
||
|
async function callAgentRounds(payload) {
|
||
|
const url = config.publicUrl.replace('{appId}', config.agent.id);
|
||
|
try {
|
||
|
if (!url) {
|
||
|
logger.error('no agent url provided\n', payload);
|
||
|
return;
|
||
|
}
|
||
|
// getUserLogger(payload.whatsAppNo).info({ url, payload });
|
||
|
const body = {
|
||
|
input: { prompt: payload.content, session_id: payload.agent_session_id },
|
||
|
// parameters: {
|
||
|
// messages: '',
|
||
|
// },
|
||
|
};
|
||
|
const res = await axios.post(url, body, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${config.apiKey}` } });
|
||
|
return { ...res.data, agent: config.agent };
|
||
|
} catch (error) {
|
||
|
logger.error(JSON.stringify({ url, payload, error: error.message }, undefined, 2), 'Error calling agent');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = { callAgentFirst, callAgentRounds };
|