-
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
29
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.
+985
−331
Draft
Changes from all commits
Commits
Show all changes
29 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 f3563dd
format
shellmayr 3227de8
style
shellmayr 068ce5d
fix getattr
shellmayr c7a1b58
simplify
shellmayr 33c63f6
fix
shellmayr dadbc79
clarify
shellmayr 1cba433
response model from response
shellmayr 43bad0c
make cost declarative in embeddings
shellmayr 3e077fe
typing
shellmayr 1e975ce
move configs to a separate file
shellmayr aa1dbfa
move type definition
shellmayr 31769ee
fix
shellmayr c0a0e10
fix swalling exception bug
shellmayr 3cbaad6
move text massaging to core file
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,88 @@ | ||
| import sentry_sdk | ||
| from sentry_sdk.consts import SPANDATA | ||
| from sentry_sdk.ai.monitoring import record_token_usage | ||
| from sentry_sdk.ai.utils import ( | ||
| get_first_from_sources, | ||
| set_data_normalized, | ||
| set_span_data_from_sources, | ||
| 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, List, Optional | ||
|
|
||
| from sentry_sdk.tracing import Span | ||
|
|
||
|
|
||
| def set_request_span_data(span, kwargs, integration, config, span_data=None): | ||
| # type: (Span, Dict[str, Any], Any, Dict[str, Any], Dict[str, Any] | None) -> None | ||
| """Set request/static span data from a declarative config.""" | ||
| for key, value in config.get("static", {}).items(): | ||
| set_data_normalized(span, key, value) | ||
| if span_data: | ||
| for key, value in span_data.items(): | ||
| set_data_normalized(span, key, value) | ||
|
|
||
| for kwarg_key, span_attr in config.get("params", {}).items(): | ||
| if kwarg_key in kwargs: | ||
| value = kwargs[kwarg_key] | ||
| set_data_normalized(span, span_attr, value) | ||
|
|
||
| if should_send_default_pii() and integration.include_prompts: | ||
| for kwarg_key, span_attr in config.get("pii_params", {}).items(): | ||
| if kwarg_key in kwargs: | ||
| value = kwargs[kwarg_key] | ||
| set_data_normalized(span, span_attr, value) | ||
|
|
||
|
|
||
| def set_request_messages(span, messages, target=None): | ||
| # type: (Span, Any, Optional[str]) -> None | ||
| """Normalize, truncate, and set request messages on the span. | ||
|
|
||
| Caller is responsible for PII gating. | ||
| """ | ||
| if not messages: | ||
| return | ||
| messages = normalize_message_roles(messages) | ||
| scope = sentry_sdk.get_current_scope() | ||
| messages = truncate_and_annotate_messages(messages, span, scope) | ||
| if messages is not None: | ||
| set_data_normalized( | ||
| span, target or SPANDATA.GEN_AI_REQUEST_MESSAGES, messages, unpack=False | ||
| ) | ||
|
|
||
|
|
||
| def set_response_span_data( | ||
| span, response, include_pii, response_config, response_text=None | ||
| ): | ||
| # type: (Span, Any, bool, Dict[str, Any], Optional[List[str]]) -> None | ||
| """Set response span data from a declarative config.""" | ||
| set_span_data_from_sources( | ||
| span, response, response_config.get("sources", {}), require_truthy=False | ||
| ) | ||
|
|
||
| if include_pii: | ||
| pii_sources = response_config.get("pii_sources") | ||
| if pii_sources: | ||
| set_span_data_from_sources(span, response, pii_sources, require_truthy=True) | ||
| if response_text: | ||
| set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, response_text) | ||
|
|
||
| usage_config = response_config.get("usage") | ||
| if usage_config: | ||
| record_token_usage( | ||
| span, | ||
| input_tokens=get_first_from_sources( | ||
| response, usage_config.get("input_tokens", []) | ||
| ), | ||
| output_tokens=get_first_from_sources( | ||
| response, usage_config.get("output_tokens", []) | ||
| ), | ||
| total_tokens=get_first_from_sources( | ||
| response, usage_config.get("total_tokens", []) | ||
| ), | ||
| ) |
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
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