Skip to content
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

feat(ai): LLM monitoring init #2514

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 90 additions & 0 deletions sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from sentry_sdk.hub import Hub
from sentry_sdk.integrations import DidNotEnable, Integration

Check warning on line 2 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L1-L2

Added lines #L1 - L2 were not covered by tests


try:
from openai import resources
except ImportError:
raise DidNotEnable("OpenAI is not installed")

Check warning on line 8 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L5-L8

Added lines #L5 - L8 were not covered by tests

print("init_monkey")

Check warning on line 10 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L10

Added line #L10 was not covered by tests


class OpenAiIntegration(Integration):
identifier = "openai"

Check warning on line 14 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L14

Added line #L14 was not covered by tests

@staticmethod

Check warning on line 16 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L16

Added line #L16 was not covered by tests
def setup_once():
print("monkey0?")

Check warning on line 18 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L18

Added line #L18 was not covered by tests
# type: () -> None
patch_openai()

Check warning on line 20 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L20

Added line #L20 was not covered by tests


def patch_openai():

Check warning on line 23 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L23

Added line #L23 was not covered by tests
# type: () -> None
old_open_ai_resources_chat_completions_create = (

Check warning on line 25 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L25

Added line #L25 was not covered by tests
resources.chat.completions.Completions.create
)

def monkeypatched_openai(*args, **kwargs):
hub = Hub.current

Check warning on line 30 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L29-L30

Added lines #L29 - L30 were not covered by tests

if hub.get_integration(OpenAiIntegration) is None:
return old_open_ai_client_create(**kwargs)

Check warning on line 33 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L33

Added line #L33 was not covered by tests

with hub.start_span(op="openai", description="init") as span:
span.set_data(

Check warning on line 36 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L36

Added line #L36 was not covered by tests
"chat_completion_input",
remove_token_from_keys(kwargs),
)
ret = old_open_ai_resources_chat_completions_create(*args, **kwargs)

Check warning on line 40 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L40

Added line #L40 was not covered by tests
if ret.usage:
span.set_data(

Check warning on line 42 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L42

Added line #L42 was not covered by tests
"chat_completion_output",
remove_token_from_keys(ret.model_dump(exclude_unset=True)),
)

return ret

Check warning on line 47 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L47

Added line #L47 was not covered by tests

resources.chat.completions.Completions.create = monkeypatched_openai

Check warning on line 49 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L49

Added line #L49 was not covered by tests


# ChatCompletion(
# id="chatcmpl-8LhQjMhPt54ydUYBmQ4N3CTtXKsss",
# choices=[
# Choice(
# finish_reason="stop",
# index=0,
# message=ChatCompletionMessage(
# content="b", role="assistant", function_call=None, tool_calls=None
# ),
# )
# ],
# created=1700182525,
# model="gpt-3.5-turbo-0613",
# object="chat.completion",
# system_fingerprint=None,
# usage=CompletionUsage(completion_tokens=1, prompt_tokens=13, total_tokens=14),
# )


def remove_token_from_keys(d):

Check warning on line 71 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L71

Added line #L71 was not covered by tests
"""
Recursively remove mentions of 'token' from the keys of the dictionary.

:param d: The dictionary to process.
:param token: The string to remove from the keys.
:return: A new dictionary with the modified keys.
"""
new_dict = {}

Check warning on line 79 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L79

Added line #L79 was not covered by tests
for key, value in d.items():
# Remove 'token' from the key
new_key = key.replace("token", "tken")

Check warning on line 82 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L82

Added line #L82 was not covered by tests

# Recursively apply the function if the value is a dictionary
if isinstance(value, dict):
value = remove_token_from_keys(value)

Check warning on line 86 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L86

Added line #L86 was not covered by tests

new_dict[new_key] = value

Check warning on line 88 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L88

Added line #L88 was not covered by tests

return new_dict

Check warning on line 90 in sentry_sdk/integrations/openai.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/openai.py#L90

Added line #L90 was not covered by tests