Skip to content

Commit

Permalink
add add_tags context manager and with_tags decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzhe-log10 committed Sep 18, 2024
1 parent 2cbbbe4 commit 01cb17c
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions log10/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ def get_session_id():
#
session_id_var = contextvars.ContextVar("session_id", default=get_session_id())
last_completion_response_var = contextvars.ContextVar("last_completion_response", default=None)
# is this tags_var for log10_session?
# first how does this tags_var get set and passed around? what's the option to set it by users
tags_var = contextvars.ContextVar("tags", default=[])
extra_tags_var = contextvars.ContextVar("extra_tags", default=[])


def get_log10_session_tags():
Expand Down Expand Up @@ -166,6 +169,64 @@ def last_completion_id(self):
return response["completionID"]


@contextmanager
def add_tags(tags: list[str]):
"""
A context manager that adds tags to the current session.
This could be used with log10_session to add extra tags to the session.
Example:
>>> from log10.load import add_tags
>>> with add_tags(["tag1", "tag2"]):
>>> completion = client.chat.completions.create(
>>> model="gpt-4o",
>>> messages=[
>>> {
>>> "role": "user",
>>> "content": "Hello?",
>>> },
>>> ],
>>> )
>>> print(completion.choices[0].message)
"""
current_tags = tags_var.get()
new_tags = current_tags + tags
new_tags_token = tags_var.set(new_tags)
try:
yield
finally:
tags_var.reset(new_tags_token)


def with_tags(tags: list[str]):
"""
A decorator that adds tags to a function call.
Example:
>>> from log10.load import with_tags
>>> @with_tags(["decorator-tags", "decorator-tags-2"])
>>> def completion_with_tags():
>>> completion = client.chat.completions.create(
>>> model="gpt-4o",
>>> messages=[
>>> {
>>> "role": "user",
>>> "content": "Hello?",
>>> },
>>> ],
>>> )
>>> print(completion.choices[0].message)
"""

def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with add_tags(tags):
return func(*args, **kwargs)

return wrapper

return decorator


@contextmanager
def timed_block(block_name):
if DEBUG:
Expand Down

0 comments on commit 01cb17c

Please sign in to comment.