-
Notifications
You must be signed in to change notification settings - Fork 137
feat: add embedding hiding configuration and align spec with instrumentation #2162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
codefromthecrypt
wants to merge
3
commits into
Arize-ai:main
Choose a base branch
from
codefromthecrypt:spec-embeddings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
python/instrumentation/openinference-instrumentation-beeai/tests/README.md
This file contains hidden or 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,27 @@ | ||
# BeeAI Instrumentation Tests | ||
|
||
## Re-recording VCR Cassettes | ||
|
||
When tests fail due to outdated VCR cassettes (e.g., API authentication errors or changed responses), follow these steps to re-record: | ||
|
||
### Prerequisites | ||
1. Ensure `OPENAI_API_KEY` is set in your environment with a valid API key | ||
2. The `passenv = OPENAI_API_KEY` directive must be present in the root `tox.ini` file | ||
|
||
### Steps to Re-record | ||
|
||
1. Delete the existing cassette file: | ||
```bash | ||
rm tests/cassettes/test_openai_embeddings.yaml | ||
``` | ||
|
||
2. Run the tests with VCR in record mode using tox: | ||
```bash | ||
OPENAI_API_KEY=$OPENAI_API_KEY uvx --with tox-uv tox -r -e py313-ci-beeai -- tests/test_instrumentor.py::test_openai_embeddings -xvs --vcr-record=once | ||
``` | ||
|
||
### Important Notes | ||
- The test reads `OPENAI_API_KEY` from the environment, falling back to "sk-test" if not set | ||
- VCR will cache responses including authentication errors (401), so always delete the cassette before re-recording | ||
- The `--vcr-record=once` flag ensures the cassette is only recorded when it doesn't exist | ||
- Use `-r` flag with tox to ensure a clean environment when re-recording |
31 changes: 31 additions & 0 deletions
31
...mentation/openinference-instrumentation-beeai/tests/cassettes/test_openai_embeddings.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
python/instrumentation/openinference-instrumentation-beeai/tests/conftest.py
This file contains hidden or 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,36 @@ | ||
from typing import Generator | ||
|
||
import pytest | ||
from opentelemetry import trace as trace_api | ||
from opentelemetry.sdk import trace as trace_sdk | ||
from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
||
from openinference.instrumentation.beeai import BeeAIInstrumentor | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def in_memory_span_exporter() -> InMemorySpanExporter: | ||
return InMemorySpanExporter() | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def tracer_provider( | ||
in_memory_span_exporter: InMemorySpanExporter, | ||
) -> trace_api.TracerProvider: | ||
tracer_provider = trace_sdk.TracerProvider() | ||
span_processor = SimpleSpanProcessor(span_exporter=in_memory_span_exporter) | ||
tracer_provider.add_span_processor(span_processor=span_processor) | ||
return tracer_provider | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def instrument( | ||
tracer_provider: trace_api.TracerProvider, | ||
in_memory_span_exporter: InMemorySpanExporter, | ||
) -> Generator[None, None, None]: | ||
BeeAIInstrumentor().instrument(tracer_provider=tracer_provider) | ||
in_memory_span_exporter.clear() | ||
yield | ||
BeeAIInstrumentor().uninstrument() | ||
in_memory_span_exporter.clear() |
2 changes: 0 additions & 2 deletions
2
python/instrumentation/openinference-instrumentation-beeai/tests/test_dummy.py
This file was deleted.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
python/instrumentation/openinference-instrumentation-beeai/tests/test_instrumentor.py
This file contains hidden or 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,109 @@ | ||
import json | ||
import os | ||
from typing import Mapping, cast | ||
|
||
import pytest | ||
from beeai_framework.adapters.openai import OpenAIEmbeddingModel | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
from opentelemetry.util.types import AttributeValue | ||
|
||
from openinference.semconv.trace import ( | ||
EmbeddingAttributes, | ||
OpenInferenceSpanKindValues, | ||
SpanAttributes, | ||
) | ||
|
||
|
||
@pytest.mark.vcr( | ||
decode_compressed_response=True, | ||
before_record_request=lambda _: _.headers.clear() or _, | ||
before_record_response=lambda _: {**_, "headers": {}}, | ||
) | ||
@pytest.mark.asyncio | ||
async def test_openai_embeddings(in_memory_span_exporter: InMemorySpanExporter) -> None: | ||
"""Test that BeeAI OpenAI embeddings are properly traced.""" | ||
# API key from environment - only used when re-recording the cassette | ||
# When using the cassette, the key is not needed | ||
api_key = os.getenv("OPENAI_API_KEY", "sk-test") | ||
|
||
# Create an embedding model | ||
embedding_model = OpenAIEmbeddingModel( | ||
model_id="text-embedding-3-small", | ||
api_key=api_key, | ||
) | ||
|
||
# Create embeddings for test texts | ||
texts = ["Hello world", "Test embedding"] | ||
|
||
# Run the embedding request | ||
response = await embedding_model.create(texts) | ||
|
||
# Verify we got embeddings back | ||
assert response is not None | ||
assert response.embeddings is not None | ||
assert len(response.embeddings) == 2 | ||
|
||
# Get the spans | ||
spans = in_memory_span_exporter.get_finished_spans() | ||
assert len(spans) == 1 | ||
|
||
# Get the single span | ||
openinference_span = spans[0] | ||
assert openinference_span is not None | ||
|
||
# Verify span attributes | ||
attributes = dict(cast(Mapping[str, AttributeValue], openinference_span.attributes)) | ||
|
||
# Check basic attributes as per spec | ||
assert ( | ||
attributes.get(SpanAttributes.OPENINFERENCE_SPAN_KIND) | ||
== OpenInferenceSpanKindValues.EMBEDDING.value | ||
) | ||
assert attributes.get(SpanAttributes.EMBEDDING_MODEL_NAME) == "text-embedding-3-small" | ||
assert attributes.get(SpanAttributes.LLM_SYSTEM) == "beeai" | ||
assert attributes.get(SpanAttributes.LLM_PROVIDER) == "openai" | ||
|
||
# Check embedding texts | ||
assert ( | ||
attributes.get( | ||
f"{SpanAttributes.EMBEDDING_EMBEDDINGS}.0.{EmbeddingAttributes.EMBEDDING_TEXT}" | ||
) | ||
== "Hello world" | ||
) | ||
assert ( | ||
attributes.get( | ||
f"{SpanAttributes.EMBEDDING_EMBEDDINGS}.1.{EmbeddingAttributes.EMBEDDING_TEXT}" | ||
) | ||
== "Test embedding" | ||
) | ||
|
||
# Check embedding vectors exist and have correct structure | ||
vector_0 = attributes.get( | ||
f"{SpanAttributes.EMBEDDING_EMBEDDINGS}.0.{EmbeddingAttributes.EMBEDDING_VECTOR}" | ||
) | ||
vector_1 = attributes.get( | ||
f"{SpanAttributes.EMBEDDING_EMBEDDINGS}.1.{EmbeddingAttributes.EMBEDDING_VECTOR}" | ||
) | ||
|
||
assert vector_0 is not None | ||
assert vector_1 is not None | ||
# Vectors are tuples in the cassette, check exact length from recorded data | ||
assert isinstance(vector_0, (list, tuple)) | ||
assert isinstance(vector_1, (list, tuple)) | ||
assert len(vector_0) == 1536 # text-embedding-3-small dimension | ||
assert len(vector_1) == 1536 # text-embedding-3-small dimension | ||
# Check first few values are correct floats from cassette | ||
assert vector_0[0] == pytest.approx(-0.002078542485833168) | ||
assert vector_0[1] == pytest.approx(-0.04908587411046028) | ||
assert vector_1[0] == pytest.approx(-0.005330947693437338) | ||
assert vector_1[1] == pytest.approx(-0.03916504979133606) | ||
|
||
# Check invocation parameters | ||
invocation_params = attributes.get("embedding.invocation_parameters") | ||
assert isinstance(invocation_params, str) | ||
assert json.loads(invocation_params) == {"abort_signal": None, "max_retries": 0} | ||
|
||
# Check token counts | ||
assert attributes.get(SpanAttributes.LLM_TOKEN_COUNT_TOTAL) == 4 | ||
assert attributes.get(SpanAttributes.LLM_TOKEN_COUNT_PROMPT) == 4 | ||
assert attributes.get(SpanAttributes.LLM_TOKEN_COUNT_COMPLETION) == 0 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need this to be ddgs as it got renamed for agno
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool.