Skip to content

A programming framework for agentic AI

[Core] Fix update_usage_summary when response doesn't have usage attribute (#1008)

Yiran Wu6bf33dfede73
diff --git a/autogen/oai/client.py b/autogen/oai/client.py
index 4b548d961..443d62c92 100644
--- a/autogen/oai/client.py
+++ b/autogen/oai/client.py
@@ -364,6 +364,15 @@ class OpenAIWrapper:
Usage is calculated no matter filter is passed or not.
"""
+ try:
+ usage = response.usage
+ assert usage is not None
+ usage.prompt_tokens = 0 if usage.prompt_tokens is None else usage.prompt_tokens
+ usage.completion_tokens = 0 if usage.completion_tokens is None else usage.completion_tokens
+ usage.total_tokens = 0 if usage.total_tokens is None else usage.total_tokens
+ except (AttributeError, AssertionError):
+ logger.debug("Usage attribute is not found in the response.", exc_info=1)
+ return
def update_usage(usage_summary):
if usage_summary is None:
@@ -373,12 +382,10 @@ class OpenAIWrapper:
usage_summary[response.model] = {
"cost": usage_summary.get(response.model, {}).get("cost", 0) + response.cost,
- "prompt_tokens": usage_summary.get(response.model, {}).get("prompt_tokens", 0)
- + response.usage.prompt_tokens,
+ "prompt_tokens": usage_summary.get(response.model, {}).get("prompt_tokens", 0) + usage.prompt_tokens,
"completion_tokens": usage_summary.get(response.model, {}).get("completion_tokens", 0)
- + response.usage.completion_tokens,
- "total_tokens": usage_summary.get(response.model, {}).get("total_tokens", 0)
- + response.usage.total_tokens,
+ + usage.completion_tokens,
+ "total_tokens": usage_summary.get(response.model, {}).get("total_tokens", 0) + usage.total_tokens,
}
return usage_summary
@@ -448,6 +455,7 @@ class OpenAIWrapper:
model = response.model
if model not in OAI_PRICE1K:
# TODO: add logging to warn that the model is not found
+ logger.debug(f"Model {model} is not found. The cost will be 0.", exc_info=1)
return 0
n_input_tokens = response.usage.prompt_tokens
diff --git a/notebook/agentchat_function_call_async.ipynb b/notebook/agentchat_function_call_async.ipynb
index 3864c4899..581022e44 100644
--- a/notebook/agentchat_function_call_async.ipynb
+++ b/notebook/agentchat_function_call_async.ipynb
@@ -256,9 +256,13 @@
"\n",
"\n",
"groupchat = autogen.GroupChat(agents=[user_proxy, coder, markdownagent], messages=[], max_round=12)\n",
+ "\n",
+ "llm_config_manager = llm_config.copy()\n",
+ "llm_config_manager.pop(\"functions\", None)\n",
+ "\n",
"manager = autogen.GroupChatManager(\n",
" groupchat=groupchat,\n",
- " llm_config=llm_config,\n",
+ " llm_config=llm_config_manager,\n",
" is_termination_msg=lambda x: \"GROUPCHAT_TERMINATE\" in x.get(\"content\", \"\"),\n",
")"
]