|
29 | 29 | OpenInferenceLLMSystemValues,
|
30 | 30 | OpenInferenceMimeTypeValues,
|
31 | 31 | OpenInferenceSpanKindValues,
|
| 32 | + RerankerAttributes, |
32 | 33 | SpanAttributes,
|
33 | 34 | ToolAttributes,
|
34 | 35 | ToolCallAttributes,
|
|
57 | 58 | from _typeshed import DataclassInstance
|
58 | 59 |
|
59 | 60 |
|
| 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 | + |
60 | 97 | def get_retriever_attributes(*, documents: List[Document]) -> Dict[str, AttributeValue]:
|
61 | 98 | attributes: Dict[str, AttributeValue] = {}
|
| 99 | + if not isinstance(documents, list): |
| 100 | + return attributes |
62 | 101 | 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 | + ) |
82 | 109 | return attributes
|
83 | 110 |
|
84 | 111 |
|
| 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 | + |
85 | 136 | def get_embedding_attributes(
|
86 | 137 | *,
|
87 | 138 | model_name: Optional[str] = None,
|
@@ -474,20 +525,24 @@ def get_llm_tool_attributes(
|
474 | 525 | # image attributes
|
475 | 526 | IMAGE_URL = ImageAttributes.IMAGE_URL
|
476 | 527 |
|
477 |
| - |
478 | 528 | # message attributes
|
479 | 529 | MESSAGE_CONTENT = MessageAttributes.MESSAGE_CONTENT
|
480 | 530 | MESSAGE_CONTENTS = MessageAttributes.MESSAGE_CONTENTS
|
481 | 531 | MESSAGE_ROLE = MessageAttributes.MESSAGE_ROLE
|
482 | 532 | MESSAGE_TOOL_CALL_ID = MessageAttributes.MESSAGE_TOOL_CALL_ID
|
483 | 533 | MESSAGE_TOOL_CALLS = MessageAttributes.MESSAGE_TOOL_CALLS
|
484 | 534 |
|
485 |
| - |
486 | 535 | # message content attributes
|
487 | 536 | MESSAGE_CONTENT_IMAGE = MessageContentAttributes.MESSAGE_CONTENT_IMAGE
|
488 | 537 | MESSAGE_CONTENT_TEXT = MessageContentAttributes.MESSAGE_CONTENT_TEXT
|
489 | 538 | MESSAGE_CONTENT_TYPE = MessageContentAttributes.MESSAGE_CONTENT_TYPE
|
490 | 539 |
|
| 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 |
491 | 546 |
|
492 | 547 | # span attributes
|
493 | 548 | EMBEDDING_EMBEDDINGS = SpanAttributes.EMBEDDING_EMBEDDINGS
|
@@ -516,11 +571,9 @@ def get_llm_tool_attributes(
|
516 | 571 | TOOL_PARAMETERS = SpanAttributes.TOOL_PARAMETERS
|
517 | 572 | USER_ID = SpanAttributes.USER_ID
|
518 | 573 |
|
519 |
| - |
520 | 574 | # tool attributes
|
521 | 575 | TOOL_JSON_SCHEMA = ToolAttributes.TOOL_JSON_SCHEMA
|
522 | 576 |
|
523 |
| - |
524 | 577 | # tool call attributes
|
525 | 578 | TOOL_CALL_FUNCTION_ARGUMENTS_JSON = ToolCallAttributes.TOOL_CALL_FUNCTION_ARGUMENTS_JSON
|
526 | 579 | TOOL_CALL_FUNCTION_NAME = ToolCallAttributes.TOOL_CALL_FUNCTION_NAME
|
|
0 commit comments