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

AutoGen logging using Azure Cosmos DB #2329

Closed
wants to merge 151 commits into from
Closed
Changes from 6 commits
Commits
Show all changes
151 commits
Select commit Hold shift + click to select a range
7109b24
Create cosmos_db_logger
wmwxwa Apr 8, 2024
4cd3ab2
Update cosmos_db_logger
wmwxwa Apr 9, 2024
f656b9a
Merge branch 'main' into patch-3
wmwxwa Apr 9, 2024
bed635d
Merge branch 'main' into patch-3
wmwxwa Apr 10, 2024
38afe7e
Rename cosmos_db_logger to cosmos_db_logger.py
wmwxwa Apr 11, 2024
fe39d95
Update cosmos_db_logger.py
wmwxwa Apr 11, 2024
fee6840
Update cosmos_db_logger.py
wmwxwa Apr 12, 2024
376cb73
Merge branch 'main' into patch-3
wmwxwa Apr 12, 2024
0c5f55d
Update cosmos_db_logger.py
wmwxwa Apr 12, 2024
aa917c6
Update logger_factory.py
wmwxwa Apr 12, 2024
8994938
Update logger_factory.py
wmwxwa Apr 12, 2024
0c361bc
Update logger_factory.py
wmwxwa Apr 12, 2024
8b20786
Update logger_factory.py
wmwxwa Apr 12, 2024
ec5b82f
Update logger_factory.py
wmwxwa Apr 12, 2024
f102df5
Update logger_factory.py
wmwxwa Apr 12, 2024
4210c39
Merge branch 'main' into patch-3
wmwxwa Apr 12, 2024
608fae1
Update __init__.py
wmwxwa Apr 12, 2024
8dde5e4
Merge branch 'main' into patch-3
wmwxwa Apr 15, 2024
bf8a3c6
Update __init__.py
wmwxwa Apr 15, 2024
0a849cd
Update logger_factory.py
wmwxwa Apr 15, 2024
7fa50bc
Update cosmos_db_logger.py
wmwxwa Apr 15, 2024
778e820
Update cosmos_db_logger.py
wmwxwa Apr 15, 2024
3ffea9d
Update cosmos_db_logger.py
wmwxwa Apr 15, 2024
0224664
Update cosmos_db_logger.py
wmwxwa Apr 15, 2024
d94a659
Update cosmos_db_logger.py
wmwxwa Apr 15, 2024
dc08134
Update cosmos_db_logger.py
wmwxwa Apr 15, 2024
fffb0ed
Merge branch 'main' into patch-3
wmwxwa Apr 17, 2024
c48b086
Merge branch 'main' into patch-3
wmwxwa Apr 17, 2024
c1f0149
Merge branch 'main' into patch-3
wmwxwa Apr 19, 2024
b7f1a7a
Merge branch 'main' into patch-3
wmwxwa Apr 23, 2024
5e7f0ed
Merge branch 'main' into patch-3
wmwxwa Apr 23, 2024
42c8e99
Update logger_factory.py
wmwxwa Apr 23, 2024
bb0679f
Merge branch 'main' into patch-3
wmwxwa Apr 25, 2024
7c8d632
Update agentchat_logging.ipynb
wmwxwa Apr 26, 2024
5258e73
Update agentchat_logging.ipynb
wmwxwa Apr 26, 2024
c3d266b
Merge branch 'main' into patch-3
wmwxwa Apr 26, 2024
4797f1b
Merge branch 'main' into patch-3
wmwxwa Apr 27, 2024
1bb8ff2
Update runtime_logging.py
wmwxwa Apr 27, 2024
2ccd71e
Update test_logging.py
wmwxwa Apr 27, 2024
3d6d61a
Update cosmos_db_logger.py
wmwxwa Apr 28, 2024
9ff08ff
Update logger_factory.py
wmwxwa Apr 28, 2024
3079515
Update cosmos_db_logger.py
wmwxwa Apr 28, 2024
6e33d3b
Update runtime_logging.py
wmwxwa Apr 28, 2024
3ba5442
Update agentchat_logging.ipynb
wmwxwa Apr 28, 2024
cac08c9
Update test_logging.py
wmwxwa Apr 28, 2024
a2c124f
Rename test_logging.py to test_logging_sqlite.py
wmwxwa Apr 28, 2024
6448144
Create test_logging_cosmos_db
wmwxwa Apr 28, 2024
95a222a
Delete notebook/agentchat_logging.ipynb
wmwxwa Apr 29, 2024
3234fa1
Add files via upload
wmwxwa Apr 29, 2024
52894c2
Merge branch 'main' into patch-3
wmwxwa Apr 29, 2024
096b9fb
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
55adb23
Update logger_factory.py
wmwxwa Apr 29, 2024
7e8ebf6
Update agentchat_logging.ipynb
wmwxwa Apr 29, 2024
0cf004f
Rename test_logging_cosmos_db to test_logging_cosmos_db.py
wmwxwa Apr 29, 2024
2feb7fe
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
965ad04
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
db2e8f3
Update logger_factory.py
wmwxwa Apr 29, 2024
51735ff
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
c294854
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
6ceaf9e
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
fc71ab1
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
49ee8e1
Update logger_factory.py
wmwxwa Apr 29, 2024
fe6d880
Update test_logging_cosmos_db.py
wmwxwa Apr 29, 2024
3288afa
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
0acb696
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
413f3d3
Update test_logging_cosmos_db.py
wmwxwa Apr 29, 2024
3c39370
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
05a23e5
Update test_logging_cosmos_db.py
wmwxwa Apr 29, 2024
09d9dff
Update test_logging_cosmos_db.py
wmwxwa Apr 29, 2024
a87c47f
Merge branch 'main' into patch-3
wmwxwa Apr 29, 2024
27ad962
Update logger_factory.py
wmwxwa Apr 29, 2024
063b087
Update cosmos_db_logger.py
wmwxwa Apr 29, 2024
e3e73d0
Update build.yml
wmwxwa Apr 29, 2024
0e78509
Merge branch 'main' into patch-3
wmwxwa Apr 30, 2024
fe4670c
Update build.yml
wmwxwa Apr 30, 2024
06e0250
Update contrib-tests.yml
wmwxwa Apr 30, 2024
e52b4ef
Merge branch 'main' into patch-3
wmwxwa Apr 30, 2024
5b6b29e
Merge branch 'main' into patch-3
wmwxwa May 1, 2024
65beaae
Merge branch 'main' into patch-3
wmwxwa May 2, 2024
b106ca9
Update samples-tools-tests.yml
wmwxwa May 2, 2024
4cebc3a
Update contrib-openai.yml
wmwxwa May 2, 2024
dde2cfd
Update contrib-tests.yml
wmwxwa May 2, 2024
dbe8981
Merge branch 'main' into patch-3
wmwxwa May 2, 2024
31c67cf
Update contrib-tests.yml
wmwxwa May 2, 2024
d0e1c7b
Update cosmos_db_logger.py
wmwxwa May 2, 2024
6468c94
Update test_logging_cosmos_db.py
wmwxwa May 2, 2024
838e71e
Update cosmos_db_logger.py
wmwxwa May 2, 2024
94157a9
Update cosmos_db_logger.py
wmwxwa May 2, 2024
094320e
Update cosmos_db_logger.py
wmwxwa May 2, 2024
ff3793c
Update test_logging_cosmos_db.py
wmwxwa May 2, 2024
3822d29
Update cosmos_db_logger.py
wmwxwa May 2, 2024
2216e73
Update cosmos_db_logger.py
wmwxwa May 2, 2024
cceb195
Update cosmos_db_logger.py
wmwxwa May 2, 2024
ae301fc
Update test_logging_cosmos_db.py
wmwxwa May 2, 2024
77d86d6
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
4e59cad
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
b09348f
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
da9fb4a
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
c8addca
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
6241485
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
e07ce63
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
b5ea5b8
Update cosmos_db_logger.py
wmwxwa May 3, 2024
0e94f8f
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
561988f
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
75fbadf
Merge branch 'main' into patch-3
wmwxwa May 3, 2024
7b9b2f8
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
79b399e
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
96103e4
Merge branch 'main' into patch-3
wmwxwa May 3, 2024
c0fbde7
Update test_logging_cosmos_db.py
wmwxwa May 3, 2024
0d27c49
Merge branch 'main' into patch-3
wmwxwa May 7, 2024
c48dc9d
Merge branch 'main' into patch-3
wmwxwa May 13, 2024
d1e5ab2
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
6fbfe2c
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
355a2fd
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
84342af
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
42d6534
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
31a269b
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
a60d08a
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
5956e84
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
321743e
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
703bcb3
Update runtime_logging.py
wmwxwa May 13, 2024
84a79a6
Merge branch 'main' into patch-3
wmwxwa May 13, 2024
106964e
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
459ba29
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
79219ba
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
30fa367
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
465b170
Update cosmos_db_logger.py
wmwxwa May 13, 2024
53b82cd
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
5d24819
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
ac0c6e4
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
a8e30a0
Update test_logging_cosmos_db.py
wmwxwa May 13, 2024
0caa245
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
eed5e61
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
c58b7e8
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
ce0c891
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
7c3b201
Update runtime_logging.py
wmwxwa May 14, 2024
4e37c0d
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
3d2d042
Update build.yml
wmwxwa May 14, 2024
3d5ac63
Update contrib-tests.yml
wmwxwa May 14, 2024
b3f8bf4
Update contrib-openai.yml
wmwxwa May 14, 2024
2e6df77
Update samples-tools-tests.yml
wmwxwa May 14, 2024
508261f
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
5402291
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
c5e84a3
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
454a163
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
cd9dba3
Update cosmos_db_logger.py
wmwxwa May 14, 2024
a8e332e
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
10b1d29
Update test_logging_cosmos_db.py
wmwxwa May 14, 2024
d3099f6
Update cosmos_db_logger.py
wmwxwa May 14, 2024
dbe71db
Update test_logging_cosmos_db.py
wmwxwa May 22, 2024
840a9e6
Update test_logging_cosmos_db.py
wmwxwa May 23, 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
104 changes: 104 additions & 0 deletions autogen/logger/cosmos_db_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import uuid
import logging
from typing import Any, Dict, Union, TYPE_CHECKING, TypedDict
from azure.cosmos import CosmosClient
wmwxwa marked this conversation as resolved.
Show resolved Hide resolved
from autogen.logger.base_logger import BaseLogger
from autogen.logger.logger_utils import get_current_ts, to_dict

