UNPKG

@twilio-alpha/assistants-eval

Version:

promptfoo extension for writing AI evaluations for Twilio AI Assistants

72 lines 2.43 kB
import fetchRetry from 'fetch-retry'; import pino from 'pino'; const fetcher = fetchRetry(global.fetch); export default class ExternalProvider { logger; defaultUrl; identifier; requestOptions; retryOptions; constructor({ id, label, config }) { this.defaultUrl = config.url; this.identifier = id ?? label ?? 'test-ai-evaluator'; const requestOptions = config.requestOptions ?? { method: 'POST', headers: { 'Content-Type': 'application/json', }, }; this.requestOptions = { method: 'POST', ...requestOptions, headers: { 'Content-Type': 'application/json', ...requestOptions.headers, }, }; this.retryOptions = config.retryOptions ?? { retries: 5, retryDelay(attempt) { return 2 ** attempt * 1000; }, retryOn: [500, 501, 502, 503, 504], }; this.logger = pino({ level: process.env.LOG_LEVEL || 'info', }).child({ module: this.constructor.name, }); } id() { return this.identifier; } getUrl(_prompt, _context, _callApiOptions) { return this.defaultUrl; } getBody(prompt, _context, _callApiOptions) { return JSON.stringify({ body: prompt, }); } async getResponse(response) { const data = await response.json(); return { output: data }; } async callApi(prompt, context, callApiOptions) { const request = { ...this.requestOptions, ...this.retryOptions, body: this.getBody(prompt, context, callApiOptions), }; this.logger.debug('Calling endpoint %s with request payload %s', this.getUrl(prompt, context, callApiOptions), JSON.stringify(request)); const fetchResponse = await fetcher(this.getUrl(prompt, context, callApiOptions), request); if (fetchResponse.status > 300) { this.logger.error('Received error response %s', fetchResponse.statusText); throw new Error(fetchResponse.statusText); } const response = await this.getResponse(fetchResponse); this.logger.debug('Received response %s', JSON.stringify(response)); return response; } } //# sourceMappingURL=external.js.map