pulse-ai-utils
Version:
Utility functions and helpers for AI-powered applications
67 lines • 2.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const openai_helper_1 = __importDefault(require("../helpers/openai-helper"));
const pulseSchemas_1 = require("../utils/pulseSchemas");
const zod_1 = require("zod");
class LLMContentHandler {
/**
* Creates an instance of LLMContentHandler.
* @param {string} [apiKey] - The OpenAI API key. If not provided, it will be read from the environment variable OPENAI_API_KEY.
* @param {OpenAIHelper} [openaiHelper] - An optional OpenAIHelper instance to use instead of creating a new one. Useful for testing.
*/
constructor(apiKey, openaiHelper) {
this.openaiHelper = openaiHelper || new openai_helper_1.default(apiKey);
}
async getContent(req, res) {
const querySchema = zod_1.z.object({
category: zod_1.z.string(),
area: zod_1.z.string(),
timeline: zod_1.z.string(),
source: zod_1.z.string(),
});
let parsed;
try {
parsed = querySchema.parse(req.query);
}
catch (err) {
return res.status(400).json({ message: 'Missing parameters' });
}
const { category, area, timeline, source } = parsed;
let schemas;
try {
schemas = (0, pulseSchemas_1.getSchemaByCategory)(String(category));
}
catch (e) {
return res.status(400).json({ message: 'Invalid category' });
}
const ResponseSchema = zod_1.z.object({ results: zod_1.z.array(schemas.zod) });
const userLocation = {
type: 'approximate',
country: 'US',
region: 'FL',
city: String(area),
};
const prompt = `Let me know any ${category} happening ${timeline} from ${source}`;
try {
const result = await this.openaiHelper.fetchStructuredDataFromWeb({
prompt,
recommendedSources: [String(source)],
zodSchema: ResponseSchema,
userLocation,
locationGranularity: String(area),
timeline: String(timeline),
responseFormatName: 'data',
});
res.json(result);
}
catch (err) {
console.error('Error fetching structured data:', err);
res.status(500).json({ message: 'Failed to fetch data' });
}
}
}
exports.default = LLMContentHandler;
//# sourceMappingURL=llm-content.js.map