UNPKG

@summarisation/openai

Version:

Client for openai

52 lines (51 loc) 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultOpenAiConfig = exports.openAiClient = void 0; const openAiClient = (config) => { let { axios, model, customisation, debug } = config; if (!model) model = "davinci"; if (!customisation) customisation = {}; return async (messages) => { const data = { model, messages, ...customisation }; try { if (debug) console.log('openAiMessagesClient', data); const response = await axios.post(`/v1/chat/completions`, data); return response.data.choices.map((x) => x.message); } catch (e) { if (e.code && e.code.includes('ERR_BAD_REQUEST')) { console.error('badrequest', data); throw new Error('Bad Request'); } throw e; } }; }; exports.openAiClient = openAiClient; const defaultOpenAiConfig = (baseURL, token, model, axios, addInterceptors) => { if (!baseURL) throw new Error('baseURL is required for open ai. Have you set up the .env file?'); if (!model) model = "davinci"; const Authorization = `Bearer ${token}`; const axiosInstance = axios.create({ baseURL, headers: { Authorization, 'Content-Type': 'application/json', }, }); addInterceptors(axiosInstance); return { axios: axiosInstance, model }; }; exports.defaultOpenAiConfig = defaultOpenAiConfig;