690604:1008 ADR-034-134 #04
CI / CD Pipeline / build (push) Successful in 4m58s
CI / CD Pipeline / deploy (push) Successful in 7m33s

This commit is contained in:
2026-06-04 10:08:22 +07:00
parent fb224a116c
commit b79895e6fb
7 changed files with 160 additions and 36 deletions
@@ -10,6 +10,7 @@
// - 2026-06-01: ปรับปรุง remapPath ให้รองรับ Windows absolute และ relative path ได้แม่นยำ 100%
// - 2026-06-01: เปลี่ยน processWithTesseract/processWithTyphoon ให้ส่ง file content ผ่าน multipart ไปยัง /ocr-upload แทนการส่ง path
// - 2026-06-02: ส่งค่า X-API-Key ใน request headers ไปยัง ocr-sidecar เพื่อความมั่นคงปลอดภัยสูงสุด (ADR-033, Suggestion 2)
// - 2026-06-04: ADR-034 — เปลี่ยน TYPHOON_ENGINE.engineName เป็น typhoon-np-dms-ocr:latest ตรงกับชื่อโมเดลใน Ollama
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@@ -77,7 +78,7 @@ const TESSERACT_ENGINE: OcrEngineConfiguration = {
const TYPHOON_ENGINE: OcrEngineConfiguration = {
engineId: TYPHOON_ENGINE_ID,
engineName: 'Typhoon OCR-3B',
engineName: 'typhoon-np-dms-ocr:latest',
engineType: OcrEngineType.TYPHOON_OCR,
isActive: true,
vramRequirementMB: TYPHOON_OCR_REQUIRED_VRAM_MB,
@@ -3,6 +3,8 @@
// - 2026-05-30: แยก SandboxOcrEngineService ออกจาก OcrService เพื่อรองรับการเลือก Typhoon OCR เฉพาะ sandbox โดยไม่กระทบ core OCR flow
// - 2026-06-01: เปลี่ยนจาก remapPath + pdfPath ไปเป็น multipart file upload ไปยัง /ocr-upload (แก้ปัญหา Docker WSL2 mount)
// - 2026-06-02: ส่งค่า X-API-Key ใน request headers ไปยัง ocr-sidecar เพื่อความมั่นคงปลอดภัยสูงสุด (ADR-033, Suggestion 2)
// - 2026-06-04: ADR-034 — เพิ่ม 'typhoon-np-dms-ocr' เป็น canonical SandboxOcrEngineType; legacy aliases ยังรองรับ
// - 2026-06-04: เพิ่ม OcrTyphoonOptions interface; รับ temperature/topP/repeatPenalty จาก frontend sandbox เพื่อ override Modelfile defaults
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@@ -10,11 +12,14 @@ import axios from 'axios';
import * as fs from 'fs';
import { OcrService } from './ocr.service';
export type SandboxOcrEngineType =
| 'auto'
| 'tesseract'
| 'typhoon-ocr-3b'
| 'typhoon-ocr1.5-3b';
export type SandboxOcrEngineType = 'auto' | 'tesseract' | 'typhoon-np-dms-ocr';
/** ค่า parameter สำหรับ Typhoon OCR ที่ override Modelfile defaults ได้จาก sandbox UI */
export interface OcrTyphoonOptions {
temperature?: number;
topP?: number;
repeatPenalty?: number;
}
interface SandboxOcrSidecarResponse {
text?: string;
@@ -52,7 +57,8 @@ export class SandboxOcrEngineService {
/** รัน OCR ตาม engine ที่เลือก โดย fallback กลับไป Tesseract baseline เมื่อ Typhoon ล้มเหลว */
async detectAndExtract(
pdfPath: string,
engineType: SandboxOcrEngineType = 'auto'
engineType: SandboxOcrEngineType = 'auto',
typhoonOptions?: OcrTyphoonOptions
): Promise<SandboxOcrResult> {
if (engineType === 'auto' || engineType === 'tesseract') {
const result = await this.ocrService.detectAndExtract({ pdfPath });
@@ -73,6 +79,15 @@ export class SandboxOcrEngineService {
'upload.pdf'
);
form.append('engine', engineType);
if (typhoonOptions?.temperature !== undefined) {
form.append('temperature', String(typhoonOptions.temperature));
}
if (typhoonOptions?.topP !== undefined) {
form.append('topP', String(typhoonOptions.topP));
}
if (typhoonOptions?.repeatPenalty !== undefined) {
form.append('repeatPenalty', String(typhoonOptions.repeatPenalty));
}
const response = await axios.post<SandboxOcrSidecarResponse>(
`${this.ocrApiUrl}/ocr-upload`,
form,