File size: 1,112 Bytes
5db30ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""LangChain hook that applies the run's spending limits to basic-education runtime calls."""

from __future__ import annotations

from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.outputs import LLMResult

from elbench.utils import spend


class SpendCallback(BaseCallbackHandler):
    """Checks the run's spending limit before each call and records what the call cost."""

    raise_error = True

    def __init__(self, role: str) -> None:
        self.role = role

    def on_chat_model_start(self, serialized, messages, **kwargs) -> None:
        spend.check(self.role)

    def on_llm_end(self, response: LLMResult, **kwargs) -> None:
        usage = (response.llm_output or {}).get("token_usage")
        if not usage:
            for generations in response.generations:
                for generation in generations:
                    message = getattr(generation, "message", None)
                    usage = getattr(message, "usage_metadata", None)
                    if usage:
                        break
        spend.record(self.role, dict(usage) if usage else None)