diff --git a/apps/bot/src/openai-parser-fallback.ts b/apps/bot/src/openai-parser-fallback.ts index 691027b..778d8f5 100644 --- a/apps/bot/src/openai-parser-fallback.ts +++ b/apps/bot/src/openai-parser-fallback.ts @@ -19,6 +19,11 @@ function asBigInt(value: string): bigint | null { return parsed > 0n ? parsed : null } +function normalizeConfidence(value: number): number { + const scaled = value >= 0 && value <= 1 ? value * 100 : value + return Math.max(0, Math.min(100, Math.round(scaled))) +} + export function createOpenAiParserFallback( apiKey: string | undefined, model: string @@ -116,7 +121,7 @@ export function createOpenAiParserFallback( amountMinor, currency: parsedJson.currency, itemDescription: parsedJson.itemDescription, - confidence: Math.max(0, Math.min(100, Math.round(parsedJson.confidence))), + confidence: normalizeConfidence(parsedJson.confidence), parserMode: 'llm', needsReview: parsedJson.needsReview } diff --git a/apps/bot/src/openai-purchase-interpreter.test.ts b/apps/bot/src/openai-purchase-interpreter.test.ts index 3b5d66a..99fc5c8 100644 --- a/apps/bot/src/openai-purchase-interpreter.test.ts +++ b/apps/bot/src/openai-purchase-interpreter.test.ts @@ -95,4 +95,48 @@ describe('createOpenAiPurchaseInterpreter', () => { globalThis.fetch = originalFetch } }) + + test('scales 0-1 confidence values into 0-100', async () => { + const interpreter = createOpenAiPurchaseInterpreter('test-key', 'gpt-5-mini') + expect(interpreter).toBeDefined() + + const originalFetch = globalThis.fetch + globalThis.fetch = (async () => + successfulResponse({ + output: [ + { + content: [ + { + text: JSON.stringify({ + decision: 'purchase', + amountMinor: '5000', + currency: 'GEL', + itemDescription: 'шампунь', + confidence: 0.92, + clarificationQuestion: null + }) + } + ] + } + ] + })) as unknown as typeof fetch + + try { + const result = await interpreter!('Купил шампунь на 50 лари', { + defaultCurrency: 'GEL' + }) + + expect(result).toEqual({ + decision: 'purchase', + amountMinor: 5000n, + currency: 'GEL', + itemDescription: 'шампунь', + confidence: 92, + parserMode: 'llm', + clarificationQuestion: null + }) + } finally { + globalThis.fetch = originalFetch + } + }) }) diff --git a/apps/bot/src/openai-purchase-interpreter.ts b/apps/bot/src/openai-purchase-interpreter.ts index 45a0482..3690651 100644 --- a/apps/bot/src/openai-purchase-interpreter.ts +++ b/apps/bot/src/openai-purchase-interpreter.ts @@ -46,6 +46,11 @@ function normalizeCurrency(value: string | null): 'GEL' | 'USD' | null { return value === 'GEL' || value === 'USD' ? value : null } +function normalizeConfidence(value: number): number { + const scaled = value >= 0 && value <= 1 ? value * 100 : value + return Math.max(0, Math.min(100, Math.round(scaled))) +} + export function createOpenAiPurchaseInterpreter( apiKey: string | undefined, model: string @@ -170,7 +175,7 @@ export function createOpenAiPurchaseInterpreter( amountMinor: asOptionalBigInt(parsedJson.amountMinor), currency: normalizeCurrency(parsedJson.currency), itemDescription: normalizeOptionalText(parsedJson.itemDescription), - confidence: Math.max(0, Math.min(100, Math.round(parsedJson.confidence))), + confidence: normalizeConfidence(parsedJson.confidence), parserMode: 'llm', clarificationQuestion }