-
Notifications
You must be signed in to change notification settings - Fork 678
/
__init__.py
268 lines (206 loc) · 7.89 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os
import openai
import json
import types
import logging
from importlib.metadata import version
from opentelemetry import context as context_api
from opentelemetry.semconv.ai import SpanAttributes
from opentelemetry.instrumentation.openai.utils import (
dont_throw,
is_openai_v1,
should_record_stream_token_usage,
)
OPENAI_LLM_USAGE_TOKEN_TYPES = ["prompt_tokens", "completion_tokens"]
# tiktoken encodings map for different model, key is model_name, value is tiktoken encoding
tiktoken_encodings = {}
logger = logging.getLogger(__name__)
def should_send_prompts():
return (
os.getenv("TRACELOOP_TRACE_CONTENT") or "true"
).lower() == "true" or context_api.get_value("override_enable_content_tracing")
def _set_span_attribute(span, name, value):
if value is not None:
if value != "":
span.set_attribute(name, value)
return
def _set_client_attributes(span, instance):
if not span.is_recording():
return
if not is_openai_v1():
return
client = instance._client # pylint: disable=protected-access
if isinstance(client, (openai.AsyncOpenAI, openai.OpenAI)):
_set_span_attribute(
span, SpanAttributes.LLM_OPENAI_API_BASE, str(client.base_url)
)
if isinstance(client, (openai.AsyncAzureOpenAI, openai.AzureOpenAI)):
_set_span_attribute(
span, SpanAttributes.LLM_OPENAI_API_VERSION, client._api_version
) # pylint: disable=protected-access
def _set_api_attributes(span):
if not span.is_recording():
return
if is_openai_v1():
return
base_url = openai.base_url if hasattr(openai, "base_url") else openai.api_base
_set_span_attribute(span, SpanAttributes.LLM_OPENAI_API_BASE, base_url)
_set_span_attribute(span, SpanAttributes.LLM_OPENAI_API_TYPE, openai.api_type)
_set_span_attribute(span, SpanAttributes.LLM_OPENAI_API_VERSION, openai.api_version)
return
def _set_functions_attributes(span, functions):
if not functions:
return
for i, function in enumerate(functions):
prefix = f"{SpanAttributes.LLM_REQUEST_FUNCTIONS}.{i}"
_set_span_attribute(span, f"{prefix}.name", function.get("name"))
_set_span_attribute(span, f"{prefix}.description", function.get("description"))
_set_span_attribute(
span, f"{prefix}.parameters", json.dumps(function.get("parameters"))
)
def set_tools_attributes(span, tools):
if not tools:
return
for i, tool in enumerate(tools):
function = tool.get("function")
if not function:
continue
prefix = f"{SpanAttributes.LLM_REQUEST_FUNCTIONS}.{i}"
_set_span_attribute(span, f"{prefix}.name", function.get("name"))
_set_span_attribute(span, f"{prefix}.description", function.get("description"))
_set_span_attribute(
span, f"{prefix}.parameters", json.dumps(function.get("parameters"))
)
def _set_request_attributes(span, kwargs):
if not span.is_recording():
return
_set_api_attributes(span)
_set_span_attribute(span, SpanAttributes.LLM_SYSTEM, "OpenAI")
_set_span_attribute(span, SpanAttributes.LLM_REQUEST_MODEL, kwargs.get("model"))
_set_span_attribute(
span, SpanAttributes.LLM_REQUEST_MAX_TOKENS, kwargs.get("max_tokens")
)
_set_span_attribute(
span, SpanAttributes.LLM_REQUEST_TEMPERATURE, kwargs.get("temperature")
)
_set_span_attribute(span, SpanAttributes.LLM_REQUEST_TOP_P, kwargs.get("top_p"))
_set_span_attribute(
span, SpanAttributes.LLM_FREQUENCY_PENALTY, kwargs.get("frequency_penalty")
)
_set_span_attribute(
span, SpanAttributes.LLM_PRESENCE_PENALTY, kwargs.get("presence_penalty")
)
_set_span_attribute(span, SpanAttributes.LLM_USER, kwargs.get("user"))
_set_span_attribute(span, SpanAttributes.LLM_HEADERS, str(kwargs.get("headers")))
# The new OpenAI SDK removed the `headers` and create new field called `extra_headers`
if kwargs.get("extra_headers") is not None:
_set_span_attribute(
span, SpanAttributes.LLM_HEADERS, str(kwargs.get("extra_headers"))
)
_set_span_attribute(
span, SpanAttributes.LLM_IS_STREAMING, kwargs.get("stream") or False
)
@dont_throw
def _set_response_attributes(span, response):
if not span.is_recording():
return
_set_span_attribute(span, SpanAttributes.LLM_RESPONSE_MODEL, response.get("model"))
_set_span_attribute(
span,
SpanAttributes.LLM_OPENAI_RESPONSE_SYSTEM_FINGERPRINT,
response.get("system_fingerprint"),
)
usage = response.get("usage")
if not usage:
return
if is_openai_v1() and not isinstance(usage, dict):
usage = usage.__dict__
_set_span_attribute(
span, SpanAttributes.LLM_USAGE_TOTAL_TOKENS, usage.get("total_tokens")
)
_set_span_attribute(
span,
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
usage.get("completion_tokens"),
)
_set_span_attribute(
span, SpanAttributes.LLM_USAGE_PROMPT_TOKENS, usage.get("prompt_tokens")
)
return
@dont_throw
def _set_span_stream_usage(span, prompt_tokens, completion_tokens):
if not span.is_recording():
return
if type(completion_tokens) is int and completion_tokens >= 0:
_set_span_attribute(
span, SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, completion_tokens
)
if type(prompt_tokens) is int and prompt_tokens >= 0:
_set_span_attribute(span, SpanAttributes.LLM_USAGE_PROMPT_TOKENS, prompt_tokens)
if (
type(prompt_tokens) is int
and type(completion_tokens) is int
and completion_tokens + prompt_tokens >= 0
):
_set_span_attribute(
span,
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
completion_tokens + prompt_tokens,
)
def _get_openai_base_url(instance):
if hasattr(instance, "_client"):
client = instance._client # pylint: disable=protected-access
if isinstance(client, (openai.AsyncOpenAI, openai.OpenAI)):
return str(client.base_url)
return ""
def is_streaming_response(response):
if is_openai_v1():
return isinstance(response, openai.Stream) or isinstance(
response, openai.AsyncStream
)
return isinstance(response, types.GeneratorType) or isinstance(
response, types.AsyncGeneratorType
)
def model_as_dict(model):
if version("pydantic") < "2.0.0":
return model.dict()
if hasattr(model, "model_dump"):
return model.model_dump()
elif hasattr(model, "parse"): # Raw API response
return model_as_dict(model.parse())
else:
return model
def get_token_count_from_string(string: str, model_name: str):
if not should_record_stream_token_usage():
return None
import tiktoken
if tiktoken_encodings.get(model_name) is None:
try:
encoding = tiktoken.encoding_for_model(model_name)
except KeyError as ex:
# no such model_name in tiktoken
logger.warning(
f"Failed to get tiktoken encoding for model_name {model_name}, error: {str(ex)}"
)
return None
tiktoken_encodings[model_name] = encoding
else:
encoding = tiktoken_encodings.get(model_name)
token_count = len(encoding.encode(string))
return token_count
def _token_type(token_type: str):
if token_type == "prompt_tokens":
return "input"
elif token_type == "completion_tokens":
return "output"
return None
def _metric_shared_attributes(
response_model: str, operation: str, server_address: str, is_streaming: bool = False
):
return {
SpanAttributes.LLM_SYSTEM: "openai",
SpanAttributes.LLM_RESPONSE_MODEL: response_model,
"gen_ai.operation.name": operation,
"server.address": server_address,
"stream": is_streaming,
}