UNPKG

together-ai-sdk

Version:

A typescript SDK for the Together AI API

62 lines (61 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rawTogetherRequest = void 0; const asyncTimeout_1 = require("../utils/asyncTimeout"); /** * A low level client to send requests to the Together API. * Recommended to use the `togetherClient` instead for users. * Can be helpful for new features or debugging. * @param userConfig - the configuration object */ const rawTogetherRequest = async (userConfig) => { const config = formConfig(userConfig); const url = `${config.protocol}://${config.address}/${config.endpoint}`; const options = { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${config.apiKey}` }, body: JSON.stringify(config.requestParams) }; let response = await config.customFetch(url, options); let numTries = 0; while (shouldRetry(response, config.retryCooldowns, numTries)) { const cooldown = config.retryCooldowns[numTries]; await (0, asyncTimeout_1.asyncTimeout)(cooldown); response = await config.customFetch(url, options); numTries += 1; } return response; }; exports.rawTogetherRequest = rawTogetherRequest; /** * Should the request be retried? * @param response - the response from the request * @param retryCooldowns - the cooldowns to use when retrying * @param numTries - the number of retries that have been attempted * @returns true if the request should be retried */ const shouldRetry = (response, retryCooldowns, numTries) => { const isRetryable = response.status === 429 || (response.status >= 500 && response.status < 600); if (!isRetryable) return false; return numTries < retryCooldowns.length; }; /** * Form the user's configuration into a complete configuration object. * @param userConfig - the user's configuration * @returns the complete configuration object with no undefined values */ const formConfig = (userConfig) => { return { apiKey: userConfig.apiKey, address: userConfig.address ?? 'api.together.xyz', protocol: userConfig.protocol ?? 'https', endpoint: userConfig.endpoint, requestParams: userConfig.requestParams ?? {}, retryCooldowns: userConfig.retryCooldowns ?? [1000, 5000, 30000], customFetch: userConfig.customFetch ?? fetch }; };