box-ui-elements-test
Version:
Box UI Elements
48 lines (40 loc) • 1.09 kB
JavaScript
/**
* @flow
* @file Helper for the Box AI API endpoint
* @author Box
*/
import Base from './Base';
import type { BoxItem } from '../common/types/core';
class Intelligence extends Base {
/**
* API endpoint to ask ai a question
*
* @param {string} prompt - Question
* @param {Array<object>} items - Array of items to ask about
* @return {Promise}
*/
async ask(prompt: string, items: Array<BoxItem>) {
if (!prompt) {
throw new Error('Missing prompt!');
}
if (!items || items.length === 0) {
throw new Error('Missing items!');
}
items.forEach(item => {
if (!item.id || !item.type) {
throw new Error('Invalid item!');
}
});
const url = `${this.getBaseApiUrl()}/ai/ask`;
return this.xhr.post({
url,
id: `file_${items[0].id}`,
data: {
mode: 'single_item_qa',
prompt,
items,
},
});
}
}
export default Intelligence;