You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_llm_usage.py 975 B

123456789101112131415161718192021222324252627282930313233
  1. import logging
  2. from agnext.application.logging import EVENT_LOGGER_NAME, LLMUsageTracker
  3. from agnext.application.logging.events import LLMCallEvent
  4. def test_llm_usage() -> None:
  5. # Set up the logging configuration to use the custom handler
  6. logger = logging.getLogger(EVENT_LOGGER_NAME)
  7. logger.setLevel(logging.INFO)
  8. llm_usage = LLMUsageTracker()
  9. logger.handlers = [llm_usage]
  10. logger.info(LLMCallEvent(prompt_tokens=10, completion_tokens=20))
  11. assert llm_usage.prompt_tokens == 10
  12. assert llm_usage.completion_tokens == 20
  13. logger.info(LLMCallEvent(prompt_tokens=1, completion_tokens=1))
  14. assert llm_usage.prompt_tokens == 11
  15. assert llm_usage.completion_tokens == 21
  16. llm_usage.reset()
  17. assert llm_usage.prompt_tokens == 0
  18. assert llm_usage.completion_tokens == 0
  19. logger.info(LLMCallEvent(prompt_tokens=1, completion_tokens=1))
  20. assert llm_usage.prompt_tokens == 1
  21. assert llm_usage.completion_tokens == 1