mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 19:24:03 +00:00
fix(bot): scale llm purchase confidence values
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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<PurchaseInterpretation>({
|
||||
decision: 'purchase',
|
||||
amountMinor: 5000n,
|
||||
currency: 'GEL',
|
||||
itemDescription: 'шампунь',
|
||||
confidence: 92,
|
||||
parserMode: 'llm',
|
||||
clarificationQuestion: null
|
||||
})
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user