|
9 | 9 | Any,
|
10 | 10 | Dict,
|
11 | 11 | Iterator,
|
| 12 | + List, |
12 | 13 | Literal,
|
13 | 14 | Optional,
|
14 | 15 | Tuple,
|
|
40 | 41 | TokenCount,
|
41 | 42 | Tool,
|
42 | 43 | )
|
| 44 | +from .helpers import safe_json_dumps |
43 | 45 |
|
44 | 46 | pydantic: Optional[ModuleType]
|
45 | 47 | try:
|
|
51 | 53 | from _typeshed import DataclassInstance
|
52 | 54 |
|
53 | 55 |
|
| 56 | +def get_context_attributes( |
| 57 | + *, |
| 58 | + session_id: Optional[str] = None, |
| 59 | + user_id: Optional[str] = None, |
| 60 | + metadata: Optional[Union[str, Dict[str, Any]]] = None, |
| 61 | + tags: Optional[List[str]] = None, |
| 62 | +) -> Dict[str, AttributeValue]: |
| 63 | + context_attributes: Dict[str, AttributeValue] = {} |
| 64 | + if session_id is not None: |
| 65 | + context_attributes.update(get_session_attributes(session_id=session_id)) |
| 66 | + if user_id is not None: |
| 67 | + context_attributes.update(get_user_id(user_id=user_id)) |
| 68 | + if metadata is not None: |
| 69 | + context_attributes.update(get_metadata_attributes(metadata=metadata)) |
| 70 | + if tags is not None: |
| 71 | + context_attributes.update(get_tag_attributes(tags=tags)) |
| 72 | + return context_attributes |
| 73 | + |
| 74 | + |
| 75 | +def get_session_attributes(*, session_id: str) -> Dict[str, AttributeValue]: |
| 76 | + return {SESSION_ID: session_id} |
| 77 | + |
| 78 | + |
| 79 | +def get_tag_attributes(*, tags: List[str]) -> Dict[str, AttributeValue]: |
| 80 | + return {TAG_TAGS: tags} |
| 81 | + |
| 82 | + |
| 83 | +def get_metadata_attributes(*, metadata: Union[str, Dict[str, Any]]) -> Dict[str, AttributeValue]: |
| 84 | + serialized_metadata: str |
| 85 | + if isinstance(metadata, str): |
| 86 | + serialized_metadata = metadata |
| 87 | + else: |
| 88 | + serialized_metadata = safe_json_dumps(metadata) |
| 89 | + return {METADATA: serialized_metadata} |
| 90 | + |
| 91 | + |
| 92 | +def get_user_id(*, user_id: str) -> Dict[str, AttributeValue]: |
| 93 | + return {USER_ID: user_id} |
| 94 | + |
| 95 | + |
54 | 96 | def get_span_kind_attributes(kind: "OpenInferenceSpanKind", /) -> Dict[str, AttributeValue]:
|
55 | 97 | normalized_kind = _normalize_openinference_span_kind(kind)
|
56 | 98 | return {
|
@@ -402,12 +444,16 @@ def get_llm_tool_attributes(
|
402 | 444 | LLM_TOKEN_COUNT_PROMPT = SpanAttributes.LLM_TOKEN_COUNT_PROMPT
|
403 | 445 | LLM_TOKEN_COUNT_TOTAL = SpanAttributes.LLM_TOKEN_COUNT_TOTAL
|
404 | 446 | LLM_TOOLS = SpanAttributes.LLM_TOOLS
|
| 447 | +METADATA = SpanAttributes.METADATA |
405 | 448 | OPENINFERENCE_SPAN_KIND = SpanAttributes.OPENINFERENCE_SPAN_KIND
|
406 | 449 | OUTPUT_MIME_TYPE = SpanAttributes.OUTPUT_MIME_TYPE
|
407 | 450 | OUTPUT_VALUE = SpanAttributes.OUTPUT_VALUE
|
| 451 | +SESSION_ID = SpanAttributes.SESSION_ID |
| 452 | +TAG_TAGS = SpanAttributes.TAG_TAGS |
408 | 453 | TOOL_DESCRIPTION = SpanAttributes.TOOL_DESCRIPTION
|
409 | 454 | TOOL_NAME = SpanAttributes.TOOL_NAME
|
410 | 455 | TOOL_PARAMETERS = SpanAttributes.TOOL_PARAMETERS
|
| 456 | +USER_ID = SpanAttributes.USER_ID |
411 | 457 |
|
412 | 458 |
|
413 | 459 | # tool attributes
|
|
0 commit comments