-
Notifications
You must be signed in to change notification settings - Fork 586
feat(cohere): upgrade integration from ai to gen_ai #5532
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
shellmayr
wants to merge
15
commits into
master
Choose a base branch
from
shellmayr/feat/upgrade-cohere-ai-integration-from-ai-to-genai
base: master
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.
+859
−330
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c3c3466
feat(cohere): upgrade integration from ai to gen_ai
shellmayr b17b79d
add instrumentation for cohere v2
shellmayr 280fbd8
wip
shellmayr 041844e
format
shellmayr 08dc0c2
correct model
shellmayr 54e99fe
wip
shellmayr ec237b4
move to separate folder
shellmayr d7814b0
wip
shellmayr a1f4ab7
make sure to capture exceptions correctly
shellmayr 130f509
use context managers where appropriate
shellmayr e9840a2
format
shellmayr 50cfff2
remove comments
shellmayr 65f9230
wip
shellmayr 9532175
format
shellmayr b1d4fbd
refacotr
shellmayr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import sentry_sdk | ||
| from sentry_sdk.consts import SPANDATA | ||
| from sentry_sdk.ai.utils import ( | ||
| set_data_normalized, | ||
| normalize_message_roles, | ||
| truncate_and_annotate_messages, | ||
| ) | ||
| from sentry_sdk.scope import should_send_default_pii | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Dict | ||
| from sentry_sdk.tracing import Span | ||
|
|
||
|
|
||
| def set_input_span_data(span, kwargs, integration, config): | ||
| # type: (Span, Dict[str, Any], Any, Dict[str, Any]) -> None | ||
| """ | ||
| Set input span data from a declarative config. | ||
|
|
||
| Config keys: | ||
| system: str - gen_ai.system value | ||
| operation: str - gen_ai.operation.name value | ||
| params: dict - kwargs key -> span attr (always set if present) | ||
| pii_params: dict - kwargs key -> span attr (only when PII allowed) | ||
| extract_messages: callable(kwargs) -> list or None | ||
| message_target: str - span attr for messages (default: GEN_AI_REQUEST_MESSAGES) | ||
| truncation_fn: callable or None - truncation function (default: truncate_and_annotate_messages, None to skip) | ||
| is_given: callable(value) -> bool - for NotGiven sentinels | ||
| extra_static: dict - additional key/value pairs to set | ||
| """ | ||
| set_data_normalized(span, SPANDATA.GEN_AI_SYSTEM, config["system"]) | ||
| set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, config["operation"]) | ||
|
|
||
| is_given = config.get("is_given") | ||
| for kwarg_key, span_attr in config.get("params", {}).items(): | ||
| if kwarg_key in kwargs: | ||
| value = kwargs[kwarg_key] | ||
| if is_given is None or is_given(value): | ||
| set_data_normalized(span, span_attr, value) | ||
|
|
||
| if should_send_default_pii() and integration.include_prompts: | ||
| extract = config.get("extract_messages") | ||
| if extract is not None: | ||
| messages = extract(kwargs) | ||
| if messages: | ||
| messages = normalize_message_roles(messages) | ||
| truncation_fn = config.get( | ||
| "truncation_fn", truncate_and_annotate_messages | ||
| ) | ||
| if truncation_fn is not None: | ||
| scope = sentry_sdk.get_current_scope() | ||
| messages = truncation_fn(messages, span, scope) | ||
| if messages is not None: | ||
| target = config.get( | ||
| "message_target", SPANDATA.GEN_AI_REQUEST_MESSAGES | ||
| ) | ||
| set_data_normalized(span, target, messages, unpack=False) | ||
|
|
||
| for kwarg_key, span_attr in config.get("pii_params", {}).items(): | ||
| if kwarg_key in kwargs: | ||
| value = kwargs[kwarg_key] | ||
| if is_given is None or is_given(value): | ||
| set_data_normalized(span, span_attr, value) | ||
|
|
||
| for key, value in config.get("extra_static", {}).items(): | ||
| set_data_normalized(span, key, value) |
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 was deleted.
Oops, something went wrong.
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.
This is needed because cohere sometimes uses the role
chatbotin their message structure