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

feat(integrations): Add WandbTracer #4521

Merged
merged 32 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8193bbd
feat(integrations): add WandbTracer with ENV var and ctx manager
May 11, 2023
92bff78
refactor(integrations): remove old wandb_callback and docs
May 11, 2023
09ff9fc
chore: fix linting, formatting and typing errors
May 11, 2023
103658b
docs: update and fix agent wandb tracing example notebook
May 11, 2023
dcdbdff
merge: pull changes from upstream main into branch
May 11, 2023
1b9691c
fix: differentiate between chains and agents using serialized name
May 12, 2023
a4ec628
Merge branch 'master' of github.com:parambharat/langchain into wandb/…
May 12, 2023
1f8467b
chore: add wandb to extra requirements for tracer
May 12, 2023
9a30e71
Merge branch 'master' of github.com:parambharat/langchain into wandb/…
May 16, 2023
967df79
refactor: add back wandb tracking
May 16, 2023
4f6157f
refactor: add back wandb tracking
May 16, 2023
149b273
refactor: add back wandb tracking
May 16, 2023
35f8efc
fix: add eol to wandb_tracking docs
parambharat May 16, 2023
ffc9a9d
Merge branch 'master' of github.com:parambharat/langchain into wandb/…
May 16, 2023
8af3ea1
Merge branch 'master' of github.com:parambharat/langchain into wandb/…
May 17, 2023
a62f27f
fix: update the WandbTracer to work with the new tracer refactor
May 17, 2023
37ec3bf
tests: add integration tests and fix lint and format errors
May 17, 2023
2c16499
tests: add integration tests and fix lint and format errors
May 17, 2023
4da0716
chore: update the wandb_tracking.ipynb with reference to the WandbTracer
May 17, 2023
5f6d5e4
fix: address review comments about tests, docs and other components.
May 18, 2023
cda7dc4
fix: update wandb tracing example notebook
May 18, 2023
c459297
Merge branch 'master' of github.com:parambharat/langchain into wandb/…
May 18, 2023
a28e7f2
fix: defer try-except handling to manager instead
May 19, 2023
e6a03f8
Merge branch 'master' of github.com:parambharat/langchain into wandb/…
May 19, 2023
1735b17
Merge branch 'master' of github.com:parambharat/langchain into wandb/…
May 24, 2023
d867b58
fix: address all lint and formating errors.
May 24, 2023
b814975
fix: setting import for annotation
May 24, 2023
a7ae656
Merge branch 'master' of https://github.com/parambharat/langchain int…
parambharat May 30, 2023
22b807e
merge
dev2049 Jun 1, 2023
d103056
cr
dev2049 Jun 1, 2023
b65079f
Merge branch 'master' into wandb/tracer
dev2049 Jun 1, 2023
8ec3a7b
cr
dev2049 Jun 1, 2023
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
238 changes: 238 additions & 0 deletions docs/integrations/agent_with_wandb_tracing.ipynb

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions docs/integrations/wandb_tracking.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"\n",
"Run in Colab: https://colab.research.google.com/drive/1DXH4beT4HFaRKy_Vm4PoxhXVDRf7Ym8L?usp=sharing\n",
"\n",
"View Report: https://wandb.ai/a-sh0ts/langchain_callback_demo/reports/Prompt-Engineering-LLMs-with-LangChain-and-W-B--VmlldzozNjk1NTUw#👋-how-to-build-a-callback-in-langchain-for-better-prompt-engineering"
"View Report: https://wandb.ai/a-sh0ts/langchain_callback_demo/reports/Prompt-Engineering-LLMs-with-LangChain-and-W-B--VmlldzozNjk1NTUw#👋-how-to-build-a-callback-in-langchain-for-better-prompt-engineering\n",
"\n",
"**Note**: _the `WandbCallbackHandler` is being deprecated in favour of the `WandbTracer`_ . In future please use the `WandbTracer` as it is more flexible and allows for more granular logging. To know more about the `WandbTracer` refer to the agent_with_wandb_tracing.ipynb notebook in docs or use the following [colab](https://colab.research.google.com/drive/1pY13ym8ENEZ8Fh7nA99ILk2GcdUQu0jR?usp=sharing)."
]
},
{
Expand Down Expand Up @@ -107,7 +109,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33mharrison-chase\u001b[0m. Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n"
"\u001B[34m\u001B[1mwandb\u001B[0m: Currently logged in as: \u001B[33mharrison-chase\u001B[0m. Use \u001B[1m`wandb login --relogin`\u001B[0m to force relogin\n"
]
},
{
Expand Down Expand Up @@ -174,7 +176,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m The wandb callback is currently in beta and is subject to change based on updates to `langchain`. Please report any issues to https://github.com/wandb/wandb/issues with the tag `langchain`.\n"
"\u001B[34m\u001B[1mwandb\u001B[0m: \u001B[33mWARNING\u001B[0m The wandb callback is currently in beta and is subject to change based on updates to `langchain`. Please report any issues to https://github.com/wandb/wandb/issues with the tag `langchain`.\n"
]
}
],
Expand Down Expand Up @@ -521,20 +523,20 @@
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.\n",
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
"\u001B[32;1m\u001B[1;3m I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.\n",
"Action: Search\n",
"Action Input: \"Leo DiCaprio girlfriend\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mDiCaprio had a steady girlfriend in Camila Morrone. He had been with the model turned actress for nearly five years, as they were first said to be dating at the end of 2017. And the now 26-year-old Morrone is no stranger to Hollywood.\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I need to calculate her age raised to the 0.43 power.\n",
"Action Input: \"Leo DiCaprio girlfriend\"\u001B[0m\n",
"Observation: \u001B[36;1m\u001B[1;3mDiCaprio had a steady girlfriend in Camila Morrone. He had been with the model turned actress for nearly five years, as they were first said to be dating at the end of 2017. And the now 26-year-old Morrone is no stranger to Hollywood.\u001B[0m\n",
"Thought:\u001B[32;1m\u001B[1;3m I need to calculate her age raised to the 0.43 power.\n",
"Action: Calculator\n",
"Action Input: 26^0.43\u001b[0m\n",
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 4.059182145592686\n",
"\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
"Final Answer: Leo DiCaprio's girlfriend is Camila Morrone and her current age raised to the 0.43 power is 4.059182145592686.\u001b[0m\n",
"Action Input: 26^0.43\u001B[0m\n",
"Observation: \u001B[33;1m\u001B[1;3mAnswer: 4.059182145592686\n",
"\u001B[0m\n",
"Thought:\u001B[32;1m\u001B[1;3m I now know the final answer.\n",
"Final Answer: Leo DiCaprio's girlfriend is Camila Morrone and her current age raised to the 0.43 power is 4.059182145592686.\u001B[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
"\u001B[1m> Finished chain.\u001B[0m\n"
]
},
{
Expand Down
2 changes: 2 additions & 0 deletions langchain/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from langchain.callbacks.manager import (
get_openai_callback,
tracing_enabled,
wandb_tracing_enabled,
)
from langchain.callbacks.mlflow_callback import MlflowCallbackHandler
from langchain.callbacks.openai_info import OpenAICallbackHandler
Expand All @@ -26,4 +27,5 @@
"AsyncIteratorCallbackHandler",
"get_openai_callback",
"tracing_enabled",
"wandb_tracing_enabled",
]
32 changes: 32 additions & 0 deletions langchain/callbacks/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from langchain.callbacks.tracers.langchain_v1 import LangChainTracerV1, TracerSessionV1
from langchain.callbacks.tracers.schemas import TracerSession
from langchain.callbacks.tracers.stdout import ConsoleCallbackHandler
from langchain.callbacks.tracers.wandb import WandbTracer
from langchain.schema import (
AgentAction,
AgentFinish,
Expand All @@ -44,6 +45,12 @@
] = ContextVar( # noqa: E501
"tracing_callback", default=None
)
wandb_tracing_callback_var: ContextVar[
Optional[WandbTracer]
] = ContextVar( # noqa: E501
"tracing_wandb_callback", default=None
)

