-
Notifications
You must be signed in to change notification settings - Fork 411
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): submit spans for streamed calls #10908
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -954,8 +954,6 @@ def _on_span_started(span: Span): | |
span.set_tag_str("langchain.request.inputs.%d.%s" % (idx, k), integration.trunc(str(v))) | ||
|
||
def _on_span_finished(span: Span, streamed_chunks): | ||
if span.error or not integration.is_pc_sampled_span(span): | ||
return | ||
if ( | ||
streamed_chunks | ||
and langchain_core | ||
|
@@ -970,6 +968,9 @@ def _on_span_finished(span: Span, streamed_chunks): | |
else: | ||
# best effort to join chunks together | ||
content = "".join([str(chunk) for chunk in streamed_chunks]) | ||
integration.llmobs_set_tags(span, args=args, kwargs=kwargs, response=content, operation="chain") | ||
if span.error or not integration.is_pc_sampled_span(span): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another clarification question here about this - do we move the check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we re-check |
||
return | ||
span.set_tag_str("langchain.response.outputs", integration.trunc(content)) | ||
|
||
return shared_stream( | ||
|
@@ -989,6 +990,7 @@ def _on_span_finished(span: Span, streamed_chunks): | |
def traced_chat_stream(langchain, pin, func, instance, args, kwargs): | ||
integration: LangChainIntegration = langchain._datadog_integration | ||
llm_provider = instance._llm_type | ||
model = _extract_model_name(instance) | ||
|
||
def _on_span_started(span: Span): | ||
if not integration.is_pc_sampled_span(span): | ||
|
@@ -1004,12 +1006,19 @@ def _on_span_started(span: Span): | |
span.set_tag_str("langchain.request.%s.parameters.%s.%s" % (llm_provider, param, k), str(v)) | ||
|
||
def _on_span_finished(span: Span, streamed_chunks): | ||
if span.error or not integration.is_pc_sampled_span(span): | ||
joined_chunks = streamed_chunks[0] | ||
for chunk in streamed_chunks[1:]: | ||
joined_chunks += chunk # base message types support __add__ for concatenation | ||
integration.llmobs_set_tags(span, args=args, kwargs=kwargs, response=joined_chunks, operation="chat") | ||
if ( | ||
span.error | ||
or not integration.is_pc_sampled_span(span) | ||
or streamed_chunks is None | ||
or len(streamed_chunks) == 0 | ||
): | ||
return | ||
content = "".join([str(getattr(chunk, "content", chunk)) for chunk in streamed_chunks]) | ||
role = ( | ||
streamed_chunks[0].__class__.__name__.replace("Chunk", "") if streamed_chunks else None | ||
) # AIMessageChunk --> AIeMessage | ||
content = str(getattr(joined_chunks, "content", joined_chunks)) | ||
role = joined_chunks.__class__.__name__.replace("Chunk", "") # AIMessageChunk --> AIMessage | ||
span.set_tag_str("langchain.response.content", integration.trunc(content)) | ||
if role: | ||
span.set_tag_str("langchain.response.message_type", role) | ||
|
@@ -1032,13 +1041,15 @@ def _on_span_finished(span: Span, streamed_chunks): | |
on_span_finished=_on_span_finished, | ||
api_key=_extract_api_key(instance), | ||
provider=llm_provider, | ||
model=model, | ||
) | ||
|
||
|
||
@with_traced_module | ||
def traced_llm_stream(langchain, pin, func, instance, args, kwargs): | ||
integration: LangChainIntegration = langchain._datadog_integration | ||
llm_provider = instance._llm_type | ||
model = _extract_model_name(instance) | ||
|
||
def _on_span_start(span: Span): | ||
if not integration.is_pc_sampled_span(span): | ||
|
@@ -1053,9 +1064,10 @@ def _on_span_start(span: Span): | |
span.set_tag_str("langchain.request.%s.parameters.%s.%s" % (llm_provider, param, k), str(v)) | ||
|
||
def _on_span_finished(span: Span, streamed_chunks): | ||
content = "".join([str(chunk) for chunk in streamed_chunks]) | ||
integration.llmobs_set_tags(span, args=args, kwargs=kwargs, response=content, operation="llm") | ||
if span.error or not integration.is_pc_sampled_span(span): | ||
return | ||
content = "".join([str(chunk) for chunk in streamed_chunks]) | ||
span.set_tag_str("langchain.response.content", integration.trunc(content)) | ||
|
||
return shared_stream( | ||
|
@@ -1070,6 +1082,7 @@ def _on_span_finished(span: Span, streamed_chunks): | |
on_span_finished=_on_span_finished, | ||
api_key=_extract_api_key(instance), | ||
provider=llm_provider, | ||
model=model, | ||
) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
features: | ||
- | | ||
LLM Observability: LangChain streamed calls (``llm.stream``, ``chat_model.stream``, and ``chain.stream``) submit to LLM Observability. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to set llm obs tags before we do the
integration.is_pc_sampled_span
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good question. it looks like we recently changed
llmobs_set_tags
to run independently ofis_pc_sampled_span
(example). followed the same logic here, but i think even before we did something likeis_pc_sampled_llmobs
, which I think always returnedTrue
.