UNPKG

ads4gpts-langchain

Version:

Ads for AI applications. Enabling the Freemium model for the AI era.

45 lines 1.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ADS4GPTsAPI = void 0; const utils_1 = require("../utils"); class ADS4GPTsAPI { apiKey; baseUrl; constructor(apiKey, baseUrl) { this.apiKey = apiKey || process.env.ADS4GPTS_API_KEY || (() => { throw new Error('ads4gpts_api_key is required. Provide it or set ADS4GPTS_API_KEY in env.'); })(); this.baseUrl = baseUrl || 'https://with.ads4gpts.com'; } async run(method, arg) { const url = `${this.baseUrl}${method}`; const headers = { Authorization: `Bearer ${this.apiKey}`, 'Content-Type': 'application/json', }; try { // arg is expected to conform to the tool’s schema (context, num_ads) const response = await (0, utils_1.fetchWithRetry)(url, { method: 'POST', headers, body: JSON.stringify(arg), }); // The response should be BannerAdsResponse or ChatAdsResponse if (response && response.data) { const ads = response.data.ads; return JSON.stringify(ads); } // If no Ads are found, return a relevant message return 'No Ads found'; } catch (error) { const errorMessage = error instanceof Error ? error.message : JSON.stringify(error); return `Error fetching ads: ${errorMessage}`; } } } exports.ADS4GPTsAPI = ADS4GPTsAPI; //# sourceMappingURL=ADS4GPTsAPI.js.map