if TYPE_CHECKING:
from autogen.agent.conversable_agent import ConversableAgent
from autogen.wrapper.openai_wrapper import OpenAIWrapper

logger = logging.getLogger(__name__)

class CosmosDBConfig(TypedDict):
connection_string: str
database_name: str # Optional key, hence not enforcing it as mandatory
container_name: str # Optional key, hence not enforcing it as mandatory

class CosmosDBLogger(BaseLogger):
def __init__(self, config: CosmosDBConfig):
self.config = config
self.client = CosmosClient.from_connection_string(config['connection_string'])
self.database_name = config.get('database_name', 'AutogenLogging')
self.database = self.client.get_database_client(self.database_name)
self.container_name = config.get('container_name', 'Logs')
self.container = self.database.get_container_client(self.container_name)
self.session_id = str(uuid.uuid4())

def start(self) -> str:
try:
self.database.create_container_if_not_exists(
id=self.container_name,
partition_key="/session_id"
)
except Exception as e:
logger.error(f"Failed to create or access container {self.container_name}: {e}")
return self.session_id

def log_chat_completion(
self,
invocation_id: uuid.UUID,
client_id: int,
wrapper_id: int,
request: Dict[str, Any],
response: Union[str, Dict[str, Any], None],
is_cached: int,
cost: float,
start_time: str,
) -> None:
document = {
'type': 'chat_completion',
'invocation_id': str(invocation_id),
'client_id': client_id,
'wrapper_id': wrapper_id,
'session_id': self.session_id,
'request': to_dict(request),
'response': to_dict(response),
'is_cached': is_cached,
'cost': cost,
'start_time': start_time,
'end_time': get_current_ts()
}
self.container.upsert_item(document)

