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(llmobs): llmobs dev mode #11219

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion ddtrace/llmobs/_llmobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ def enable(
api_key: Optional[str] = None,
env: Optional[str] = None,
service: Optional[str] = None,
dev_mode: bool = True,
_tracer: Optional[ddtrace.Tracer] = None,
) -> None:
"""
Enable LLM Observability tracing.
Enable LLMObs tracing.

:param str ml_app: The name of your ml application.
:param bool integrations_enabled: Set to `true` to enable LLM integrations.
Expand Down Expand Up @@ -288,6 +289,7 @@ def llm(
session_id: Optional[str] = None,
ml_app: Optional[str] = None,
) -> Span:
print("[✧ LLMObs] LLM ✨: {} running ...".format(name), flush=True)
"""
Trace an invocation call to an LLM where inputs and outputs are represented as text.

Expand Down Expand Up @@ -325,6 +327,7 @@ def tool(cls, name: Optional[str] = None, session_id: Optional[str] = None, ml_a

:returns: The Span object representing the traced operation.
"""
print("[✧ LLMObs] Tool 🔧: {} running ...".format(name), flush=True)
if cls.enabled is False:
log.warning(SPAN_START_WHILE_DISABLED_WARNING)
return cls._instance._start_span("tool", name=name, session_id=session_id, ml_app=ml_app)
Expand All @@ -341,12 +344,14 @@ def task(cls, name: Optional[str] = None, session_id: Optional[str] = None, ml_a

:returns: The Span object representing the traced operation.
"""
print("[✧ LLMObs] Task 📌: {} running...".format(name), flush=True)
if cls.enabled is False:
log.warning(SPAN_START_WHILE_DISABLED_WARNING)
return cls._instance._start_span("task", name=name, session_id=session_id, ml_app=ml_app)

@classmethod
def agent(cls, name: Optional[str] = None, session_id: Optional[str] = None, ml_app: Optional[str] = None) -> Span:
print("[✧ LLMObs] Agent 🤖: {} running ...".format(name), flush=True)
"""
Trace a dynamic workflow in which an embedded language model (agent) decides what sequence of actions to take.

Expand All @@ -365,6 +370,7 @@ def agent(cls, name: Optional[str] = None, session_id: Optional[str] = None, ml_
def workflow(
cls, name: Optional[str] = None, session_id: Optional[str] = None, ml_app: Optional[str] = None
) -> Span:
print("[✧ LLMObs] Workflow 🔗: {} running ...".format(name), flush=True)
"""
Trace a predefined or static sequence of operations.

Expand Down Expand Up @@ -422,6 +428,7 @@ def embedding(
def retrieval(
cls, name: Optional[str] = None, session_id: Optional[str] = None, ml_app: Optional[str] = None
) -> Span:
print("[✧ LLMObs] Retrieval 🔎: {} running ...".format(name), flush=True)
"""
Trace a vector search operation involving a list of documents being returned from an external knowledge base.

Expand Down
13 changes: 13 additions & 0 deletions ddtrace/llmobs/_trace_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def submit_llmobs_span(self, span: Span) -> None:
def _llmobs_span_event(self, span: Span) -> Dict[str, Any]:
"""Span event object structure."""
span_kind = span._meta.pop(SPAN_KIND)

meta: Dict[str, Any] = {"span.kind": span_kind, "input": {}, "output": {}}
if span_kind in ("llm", "embedding") and span.get_tag(MODEL_NAME) is not None:
meta["model_name"] = span._meta.pop(MODEL_NAME)
Expand Down Expand Up @@ -103,6 +104,18 @@ def _llmobs_span_event(self, span: Span) -> Dict[str, Any]:
span.set_tag_str(SESSION_ID, session_id)
parent_id = str(_get_llmobs_parent_id(span) or "undefined")
span._meta.pop(PARENT_ID_KEY, None)

if parent_id == "undefined":
url = """[✧ LLMObs] Trace with root span name "{span_name}" finished in {span_duration} seconds 🎉!

View your trace at:
https://dd.datad0g.com/llm/traces?query=%40ml_app%3Aai-chat
""".format(
span_name=span.name,
span_duration=span.duration,
)
print(url, flush=True)

return {
"trace_id": "{:x}".format(span.trace_id),
"span_id": str(span.span_id),
Expand Down
Loading