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

chore(weave): add weave.finish #1696

Merged
merged 7 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
18 changes: 17 additions & 1 deletion weave/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ def from_pandas(df):

#### Newer API below

_current_inited_client = None


def init(project_name: str) -> _weave_client.WeaveClient:
"""Initialize weave tracking, logging to a wandb project.
Expand All @@ -153,11 +155,13 @@ def init(project_name: str) -> _weave_client.WeaveClient:
Returns:
A Weave client.
"""
global _current_inited_client
# This is the stream-table backend. Disabling it in favor of the new
# trace-server backend.
# return _weave_init.init_wandb(project_name).client
# return _weave_init.init_trace_remote(project_name).client
return _weave_init.init_weave(project_name).client
_current_inited_client = _weave_init.init_weave(project_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non blocking comment: can you share why the api.py layer is responsible for handling the _current_inited_client state? It seems like this might want to be part of weave_init.py?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reasoning, it does make more sense to move that logic lower

return _current_inited_client.client


@contextlib.contextmanager
Expand Down Expand Up @@ -350,4 +354,16 @@ def run():
raise ValueError("Should not reach here")


def finish() -> None:
"""Stops logging to weave.

Following finish, calls of weave.op() decorated functions will no longer be logged. You will need to run weave.init() again to resume logging.

"""
global _current_inited_client
if _current_inited_client is not None:
_weave_init.finish(_current_inited_client)
_current_inited_client = None


__docspec__ = [init, publish, ref]
25 changes: 25 additions & 0 deletions weave/autopatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ def autopatch_openai() -> None:
patch()


def unpatch_openai() -> None:
try:
import openai # type: ignore
except ImportError:
pass
else:
if openai.__version__ < "1":
return
from weave.monitoring.openai import unpatch

unpatch()


def autopatch() -> None:
autopatch_openai()

Expand All @@ -31,3 +44,15 @@ def autopatch() -> None:
mistral_patcher.attempt_patch()
litellm_patcher.attempt_patch()
llamaindex_patcher.attempt_patch()


def reset_autopatch() -> None:
unpatch_openai()

from .integrations.mistral.mistral import mistral_patcher
from .integrations.litellm.litellm import litellm_patcher
from .integrations.llamaindex.llamaindex import llamaindex_patcher

mistral_patcher.undo_patch()
litellm_patcher.undo_patch()
llamaindex_patcher.undo_patch()
2 changes: 1 addition & 1 deletion weave/monitoring/openai/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _patch() -> None:

def unpatch() -> None:
# if _get_global_monitor() is not None:
info("Unpatching OpenAI completions")
# info("Unpatching OpenAI completions")
openai.resources.chat.completions.Completions.create = old_create
openai.resources.chat.completions.AsyncCompletions.create = old_async_create

Expand Down
6 changes: 6 additions & 0 deletions weave/weave_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,9 @@ def init_local() -> InitializedClient:
server.setup_tables()
client = weave_client.WeaveClient("none", "none", server)
return InitializedClient(client)


def finish(init_client: InitializedClient) -> None:
init_client.reset()
autopatch.reset_autopatch()
trace_sentry.global_trace_sentry.end_session()
Loading