ragas icon indicating copy to clipboard operation
ragas copied to clipboard

OpenAI model cost based on model name fix

Open JonasElburgUVA opened this issue 9 months ago • 2 comments

the CostCallbackHandler.total_cost method can take a dictionary of models and associated cost per i/o tokens. However, this was not functional because the token usage parser did not save the model's name. The model's name was also removed after adding TokenUsage objects together. I fixed these issues for the OpenAI parser.

Note that the same problem still exists for the anthropic and bedrock parsers. I did not update this, since I don't use these API's and would not be able to test my implementation.

JonasElburgUVA avatar Mar 04 '25 14:03 JonasElburgUVA

thanks for bringing this up @JonasElburgUVA I'll fix the others too after merging this but could you check the tests too?

seems like some tests were also incorrect

jjmachan avatar Mar 04 '25 17:03 jjmachan

Sorry for the failing tests in the initial PR. The test_cost.py tests did not expect a model ID as part of TokenUsage objects, even though the LLMResult samples do contain model IDs. I updated the tests accordingly. I also added the model ID parsing logic for the other parsers based on the LLMResult samples in test_cost.py.

codestyle check is still failing, I am not sure why?

JonasElburgUVA avatar Mar 07 '25 09:03 JonasElburgUVA

@JonasElburgUVA Could you please rebase this PR so we can take final look at the tests.

anistark avatar Aug 28 '25 10:08 anistark

continued changes via #2287

anistark avatar Sep 17 '25 09:09 anistark