|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from sentry_sdk._types import TYPE_CHECKING |
| 4 | + |
| 5 | +if TYPE_CHECKING: |
| 6 | + from typing import Iterator, Any, TypeVar, Callable |
| 7 | + |
| 8 | + F = TypeVar("F", bound=Callable[..., Any]) |
| 9 | + |
| 10 | +from sentry_sdk._functools import wraps |
| 11 | +from sentry_sdk.hub import Hub |
| 12 | +from sentry_sdk.integrations import DidNotEnable, Integration |
| 13 | +from sentry_sdk.utils import logger, capture_internal_exceptions |
| 14 | + |
| 15 | +try: |
| 16 | + from openai.types.chat import ChatCompletionChunk |
| 17 | + from openai.resources.chat.completions import Completions |
| 18 | + from openai.resources import Embeddings |
| 19 | +except ImportError: |
| 20 | + raise DidNotEnable("OpenAI not installed") |
| 21 | + |
| 22 | +try: |
| 23 | + import tiktoken |
| 24 | + |
| 25 | + enc = tiktoken.get_encoding("cl100k_base") |
| 26 | + |
| 27 | + def count_tokens(s): |
| 28 | + # type: (str) -> int |
| 29 | + return len(enc.encode_ordinary(s)) |
| 30 | + |
| 31 | + logger.debug("[OpenAI] using tiktoken to count tokens") |
| 32 | +except ImportError: |
| 33 | + logger.info( |
| 34 | + "The Sentry Python SDK requires 'tiktoken' in order to measure token usage from some OpenAI APIs" |
| 35 | + "Please install 'tiktoken' if you aren't receiving token usage in Sentry." |
| 36 | + "See https://docs.sentry.io/platforms/python/guides/openai/ for more information." |
| 37 | + ) |
| 38 | + |
| 39 | + def count_tokens(s): |
| 40 | + # type: (str) -> int |
| 41 | + return 0 |
| 42 | + |
| 43 | + |
| 44 | +COMPLETION_TOKENS = "completion_tоkens" |
| 45 | +PROMPT_TOKENS = "prompt_tоkens" |
| 46 | +TOTAL_TOKENS = "total_tоkens" |
| 47 | + |
| 48 | + |
| 49 | +class OpenAIIntegration(Integration): |
| 50 | + identifier = "openai" |
| 51 | + |
| 52 | + @staticmethod |
| 53 | + def setup_once(): |
| 54 | + # TODO minimum version |
| 55 | + Completions.create = _wrap_chat_completion_create(Completions.create) |
| 56 | + Embeddings.create = _wrap_enbeddings_create(Embeddings.create) |
| 57 | + |
| 58 | + |
| 59 | +def _calculate_chat_completion_usage( |
| 60 | + messages, response, span, streaming_message_responses=None |
| 61 | +): |
| 62 | + completion_tokens = 0 |
| 63 | + prompt_tokens = 0 |
| 64 | + total_tokens = 0 |
| 65 | + if hasattr(response, "usage"): |
| 66 | + if hasattr(response.usage, "completion_tokens") and isinstance( |
| 67 | + response.usage.completion_tokens, int |
| 68 | + ): |
| 69 | + completion_tokens = response.usage.completion_tokens |
| 70 | + if hasattr(response.usage, "prompt_tokens") and isinstance( |
| 71 | + response.usage.prompt_tokens, int |
| 72 | + ): |
| 73 | + prompt_tokens = response.usage.prompt_tokens |
| 74 | + if hasattr(response.usage, "total_tokens") and isinstance( |
| 75 | + response.usage.total_tokens, int |
| 76 | + ): |
| 77 | + total_tokens = response.usage.total_tokens |
| 78 | + |
| 79 | + if prompt_tokens == 0: |
| 80 | + for message in messages: |
| 81 | + if hasattr(message, "content"): |
| 82 | + prompt_tokens += count_tokens(message.content) |
| 83 | + elif "content" in message: |
| 84 | + prompt_tokens += count_tokens(message["content"]) |
| 85 | + |
| 86 | + if completion_tokens == 0: |
| 87 | + if streaming_message_responses is not None: |
| 88 | + for message in streaming_message_responses: |
| 89 | + completion_tokens += count_tokens(message) |
| 90 | + elif hasattr(response, "choices"): |
| 91 | + for choice in response.choices: |
| 92 | + if hasattr(choice, "message"): |
| 93 | + completion_tokens += count_tokens(choice.message) |
| 94 | + |
| 95 | + if total_tokens == 0: |
| 96 | + total_tokens = prompt_tokens + completion_tokens |
| 97 | + |
| 98 | + if completion_tokens != 0: |
| 99 | + span.set_data(COMPLETION_TOKENS, completion_tokens) |
| 100 | + if prompt_tokens != 0: |
| 101 | + span.set_data(PROMPT_TOKENS, prompt_tokens) |
| 102 | + if total_tokens != 0: |
| 103 | + span.set_data(TOTAL_TOKENS, total_tokens) |
| 104 | + |
| 105 | + |
| 106 | +def _wrap_chat_completion_create(f): |
| 107 | + # type: (F) -> F |
| 108 | + @wraps(f) |
| 109 | + def new_chat_completion(*args, **kwargs): |
| 110 | + # type: (*Any, **Any) -> Any |
| 111 | + hub = Hub.current |
| 112 | + integration = hub.get_integration(OpenAIIntegration) |
| 113 | + if integration is None: |
| 114 | + return f(*args, **kwargs) |
| 115 | + |
| 116 | + if "messages" not in kwargs: |
| 117 | + # invalid call (in all versions of openai), let it return error |
| 118 | + return f(*args, **kwargs) |
| 119 | + |
| 120 | + try: |
| 121 | + iter(kwargs["messages"]) |
| 122 | + except TypeError: |
| 123 | + # invalid call (in all versions), messages must be iterable |
| 124 | + return f(*args, **kwargs) |
| 125 | + |
| 126 | + kwargs["messages"] = list(kwargs["messages"]) |
| 127 | + messages = kwargs["messages"] |
| 128 | + model = kwargs.get("model") |
| 129 | + streaming = kwargs.get("stream") # TODO handle streaming |
| 130 | + |
| 131 | + span = hub.start_span(op="openai", description="Chat Completion") |
| 132 | + span.__enter__() |
| 133 | + res = f(*args, **kwargs) |
| 134 | + with capture_internal_exceptions(): |
| 135 | + span.set_data("messages", messages) |
| 136 | + span.set_tag("model", model) |
| 137 | + span.set_tag("streaming", streaming) |
| 138 | + |
| 139 | + if hasattr(res, "choices"): |
| 140 | + span.set_data("response", res.choices[0].message) |
| 141 | + _calculate_chat_completion_usage(messages, res, span) |
| 142 | + span.__exit__(None, None, None) |
| 143 | + elif hasattr(res, "_iterator"): |
| 144 | + data_buf: list[list[str]] = [] # one for each choice |
| 145 | + |
| 146 | + old_iterator: Iterator[ChatCompletionChunk] = res._iterator |
| 147 | + |
| 148 | + def new_iterator() -> Iterator[ChatCompletionChunk]: |
| 149 | + with capture_internal_exceptions(): |
| 150 | + for x in old_iterator: |
| 151 | + if hasattr(x, "choices"): |
| 152 | + choice_index = 0 |
| 153 | + for choice in x.choices: |
| 154 | + if hasattr(choice, "delta") and hasattr( |
| 155 | + choice.delta, "content" |
| 156 | + ): |
| 157 | + content = choice.delta.content |
| 158 | + if len(data_buf) <= choice_index: |
| 159 | + data_buf.append([]) |
| 160 | + data_buf[choice_index].append(content or "") |
| 161 | + choice_index += 1 |
| 162 | + yield x |
| 163 | + if len(data_buf) > 0: |
| 164 | + all_responses = list( |
| 165 | + map(lambda chunk: "".join(chunk), data_buf) |
| 166 | + ) |
| 167 | + span.set_data("responses", all_responses) |
| 168 | + _calculate_chat_completion_usage( |
| 169 | + messages, res, span, all_responses |
| 170 | + ) |
| 171 | + span.__exit__(None, None, None) |
| 172 | + |
| 173 | + res._iterator = new_iterator() |
| 174 | + else: |
| 175 | + span.set_tag("unknown_response", True) |
| 176 | + span.__exit__(None, None, None) |
| 177 | + return res |
| 178 | + |
| 179 | + return new_chat_completion |
| 180 | + |
| 181 | + |
| 182 | +def _wrap_enbeddings_create(f): |
| 183 | + # type: (F) -> F |
| 184 | + |
| 185 | + @wraps(f) |
| 186 | + def new_embeddings_create(*args, **kwargs): |
| 187 | + hub = Hub.current |
| 188 | + integration = hub.get_integration(OpenAIIntegration) |
| 189 | + if integration is None: |
| 190 | + return f(*args, **kwargs) |
| 191 | + |
| 192 | + with hub.start_span(op="openai", description="Embeddings Creation") as span: |
| 193 | + if "input" in kwargs and isinstance(kwargs["input"], str): |
| 194 | + span.set_data("input", kwargs["input"]) |
| 195 | + if "model" in kwargs: |
| 196 | + span.set_tag("model", kwargs["model"]) |
| 197 | + if "dimensions" in kwargs: |
| 198 | + span.set_tag("dimensions", kwargs["dimensions"]) |
| 199 | + response = f(*args, **kwargs) |
| 200 | + |
| 201 | + prompt_tokens = 0 |
| 202 | + total_tokens = 0 |
| 203 | + if hasattr(response, "usage"): |
| 204 | + if hasattr(response.usage, "prompt_tokens") and isinstance( |
| 205 | + response.usage.prompt_tokens, int |
| 206 | + ): |
| 207 | + prompt_tokens = response.usage.prompt_tokens |
| 208 | + if hasattr(response.usage, "total_tokens") and isinstance( |
| 209 | + response.usage.total_tokens, int |
| 210 | + ): |
| 211 | + total_tokens = response.usage.total_tokens |
| 212 | + |
| 213 | + if prompt_tokens == 0: |
| 214 | + prompt_tokens = count_tokens(kwargs["input"] or "") |
| 215 | + |
| 216 | + if total_tokens == 0: |
| 217 | + total_tokens = prompt_tokens |
| 218 | + |
| 219 | + span.set_data(PROMPT_TOKENS, prompt_tokens) |
| 220 | + span.set_data(TOTAL_TOKENS, total_tokens) |
| 221 | + |
| 222 | + return response |
| 223 | + |
| 224 | + return new_embeddings_create |
0 commit comments