tracing_v2_callback_var: ContextVar[
Optional[LangChainTracer]
] = ContextVar( # noqa: E501
Expand Down Expand Up @@ -76,6 +83,17 @@ def tracing_enabled(
tracing_callback_var.set(None)


@contextmanager
def wandb_tracing_enabled(
session_name: str = "default",
) -> Generator[None, None, None]:
"""Get WandbTracer in a context manager."""
cb = WandbTracer()
wandb_tracing_callback_var.set(cb)
yield None
wandb_tracing_callback_var.set(None)


@contextmanager
def tracing_v2_enabled(
session_name: Optional[str] = None,
Expand Down Expand Up @@ -831,12 +849,17 @@ def _configure(
callback_manager.add_handler(handler, False)

tracer = tracing_callback_var.get()
wandb_tracer = wandb_tracing_callback_var.get()
open_ai = openai_callback_var.get()
tracing_enabled_ = (
os.environ.get("LANGCHAIN_TRACING") is not None
or tracer is not None
or os.environ.get("LANGCHAIN_HANDLER") is not None
)
wandb_tracing_enabled_ = (
os.environ.get("LANGCHAIN_WANDB_TRACING") is not None
or wandb_tracer is not None
)

tracer_v2 = tracing_v2_callback_var.get()
tracing_v2_enabled_ = (
Expand All @@ -851,6 +874,7 @@ def _configure(
or debug
or tracing_enabled_
or tracing_v2_enabled_
or wandb_tracing_enabled_
or open_ai is not None
):
if verbose and not any(
Expand All @@ -876,6 +900,14 @@ def _configure(
handler = LangChainTracerV1()
handler.load_session(tracer_session)
callback_manager.add_handler(handler, True)
if wandb_tracing_enabled_ and not any(
isinstance(handler, WandbTracer) for handler in callback_manager.handlers
):
if wandb_tracer:
callback_manager.add_handler(wandb_tracer, True)
else:
handler = WandbTracer()
callback_manager.add_handler(handler, True)
if tracing_v2_enabled_ and not any(
isinstance(handler, LangChainTracer)
for handler in callback_manager.handlers
Expand Down
8 changes: 7 additions & 1 deletion langchain/callbacks/tracers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
from langchain.callbacks.tracers.langchain import LangChainTracer
from langchain.callbacks.tracers.langchain_v1 import LangChainTracerV1
from langchain.callbacks.tracers.stdout import ConsoleCallbackHandler
from langchain.callbacks.tracers.wandb import WandbTracer

__all__ = ["LangChainTracer", "LangChainTracerV1", "ConsoleCallbackHandler"]
__all__ = [
"LangChainTracer",
"LangChainTracerV1",
"ConsoleCallbackHandler",
"WandbTracer",
]
Loading