발행이 SiteOntology 를 부르는데 서버는 따로 띄워야 했다. 실측(2026-09-14): 서버가 없으면 `[seo] SiteOntology 실패 — 키워드 없이 발행: ConnectError` 로 빌드는 성공하고 메타만 빈다 — 화면으로는 안 보이는 종류다. 한 벌로 묶어 "코드는 올라갔는데 서버가 없는" 상태를 없앤다. - ontology/: gitea.o2o.kr/Web4ai/o2o-site-ontology 를 이 레포로 편입(그 원격은 그대로 남는다) - ontology/Dockerfile(신규): 베이스는 node:22-slim. alpine 은 임베딩 런타임(onnxruntime)이 musl 바이너리를 안 줘서 적재가 ERR_DLOPEN_FAILED 로 죽는다 — 빌드는 성공하고 실행에서만 터진다 - docker-compose.yml: ontology · ontology-postgres(pgvector) · ontology-redis 추가. 자체 DB 를 쓰는 이유는 pgvector 확장 때문이다 — web4ai_db 를 남의 서비스 확장에 묶지 않는다 - 임베딩 모델(120MB)은 이미지에 굽지 않고 볼륨(ontology-model)에 남긴다 - 컨테이너끼리는 `http://ontology:3100` 으로 만난다. `.env` 의 127.0.0.1 은 컨테이너 자기 자신이라 안 닿는다 검증: 3개 기동 · 백엔드 컨테이너에서 ontology:3100/demo HTTP 200 · 마이그레이션·시드 완료 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
import { Injectable, Logger } from '@nestjs/common';
|
|
import { EMBEDDING_DIM, env } from '../config/env';
|
|
import { EmbedKind, EmbeddingProvider } from './types';
|
|
|
|
/** CommonJS 빌드에서 ESM 전용 패키지를 로드하기 위한 우회 (TS 가 require 로 바꾸지 못하게 한다) */
|
|
const esmImport = new Function('s', 'return import(s)') as (s: string) => Promise<any>;
|
|
|
|
/**
|
|
* 로컬 multilingual-e5-small (384차원, onnxruntime CPU).
|
|
* 최초 1회 모델을 내려받아 캐시하며 그 뒤로는 오프라인 동작한다.
|
|
*/
|
|
@Injectable()
|
|
export class LocalEmbeddingProvider extends EmbeddingProvider {
|
|
readonly name = 'local:multilingual-e5-small';
|
|
readonly dimensions = EMBEDDING_DIM;
|
|
private readonly logger = new Logger(LocalEmbeddingProvider.name);
|
|
private extractor: any | null = null;
|
|
private loading: Promise<any> | null = null;
|
|
|
|
private async pipe() {
|
|
if (this.extractor) return this.extractor;
|
|
if (!this.loading) {
|
|
this.loading = (async () => {
|
|
const t0 = Date.now();
|
|
const { pipeline } = await esmImport('@huggingface/transformers');
|
|
const fe = await pipeline('feature-extraction', env.embedding.localModel);
|
|
this.logger.log(`model ready: ${env.embedding.localModel} (${Date.now() - t0}ms)`);
|
|
this.extractor = fe;
|
|
return fe;
|
|
})();
|
|
}
|
|
return this.loading;
|
|
}
|
|
|
|
async embed(texts: string[], kind: EmbedKind = 'passage'): Promise<number[][]> {
|
|
if (texts.length === 0) return [];
|
|
const fe = await this.pipe();
|
|
const prefixed = texts.map((t) => `${kind}: ${t}`);
|
|
const out: number[][] = [];
|
|
const BATCH = 64;
|
|
for (let i = 0; i < prefixed.length; i += BATCH) {
|
|
const slice = prefixed.slice(i, i + BATCH);
|
|
const res = await fe(slice, { pooling: 'mean', normalize: true });
|
|
out.push(...(res.tolist() as number[][]));
|
|
}
|
|
return out;
|
|
}
|
|
}
|