20
20
from typing_extensions import TypeGuard
21
21
22
22
from openinference .semconv .trace import (
23
+ DocumentAttributes ,
23
24
EmbeddingAttributes ,
24
25
ImageAttributes ,
25
26
MessageAttributes ,
34
35
)
35
36
36
37
from ._types import (
38
+ Document ,
37
39
Embedding ,
38
40
Message ,
39
41
OpenInferenceLLMProvider ,
55
57
from _typeshed import DataclassInstance
56
58
57
59
60
+ def get_retriever_attributes (* , documents : List [Document ]) -> Dict [str , AttributeValue ]:
61
+ attributes : Dict [str , AttributeValue ] = {}
62
+ for index , document in enumerate (documents ):
63
+ if not isinstance (document , dict ):
64
+ continue
65
+ if (content := document .get ("content" )) is not None :
66
+ key = f"{ RETRIEVAL_DOCUMENTS } .{ index } .{ DOCUMENT_CONTENT } "
67
+ attributes [key ] = content
68
+ if (document_id := document .get ("id" )) is not None :
69
+ key = f"{ RETRIEVAL_DOCUMENTS } .{ index } .{ DOCUMENT_ID } "
70
+ attributes [key ] = document_id
71
+ if (metadata := document .get ("metadata" )) is not None :
72
+ key = f"{ RETRIEVAL_DOCUMENTS } .{ index } .{ DOCUMENT_METADATA } "
73
+ serialized_metadata : str
74
+ if isinstance (metadata , str ):
75
+ serialized_metadata = metadata
76
+ else :
77
+ serialized_metadata = safe_json_dumps (metadata )
78
+ attributes [key ] = serialized_metadata
79
+ if (score := document .get ("score" )) is not None :
80
+ key = f"{ RETRIEVAL_DOCUMENTS } .{ index } .{ DOCUMENT_SCORE } "
81
+ attributes [key ] = score
82
+ return attributes
83
+
84
+
58
85
def get_embedding_attributes (
59
86
* ,
60
87
model_name : Optional [str ] = None ,
61
88
embeddings : Optional [List [Embedding ]] = None ,
62
- ) -> Dict [str , Any ]:
63
- embedding_attributes : Dict [str , AttributeValue ] = {}
89
+ ) -> Dict [str , AttributeValue ]:
90
+ attributes : Dict [str , AttributeValue ] = {}
64
91
if model_name is not None :
65
- embedding_attributes [EMBEDDING_MODEL_NAME ] = model_name
92
+ attributes [EMBEDDING_MODEL_NAME ] = model_name
66
93
if isinstance (embeddings , list ):
67
94
for index , embedding in enumerate (embeddings ):
68
95
if (text := embedding .get ("text" )) is not None :
69
96
key = f"{ EMBEDDING_EMBEDDINGS } .{ index } .{ EMBEDDING_TEXT } "
70
- embedding_attributes [key ] = text
97
+ attributes [key ] = text
71
98
if (vector := embedding .get ("vector" )) is not None :
72
99
key = f"{ EMBEDDING_EMBEDDINGS } .{ index } .{ EMBEDDING_VECTOR } "
73
- embedding_attributes [key ] = vector
74
- return embedding_attributes
100
+ attributes [key ] = vector
101
+ return attributes
75
102
76
103
77
104
def get_context_attributes (
@@ -81,16 +108,16 @@ def get_context_attributes(
81
108
metadata : Optional [Union [str , Dict [str , Any ]]] = None ,
82
109
tags : Optional [List [str ]] = None ,
83
110
) -> Dict [str , AttributeValue ]:
84
- context_attributes : Dict [str , AttributeValue ] = {}
111
+ attributes : Dict [str , AttributeValue ] = {}
85
112
if session_id is not None :
86
- context_attributes .update (get_session_attributes (session_id = session_id ))
113
+ attributes .update (get_session_attributes (session_id = session_id ))
87
114
if user_id is not None :
88
- context_attributes .update (get_user_id (user_id = user_id ))
115
+ attributes .update (get_user_id (user_id = user_id ))
89
116
if metadata is not None :
90
- context_attributes .update (get_metadata_attributes (metadata = metadata ))
117
+ attributes .update (get_metadata_attributes (metadata = metadata ))
91
118
if tags is not None :
92
- context_attributes .update (get_tag_attributes (tags = tags ))
93
- return context_attributes
119
+ attributes .update (get_tag_attributes (tags = tags ))
120
+ return attributes
94
121
95
122
96
123
def get_session_attributes (* , session_id : str ) -> Dict [str , AttributeValue ]:
@@ -434,6 +461,12 @@ def get_llm_tool_attributes(
434
461
return attributes
435
462
436
463
464
+ # document attributes
465
+ DOCUMENT_CONTENT = DocumentAttributes .DOCUMENT_CONTENT
466
+ DOCUMENT_ID = DocumentAttributes .DOCUMENT_ID
467
+ DOCUMENT_METADATA = DocumentAttributes .DOCUMENT_METADATA
468
+ DOCUMENT_SCORE = DocumentAttributes .DOCUMENT_SCORE
469
+
437
470
# embedding attributes
438
471
EMBEDDING_TEXT = EmbeddingAttributes .EMBEDDING_TEXT
439
472
EMBEDDING_VECTOR = EmbeddingAttributes .EMBEDDING_VECTOR
@@ -475,6 +508,7 @@ def get_llm_tool_attributes(
475
508
OPENINFERENCE_SPAN_KIND = SpanAttributes .OPENINFERENCE_SPAN_KIND
476
509
OUTPUT_MIME_TYPE = SpanAttributes .OUTPUT_MIME_TYPE
477
510
OUTPUT_VALUE = SpanAttributes .OUTPUT_VALUE
511
+ RETRIEVAL_DOCUMENTS = SpanAttributes .RETRIEVAL_DOCUMENTS
478
512
SESSION_ID = SpanAttributes .SESSION_ID
479
513
TAG_TAGS = SpanAttributes .TAG_TAGS
480
514
TOOL_DESCRIPTION = SpanAttributes .TOOL_DESCRIPTION
0 commit comments