fix: default cost to NaN when calculation fails in LiteLLM backend (#912)

* fix: default cost to NaN when calculation fails in LiteLLM backend

* lint

* lint
This commit is contained in:
you-n-g
2025-05-29 22:58:29 +08:00
committed by GitHub
parent 9e854a1346
commit fca945c77f
3 changed files with 5 additions and 1 deletions
+2
View File
@@ -17,6 +17,7 @@ from typing import Any, Optional, cast
import numpy as np
import openai
import tiktoken
from openai.types.chat import ChatCompletion
from rdagent.core.utils import LLM_CACHE_SEED_GEN, SingletonBaseClass, import_class
from rdagent.log import LogColors
@@ -385,6 +386,7 @@ class DeprecBackend(APIBackend):
if len(chunk.choices) > 0 and chunk.choices[0].finish_reason is not None:
finish_reason = chunk.choices[0].finish_reason
else:
response = cast(ChatCompletion, response)
resp = response.choices[0].message.content
finish_reason = response.choices[0].finish_reason
if LLM_SETTINGS.log_llm_chat_content:
+2
View File
@@ -1,5 +1,6 @@
from typing import Any, Literal, cast
import numpy as np
from litellm import (
completion,
completion_cost,
@@ -139,6 +140,7 @@ class LiteLLMAPIBackend(APIBackend):
cost = completion_cost(model=model, messages=messages, completion=content)
except Exception as e:
logger.warning(f"Cost calculation failed for model {model}: {e}. Skip cost statistics.")
cost = np.nan
else:
ACC_COST += cost
logger.info(
+1 -1
View File
@@ -826,7 +826,7 @@ class QTDockerEnv(DockerEnv):
def __init__(self, conf: DockerConf = QlibDockerConf()):
super().__init__(conf)
def prepare(self, *args, **kwargs) -> None: # type: ignore[explicit-override, no-untyped-def]
def prepare(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def]
"""
Download image & data if it doesn't exist
"""