-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathpatch.py
254 lines (233 loc) · 8.43 KB
/
patch.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
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from contextlib import contextmanager
from typing import (
TYPE_CHECKING,
Any,
AsyncIterable,
Awaitable,
Callable,
Iterable,
MutableSequence,
)
from opentelemetry._events import EventLogger
from opentelemetry.instrumentation.vertexai.utils import (
GenerateContentParams,
get_genai_request_attributes,
get_genai_response_attributes,
get_server_attributes,
get_span_name,
request_to_events,
response_to_events,
)
from opentelemetry.trace import SpanKind, Tracer
if TYPE_CHECKING:
from google.cloud.aiplatform_v1.services.prediction_service import (
async_client,
client,
)
from google.cloud.aiplatform_v1.types import (
content,
prediction_service,
)
from google.cloud.aiplatform_v1beta1.services.prediction_service import (
async_client as async_client_v1beta1,
)
from google.cloud.aiplatform_v1beta1.services.prediction_service import (
client as client_v1beta1,
)
from google.cloud.aiplatform_v1beta1.types import (
content as content_v1beta1,
)
from google.cloud.aiplatform_v1beta1.types import (
prediction_service as prediction_service_v1beta1,
)
# Use parameter signature from
# https://github.com/googleapis/python-aiplatform/blob/v1.76.0/google/cloud/aiplatform_v1/services/prediction_service/client.py#L2088
# to handle named vs positional args robustly
def _extract_params(
request: prediction_service.GenerateContentRequest
| prediction_service_v1beta1.GenerateContentRequest
| dict[Any, Any]
| None = None,
*,
model: str | None = None,
contents: MutableSequence[content.Content]
| MutableSequence[content_v1beta1.Content]
| None = None,
**_kwargs: Any,
) -> GenerateContentParams:
# Request vs the named parameters are mututally exclusive or the RPC will fail
if not request:
return GenerateContentParams(
model=model or "",
contents=contents,
)
if isinstance(request, dict):
return GenerateContentParams(**request)
return GenerateContentParams(
model=request.model,
contents=request.contents,
system_instruction=request.system_instruction,
tools=request.tools,
tool_config=request.tool_config,
labels=request.labels,
safety_settings=request.safety_settings,
generation_config=request.generation_config,
)
class MethodWrappers:
def __init__(
self, tracer: Tracer, event_logger: EventLogger, capture_content: bool
) -> None:
self.tracer = tracer
self.event_logger = event_logger
self.capture_content = capture_content
@contextmanager
def _with_instrumentation(
self,
instance: client.PredictionServiceClient
| client_v1beta1.PredictionServiceClient
| async_client.PredictionServiceAsyncClient
| async_client_v1beta1.PredictionServiceAsyncClient,
args: Any,
kwargs: Any,
):
params = _extract_params(*args, **kwargs)
api_endpoint: str = instance.api_endpoint # type: ignore[reportUnknownMemberType]
span_attributes = {
**get_genai_request_attributes(params),
**get_server_attributes(api_endpoint),
}
span_name = get_span_name(span_attributes)
with self.tracer.start_as_current_span(
name=span_name,
kind=SpanKind.CLIENT,
attributes=span_attributes,
) as span:
for event in request_to_events(
params=params, capture_content=self.capture_content
):
self.event_logger.emit(event)
# TODO: set error.type attribute
# https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-spans.md
def handle_response(
response: prediction_service.GenerateContentResponse
| prediction_service_v1beta1.GenerateContentResponse,
) -> None:
if span.is_recording():
# When streaming, this is called multiple times so attributes would be
# overwritten. In practice, it looks the API only returns the interesting
# attributes on the last streamed response. However, I couldn't find
# documentation for this and setting attributes shouldn't be too expensive.
span.set_attributes(
get_genai_response_attributes(response)
)
for event in response_to_events(
response=response, capture_content=self.capture_content
):
self.event_logger.emit(event)
yield handle_response
def generate_content(
self,
wrapped: Callable[
...,
prediction_service.GenerateContentResponse
| prediction_service_v1beta1.GenerateContentResponse,
],
instance: client.PredictionServiceClient
| client_v1beta1.PredictionServiceClient,
args: Any,
kwargs: Any,
) -> (
prediction_service.GenerateContentResponse
| prediction_service_v1beta1.GenerateContentResponse
):
with self._with_instrumentation(
instance, args, kwargs
) as handle_response:
response = wrapped(*args, **kwargs)
handle_response(response)
return response
async def agenerate_content(
self,
wrapped: Callable[
...,
Awaitable[
prediction_service.GenerateContentResponse
| prediction_service_v1beta1.GenerateContentResponse
],
],
instance: async_client.PredictionServiceAsyncClient
| async_client_v1beta1.PredictionServiceAsyncClient,
args: Any,
kwargs: Any,
) -> (
prediction_service.GenerateContentResponse
| prediction_service_v1beta1.GenerateContentResponse
):
with self._with_instrumentation(
instance, args, kwargs
) as handle_response:
response = await wrapped(*args, **kwargs)
handle_response(response)
return response
def stream_generate_content(
self,
wrapped: Callable[
...,
Iterable[prediction_service.GenerateContentResponse]
| Iterable[prediction_service_v1beta1.GenerateContentResponse],
],
instance: client.PredictionServiceClient
| client_v1beta1.PredictionServiceClient,
args: Any,
kwargs: Any,
) -> Iterable[
prediction_service.GenerateContentResponse
| prediction_service_v1beta1.GenerateContentResponse,
]:
with self._with_instrumentation(
instance, args, kwargs
) as handle_response:
for response in wrapped(*args, **kwargs):
handle_response(response)
yield response
async def astream_generate_content(
self,
wrapped: Callable[
...,
Awaitable[
AsyncIterable[prediction_service.GenerateContentResponse]
]
| Awaitable[
AsyncIterable[
prediction_service_v1beta1.GenerateContentResponse
]
],
],
instance: async_client.PredictionServiceAsyncClient
| async_client_v1beta1.PredictionServiceAsyncClient,
args: Any,
kwargs: Any,
) -> AsyncIterable[
prediction_service.GenerateContentResponse
| prediction_service_v1beta1.GenerateContentResponse,
]:
with self._with_instrumentation(
instance, args, kwargs
) as handle_response:
async for response in await wrapped(*args, **kwargs):
handle_response(response)
yield response