-
Notifications
You must be signed in to change notification settings - Fork 16
feat(integrations): add discovery engine v1 instrumentation #776
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
65 changes: 65 additions & 0 deletions
65
py/src/braintrust/integrations/auto_test_scripts/test_auto_google_discoveryengine.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,65 @@ | ||
| """Both import orders and opt-out, using the real REST ranking cassette.""" | ||
|
|
||
| import inspect | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
| from urllib.parse import urlsplit | ||
|
|
||
| import yaml | ||
| from braintrust.auto import auto_instrument | ||
| from braintrust.integrations.conftest import _versioned_cassette_dir | ||
| from braintrust.integrations.test_utils import autoinstrument_test_context | ||
|
|
||
|
|
||
| if len(sys.argv) == 1: | ||
| for order in ("before", "after"): | ||
| subprocess.run([sys.executable, __file__, order], check=True) | ||
| print("SUCCESS") | ||
| sys.exit(0) | ||
|
|
||
| options = {name: False for name in inspect.signature(auto_instrument).parameters} | ||
| assert auto_instrument(**options) == {} | ||
| RankServiceClient = None | ||
| if sys.argv[1] == "before": | ||
| from google.cloud.discoveryengine_v1 import RankServiceClient | ||
|
|
||
| options["google_discoveryengine"] = True | ||
| assert auto_instrument(**options) == {"google_discoveryengine": True} | ||
| assert auto_instrument(**options) == {"google_discoveryengine": True} | ||
| from google.auth.credentials import AnonymousCredentials | ||
|
|
||
|
|
||
| if sys.argv[1] == "after": | ||
| from google.cloud.discoveryengine_v1 import RankServiceClient | ||
|
|
||
|
|
||
| cassette_dir = Path( | ||
| _versioned_cassette_dir(str(Path(__file__).parent.parent / "google_discoveryengine" / "cassettes")) | ||
| ) | ||
| cassette = yaml.safe_load((cassette_dir / "test_rank.yaml").read_text()) | ||
| ranking_config = urlsplit(cassette["interactions"][0]["request"]["uri"]).path.removeprefix("/v1/").split(":rank")[0] | ||
|
|
||
| assert RankServiceClient is not None | ||
| with autoinstrument_test_context( | ||
| "test_rank", integration="google_discoveryengine", vcr_config={"record_mode": "none"} | ||
| ) as memory_logger: | ||
| client = RankServiceClient(transport="rest", credentials=AnonymousCredentials()) | ||
| result = client.rank( | ||
| request={ | ||
| "ranking_config": ranking_config, | ||
| "model": "semantic-ranker-512@latest", | ||
| "query": "What is Braintrust?", | ||
| "records": [ | ||
| {"id": "1", "content": "Braintrust is a platform for evaluating and monitoring AI applications."}, | ||
| {"id": "2", "content": "The moon orbits the Earth."}, | ||
| ], | ||
| "top_n": 1, | ||
| }, | ||
| retry=None, | ||
| ) | ||
| assert result.records[0].id == "1" | ||
| spans = memory_logger.pop() | ||
| assert len(spans) == 1 | ||
| assert spans[0]["metadata"]["provider"] == "google" | ||
| assert spans[0]["context"]["span_origin"]["instrumentation"]["name"] == "google-discoveryengine-auto" |
12 changes: 12 additions & 0 deletions
12
py/src/braintrust/integrations/google_discoveryengine/__init__.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,12 @@ | ||
| """Braintrust integration for google-cloud-discoveryengine v1.""" | ||
|
|
||
| from .integration import GoogleDiscoveryEngineIntegration | ||
| from .patchers import wrap_google_discoveryengine | ||
|
|
||
|
|
||
| __all__ = ["GoogleDiscoveryEngineIntegration", "setup_google_discoveryengine", "wrap_google_discoveryengine"] | ||
|
|
||
|
|
||
| def setup_google_discoveryengine() -> bool: | ||
| """Instrument supported v1 clients in this process.""" | ||
| return GoogleDiscoveryEngineIntegration.setup() |
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.
Uh oh!
There was an error while loading. Please reload this page.