Skip to content

Commit 6d9b34e

Browse files
committed
embedding attributes
1 parent fe94b12 commit 6d9b34e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from typing_extensions import TypeGuard
2121

2222
from openinference.semconv.trace import (
23+
EmbeddingAttributes,
2324
ImageAttributes,
2425
MessageAttributes,
2526
MessageContentAttributes,
@@ -33,6 +34,7 @@
3334
)
3435

3536
from ._types import (
37+
Embedding,
3638
Message,
3739
OpenInferenceLLMProvider,
3840
OpenInferenceLLMSystem,
@@ -53,6 +55,25 @@
5355
from _typeshed import DataclassInstance
5456

5557

58+
def get_embedding_attributes(
59+
*,
60+
model_name: Optional[str] = None,
61+
embeddings: Optional[List[Embedding]] = None,
62+
) -> Dict[str, Any]:
63+
embedding_attributes: Dict[str, AttributeValue] = {}
64+
if model_name is not None:
65+
embedding_attributes[EMBEDDING_MODEL_NAME] = model_name
66+
if isinstance(embeddings, list):
67+
for index, embedding in enumerate(embeddings):
68+
if (text := embedding.get("text")) is not None:
69+
key = f"{EMBEDDING_EMBEDDINGS}.{index}.{EMBEDDING_TEXT}"
70+
embedding_attributes[key] = text
71+
if (vector := embedding.get("vector")) is not None:
72+
key = f"{EMBEDDING_EMBEDDINGS}.{index}.{EMBEDDING_VECTOR}"
73+
embedding_attributes[key] = vector
74+
return embedding_attributes
75+
76+
5677
def get_context_attributes(
5778
*,
5879
session_id: Optional[str] = None,
@@ -413,6 +434,10 @@ def get_llm_tool_attributes(
413434
return attributes
414435

415436

437+
# embedding attributes
438+
EMBEDDING_TEXT = EmbeddingAttributes.EMBEDDING_TEXT
439+
EMBEDDING_VECTOR = EmbeddingAttributes.EMBEDDING_VECTOR
440+
416441
# image attributes
417442
IMAGE_URL = ImageAttributes.IMAGE_URL
418443

@@ -432,6 +457,8 @@ def get_llm_tool_attributes(
432457

433458

434459
# span attributes
460+
EMBEDDING_EMBEDDINGS = SpanAttributes.EMBEDDING_EMBEDDINGS
461+
EMBEDDING_MODEL_NAME = SpanAttributes.EMBEDDING_MODEL_NAME
435462
INPUT_MIME_TYPE = SpanAttributes.INPUT_MIME_TYPE
436463
INPUT_VALUE = SpanAttributes.INPUT_VALUE
437464
LLM_INPUT_MESSAGES = SpanAttributes.LLM_INPUT_MESSAGES

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Sequence
2-
from typing import Any, Dict, Literal, TypedDict, Union
2+
from typing import Any, Dict, List, Literal, TypedDict, Union
33

44
from typing_extensions import Required, TypeAlias
55

@@ -76,3 +76,8 @@ class TokenCount(TypedDict, total=False):
7676

7777
class Tool(TypedDict, total=False):
7878
json_schema: Required[Union[str, Dict[str, Any]]]
79+
80+
81+
class Embedding(TypedDict, total=False):
82+
text: str
83+
vector: List[float]

0 commit comments

Comments
 (0)