Skip to content

Commit

Permalink
Log messages and response for LLMCall event (#4910)
Browse files Browse the repository at this point in the history
* Log messages and response for LLMCall event

* Remove accidental change

* newline
  • Loading branch information
jackgerrits authored Jan 7, 2025
1 parent 3105649 commit f4382f0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
20 changes: 18 additions & 2 deletions python/packages/autogen-core/src/autogen_core/logging.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import json
from enum import Enum
from typing import Any, cast
from typing import Any, Dict, cast

from ._agent_id import AgentId
from ._topic import TopicId


class 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:
Expand All @@ -26,8 +38,12 @@ def __init__(self, *, prompt_tokens: int, completion_tokens: int, **kwargs: Any)
"""
self.kwargs = kwargs
self.kwargs["type"] = "LLMCall"
self.kwargs["messages"] = messages
self.kwargs["response"] = response
self.kwargs["prompt_tokens"] = prompt_tokens
self.kwargs["completion_tokens"] = completion_tokens
self.kwargs["agent_id"] = None if agent_id is None else str(agent_id)
self.kwargs["type"] = "LLMCall"

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
Component,
FunctionCall,
Image,
MessageHandlerContext,
)
from autogen_core.logging import LLMCallEvent
from autogen_core.models import (
Expand Down Expand Up @@ -493,20 +494,28 @@ async def create(
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 counting
prompt_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(
Expand Down

0 comments on commit f4382f0

Please sign in to comment.