47b3b9f93a
- v2.2.299 아키텍처 수렴: coreChat 통일(엔진 휴리스틱 3벌 제거), 기업 모드 검색 오케스트레이터 승격, lib/execUtil(실행 래퍼 6곳·Python 탐지 3벌 단일화), lib/kstSchedule(워처 4개 nowInKst 통합), estimateTokens 통합, 설정 접근 규칙 명문화 - v2.2.300 채팅 화면 정리: LiveReasoningFilter(스트리밍 중 <think>/Harmony 추론 토큰 단위 차단), 확신도·검토요청 footer 기본 숨김(계산·Reflection 은 유지) - v2.2.301 문맥·의도 이해: [답변 전 이해 원칙] 상시 주입, 워크플로우 의도 브리핑, Report QA 루프(규칙 레지스트리+실측치+회귀 게이트, 블로그_v3 개념 이식) - v2.2.302 /benchmark 비즈니스 렌즈(가격·수익·운영)+빌드 프롬프트 모드+QA 연계 - v2.2.303 handoff 모드(측정치 무손실 인수인계 문서)+/claude(Claude Code 터미널 위임) - v2.2.304 이식성: 지식 경로 두뇌-상대 규약(pickWikiDir 상대 해석), 이사 체크리스트 - v2.2.305 Claude 구독 엔진: claude: 프로바이더(CLI 위임, 모델 드롭다운 자동 노출, coreChat 지원 — 워크플로우·QA도 구독 모델 가능) - v2.2.306 Tone Guard: AI 상투어 금지 레지스트리(상담사 화법 실사례 8종+대조 예시) - v2.2.307 /benchmark 레이아웃 골격(sectionRoles 결정론 분석, 롤링 배너 즉답), 파트별 실패 격리, 합성 타임아웃 120→300초 검증: tsc 무오류 + jest 888 통과 + esbuild 정상 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
204 lines
7.7 KiB
TypeScript
204 lines
7.7 KiB
TypeScript
/**
|
|
* Post-hoc Self-Check — 답변 *완료 후* LLM 한 번 호출로 3가지 평가.
|
|
*
|
|
* 사용자 제안: "[Self-Check] 단계 — 이 답변이 사용자 질문에 직접 답하는가 / 규칙
|
|
* 준수 / 논리 모순 없는가".
|
|
*
|
|
* 기존 CoVe (v2.2.184) 와 차이:
|
|
* - CoVe = *답변 작성 전* 모델에게 self-verify 지시 (instructional, 1 pass)
|
|
* - Self-Check = *답변 완료 후* 별도 LLM 호출로 검증 (post-hoc, 2 pass)
|
|
*
|
|
* 비용·위험:
|
|
* - 매 turn 추가 LLM 호출 1회 (latency 비용)
|
|
* - 기본 OFF — semantic re-rank 와 같은 opt-in 패턴
|
|
* - 짧은 timeout (기본 6초). 실패해도 답변 자체엔 영향 없음 — 그냥 평가 못 함.
|
|
* - 빠른 작은 모델 권장 (예: gemma2:2b)
|
|
*
|
|
* 위치: 답변 streaming 완료 후, `usedScope` 메시지 전송 직전. 비동기 — 답변
|
|
* 표시를 *블록 하지 않음*. 결과는 webview 에 별도 메시지로 push.
|
|
*/
|
|
import { coreChat } from '../core/services';
|
|
|
|
|
|
export interface SelfCheckOptions {
|
|
ollamaUrl: string;
|
|
model: string;
|
|
timeoutMs: number;
|
|
/** 출처 컨텍스트 미리보기 길이. 기본 180 chars. */
|
|
excerptLength: number;
|
|
/** 컨텍스트로 넘길 최대 출처 개수. 기본 5. */
|
|
maxSources: number;
|
|
}
|
|
|
|
export const DEFAULT_SELF_CHECK_OPTIONS: Omit<SelfCheckOptions, 'ollamaUrl' | 'model'> = {
|
|
timeoutMs: 6000,
|
|
excerptLength: 180,
|
|
maxSources: 5,
|
|
};
|
|
|
|
export type SelfCheckVerdict = 'yes' | 'partial' | 'no' | 'unknown';
|
|
export type ContradictionLevel = 'none' | 'minor' | 'major' | 'unknown';
|
|
|
|
export interface SelfCheckResult {
|
|
success: boolean;
|
|
answersQuestion: SelfCheckVerdict;
|
|
grounded: SelfCheckVerdict;
|
|
contradiction: ContradictionLevel;
|
|
note: string;
|
|
durationMs: number;
|
|
/** 디버그·footer 표시용. */
|
|
rawResponse?: string;
|
|
}
|
|
|
|
const FAILURE_RESULT: Omit<SelfCheckResult, 'durationMs' | 'note'> = {
|
|
success: false,
|
|
answersQuestion: 'unknown',
|
|
grounded: 'unknown',
|
|
contradiction: 'unknown',
|
|
};
|
|
|
|
function shortExcerpt(text: string, n: number): string {
|
|
if (!text) return '';
|
|
const cleaned = text.replace(/\s+/g, ' ').trim();
|
|
return cleaned.length <= n ? cleaned : cleaned.slice(0, n) + '…';
|
|
}
|
|
|
|
function buildPrompt(
|
|
userPrompt: string,
|
|
answer: string,
|
|
sources: { title: string; excerpt: string }[],
|
|
excerptLength: number,
|
|
): { system: string; user: string } {
|
|
const system = [
|
|
'당신은 답변 검증기 (judge). 사용자 질문, 답변, 출처를 받아 3가지 평가:',
|
|
'',
|
|
'1. answersQuestion: 답변이 질문에 *직접* 답하는가? (yes/partial/no)',
|
|
'2. grounded: 답변이 *제공된 출처에 근거* 하는가? (출처 없으면 unknown 가능) (yes/partial/no/unknown)',
|
|
'3. contradiction: 답변에 *논리적 모순* 이 있나? (none/minor/major)',
|
|
'',
|
|
'[출력 형식 — 정확히 한 줄 JSON, 다른 텍스트 절대 금지]',
|
|
'{"answersQuestion":"yes","grounded":"partial","contradiction":"none","note":"답변은 직접적이나 일부 주장이 모델 일반 지식 기반"}',
|
|
'',
|
|
'[규칙]',
|
|
'- partial/minor 는 *진짜* 애매한 경우에만. 둘 중 하나로 단정 가능하면 단정.',
|
|
'- note 는 1문장, 80자 이내, 핵심 평가 근거.',
|
|
'- JSON 한 줄 외 텍스트 (서론·설명·코드블록) 절대 출력 금지.',
|
|
].join('\n');
|
|
|
|
const srcLines = sources.length > 0
|
|
? sources.map((s, i) => `[S${i + 1}] ${s.title}\n ${shortExcerpt(s.excerpt, excerptLength)}`).join('\n')
|
|
: '(검색된 출처 없음 — grounded 는 unknown 또는 no 평가)';
|
|
|
|
const user = [
|
|
'[사용자 질문]',
|
|
userPrompt,
|
|
'',
|
|
'[답변]',
|
|
answer,
|
|
'',
|
|
'[제공된 출처]',
|
|
srcLines,
|
|
'',
|
|
'위 평가 기준에 따라 JSON 한 줄 출력.',
|
|
].join('\n');
|
|
|
|
return { system, user };
|
|
}
|
|
|
|
function parseResult(raw: string): Omit<SelfCheckResult, 'durationMs' | 'rawResponse'> | null {
|
|
if (!raw) return null;
|
|
const match = raw.match(/\{[\s\S]*?\}/);
|
|
if (!match) return null;
|
|
try {
|
|
const parsed = JSON.parse(match[0]);
|
|
const aq = String(parsed?.answersQuestion || '').toLowerCase();
|
|
const gr = String(parsed?.grounded || '').toLowerCase();
|
|
const co = String(parsed?.contradiction || '').toLowerCase();
|
|
const validVerdict = (v: string): v is SelfCheckVerdict => ['yes', 'partial', 'no', 'unknown'].includes(v);
|
|
const validCo = (v: string): v is ContradictionLevel => ['none', 'minor', 'major', 'unknown'].includes(v);
|
|
if (!validVerdict(aq) || !validVerdict(gr) || !validCo(co)) return null;
|
|
const note = typeof parsed?.note === 'string' ? parsed.note.slice(0, 120) : '';
|
|
return {
|
|
success: true,
|
|
answersQuestion: aq,
|
|
grounded: gr,
|
|
contradiction: co,
|
|
note: note || '평가 노트 없음',
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function postHocSelfCheck(
|
|
userPrompt: string,
|
|
answer: string,
|
|
sources: { title: string; excerpt: string }[],
|
|
options: SelfCheckOptions,
|
|
): Promise<SelfCheckResult> {
|
|
const start = Date.now();
|
|
if (!userPrompt.trim() || !answer.trim()) {
|
|
return { ...FAILURE_RESULT, note: 'empty input', durationMs: Date.now() - start };
|
|
}
|
|
const sourcesCap = (sources || []).slice(0, options.maxSources);
|
|
const { system, user } = buildPrompt(userPrompt, answer, sourcesCap, options.excerptLength);
|
|
|
|
// [코어 수렴] 자체 fetch·엔진 휴리스틱 제거 — coreChat 으로 통일 (온도 0, 200토큰).
|
|
// (이 모듈은 원래 import 0개의 자립 모듈이었으나, 엔진 폴백·로깅 일원화가 더 큰 가치)
|
|
let raw = '';
|
|
try {
|
|
const result = await coreChat({
|
|
system, user, model: options.model,
|
|
timeoutMs: options.timeoutMs,
|
|
temperature: 0.0,
|
|
maxTokens: 200,
|
|
});
|
|
raw = String(result.content || '');
|
|
} catch (e: any) {
|
|
return {
|
|
...FAILURE_RESULT,
|
|
note: `LLM call failed: ${e?.name || e?.message || 'unknown'}`,
|
|
durationMs: Date.now() - start,
|
|
rawResponse: '',
|
|
};
|
|
}
|
|
|
|
const parsed = parseResult(raw);
|
|
if (!parsed) {
|
|
return {
|
|
...FAILURE_RESULT,
|
|
note: 'unparseable response',
|
|
durationMs: Date.now() - start,
|
|
rawResponse: raw.slice(0, 200),
|
|
};
|
|
}
|
|
return {
|
|
...parsed,
|
|
durationMs: Date.now() - start,
|
|
rawResponse: raw.slice(0, 200),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 결과를 markdown 한 줄 footer 로 포맷 — 사용자가 답변 아래에서 바로 봄.
|
|
*
|
|
* 형식: `\n\n---\n_🔍 Self-check_: 답함=✓ · 근거=○ · 모순=없음 _(2.4s · 모델: gemma2:2b)_`
|
|
*
|
|
* 실패면 흐릿한 한 줄.
|
|
*/
|
|
export function formatSelfCheckFooter(result: SelfCheckResult, model: string): string {
|
|
if (!result.success) {
|
|
return `\n\n---\n_🔍 Self-check: ⊘ ${result.note} (${(result.durationMs / 1000).toFixed(1)}s)_`;
|
|
}
|
|
const aq = result.answersQuestion === 'yes' ? '✓'
|
|
: result.answersQuestion === 'partial' ? '◐'
|
|
: result.answersQuestion === 'no' ? '✗' : '?';
|
|
const gr = result.grounded === 'yes' ? '✓'
|
|
: result.grounded === 'partial' ? '◐'
|
|
: result.grounded === 'no' ? '✗' : '?';
|
|
const co = result.contradiction === 'none' ? '없음'
|
|
: result.contradiction === 'minor' ? '경미'
|
|
: result.contradiction === 'major' ? '⚠️ 중대' : '?';
|
|
return `\n\n---\n_🔍 **Self-check**: 답함=${aq} · 근거=${gr} · 모순=${co} — ${result.note} _(${(result.durationMs / 1000).toFixed(1)}s · ${model})__`;
|
|
}
|