1// llmRedis.js - Redis-backed LLM utilities for llama.cpp
2const Redis = require('ioredis');
3const crypto = require('crypto');
4const redisConfig = require('../config/redis');
5const redis = new Redis(redisConfig);
8 * Generate SHA-256 hash of text.
9 * @param {string} text - Text to hash
10 * @returns {string} Hex-encoded hash
13 return crypto.createHash('sha256').update(text).digest('hex');
17 * Get cached LLM result from Redis.
18 * @param {string} type - LLM operation type (e.g., 'summarize', 'classify')
19 * @param {string} text - Input text
20 * @returns {Promise<string|null>} Cached result or null
22async function getCachedLLMResult(type, text) {
23 const key = `llm:${type}:${hash(text)}`;
24 return await redis.get(key);
28 * Set cached LLM result in Redis.
29 * @param {string} type - LLM operation type
30 * @param {string} text - Input text
31 * @param {string} result - LLM result to cache
32 * @param {number} [ttl] - Time to live in seconds (default: 604800 = 7 days)
33 * @returns {Promise<void>}
35async function setCachedLLMResult(type, text, result, ttl = 604800) {
36 const key = `llm:${type}:${hash(text)}`;
37 await redis.set(key, result, 'EX', ttl);
41 * Check rate limit for tenant.
42 * @param {number} tenantId - Tenant ID
43 * @param {number} [limit] - Maximum requests allowed (default: 20)
44 * @param {number} [window] - Time window in seconds (default: 60)
45 * @returns {Promise<boolean>} True if rate limit exceeded
47async function rateLimit(tenantId, limit = 20, window = 60) {
48 const key = `llm:rate:${tenantId}`;
49 const count = await redis.incr(key);
50 if (count === 1) await redis.expire(key, window);
55 * Acquire lock for document processing.
56 * @param {string} docId - Document ID
57 * @param {number} [ttl] - Lock TTL in seconds (default: 3)
58 * @returns {Promise<boolean>} True if lock acquired
60async function acquireLock(docId, ttl = 3) {
61 const key = `llm:lock:${docId}`;
62 const lock = await redis.set(key, 1, 'NX', 'EX', ttl);