feat(ai): ADR-032 Typhoon OCR integration - models, processors, cache, VRAM monitor, sandbox UI
CI / CD Pipeline / build (push) Successful in 4m51s
CI / CD Pipeline / deploy (push) Successful in 12m7s

This commit is contained in:
2026-05-30 22:18:51 +07:00
parent f86fcc05f5
commit ae1b1f35e1
56 changed files with 4057 additions and 153 deletions
@@ -17,6 +17,7 @@ import { EmbeddingService } from '../services/embedding.service';
import { AiRagService } from '../ai-rag.service';
import { Attachment } from '../../../common/file-storage/entities/attachment.entity';
import { OcrService } from '../services/ocr.service';
import { SandboxOcrEngineService } from '../services/sandbox-ocr-engine.service';
import { OllamaService } from '../services/ollama.service';
import { Project } from '../../project/entities/project.entity';
import { AiAuditLog } from '../entities/ai-audit-log.entity';
@@ -29,6 +30,7 @@ describe('AiBatchProcessor', () => {
let embeddingService: jest.Mocked<EmbeddingService>;
let ragService: jest.Mocked<AiRagService>;
let ocrService: jest.Mocked<OcrService>;
let sandboxOcrEngineService: jest.Mocked<SandboxOcrEngineService>;
let ollamaService: jest.Mocked<OllamaService>;
let redis: Record<string, jest.Mock>;
let attachmentRepo: jest.Mocked<Repository<Attachment>>;
@@ -46,6 +48,14 @@ describe('AiBatchProcessor', () => {
.fn()
.mockResolvedValue({ text: 'OCR text LCBP3-CIV-001 Civil' }),
};
const mockSandboxOcrEngineService = {
detectAndExtract: jest.fn().mockResolvedValue({
text: 'OCR text LCBP3-CIV-001 Civil',
ocrUsed: true,
engineUsed: 'typhoon-ocr-3b',
fallbackUsed: false,
}),
};
const mockOllamaService = {
getMainModelName: jest.fn().mockReturnValue('gemma4:e4b'),
generate: jest.fn().mockResolvedValue(
@@ -131,6 +141,10 @@ describe('AiBatchProcessor', () => {
{ provide: EmbeddingService, useValue: mockEmbeddingService },
{ provide: AiRagService, useValue: mockRagService },
{ provide: OcrService, useValue: mockOcrService },
{
provide: SandboxOcrEngineService,
useValue: mockSandboxOcrEngineService,
},
{ provide: OllamaService, useValue: mockOllamaService },
{ provide: DEFAULT_REDIS_TOKEN, useValue: mockRedis },
{
@@ -154,6 +168,7 @@ describe('AiBatchProcessor', () => {
embeddingService = module.get(EmbeddingService);
ragService = module.get(AiRagService);
ocrService = module.get(OcrService);
sandboxOcrEngineService = module.get(SandboxOcrEngineService);
ollamaService = module.get(OllamaService);
redis = module.get(DEFAULT_REDIS_TOKEN);
attachmentRepo = module.get(getRepositoryToken(Attachment));
@@ -218,9 +233,10 @@ describe('AiBatchProcessor', () => {
},
} as unknown as Job<AiBatchJobData>;
await processor.process(job);
expect(ocrService.detectAndExtract).toHaveBeenCalledWith({
pdfPath: '/files/test.pdf',
});
expect(sandboxOcrEngineService.detectAndExtract).toHaveBeenCalledWith(
'/files/test.pdf',
'auto'
);
expect(ollamaService.generate).toHaveBeenCalledTimes(1);
expect(redis.setex).toHaveBeenCalledTimes(2);
expect(redis.setex).toHaveBeenLastCalledWith(
@@ -22,6 +22,10 @@ import { QUEUE_AI_BATCH } from '../../common/constants/queue.constants';
import { EmbeddingService } from '../services/embedding.service';
import { AiRagService } from '../ai-rag.service';
import { OcrService } from '../services/ocr.service';
import {
SandboxOcrEngineService,
SandboxOcrEngineType,
} from '../services/sandbox-ocr-engine.service';
import { OllamaService } from '../services/ollama.service';
import { Project } from '../../project/entities/project.entity';
import { AiAuditLog, AiAuditStatus } from '../entities/ai-audit-log.entity';
@@ -147,6 +151,7 @@ export class AiBatchProcessor extends WorkerHost {
private readonly embeddingService: EmbeddingService,
private readonly ragService: AiRagService,
private readonly ocrService: OcrService,
private readonly sandboxOcrEngineService: SandboxOcrEngineService,
private readonly ollamaService: OllamaService,
private readonly tagsService: TagsService,
private readonly migrationService: MigrationService,
@@ -295,6 +300,7 @@ export class AiBatchProcessor extends WorkerHost {
private async processSandboxExtract(data: AiBatchJobData): Promise<void> {
const { idempotencyKey, payload, projectPublicId } = data;
const pdfPath = payload.pdfPath as string;
const engineType = (payload.engineType as SandboxOcrEngineType) || 'auto';
const overrideProjPublicId =
(payload.projectPublicId as string) || projectPublicId;
if (!pdfPath) {
@@ -309,7 +315,10 @@ export class AiBatchProcessor extends WorkerHost {
})
);
try {
const ocrResult = await this.ocrService.detectAndExtract({ pdfPath });
const ocrResult = await this.sandboxOcrEngineService.detectAndExtract(
pdfPath,
engineType
);
const activePrompt =
await this.aiPromptsService.getActive('ocr_extraction');
@@ -362,6 +371,8 @@ export class AiBatchProcessor extends WorkerHost {
answer: JSON.stringify(extractedMetadata, null, 2),
ocrText: ocrResult.text,
ocrUsed: ocrResult.ocrUsed,
engineUsed: ocrResult.engineUsed,
fallbackUsed: ocrResult.fallbackUsed,
promptVersionUsed: activePrompt.versionNumber,
completedAt: new Date().toISOString(),
})
@@ -387,6 +398,7 @@ export class AiBatchProcessor extends WorkerHost {
private async processSandboxOcrOnly(data: AiBatchJobData): Promise<void> {
const { idempotencyKey, payload } = data;
const pdfPath = payload.pdfPath as string;
const engineType = (payload.engineType as SandboxOcrEngineType) || 'auto';
if (!pdfPath) {
throw new Error('pdfPath is required for sandbox-ocr-only job');
@@ -402,7 +414,10 @@ export class AiBatchProcessor extends WorkerHost {
);
try {
const ocrResult = await this.ocrService.detectAndExtract({ pdfPath });
const ocrResult = await this.sandboxOcrEngineService.detectAndExtract(
pdfPath,
engineType
);
// Cache OCR text สำหรับ Step 2
await this.redis.setex(
@@ -411,6 +426,8 @@ export class AiBatchProcessor extends WorkerHost {
JSON.stringify({
ocrText: ocrResult.text,
ocrUsed: ocrResult.ocrUsed,
engineUsed: ocrResult.engineUsed,
fallbackUsed: ocrResult.fallbackUsed,
timestamp: new Date().toISOString(),
})
);
@@ -423,6 +440,8 @@ export class AiBatchProcessor extends WorkerHost {
status: 'completed',
ocrText: ocrResult.text,
ocrUsed: ocrResult.ocrUsed,
engineUsed: ocrResult.engineUsed,
fallbackUsed: ocrResult.fallbackUsed,
completedAt: new Date().toISOString(),
})
);
@@ -470,6 +489,8 @@ export class AiBatchProcessor extends WorkerHost {
const parsedOcr = JSON.parse(cachedOcr) as {
ocrText: string;
ocrUsed: boolean;
engineUsed?: string;
fallbackUsed?: boolean;
timestamp: string;
};
const { ocrText } = parsedOcr;
@@ -542,6 +563,8 @@ export class AiBatchProcessor extends WorkerHost {
answer: JSON.stringify(extractedMetadata, null, 2),
ocrText,
ocrUsed: parsedOcr.ocrUsed,
engineUsed: parsedOcr.engineUsed,
fallbackUsed: parsedOcr.fallbackUsed,
promptVersionUsed: targetPrompt.versionNumber,
completedAt: new Date().toISOString(),
})
@@ -0,0 +1,202 @@
// File: src/modules/ai/processors/typhoon-llm.processor.ts
// Change Log
// - 2026-05-30: Initial processor สำหรับ Typhoon LLM sequential jobs (T009d, ADR-032)
// รันด้วย concurrency=1 เพื่อป้องกัน VRAM overflow บน RTX 2060 Super (8GB)
// ใช้ keep_alive=0 ผ่าน Ollama API เพื่อ unload model หลังประมวลผล
import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Logger } from '@nestjs/common';
import { Job } from 'bullmq';
import { InjectRedis } from '@nestjs-modules/ioredis';
import Redis from 'ioredis';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import axios from 'axios';
import { AiAuditLog, AiAuditStatus } from '../entities/ai-audit-log.entity';
import { VramMonitorService } from '../services/vram-monitor.service';
/** ชื่อ queue สำหรับ Typhoon LLM jobs */
export const QUEUE_TYPHOON_LLM = 'typhoon-llm';
/** รูปแบบข้อมูล job ใน Typhoon LLM queue */
export interface TyphoonLlmJobData {
/** prompt ที่จะส่งให้ Typhoon LLM */
prompt: string;
/** ชื่อ model เช่น scb10x/typhoon2.1-gemma3-4b */
model?: string;
/** idempotencyKey สำหรับ Redis result key */
idempotencyKey: string;
/** documentPublicId สำหรับ audit log (optional) */
documentPublicId?: string;
/** projectPublicId สำหรับ data isolation */
projectPublicId?: string;
}
/** Ollama generate API response */
interface OllamaGenerateResponse {
response: string;
done: boolean;
}
// VRAM ที่ Typhoon 2.1 Gemma3 4B ต้องการ (MB) — ตาม ADR-032
const TYPHOON_LLM_REQUIRED_VRAM_MB = 4500;
// Timeout 120 วินาทีสำหรับ LLM generation
const TYPHOON_LLM_TIMEOUT_MS = 120000;
/**
* Processor สำหรับ Typhoon LLM jobs ที่รันแบบ sequential (concurrency=1)
* เพื่อป้องกัน VRAM overflow เมื่อรัน LLM หลายงานพร้อมกันบน RTX 2060 Super
* ตาม ADR-032: lockDuration=180000ms รองรับ 120s timeout + buffer
*/
@Processor(QUEUE_TYPHOON_LLM, { concurrency: 1, lockDuration: 180000 })
export class TyphoonLlmProcessor extends WorkerHost {
private readonly logger = new Logger(TyphoonLlmProcessor.name);
private readonly ollamaUrl: string;
private readonly defaultModel: string;
constructor(
private readonly configService: ConfigService,
@InjectRedis() private readonly redis: Redis,
@InjectRepository(AiAuditLog)
private readonly auditLogRepo: Repository<AiAuditLog>,
private readonly vramMonitorService: VramMonitorService
) {
super();
this.ollamaUrl = this.configService.get<string>(
'OLLAMA_URL',
this.configService.get<string>('AI_HOST_URL', 'http://localhost:11434')
);
this.defaultModel = this.configService.get<string>(
'OLLAMA_MODEL_TYPHOON',
'scb10x/typhoon2.1-gemma3-4b'
);
}
/** ประมวลผล Typhoon LLM job ทีละงาน */
async process(job: Job<TyphoonLlmJobData>): Promise<void> {
const { prompt, model, idempotencyKey, documentPublicId } = job.data;
const startTime = Date.now();
const targetModel = model ?? this.defaultModel;
this.logger.log(
`Typhoon LLM job started — idempotencyKey=${idempotencyKey}, model=${targetModel}`
);
// ตรวจสอบ VRAM ก่อนโหลด model
const hasCapacity = await this.vramMonitorService.hasVramCapacity(
TYPHOON_LLM_REQUIRED_VRAM_MB
);
if (!hasCapacity) {
const errMsg = `VRAM ไม่เพียงพอสำหรับ ${targetModel} (ต้องการ ${TYPHOON_LLM_REQUIRED_VRAM_MB}MB) — retry ภายหลัง`;
this.logger.warn(errMsg);
await this.saveResult(idempotencyKey, {
status: 'failed',
errorMessage: errMsg,
processingTimeMs: Date.now() - startTime,
});
await this.writeAuditLog({
documentPublicId,
model: targetModel,
status: AiAuditStatus.FAILED,
errorMessage: errMsg,
processingTimeMs: Date.now() - startTime,
});
throw new Error(errMsg);
}
try {
// เรียก Ollama generate API พร้อม keep_alive=0 เพื่อ unload model หลังประมวลผล
const response = await axios.post<OllamaGenerateResponse>(
`${this.ollamaUrl}/api/generate`,
{
model: targetModel,
prompt,
stream: false,
options: {
temperature: 0.0,
top_p: 0.9,
repeat_penalty: 1.0,
},
keep_alive: 0,
},
{ timeout: TYPHOON_LLM_TIMEOUT_MS }
);
const processingTimeMs = Date.now() - startTime;
const generatedText = response.data.response ?? '';
// Invalidate VRAM cache เพราะ keep_alive=0 unloaded model แล้ว
await this.vramMonitorService.invalidateCache();
await this.saveResult(idempotencyKey, {
status: 'completed',
response: generatedText,
model: targetModel,
processingTimeMs,
});
await this.writeAuditLog({
documentPublicId,
model: targetModel,
status: AiAuditStatus.SUCCESS,
processingTimeMs,
});
this.logger.log(
`Typhoon LLM completed — ${generatedText.length} chars, ${processingTimeMs}ms`
);
} catch (err: unknown) {
const errMsg = err instanceof Error ? err.message : String(err);
this.logger.error(`Typhoon LLM job failed: ${errMsg}`);
await this.saveResult(idempotencyKey, {
status: 'failed',
errorMessage: errMsg,
processingTimeMs: Date.now() - startTime,
});
await this.writeAuditLog({
documentPublicId,
model: targetModel,
status: AiAuditStatus.FAILED,
errorMessage: errMsg,
processingTimeMs: Date.now() - startTime,
});
throw err;
}
}
/** บันทึกผลลัพธ์ LLM ลง Redis สำหรับ polling */
private async saveResult(
idempotencyKey: string,
result: {
status: 'completed' | 'failed';
response?: string;
model?: string;
processingTimeMs: number;
errorMessage?: string;
}
): Promise<void> {
await this.redis.setex(
`ai:typhoon:llm:${idempotencyKey}`,
3600,
JSON.stringify({
idempotencyKey,
...result,
completedAt: new Date().toISOString(),
})
);
}
/** บันทึก audit log สำหรับ Typhoon LLM interaction */
private async writeAuditLog(params: {
documentPublicId?: string;
model: string;
status: AiAuditStatus;
processingTimeMs: number;
errorMessage?: string;
}): Promise<void> {
const log = this.auditLogRepo.create({
documentPublicId: params.documentPublicId,
aiModel: 'typhoon-llm',
modelName: params.model,
modelType: 'llm',
status: params.status,
processingTimeMs: params.processingTimeMs,
cacheHit: false,
errorMessage: params.errorMessage,
});
await this.auditLogRepo.save(log);
}
}
@@ -0,0 +1,196 @@
// File: src/modules/ai/processors/typhoon-ocr.processor.ts
// Change Log
// - 2026-05-30: Initial processor สำหรับ Typhoon OCR sequential jobs (T009c, ADR-032)
// รันด้วย concurrency=1 เพื่อป้องกัน VRAM overflow บน RTX 2060 Super (8GB)
// ใช้ keep_alive=0 ผ่าน sidecar Ollama API เพื่อ unload model หลังประมวลผล
import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Logger } from '@nestjs/common';
import { Job } from 'bullmq';
import { InjectRedis } from '@nestjs-modules/ioredis';
import Redis from 'ioredis';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AiAuditLog, AiAuditStatus } from '../entities/ai-audit-log.entity';
import { OcrCacheService } from '../services/ocr-cache.service';
import { VramMonitorService } from '../services/vram-monitor.service';
import {
SandboxOcrEngineService,
SandboxOcrEngineType,
} from '../services/sandbox-ocr-engine.service';
/** ชื่อ queue สำหรับ Typhoon OCR jobs */
export const QUEUE_TYPHOON_OCR = 'typhoon-ocr';
/** รูปแบบข้อมูล job ใน Typhoon OCR queue */
export interface TyphoonOcrJobData {
/** public path ของไฟล์ PDF ที่ต้องการ OCR */
pdfPath: string;
/** engineType: เสมอเป็น 'typhoon-ocr-3b' สำหรับ queue นี้ */
engineType: SandboxOcrEngineType;
/** idempotencyKey สำหรับ Redis result key */
idempotencyKey: string;
/** documentPublicId สำหรับ audit log (optional) */
documentPublicId?: string;
}
// VRAM ที่ Typhoon OCR-3B ต้องการ (MB) — ตาม ADR-032
const TYPHOON_OCR_REQUIRED_VRAM_MB = 4000;
/**
* Processor สำหรับ Typhoon OCR jobs ที่รันแบบ sequential (concurrency=1)
* เพื่อป้องกัน VRAM overflow เมื่อทำ OCR หลายงานพร้อมกันบน RTX 2060 Super
* ตาม ADR-032: lockDuration=180000ms รองรับ 120s timeout + buffer
*/
@Processor(QUEUE_TYPHOON_OCR, { concurrency: 1, lockDuration: 180000 })
export class TyphoonOcrProcessor extends WorkerHost {
private readonly logger = new Logger(TyphoonOcrProcessor.name);
constructor(
@InjectRedis() private readonly redis: Redis,
@InjectRepository(AiAuditLog)
private readonly auditLogRepo: Repository<AiAuditLog>,
private readonly ocrCacheService: OcrCacheService,
private readonly vramMonitorService: VramMonitorService,
private readonly sandboxOcrEngineService: SandboxOcrEngineService
) {
super();
}
/** ประมวลผล Typhoon OCR job ทีละงาน */
async process(job: Job<TyphoonOcrJobData>): Promise<void> {
const { pdfPath, engineType, idempotencyKey, documentPublicId } = job.data;
const startTime = Date.now();
this.logger.log(
`Typhoon OCR job started — idempotencyKey=${idempotencyKey}, engine=${engineType}`
);
// ตรวจสอบ Redis cache ก่อน — ถ้ามีผลลัพธ์แล้วไม่ต้องรัน OCR ซ้ำ
const cached = await this.ocrCacheService.get(pdfPath, engineType);
if (cached) {
this.logger.log(
`OCR cache hit: ${idempotencyKey} (engine=${engineType})`
);
await this.saveResult(idempotencyKey, {
text: cached.text,
engineUsed: cached.engineUsed,
cacheHit: true,
processingTimeMs: Date.now() - startTime,
});
await this.writeAuditLog({
documentPublicId,
engineType,
status: AiAuditStatus.SUCCESS,
processingTimeMs: Date.now() - startTime,
cacheHit: true,
});
return;
}
// ตรวจสอบ VRAM ก่อนโหลด model
const hasCapacity = await this.vramMonitorService.hasVramCapacity(
TYPHOON_OCR_REQUIRED_VRAM_MB
);
if (!hasCapacity) {
const errMsg = `VRAM ไม่เพียงพอสำหรับ Typhoon OCR-3B (ต้องการ ${TYPHOON_OCR_REQUIRED_VRAM_MB}MB) — retry ภายหลัง`;
this.logger.warn(errMsg);
await this.writeAuditLog({
documentPublicId,
engineType,
status: AiAuditStatus.FAILED,
errorMessage: errMsg,
processingTimeMs: Date.now() - startTime,
cacheHit: false,
});
throw new Error(errMsg);
}
// รัน OCR ผ่าน SandboxOcrEngineService (ซึ่งส่งคำขอไป sidecar → Ollama)
try {
const result = await this.sandboxOcrEngineService.detectAndExtract(
pdfPath,
engineType
);
const processingTimeMs = Date.now() - startTime;
// บันทึกผลลัพธ์ใน Redis cache (24h TTL)
await this.ocrCacheService.set(pdfPath, engineType, {
text: result.text,
engineUsed: result.engineUsed,
charCount: result.text.length,
});
// Invalidate VRAM cache เพราะ keep_alive=0 unloaded model แล้ว
await this.vramMonitorService.invalidateCache();
await this.saveResult(idempotencyKey, {
text: result.text,
engineUsed: result.engineUsed,
fallbackUsed: result.fallbackUsed,
cacheHit: false,
processingTimeMs,
});
await this.writeAuditLog({
documentPublicId,
engineType,
status: AiAuditStatus.SUCCESS,
processingTimeMs,
cacheHit: false,
});
this.logger.log(
`Typhoon OCR completed — ${result.text.length} chars, ${processingTimeMs}ms`
);
} catch (err: unknown) {
const errMsg = err instanceof Error ? err.message : String(err);
this.logger.error(`Typhoon OCR job failed: ${errMsg}`);
await this.writeAuditLog({
documentPublicId,
engineType,
status: AiAuditStatus.FAILED,
errorMessage: errMsg,
processingTimeMs: Date.now() - startTime,
cacheHit: false,
});
throw err;
}
}
/** บันทึกผลลัพธ์ OCR ลง Redis สำหรับ polling */
private async saveResult(
idempotencyKey: string,
result: {
text: string;
engineUsed: string;
fallbackUsed?: boolean;
cacheHit: boolean;
processingTimeMs: number;
}
): Promise<void> {
await this.redis.setex(
`ai:typhoon:ocr:${idempotencyKey}`,
3600,
JSON.stringify({
idempotencyKey,
status: 'completed',
...result,
completedAt: new Date().toISOString(),
})
);
}
/** บันทึก audit log สำหรับ Typhoon OCR interaction */
private async writeAuditLog(params: {
documentPublicId?: string;
engineType: string;
status: AiAuditStatus;
processingTimeMs: number;
cacheHit: boolean;
errorMessage?: string;
}): Promise<void> {
const log = this.auditLogRepo.create({
documentPublicId: params.documentPublicId,
aiModel: 'typhoon-ocr',
modelName: 'scb10x/typhoon-ocr-3b',
modelType: params.engineType,
status: params.status,
processingTimeMs: params.processingTimeMs,
cacheHit: params.cacheHit,
errorMessage: params.errorMessage,
});
await this.auditLogRepo.save(log);
}
}