Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging #1146

Merged
merged 92 commits into from
Feb 15, 2024
Merged

Logging #1146

Show file tree
Hide file tree
Changes from 79 commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
9a551a1
WIP:logging
cheng-tan Jan 3, 2024
5e5c20b
serialize request, response and client
cheng-tan Jan 4, 2024
8d0d489
Resolved conflict, merge main.
afourney Jan 22, 2024
98e65d5
Fixed code formatting.
afourney Jan 22, 2024
a64aec9
Updated to use a global package, and added some test cases. Still ver…
afourney Jan 23, 2024
b67995e
Merge branch 'main' into logging
afourney Jan 23, 2024
7306b1c
Update work in progress.
afourney Jan 23, 2024
07dc5b8
adding cost
cheng-tan Jan 23, 2024
73220c8
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 23, 2024
5b5ec6f
log new agent
cheng-tan Jan 23, 2024
9796142
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 23, 2024
ff5863f
update log_completion test in test_agent_telemetry
cheng-tan Jan 24, 2024
c876d62
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 24, 2024
c27def1
tests
cheng-tan Jan 24, 2024
dab2530
fix formatting
cheng-tan Jan 24, 2024
ede5418
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 24, 2024
5862d0e
Added additional telemetry for wrappers and clients.
afourney Jan 25, 2024
526fd10
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 25, 2024
8ed603d
Merge branch 'logging' of github.com:cheng-tan/autogen into logging
cheng-tan Jan 25, 2024
33b188c
WIP: add test for oai client and oai wrapper table
cheng-tan Jan 25, 2024
b51bc29
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 26, 2024
d39b50f
update test_telemetry
cheng-tan Jan 28, 2024
6cbf22a
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 28, 2024
eb2dfa3
fix format
cheng-tan Jan 28, 2024
1ea7cf5
More tests, update doc and clean up
cheng-tan Jan 29, 2024
765f920
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 30, 2024
9b39f49
Merge branch 'main' into logging
afourney Jan 31, 2024
453b59e
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 31, 2024
580295c
small fix for session id - moved to start_logging and return from sta…
cheng-tan Jan 31, 2024
f1911ac
update start_logging type to return str, add notebook to demonstrate …
victordibia Jan 31, 2024
e2918fc
add ability to get log dataframe
victordibia Jan 31, 2024
53d520c
precommit formatting fixes
victordibia Jan 31, 2024
99d01f9
formatting fix
victordibia Jan 31, 2024
8118256
Remove pandas dependency from telemetry and only use in notebook
victordibia Jan 31, 2024
570f011
formatting fixes
victordibia Jan 31, 2024
512586a
log query exceptions
cheng-tan Jan 31, 2024
d9b12af
Merge branch 'logging' of github.com:cheng-tan/autogen into logging
cheng-tan Jan 31, 2024
595ffce
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Jan 31, 2024
79202a2
fix formatting
cheng-tan Jan 31, 2024
f7ef2d1
fix ci
cheng-tan Jan 31, 2024
4c4bac3
Merge branch 'main' into logging
cheng-tan Feb 1, 2024
3556600
fix comment - add notebook link in doc and fix groupchat serialization
cheng-tan Feb 2, 2024
e4569a1
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 2, 2024
a14c7c9
Merge branch 'logging' of github.com:cheng-tan/autogen into logging
cheng-tan Feb 2, 2024
ee3ddeb
small fix
cheng-tan Feb 2, 2024
e34a5f5
do not serialize Agent
cheng-tan Feb 2, 2024
f27a38c
formatting
cheng-tan Feb 2, 2024
27f92fb
wip
cheng-tan Feb 2, 2024
f031cf4
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 5, 2024
789a833
fix test
cheng-tan Feb 5, 2024
d6883cd
serialization bug fix for soc moderator
cheng-tan Feb 5, 2024
d78a790
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 5, 2024
d6f74b4
fix test and clean up
cheng-tan Feb 6, 2024
a4092b5
wip: add version table
cheng-tan Feb 6, 2024
011023a
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 6, 2024
9472ba4
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 6, 2024
3f6816b
fix test
cheng-tan Feb 6, 2024
249c435
fix test
cheng-tan Feb 6, 2024
90ed77c
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 6, 2024
5106b9b
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 9, 2024
4f7942b
fix test
cheng-tan Feb 9, 2024
87847c5
make the logging interface more general and fix client model logging
cheng-tan Feb 9, 2024
b3db6f5
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 12, 2024
5ba1593
fix format
cheng-tan Feb 12, 2024
bc3da16
fix formatting and tests
cheng-tan Feb 12, 2024
4e6024e
fix
cheng-tan Feb 12, 2024
3d4c0d6
fix comment
cheng-tan Feb 12, 2024
57e257a
Renaming telemetry to logging
cheng-tan Feb 12, 2024
fee17a6
update notebook
cheng-tan Feb 12, 2024
d457e19
update doc
cheng-tan Feb 12, 2024
ca76675
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 12, 2024
fdd210b
formatting
cheng-tan Feb 12, 2024
616e633
formatting and clean up
cheng-tan Feb 12, 2024
6a1d8dd
fix doc
cheng-tan Feb 12, 2024
cc86287
fix link and title
cheng-tan Feb 12, 2024
7e1e7c5
fix notebook format and fix comment
cheng-tan Feb 13, 2024
60d5a9e
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 13, 2024
7d8cc6c
format
cheng-tan Feb 13, 2024
a7c0510
Merge branch 'main' into logging
cheng-tan Feb 13, 2024
8e219bf
try fixing agent test and update migration guide
cheng-tan Feb 13, 2024
f56d1c4
Merge branch 'logging' of github.com:cheng-tan/autogen into logging
cheng-tan Feb 13, 2024
68ae33e
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 13, 2024
d59b4c2
fix link
cheng-tan Feb 13, 2024
b4e7829
debug print
cheng-tan Feb 14, 2024
861b742
debug
cheng-tan Feb 14, 2024
e02a12d
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 14, 2024
848e97b
format
cheng-tan Feb 14, 2024
a1d33f4
add back tests
cheng-tan Feb 14, 2024
7a223a0
Merge branch 'main' into logging
sonichi Feb 14, 2024
6912f83
fix tests
cheng-tan Feb 14, 2024
0e6a8c9
Merge branch 'main' of github.com:microsoft/autogen into logging
cheng-tan Feb 14, 2024
9adf462
Merge branch 'logging' of github.com:cheng-tan/autogen into logging
cheng-tan Feb 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions autogen/agentchat/assistant_agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Callable, Dict, Literal, Optional, Union

