-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle multiple embedding events for llama-index (#1166)
- Loading branch information
1 parent
4e5d219
commit 0cef233
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...e-instrumentation-llama-index/tests/openinference/instrumentation/llama_index/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
||
|
||
@pytest.fixture | ||
def in_memory_span_exporter() -> InMemorySpanExporter: | ||
return InMemorySpanExporter() | ||
|
||
|
||
@pytest.fixture | ||
def tracer_provider( | ||
in_memory_span_exporter: InMemorySpanExporter, | ||
) -> TracerProvider: | ||
tracer_provider = TracerProvider() | ||
tracer_provider.add_span_processor(SimpleSpanProcessor(in_memory_span_exporter)) | ||
return tracer_provider |
49 changes: 49 additions & 0 deletions
49
...a-index/tests/openinference/instrumentation/llama_index/test_multiple_embedding_events.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from itertools import product | ||
from typing import Iterator | ||
|
||
import pytest | ||
from llama_index.core.instrumentation import get_dispatcher | ||
from llama_index.core.instrumentation.events.embedding import EmbeddingEndEvent | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
||
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor | ||
from openinference.semconv.trace import EmbeddingAttributes, SpanAttributes | ||
|
||
dispatcher = get_dispatcher(__name__) | ||
|
||
|
||
@dispatcher.span # type: ignore[misc,unused-ignore] | ||
def foo(m: int, n: int) -> None: | ||
for i in range(m): | ||
chunks = [f"{i}-{j}" for j in range(n)] | ||
embeddings = [list(map(float, [i, j])) for j in range(n)] | ||
dispatcher.event(EmbeddingEndEvent(chunks=chunks, embeddings=embeddings)) | ||
|
||
|
||
async def test_multiple_embedding_events( | ||
in_memory_span_exporter: InMemorySpanExporter, | ||
) -> None: | ||
m, n = 3, 2 | ||
foo(m, n) | ||
span = in_memory_span_exporter.get_finished_spans()[0] | ||
assert span.attributes | ||
for k, (i, j) in enumerate(product(range(m), range(n))): | ||
text, vector = f"{i}-{j}", tuple(map(float, [i, j])) | ||
assert span.attributes[f"{EMBEDDING_EMBEDDINGS}.{k}.{EMBEDDING_TEXT}"] == text | ||
assert span.attributes[f"{EMBEDDING_EMBEDDINGS}.{k}.{EMBEDDING_VECTOR}"] == vector | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def instrument( | ||
tracer_provider: TracerProvider, | ||
in_memory_span_exporter: InMemorySpanExporter, | ||
) -> Iterator[None]: | ||
LlamaIndexInstrumentor().instrument(tracer_provider=tracer_provider) | ||
yield | ||
LlamaIndexInstrumentor().uninstrument() | ||
|
||
|
||
EMBEDDING_EMBEDDINGS = SpanAttributes.EMBEDDING_EMBEDDINGS | ||
EMBEDDING_TEXT = EmbeddingAttributes.EMBEDDING_TEXT | ||
EMBEDDING_VECTOR = EmbeddingAttributes.EMBEDDING_VECTOR |