Spaces:
Sleeping
Sleeping
File size: 2,252 Bytes
c6067e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | from voiceledger.parser.llm_parser import LLMParser
from voiceledger.parser.rules import RuleParser
from voiceledger.parser.schema import Transaction
class FakeInferenceClient:
def __init__(self, response: object) -> None:
self.response = response
def text_generation(self, prompt: str, **kwargs: object) -> object:
self.prompt = prompt
self.kwargs = kwargs
return self.response
def test_rule_parser_implements_parser_interface() -> None:
transaction = RuleParser().parse("Sold 12 mangoes, 20 each")
assert isinstance(transaction, Transaction)
assert transaction.transaction_type == "sale"
assert transaction.amount == 240
def test_llm_parser_returns_valid_transaction_from_strict_json() -> None:
client = FakeInferenceClient(
"""
{
"transaction_type": "customer_credit",
"item": null,
"quantity": null,
"unit_price": null,
"amount": 100,
"customer": "Amit",
"payment_status": "credit",
"notes": "Amit owes 100",
"confidence": 0.97
}
"""
)
transaction = LLMParser(client=client, model="test-model").parse("Amit owes 100")
assert transaction.transaction_type == "customer_credit"
assert transaction.customer == "Amit"
assert transaction.amount == 100
assert client.kwargs["temperature"] == 0.0
assert client.kwargs["model"] == "test-model"
def test_llm_parser_falls_back_to_rules_on_invalid_json() -> None:
client = FakeInferenceClient("not json")
transaction = LLMParser(client=client).parse("Sold 12 mangoes, 20 each")
assert transaction.transaction_type == "sale"
assert transaction.item == "mangoes"
assert transaction.amount == 240
def test_llm_parser_falls_back_to_rules_on_schema_error() -> None:
client = FakeInferenceClient(
"""
{
"transaction_type": "not_supported",
"notes": "Amit owes 100",
"confidence": 0.9
}
"""
)
transaction = LLMParser(client=client).parse("Amit owes 100")
assert transaction.transaction_type == "customer_credit"
assert transaction.customer == "Amit"
assert transaction.amount == 100
|