from .conversable_agent import ConversableAgent
from autogen.runtime_logging import logging_enabled, log_new_agent


class AssistantAgent(ConversableAgent):
Expand Down Expand Up @@ -67,6 +68,8 @@ def __init__(
description=description,
**kwargs,
)
if logging_enabled():
log_new_agent(self, locals())

# Update the provided description if None, and we are using the default system_message,
# then use the default description.
Expand Down
4 changes: 4 additions & 0 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ..coding.factory import CodeExecutorFactory

from ..oai.client import OpenAIWrapper, ModelClient
from ..runtime_logging import logging_enabled, log_new_agent
from ..cache.cache import Cache
from ..code_utils import (
UNKNOWN,
Expand Down Expand Up @@ -145,6 +146,9 @@ def __init__(
self.llm_config.update(llm_config)
self.client = OpenAIWrapper(**self.llm_config)

if logging_enabled():
log_new_agent(self, locals())

# Initialize standalone client cache object.
self.client_cache = None

Expand Down
4 changes: 3 additions & 1 deletion autogen/agentchat/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from ..code_utils import content_str
from .agent import Agent
from .conversable_agent import ConversableAgent
from ..runtime_logging import logging_enabled, log_new_agent
from ..graph_utils import check_graph_validity, invert_disallowed_to_allowed


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -463,6 +463,8 @@ def __init__(
system_message=system_message,
**kwargs,
)
if logging_enabled():
log_new_agent(self, locals())
# Store groupchat
self._groupchat = groupchat

Expand Down
4 changes: 4 additions & 0 deletions autogen/agentchat/user_proxy_agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Callable, Dict, List, Literal, Optional, Union

from .conversable_agent import ConversableAgent
from ..runtime_logging import logging_enabled, log_new_agent


class UserProxyAgent(ConversableAgent):
Expand Down Expand Up @@ -93,3 +94,6 @@ def __init__(
if description is not None
else self.DEFAULT_USER_PROXY_AGENT_DESCRIPTIONS[human_input_mode],
)

if logging_enabled():
log_new_agent(self, locals())
4 changes: 4 additions & 0 deletions autogen/logger/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .logger_factory import LoggerFactory
from .sqlite_logger import SqliteLogger

__all__ = ("LoggerFactory", "SqliteLogger")
101 changes: 101 additions & 0 deletions autogen/logger/base_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Dict, TYPE_CHECKING, Union
import sqlite3
import uuid

from openai import OpenAI, AzureOpenAI
from openai.types.chat import ChatCompletion

if TYPE_CHECKING:
from autogen import ConversableAgent, OpenAIWrapper


cheng-tan marked this conversation as resolved.
Show resolved Hide resolved
class BaseLogger(ABC):
@abstractmethod
def start(self) -> str:
"""
Open a connection to the logging database, and start recording.

Returns:
session_id (str): a unique id for the logging session
"""
...

@abstractmethod
def log_chat_completion(
invocation_id: uuid.UUID,
client_id: int,
wrapper_id: int,
request: Dict,
response: Union[str, ChatCompletion],
is_cached: int,
cost: float,
start_time: str,
) -> None:
"""
Log a chat completion to database.

In AutoGen, chat completions are somewhat complicated because they are handled by the `autogen.oai.OpenAIWrapper` class.
One invocation to `create` can lead to multiple underlying OpenAI calls, depending on the llm_config list used, and
any errors or retries.

Args:
invocation_id (uuid): A unique identifier for the invocation to the OpenAIWrapper.create method call
client_id (int): A unique identifier for the underlying OpenAI client instance
wrapper_id (int): A unique identifier for the OpenAIWrapper instance
request (dict): A dictionary representing the the request or call to the OpenAI client endpoint
response (str or ChatCompletion): The response from OpenAI
is_chached (int): 1 if the response was a cache hit, 0 otherwise
cost(float): The cost for OpenAI response
start_time (str): A string representing the moment the request was initiated
"""
...

@abstractmethod
def log_new_agent(agent: ConversableAgent, init_args: Dict) -> None:
"""
Log the birth of a new agent.

Args:
agent (ConversableAgent): The agent to log.
init_args (dict): The arguments passed to the construct the conversable agent
"""
...

@abstractmethod
def log_new_wrapper(wrapper: OpenAIWrapper, init_args: Dict) -> None:
"""
Log the birth of a new OpenAIWrapper.

Args:
wrapper (OpenAIWrapper): The wrapper to log.
init_args (dict): The arguments passed to the construct the wrapper
"""
...

@abstractmethod
def log_new_client(client: Union[AzureOpenAI, OpenAI], wrapper: OpenAIWrapper, init_args: Dict) -> None:
"""
Log the birth of a new OpenAIWrapper.

Args:
wrapper (OpenAI): The OpenAI client to log.
init_args (dict): The arguments passed to the construct the client
"""
...

@abstractmethod
def stop() -> None:
"""
Close the connection to the logging database, and stop logging.
"""
...

@abstractmethod
def get_connection() -> Union[sqlite3.Connection]:
"""
Return a connection to the logging database.
"""
...
17 changes: 17 additions & 0 deletions autogen/logger/logger_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Any, Dict, Optional
from autogen.logger.base_logger import BaseLogger
from autogen.logger.sqlite_logger import SqliteLogger

__all__ = ("LoggerFactory",)


class LoggerFactory:
@staticmethod
def get_logger(logger_type: str = "sqlite", config: Optional[Dict[str, Any]] = None) -> BaseLogger:
if config is None:
config = {}

if logger_type == "sqlite":
return SqliteLogger(config)
else:
raise ValueError(f"[logger_factory] Unknown logger type: {logger_type}")
36 changes: 36 additions & 0 deletions autogen/logger/logger_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import datetime
import inspect
from typing import Any, Dict, List, Tuple, Union

__all__ = ("get_current_ts", "to_dict")


def get_current_ts():
return datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S.%f")


def to_dict(
obj: Union[int, float, str, bool, Dict[Any, Any], List[Any], Tuple[Any, ...], Any],
exclude: Tuple[str] = (),
no_recursive: Tuple[str] = (),
) -> Any:
if isinstance(obj, (int, float, str, bool)):
return obj
elif callable(obj):
return inspect.getsource(obj).strip()
elif isinstance(obj, dict):
return {
str(k): to_dict(str(v)) if isinstance(v, no_recursive) else to_dict(v, exclude, no_recursive)
for k, v in obj.items()
if k not in exclude
}
elif isinstance(obj, (list, tuple)):
return [to_dict(str(v)) if isinstance(v, no_recursive) else to_dict(v, exclude, no_recursive) for v in obj]
elif hasattr(obj, "__dict__"):
return {
str(k): to_dict(str(v)) if isinstance(v, no_recursive) else to_dict(v, exclude, no_recursive)
for k, v in vars(obj).items()
if k not in exclude
}
else:
return obj
Loading
Loading