Skip to content

Commit

Permalink
feat: Instrumentation reads session id from metadata (#446)
Browse files Browse the repository at this point in the history
Co-authored-by: Xander Song <axiomofjoy@gmail.com>
  • Loading branch information
fjcasti1 and axiomofjoy committed May 10, 2024
1 parent 108c8bd commit 5490f68
Show file tree
Hide file tree
Showing 5 changed files with 523 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
"key-2": "val-2",
},
},
prompt_template="Who won the soccer match in {city} on {date}",
prompt_template_version="v1.0",
prompt_template_variables={
"city": "Johannesburg",
"date": "July 11th",
},
tags=["tag-1", "tag-2"],
):
prompt_template = "Tell me a {adjective} joke"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
},
},
tags=["tag-1", "tag-2"],
prompt_template="Who won the soccer match in {city} on {date}",
prompt_template_version="v1.0",
prompt_template_variables={
"city": "Johannesburg",
"date": "July 11th",
},
):
for chunk in ChatOpenAI(model_name="gpt-3.5-turbo").stream("Write a haiku."):
print(chunk.content, end="", flush=True)
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = [
"opentelemetry-api",
"opentelemetry-instrumentation",
"opentelemetry-semantic-conventions",
"openinference-instrumentation>=0.1.3",
"openinference-instrumentation>=0.1.5",
"openinference-semantic-conventions",
"wrapt",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def _update_span(span: trace_api.Span, run: Run) -> None:
else _langchain_run_type_to_span_kind(run.run_type)
)
span.set_attribute(OPENINFERENCE_SPAN_KIND, span_kind.value)
span.set_attributes(dict(get_attributes_from_context()))
span.set_attributes(
dict(
_flatten(
Expand All @@ -213,7 +214,6 @@ def _update_span(span: trace_api.Span, run: Run) -> None:
)
)
)
span.set_attributes(dict(get_attributes_from_context()))


def _langchain_run_type_to_span_kind(run_type: str) -> OpenInferenceSpanKindValues:
Expand Down Expand Up @@ -569,6 +569,12 @@ def _metadata(run: Run) -> Iterator[Tuple[str, str]]:
if not run.extra or not (metadata := run.extra.get("metadata")):
return
assert isinstance(metadata, Mapping), f"expected Mapping, found {type(metadata)}"
if session_id := (
metadata.get(LANGCHAIN_SESSION_ID)
or metadata.get(LANGCHAIN_CONVERSATION_ID)
or metadata.get(LANGCHAIN_THREAD_ID)
):
yield SESSION_ID, session_id
yield METADATA, json.dumps(metadata)


Expand Down Expand Up @@ -599,6 +605,10 @@ def _as_utc_nano(dt: datetime) -> int:
return int(dt.astimezone(timezone.utc).timestamp() * 1_000_000_000)


LANGCHAIN_SESSION_ID = "session_id"
LANGCHAIN_CONVERSATION_ID = "conversation_id"
LANGCHAIN_THREAD_ID = "thread_id"

DOCUMENT_CONTENT = DocumentAttributes.DOCUMENT_CONTENT
DOCUMENT_ID = DocumentAttributes.DOCUMENT_ID
DOCUMENT_METADATA = DocumentAttributes.DOCUMENT_METADATA
Expand Down Expand Up @@ -641,3 +651,4 @@ def _as_utc_nano(dt: datetime) -> int:
TOOL_NAME = SpanAttributes.TOOL_NAME
TOOL_PARAMETERS = SpanAttributes.TOOL_PARAMETERS
METADATA = SpanAttributes.METADATA
SESSION_ID = SpanAttributes.SESSION_ID

0 comments on commit 5490f68

Please sign in to comment.