UNPKG

@sconedev/ai_toolkit

Version:

Simplify AI integration in web apps with local and offline model support

22 lines (21 loc) 779 B
import { getProvider } from './providers'; import { getConfig } from './config'; import { getCached, setCached } from './cache'; import { retry } from '../utils/retry'; // Import the custom retry function export async function chat(messages, options) { const config = getConfig(); const cacheKey = `chat:${JSON.stringify(messages)}`; const cached = getCached(cacheKey); if (cached) { return cached; } if (config.onRequest) { config.onRequest({ provider: config.provider, function: 'chat', input: messages }); } const provider = getProvider(); const response = await retry(async () => { return provider.chat(messages, options); }, 3); // Retry up to 3 times setCached(cacheKey, response); return response; }