-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add config variables for offline mode * Add message processing * Fix lint errors * Fix lint errors, change some names * Refactor names, add message_processing namespace * Move prompt sending logic to online_senders * Move sending logic for chains to online_senders.chain * Fix some lint errors * Update message processing implementation, split the logic and refactor unit tests * Fix some lint errors * Remove odd files from another branch that were added here accidentally * Fix lint errors * Add draft version of offline_message_processor (without file rotations) * Fix lint errors, add file rotation logic * Refactor message processing logic, add unit tests * Exclude api_key from serialized messages, rename experiment_information to experiment_info or experiment_info_ * Add test for message to_dict and from_dict * Add directory creation if it doesnt exist, add versions to messages, fix tests * Don't raise errors if offline mode enabled * Fix lint errors * Enable error raising in test workflows, fix few tests * Add new test for offline message processor * Rename test * Add random numbers to the end of the file * Config renamings
- Loading branch information
1 parent
4ca66ba
commit d4c7409
Showing
29 changed files
with
858 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
name: Sanity Tests Comet LLM | ||
env: | ||
COMET_RAISE_EXCEPTIONS_ON_ERROR: "1" | ||
on: | ||
pull_request: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
name: Unit Tests Comet LLM | ||
env: | ||
COMET_RAISE_EXCEPTIONS_ON_ERROR: "1" | ||
on: | ||
pull_request: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
# ******************************************************* | ||
# ____ _ _ | ||
# / ___|___ _ __ ___ ___| |_ _ __ ___ | | | ||
# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | | ||
# | |__| (_) | | | | | | __/ |_ _| | | | | | | | ||
# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| | ||
# | ||
# Sign up for free at https://www.comet.com | ||
# Copyright (C) 2015-2024 Comet ML INC | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this package. | ||
# ******************************************************* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
# ******************************************************* | ||
# ____ _ _ | ||
# / ___|___ _ __ ___ ___| |_ _ __ ___ | | | ||
# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | | ||
# | |__| (_) | | | | | | __/ |_ _| | | | | | | | ||
# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| | ||
# | ||
# Sign up for free at https://www.comet.com | ||
# Copyright (C) 2015-2024 Comet ML INC | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this package. | ||
# ******************************************************* | ||
|
||
from typing import Union | ||
|
||
from .. import config | ||
from . import offline_message_processor, online_message_processor | ||
|
||
MESSAGE_PROCESSOR: Union[ | ||
offline_message_processor.OfflineMessageProcessor, | ||
online_message_processor.OnlineMessageProcessor, | ||
] | ||
|
||
if config.offline_enabled(): | ||
MESSAGE_PROCESSOR = offline_message_processor.OfflineMessageProcessor( | ||
offline_directory=config.offline_directory(), | ||
file_usage_duration=config.offline_batch_duration_seconds(), | ||
) | ||
else: | ||
MESSAGE_PROCESSOR = online_message_processor.OnlineMessageProcessor() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# -*- coding: utf-8 -*- | ||
# ******************************************************* | ||
# ____ _ _ | ||
# / ___|___ _ __ ___ ___| |_ _ __ ___ | | | ||
# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | | ||
# | |__| (_) | | | | | | __/ |_ _| | | | | | | | ||
# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| | ||
# | ||
# Sign up for free at https://www.comet.com | ||
# Copyright (C) 2015-2023 Comet ML INC | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this package. | ||
# ******************************************************* | ||
|
||
import dataclasses | ||
import inspect | ||
from typing import Any, ClassVar, Dict, List, Optional, Union | ||
|
||
from comet_llm.types import JSONEncodable | ||
|
||
from .. import experiment_info, logging_messages | ||
|
||
|
||
@dataclasses.dataclass | ||
class BaseMessage: | ||
experiment_info_: experiment_info.ExperimentInfo | ||
VERSION: ClassVar[int] | ||
|
||
@classmethod | ||
def from_dict( | ||
cls, d: Dict[str, Any], api_key: Optional[str] = None | ||
) -> "BaseMessage": | ||
d.pop("VERSION") # | ||
|
||
experiment_info_dict: Dict[str, Optional[str]] = d.pop("experiment_info_") | ||
experiment_info_ = experiment_info.get( | ||
**experiment_info_dict, | ||
api_key=api_key, | ||
api_key_not_found_message=logging_messages.API_KEY_NOT_CONFIGURED | ||
) | ||
|
||
return cls(experiment_info_=experiment_info_, **d) | ||
|
||
def to_dict(self) -> Dict[str, Any]: | ||
result = dataclasses.asdict(self) | ||
|
||
del result["experiment_info_"]["api_key"] | ||
result["VERSION"] = self.VERSION | ||
|
||
return result | ||
|
||
|
||
@dataclasses.dataclass | ||
class PromptMessage(BaseMessage): | ||
prompt_asset_data: Dict[str, Any] | ||
duration: Optional[float] | ||
metadata: Optional[Dict[str, Union[str, bool, float, None]]] | ||
tags: Optional[List[str]] | ||
|
||
VERSION: ClassVar[int] = 1 | ||
|
||
|
||
@dataclasses.dataclass | ||
class ChainMessage(BaseMessage): | ||
chain_data: Dict[str, JSONEncodable] | ||
duration: float | ||
tags: Optional[List[str]] | ||
metadata: Optional[Dict[str, JSONEncodable]] | ||
others: Dict[str, JSONEncodable] | ||
# 'other' - is a name of an attribute of experiment, logged via log_other | ||
|
||
VERSION: ClassVar[int] = 1 |
71 changes: 71 additions & 0 deletions
71
src/comet_llm/message_processing/offline_message_processor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# -*- coding: utf-8 -*- | ||
# ******************************************************* | ||
# ____ _ _ | ||
# / ___|___ _ __ ___ ___| |_ _ __ ___ | | | ||
# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | | ||
# | |__| (_) | | | | | | __/ |_ _| | | | | | | | ||
# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| | ||
# | ||
# Sign up for free at https://www.comet.com | ||
# Copyright (C) 2015-2024 Comet ML INC | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this package. | ||
# ******************************************************* | ||
|
||
import logging | ||
import os | ||
import pathlib | ||
import random | ||
import threading | ||
import time | ||
from typing import Optional | ||
|
||
from . import messages | ||
from .offline_senders import chain, prompt | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class OfflineMessageProcessor: | ||
def __init__(self, offline_directory: str, file_usage_duration: float) -> None: | ||
self._offline_directory = offline_directory | ||
self._batch_duration_seconds = file_usage_duration | ||
self._lock = threading.Lock() | ||
|
||
self._current_file_started_at: Optional[float] = None | ||
self._current_file_name: Optional[str] = None | ||
|
||
os.makedirs(self._offline_directory, exist_ok=True) | ||
|
||
def process(self, message: messages.BaseMessage) -> None: | ||
with self._lock: | ||
self._update_current_file_if_needed() | ||
assert self._current_file_name is not None | ||
file_path = pathlib.Path(self._offline_directory, self._current_file_name) | ||
|
||
if isinstance(message, messages.PromptMessage): | ||
try: | ||
return prompt.send(message, str(file_path)) | ||
except Exception: | ||
LOGGER.error("Failed to log prompt", exc_info=True) | ||
elif isinstance(message, messages.ChainMessage): | ||
try: | ||
return chain.send(message, str(file_path)) | ||
except Exception: | ||
LOGGER.error("Failed to log chain", exc_info=True) | ||
|
||
LOGGER.debug(f"Unsupported message type {message}") | ||
return None | ||
|
||
def _update_current_file_if_needed(self) -> None: | ||
current_time = time.time() | ||
|
||
if ( | ||
self._current_file_started_at is None | ||
or (current_time - self._current_file_started_at) | ||
>= self._batch_duration_seconds | ||
): | ||
self._current_file_started_at = current_time | ||
self._current_file_name = ( | ||
f"messages_{current_time}_{random.randint(1111,9999)}.jsonl" | ||
) |
13 changes: 13 additions & 0 deletions
13
src/comet_llm/message_processing/offline_senders/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
# ******************************************************* | ||
# ____ _ _ | ||
# / ___|___ _ __ ___ ___| |_ _ __ ___ | | | ||
# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | | ||
# | |__| (_) | | | | | | __/ |_ _| | | | | | | | ||
# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| | ||
# | ||
# Sign up for free at https://www.comet.com | ||
# Copyright (C) 2015-2024 Comet ML INC | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this package. | ||
# ******************************************************* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
# ******************************************************* | ||
# ____ _ _ | ||
# / ___|___ _ __ ___ ___| |_ _ __ ___ | | | ||
# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | | ||
# | |__| (_) | | | | | | __/ |_ _| | | | | | | | ||
# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| | ||
# | ||
# Sign up for free at https://www.comet.com | ||
# Copyright (C) 2015-2024 Comet ML INC | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this package. | ||
# ******************************************************* | ||
|
||
import json | ||
import pathlib | ||
|
||
from .. import messages | ||
|
||
|
||
def send(message: messages.ChainMessage, file_name: str) -> None: | ||
to_dump = {"type": "ChainMessage", "message": message.to_dict()} | ||
with open(file_name, mode="at", encoding="utf-8") as out_stream: | ||
out_stream.write(json.dumps(to_dump) + "\n") | ||
|
||
return None |
Oops, something went wrong.