def log_new_agent(self, agent: ConversableAgent, init_args: Dict[str, Any]) -> None:
document = {
'type': 'new_agent',
'session_id': self.session_id,
'agent_id': id(agent),
'agent_name': agent.name,
'init_args': to_dict(init_args),
'timestamp': get_current_ts()
}
self.container.upsert_item(document)

def log_new_wrapper(self, wrapper: OpenAIWrapper, init_args: Dict[str, Any]) -> None:
document = {
'type': 'new_wrapper',
'session_id': self.session_id,
'wrapper_id': id(wrapper),
'init_args': to_dict(init_args),
'timestamp': get_current_ts()
}
self.container.upsert_item(document)

def log_new_client(self, client: Any, wrapper: OpenAIWrapper, init_args: Dict[str, Any]) -> None:
document = {
'type': 'new_client',
'session_id': self.session_id,
'client_id': id(client),
'wrapper_id': id(wrapper),
'client_class': type(client).__name__,
'init_args': to_dict(init_args),
'timestamp': get_current_ts()
}
self.container.upsert_item(document)

def stop(self) -> None:
# Cosmos DB SDK handles connection disposal automatically.
pass

def get_connection(self) -> None:
# Cosmos DB connection management differs from SQLite and is handled by the SDK.
return None
Loading