A programming framework for agentic AI
Log messages and response for LLMCall event (#4910)
Jack Gerritsf4382f01c82a
diff --git a/python/packages/autogen-core/src/autogen_core/logging.py b/python/packages/autogen-core/src/autogen_core/logging.pyindex 11bb46c04..057458e18 100644--- a/python/packages/autogen-core/src/autogen_core/logging.py+++ b/python/packages/autogen-core/src/autogen_core/logging.py@@ -1,18 +1,30 @@import jsonfrom enum import Enum-from typing import Any, cast+from typing import Any, Dict, castfrom ._agent_id import AgentIdfrom ._topic import TopicIdclass LLMCallEvent:- def __init__(self, *, prompt_tokens: int, completion_tokens: int, **kwargs: Any) -> None:+ def __init__(+ self,+ *,+ messages: Dict[str, Any],+ response: Dict[str, Any],+ prompt_tokens: int,+ completion_tokens: int,+ agent_id: AgentId | None = None,+ **kwargs: Any,+ ) -> None:"""To be used by model clients to log the call to the LLM.Args:+ messages (Dict[str, Any]): The messages of the call. Must be json serializable.+ response (Dict[str, Any]): The response of the call. Must be json serializable.prompt_tokens (int): Number of tokens used in the prompt.completion_tokens (int): Number of tokens used in the completion.+ agent_id (AgentId | None, optional): The agent id of the model. Defaults to None.Example:@@ -26,8 +38,12 @@ class LLMCallEvent:"""self.kwargs = kwargs+ self.kwargs["type"] = "LLMCall"+ self.kwargs["messages"] = messages+ self.kwargs["response"] = responseself.kwargs["prompt_tokens"] = prompt_tokensself.kwargs["completion_tokens"] = completion_tokens+ self.kwargs["agent_id"] = None if agent_id is None else str(agent_id)self.kwargs["type"] = "LLMCall"@propertydiff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.pyindex 3fb8710b6..e3117a5d9 100644--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py@@ -28,6 +28,7 @@ from autogen_core import (Component,FunctionCall,Image,+ MessageHandlerContext,)from autogen_core.logging import LLMCallEventfrom autogen_core.models import (@@ -493,20 +494,28 @@ class BaseOpenAIChatCompletionClient(ChatCompletionClient):if use_beta_client:result = cast(ParsedChatCompletion[Any], result)- if result.usage is not None:- logger.info(- LLMCallEvent(- prompt_tokens=result.usage.prompt_tokens,- completion_tokens=result.usage.completion_tokens,- )- )-usage = RequestUsage(# TODO backup token countingprompt_tokens=result.usage.prompt_tokens if result.usage is not None else 0,completion_tokens=(result.usage.completion_tokens if result.usage is not None else 0),)+ # If we are running in the context of a handler we can get the agent_id+ try:+ agent_id = MessageHandlerContext.agent_id()+ except RuntimeError:+ agent_id = None++ logger.info(+ LLMCallEvent(+ messages=cast(Dict[str, Any], oai_messages),+ response=result.model_dump(),+ prompt_tokens=usage.prompt_tokens,+ completion_tokens=usage.completion_tokens,+ agent_id=agent_id,+ )+ )+if self._resolved_model is not None:if self._resolved_model != result.model:warnings.warn(