Skip to content

Commit

Permalink
[NA] Filter log user feedback errors (#94)
Browse files Browse the repository at this point in the history
* Add error filtering to log_user_feedback, add option to use filter without a summary

* Fix lint errors
  • Loading branch information
alexkuzmik authored Nov 9, 2023
1 parent f8db13a commit 3877fe0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/comet_llm/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import logging
from typing import Optional

from . import experiment_info, logging_messages
from . import config, exceptions, experiment_info, logging_messages
from .experiment_api import ExperimentAPI

LOGGER = logging.getLogger(__name__)


@exceptions.filter(allow_raising=config.raising_enabled())
def log_user_feedback(id: str, score: float, api_key: Optional[str] = None) -> None:
"""
Logs user feedback for the provided Prompt or Chain ID. This will
Expand Down
9 changes: 6 additions & 3 deletions src/comet_llm/exceptions/filter_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import functools
import logging
from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any, Callable, Optional

from comet_llm import logging as comet_logging

Expand All @@ -24,14 +24,17 @@
LOGGER = logging.getLogger(__name__)


def filter(allow_raising: bool, summary: "summary.Summary") -> Callable:
def filter(
allow_raising: bool, summary: Optional["summary.Summary"] = None
) -> Callable:
def decorator(function: Callable) -> Callable:
@functools.wraps(function)
def wrapper(*args, **kwargs) -> Any: # type: ignore
try:
return function(*args, **kwargs)
except Exception as exception:
summary.increment_failed()
if summary is not None:
summary.increment_failed()

if allow_raising:
raise
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/exceptions/test_filter_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,18 @@ def f():
extra={"show_traceback": True}
)
assert f() is None


def test_filter__upraising_not_allowed__summary_not_passed_to_filter__function_raised_exception__nothing_done_with_summary():
@filter_decorator.filter(allow_raising=False)
def f():
raise exceptions.CometLLMException("some-message", log_message_once=True)
with Scenario() as s:
s.comet_logging.log_once_at_level(
filter_decorator.LOGGER,
logging.ERROR,
"some-message",
exc_info=True,
extra={"show_traceback": True}
)
assert f() is None

0 comments on commit 3877fe0

Please sign in to comment.