Skip to content

Commit

Permalink
Add Graph Definition tracking to our LangGraph integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothiraldan committed Dec 11, 2024
1 parent 9547626 commit e24bf19
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ You can use the [`OpikTracer`](https://www.comet.com/docs/opik/python-sdk-refere
```python
from opik.integrations.langchain import OpikTracer

opik_tracer = OpikTracer()

# create your LangGraph graph
graph = ...
app = graph.compile(...)

opik_tracer = OpikTracer(graph=app.get_graph(xray=True))

# Pass the OpikTracer callback to the graph.stream function
for s in graph.stream({"messages": [HumanMessage(content = QUESTION)]},
# Pass the OpikTracer callback to the Graph.stream function
for s in app.stream({"messages": [HumanMessage(content = QUESTION)]},
config={"callbacks": [opik_tracer]}):
print(s)

# Pass the OpikTracer callback to the graph.invoke function
result = graph.invoke({"messages": [HumanMessage(content = QUESTION)]},
# Pass the OpikTracer callback to the Graph.invoke function
result = app.invoke({"messages": [HumanMessage(content = QUESTION)]},
config={"callbacks": [opik_tracer]})
```

Expand Down
10 changes: 10 additions & 0 deletions sdks/python/src/opik/integrations/langchain/opik_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from uuid import UUID

from langchain_core.tracers.schemas import Run
from langchain_core.runnables.graph import Graph

LOGGER = logging.getLogger(__name__)

Expand All @@ -35,18 +36,27 @@ class OpikTracer(BaseTracer):
Args:
tags: List of tags to be applied to each trace logged by the tracer.
metadata: Additional metadata for each trace logged by the tracer.
graph: A LangGraph Graph object to track the Graph Definition in Opik.
project_name: The name of the project to log data.
"""

def __init__(
self,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
graph: Optional["Graph"] = None,
project_name: Optional[str] = None,
**kwargs: Any,
) -> None:
super().__init__(**kwargs)
self._trace_default_metadata = metadata if metadata is not None else {}

if graph:
self._trace_default_metadata["_opik_graph_definition"] = {
"format": "mermaid",
"data": graph.draw_mermaid(),
}

self._trace_default_tags = tags

self._span_data_map: Dict["UUID", span.SpanData] = {}
Expand Down

0 comments on commit e24bf19

Please # to comment.