Skip to content

Commit bc3e5b6

Browse files
committed
rerankers
1 parent 0c78e2a commit bc3e5b6

File tree

1 file changed

+76
-23
lines changed
  • python/openinference-instrumentation/src/openinference/instrumentation

1 file changed

+76
-23
lines changed

python/openinference-instrumentation/src/openinference/instrumentation/_attributes.py

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
OpenInferenceLLMSystemValues,
3030
OpenInferenceMimeTypeValues,
3131
OpenInferenceSpanKindValues,
32+
RerankerAttributes,
3233
SpanAttributes,
3334
ToolAttributes,
3435
ToolCallAttributes,
@@ -57,31 +58,81 @@
5758
from _typeshed import DataclassInstance
5859

5960

61+
def get_reranker_attributes(
62+
*,
63+
query: Optional[str] = None,
64+
model_name: Optional[str] = None,
65+
input_documents: Optional[List[Document]] = None,
66+
output_documents: Optional[List[Document]] = None,
67+
top_k: Optional[int] = None,
68+
) -> Dict[str, AttributeValue]:
69+
attributes: Dict[str, AttributeValue] = {}
70+
if query is not None:
71+
attributes[RERANKER_QUERY] = query
72+
if model_name is not None:
73+
attributes[RERANKER_MODEL_NAME] = model_name
74+
if top_k is not None:
75+
attributes[RERANKER_TOP_K] = top_k
76+
if isinstance(input_documents, list):
77+
for index, document in enumerate(input_documents):
78+
attributes.update(
79+
_document_attributes(
80+
document=document,
81+
document_index=index,
82+
key_prefix=RERANKER_INPUT_DOCUMENTS,
83+
)
84+
)
85+
if isinstance(output_documents, list):
86+
for index, document in enumerate(output_documents):
87+
attributes.update(
88+
_document_attributes(
89+
document=document,
90+
document_index=index,
91+
key_prefix=RERANKER_OUTPUT_DOCUMENTS,
92+
)
93+
)
94+
return attributes
95+
96+
6097
def get_retriever_attributes(*, documents: List[Document]) -> Dict[str, AttributeValue]:
6198
attributes: Dict[str, AttributeValue] = {}
99+
if not isinstance(documents, list):
100+
return attributes
62101
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
102+
attributes.update(
103+
_document_attributes(
104+
document=document,
105+
document_index=index,
106+
key_prefix=RETRIEVAL_DOCUMENTS,
107+
)
108+
)
82109
return attributes
83110

84111

112+
def _document_attributes(
113+
*,
114+
document: Document,
115+
document_index: str,
116+
key_prefix: str,
117+
) -> Iterator[Tuple[str, AttributeValue]]:
118+
if not isinstance(document, dict):
119+
return
120+
if (content := document.get("content")) is not None:
121+
yield f"{key_prefix}.{document_index}.{DOCUMENT_CONTENT}", content
122+
if (document_id := document.get("id")) is not None:
123+
yield f"{key_prefix}.{document_index}.{DOCUMENT_ID}", document_id
124+
if (metadata := document.get("metadata")) is not None:
125+
key = f"{key_prefix}.{document_index}.{DOCUMENT_METADATA}"
126+
serialized_metadata: str
127+
if isinstance(metadata, str):
128+
serialized_metadata = metadata
129+
else:
130+
serialized_metadata = safe_json_dumps(metadata)
131+
yield key, serialized_metadata
132+
if (score := document.get("score")) is not None:
133+
return f"{key_prefix}.{document_index}.{DOCUMENT_SCORE}", score
134+
135+
85136
def get_embedding_attributes(
86137
*,
87138
model_name: Optional[str] = None,
@@ -474,20 +525,24 @@ def get_llm_tool_attributes(
474525
# image attributes
475526
IMAGE_URL = ImageAttributes.IMAGE_URL
476527

477-
478528
# message attributes
479529
MESSAGE_CONTENT = MessageAttributes.MESSAGE_CONTENT
480530
MESSAGE_CONTENTS = MessageAttributes.MESSAGE_CONTENTS
481531
MESSAGE_ROLE = MessageAttributes.MESSAGE_ROLE
482532
MESSAGE_TOOL_CALL_ID = MessageAttributes.MESSAGE_TOOL_CALL_ID
483533
MESSAGE_TOOL_CALLS = MessageAttributes.MESSAGE_TOOL_CALLS
484534

485-
486535
# message content attributes
487536
MESSAGE_CONTENT_IMAGE = MessageContentAttributes.MESSAGE_CONTENT_IMAGE
488537
MESSAGE_CONTENT_TEXT = MessageContentAttributes.MESSAGE_CONTENT_TEXT
489538
MESSAGE_CONTENT_TYPE = MessageContentAttributes.MESSAGE_CONTENT_TYPE
490539

540+
# reranker attributes
541+
RERANKER_INPUT_DOCUMENTS = RerankerAttributes.RERANKER_INPUT_DOCUMENTS
542+
RERANKER_MODEL_NAME = RerankerAttributes.RERANKER_MODEL_NAME
543+
RERANKER_OUTPUT_DOCUMENTS = RerankerAttributes.RERANKER_OUTPUT_DOCUMENTS
544+
RERANKER_QUERY = RerankerAttributes.RERANKER_QUERY
545+
RERANKER_TOP_K = RerankerAttributes.RERANKER_TOP_K
491546

492547
# span attributes
493548
EMBEDDING_EMBEDDINGS = SpanAttributes.EMBEDDING_EMBEDDINGS
@@ -516,11 +571,9 @@ def get_llm_tool_attributes(
516571
TOOL_PARAMETERS = SpanAttributes.TOOL_PARAMETERS
517572
USER_ID = SpanAttributes.USER_ID
518573

519-
520574
# tool attributes
521575
TOOL_JSON_SCHEMA = ToolAttributes.TOOL_JSON_SCHEMA
522576

523-
524577
# tool call attributes
525578
TOOL_CALL_FUNCTION_ARGUMENTS_JSON = ToolCallAttributes.TOOL_CALL_FUNCTION_ARGUMENTS_JSON
526579
TOOL_CALL_FUNCTION_NAME = ToolCallAttributes.TOOL_CALL_FUNCTION_NAME

0 commit comments

Comments
 (0)