-
Notifications
You must be signed in to change notification settings - Fork 589
feat(cohere): upgrade integration from ai to gen_ai #5597
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
base: master
Are you sure you want to change the base?
Changes from all commits
8fefe69
623f173
2113d8f
f0e31e9
8025537
08c1c23
024cbff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
|
|
||
| from sentry_sdk import consts | ||
| from sentry_sdk.ai.monitoring import record_token_usage | ||
| from sentry_sdk.consts import SPANDATA | ||
| from sentry_sdk.consts import OP, SPANDATA | ||
| from sentry_sdk.ai.utils import set_data_normalized | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
@@ -39,33 +39,35 @@ | |
| from cohere import StreamedChatResponse_StreamEnd as StreamEndStreamedChatResponse | ||
|
|
||
|
|
||
| COHERE_ROLE_MAPPING = { | ||
| "SYSTEM": "system", | ||
| "USER": "user", | ||
| "CHATBOT": "assistant", | ||
| "TOOL": "tool", | ||
| } | ||
|
|
||
|
|
||
| COLLECTED_CHAT_PARAMS = { | ||
| "model": SPANDATA.AI_MODEL_ID, | ||
| "k": SPANDATA.AI_TOP_K, | ||
| "p": SPANDATA.AI_TOP_P, | ||
| "seed": SPANDATA.AI_SEED, | ||
| "frequency_penalty": SPANDATA.AI_FREQUENCY_PENALTY, | ||
| "presence_penalty": SPANDATA.AI_PRESENCE_PENALTY, | ||
| "raw_prompting": SPANDATA.AI_RAW_PROMPTING, | ||
| "model": SPANDATA.GEN_AI_REQUEST_MODEL, | ||
| "k": SPANDATA.GEN_AI_REQUEST_TOP_K, | ||
| "p": SPANDATA.GEN_AI_REQUEST_TOP_P, | ||
| "seed": SPANDATA.GEN_AI_REQUEST_SEED, | ||
| "frequency_penalty": SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, | ||
| "presence_penalty": SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, | ||
| } | ||
|
|
||
| COLLECTED_PII_CHAT_PARAMS = { | ||
| "tools": SPANDATA.AI_TOOLS, | ||
| "preamble": SPANDATA.AI_PREAMBLE, | ||
| "tools": SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, | ||
| "preamble": SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, | ||
| } | ||
|
|
||
| COLLECTED_CHAT_RESP_ATTRS = { | ||
| "generation_id": SPANDATA.AI_GENERATION_ID, | ||
| "is_search_required": SPANDATA.AI_SEARCH_REQUIRED, | ||
| "finish_reason": SPANDATA.AI_FINISH_REASON, | ||
| "generation_id": SPANDATA.GEN_AI_RESPONSE_ID, | ||
| "finish_reason": SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, | ||
shellmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| COLLECTED_PII_CHAT_RESP_ATTRS = { | ||
| "citations": SPANDATA.AI_CITATIONS, | ||
| "documents": SPANDATA.AI_DOCUMENTS, | ||
| "search_queries": SPANDATA.AI_SEARCH_QUERIES, | ||
| "search_results": SPANDATA.AI_SEARCH_RESULTS, | ||
| "tool_calls": SPANDATA.AI_TOOL_CALLS, | ||
| "tool_calls": SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -102,16 +104,19 @@ def collect_chat_response_fields( | |
| if hasattr(res, "text"): | ||
| set_data_normalized( | ||
| span, | ||
| SPANDATA.AI_RESPONSES, | ||
| SPANDATA.GEN_AI_RESPONSE_TEXT, | ||
| [res.text], | ||
| ) | ||
| for pii_attr in COLLECTED_PII_CHAT_RESP_ATTRS: | ||
| if hasattr(res, pii_attr): | ||
| set_data_normalized(span, "ai." + pii_attr, getattr(res, pii_attr)) | ||
| for attr, spandata_key in COLLECTED_PII_CHAT_RESP_ATTRS.items(): | ||
| if hasattr(res, attr): | ||
| set_data_normalized(span, spandata_key, getattr(res, attr)) | ||
|
|
||
| for attr in COLLECTED_CHAT_RESP_ATTRS: | ||
| for attr, spandata_key in COLLECTED_CHAT_RESP_ATTRS.items(): | ||
| if hasattr(res, attr): | ||
| set_data_normalized(span, "ai." + attr, getattr(res, attr)) | ||
| value = getattr(res, attr) | ||
| if spandata_key == SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS: | ||
| value = [value] | ||
| set_data_normalized(span, spandata_key, value) | ||
|
|
||
| if hasattr(res, "meta"): | ||
| if hasattr(res.meta, "billed_units"): | ||
|
|
@@ -127,9 +132,6 @@ def collect_chat_response_fields( | |
| output_tokens=res.meta.tokens.output_tokens, | ||
| ) | ||
|
|
||
| if hasattr(res.meta, "warnings"): | ||
| set_data_normalized(span, SPANDATA.AI_WARNINGS, res.meta.warnings) | ||
|
|
||
| @wraps(f) | ||
| def new_chat(*args: "Any", **kwargs: "Any") -> "Any": | ||
| integration = sentry_sdk.get_client().get_integration(CohereIntegration) | ||
|
|
@@ -142,13 +144,19 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any": | |
| return f(*args, **kwargs) | ||
|
|
||
| message = kwargs.get("message") | ||
| model = kwargs.get("model", "") | ||
|
|
||
| span = sentry_sdk.start_span( | ||
| op=consts.OP.COHERE_CHAT_COMPLETIONS_CREATE, | ||
| name="cohere.client.Chat", | ||
| op=OP.GEN_AI_CHAT, | ||
| name=f"chat {model}".strip(), | ||
| origin=CohereIntegration.origin, | ||
| ) | ||
| span.__enter__() | ||
|
|
||
| with capture_internal_exceptions(): | ||
| span.set_data(SPANDATA.GEN_AI_SYSTEM, "cohere") | ||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | ||
|
|
||
| try: | ||
| res = f(*args, **kwargs) | ||
| except Exception as e: | ||
|
|
@@ -160,19 +168,21 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any": | |
|
|
||
| with capture_internal_exceptions(): | ||
| if should_send_default_pii() and integration.include_prompts: | ||
| messages = [] | ||
| for x in kwargs.get("chat_history", []): | ||
| role = getattr(x, "role", "") | ||
| messages.append( | ||
| { | ||
| "role": COHERE_ROLE_MAPPING.get(role, role), | ||
| "content": getattr(x, "message", ""), | ||
| } | ||
| ) | ||
| messages.append({"role": "user", "content": message}) | ||
| set_data_normalized( | ||
| span, | ||
| SPANDATA.AI_INPUT_MESSAGES, | ||
| list( | ||
| map( | ||
| lambda x: { | ||
| "role": getattr(x, "role", "").lower(), | ||
| "content": getattr(x, "message", ""), | ||
| }, | ||
| kwargs.get("chat_history", []), | ||
| ) | ||
| ) | ||
| + [{"role": "user", "content": message}], | ||
| SPANDATA.GEN_AI_REQUEST_MESSAGES, | ||
| messages, | ||
| unpack=False, | ||
| ) | ||
| for k, v in COLLECTED_PII_CHAT_PARAMS.items(): | ||
| if k in kwargs: | ||
|
|
@@ -181,7 +191,7 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any": | |
| for k, v in COLLECTED_CHAT_PARAMS.items(): | ||
| if k in kwargs: | ||
| set_data_normalized(span, v, kwargs[k]) | ||
| set_data_normalized(span, SPANDATA.AI_STREAMING, False) | ||
| set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_STREAMING, streaming) | ||
|
|
||
| if streaming: | ||
| old_iterator = res | ||
|
|
@@ -226,27 +236,36 @@ def new_embed(*args: "Any", **kwargs: "Any") -> "Any": | |
| if integration is None: | ||
| return f(*args, **kwargs) | ||
|
|
||
| model = kwargs.get("model", "") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the documentation currently does not specify the AI Client Span's description when there is no request model. I'll bring this up in the sync, update the devdocs, and ping you once there's a concrete spec. https://develop.sentry.dev/sdk/telemetry/traces/modules/ai-agents/
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what to do here, is there any news? |
||
|
|
||
| with sentry_sdk.start_span( | ||
| op=consts.OP.COHERE_EMBEDDINGS_CREATE, | ||
| name="Cohere Embedding Creation", | ||
| op=OP.GEN_AI_EMBEDDINGS, | ||
| name=f"embeddings {model}".strip(), | ||
| origin=CohereIntegration.origin, | ||
| ) as span: | ||
| span.set_data(SPANDATA.GEN_AI_SYSTEM, "cohere") | ||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "embeddings") | ||
|
|
||
| if "texts" in kwargs and ( | ||
| should_send_default_pii() and integration.include_prompts | ||
| ): | ||
| if isinstance(kwargs["texts"], str): | ||
sentry[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| set_data_normalized(span, SPANDATA.AI_TEXTS, [kwargs["texts"]]) | ||
| set_data_normalized( | ||
| span, SPANDATA.GEN_AI_EMBEDDINGS_INPUT, [kwargs["texts"]] | ||
sentry[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| elif ( | ||
| isinstance(kwargs["texts"], list) | ||
| and len(kwargs["texts"]) > 0 | ||
| and isinstance(kwargs["texts"][0], str) | ||
| ): | ||
| set_data_normalized( | ||
| span, SPANDATA.AI_INPUT_MESSAGES, kwargs["texts"] | ||
| span, SPANDATA.GEN_AI_EMBEDDINGS_INPUT, kwargs["texts"] | ||
| ) | ||
|
|
||
| if "model" in kwargs: | ||
| set_data_normalized(span, SPANDATA.AI_MODEL_ID, kwargs["model"]) | ||
| set_data_normalized( | ||
| span, SPANDATA.GEN_AI_REQUEST_MODEL, kwargs["model"] | ||
| ) | ||
| try: | ||
| res = f(*args, **kwargs) | ||
| except Exception as e: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.