-
Notifications
You must be signed in to change notification settings - Fork 682
feat(typesafe): Add integration with system_one() patches
#7716
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
+606
−13
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97ecaa4
feat(typesafe): Add integration with system_one() patches
alexander-alderman-webb a5e7dfc
tests
alexander-alderman-webb 85cb1c4
.
alexander-alderman-webb 8958cc7
.
alexander-alderman-webb 3d4b797
Merge branch 'master' into webb/typesafe
alexander-alderman-webb 1c87f8f
add minimum version
alexander-alderman-webb 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -95,6 +95,7 @@ | |
| "mistral", | ||
| "openai-base", | ||
| "openai-notiktoken", | ||
| "typesafe", | ||
| ], | ||
| "Cloud": [ | ||
| "aws_lambda", | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| from functools import wraps | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.ai.utils import get_start_span_function | ||
| from sentry_sdk.consts import OP, SPANDATA | ||
| from sentry_sdk.integrations import DidNotEnable, Integration | ||
| from sentry_sdk.tracing_utils import has_span_streaming_enabled | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Callable | ||
|
|
||
| try: | ||
| from typesafe_sdk._core.client.aio.client import AsyncTypeSafeClient | ||
| from typesafe_sdk._core.client.sync.client import TypeSafeClient | ||
| except ImportError: | ||
| raise DidNotEnable("typesafe-sdk not installed") | ||
|
|
||
|
|
||
| class TypeSafeIntegration(Integration): | ||
| identifier = "typesafe" | ||
| origin = f"auto.ai.{identifier}" | ||
|
|
||
| @staticmethod | ||
| def setup_once() -> None: | ||
| TypeSafeClient.system_one = _wrap_system_one(TypeSafeClient.system_one) # type: ignore[method-assign] | ||
| AsyncTypeSafeClient.system_one = _wrap_system_one_async( # type: ignore[method-assign] | ||
| AsyncTypeSafeClient.system_one | ||
| ) | ||
|
|
||
|
|
||
| def _wrap_system_one(f: "Callable[..., Any]") -> "Callable[..., Any]": | ||
| @wraps(f) | ||
| def wrap_system_one(self: "TypeSafeClient", *args: "Any", **kwargs: "Any") -> "Any": | ||
| client = sentry_sdk.get_client() | ||
| integration = client.get_integration(TypeSafeIntegration) | ||
| if integration is None: | ||
| return f(self, *args, **kwargs) | ||
|
|
||
| model = kwargs.get("model") | ||
| if ( | ||
| model is None | ||
| and hasattr(self, "_config") | ||
| and hasattr(self._config, "default_model") | ||
| ): | ||
| model = self._config.default_model | ||
|
|
||
| if has_span_streaming_enabled(client.options): | ||
| span = sentry_sdk.traces.start_span( | ||
| name=f"evaluate {model}".strip(), | ||
| attributes={ | ||
| "sentry.op": OP.GEN_AI_EVALUATE, | ||
| "sentry.origin": TypeSafeIntegration.origin, | ||
| SPANDATA.GEN_AI_PROVIDER_NAME: "typesafe", | ||
| SPANDATA.GEN_AI_OPERATION_NAME: "evaluate", | ||
| }, | ||
| ) | ||
| set_on_span = span.set_attribute | ||
| else: | ||
| span = get_start_span_function()( | ||
| op=OP.GEN_AI_EVALUATE, | ||
| name=f"evaluate {model}".strip(), | ||
| origin=TypeSafeIntegration.origin, | ||
| ) | ||
| span.set_data(SPANDATA.GEN_AI_PROVIDER_NAME, "typesafe") | ||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "evaluate") | ||
| set_on_span = span.set_data | ||
|
|
||
| with span: | ||
| if model is not None: | ||
| set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model) | ||
|
|
||
| return f(self, *args, **kwargs) | ||
|
|
||
| return wrap_system_one | ||
|
|
||
|
|
||
| def _wrap_system_one_async(f: "Callable[..., Any]") -> "Callable[..., Any]": | ||
| @wraps(f) | ||
| async def wrap_system_one_async( | ||
| self: "AsyncTypeSafeClient", *args: "Any", **kwargs: "Any" | ||
| ) -> "Any": | ||
| client = sentry_sdk.get_client() | ||
| integration = client.get_integration(TypeSafeIntegration) | ||
| if integration is None: | ||
| return await f(self, *args, **kwargs) | ||
|
|
||
| model = kwargs.get("model") | ||
| if ( | ||
| model is None | ||
| and hasattr(self, "_config") | ||
| and hasattr(self._config, "default_model") | ||
| ): | ||
| model = self._config.default_model | ||
|
|
||
| if has_span_streaming_enabled(client.options): | ||
| span = sentry_sdk.traces.start_span( | ||
| name=f"evaluate {model}".strip(), | ||
| attributes={ | ||
| "sentry.op": OP.GEN_AI_EVALUATE, | ||
| "sentry.origin": TypeSafeIntegration.origin, | ||
| SPANDATA.GEN_AI_PROVIDER_NAME: "typesafe", | ||
| SPANDATA.GEN_AI_OPERATION_NAME: "evaluate", | ||
| }, | ||
| ) | ||
| set_on_span = span.set_attribute | ||
| else: | ||
| span = get_start_span_function()( | ||
| op=OP.GEN_AI_EVALUATE, | ||
| name=f"evaluate {model}".strip(), | ||
| origin=TypeSafeIntegration.origin, | ||
| ) | ||
| span.set_data(SPANDATA.GEN_AI_PROVIDER_NAME, "typesafe") | ||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "evaluate") | ||
| set_on_span = span.set_data | ||
|
|
||
| with span: | ||
| if model is not None: | ||
| set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model) | ||
|
|
||
| return await f(self, *args, **kwargs) | ||
|
|
||
| return wrap_system_one_async |
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,3 @@ | ||
| import pytest | ||
|
|
||
| pytest.importorskip("typesafe_sdk") |
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.
Uh oh!
There was an error while loading. Please reload this page.