promptlayer 1.0.14__py3-none-any.whl → 1.0.16__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- promptlayer/__init__.py +1 -1
- promptlayer/promptlayer.py +56 -75
- promptlayer/span_exporter.py +1 -3
- {promptlayer-1.0.14.dist-info → promptlayer-1.0.16.dist-info}/METADATA +1 -1
- {promptlayer-1.0.14.dist-info → promptlayer-1.0.16.dist-info}/RECORD +7 -7
- {promptlayer-1.0.14.dist-info → promptlayer-1.0.16.dist-info}/LICENSE +0 -0
- {promptlayer-1.0.14.dist-info → promptlayer-1.0.16.dist-info}/WHEEL +0 -0
promptlayer/__init__.py
CHANGED
promptlayer/promptlayer.py
CHANGED
|
@@ -61,7 +61,6 @@ class PromptLayer:
|
|
|
61
61
|
self,
|
|
62
62
|
api_key: str = None,
|
|
63
63
|
enable_tracing: bool = False,
|
|
64
|
-
workspace_id: int = None,
|
|
65
64
|
):
|
|
66
65
|
if api_key is None:
|
|
67
66
|
api_key = os.environ.get("PROMPTLAYER_API_KEY")
|
|
@@ -72,15 +71,11 @@ class PromptLayer:
|
|
|
72
71
|
"Please set the PROMPTLAYER_API_KEY environment variable or pass the api_key parameter."
|
|
73
72
|
)
|
|
74
73
|
|
|
75
|
-
if enable_tracing and not workspace_id:
|
|
76
|
-
raise ValueError("Please set a workspace_id to enable tracing.")
|
|
77
|
-
|
|
78
74
|
self.api_key = api_key
|
|
79
75
|
self.templates = TemplateManager(api_key)
|
|
80
76
|
self.group = GroupManager(api_key)
|
|
81
|
-
self.tracer = self._initialize_tracer(api_key, enable_tracing
|
|
77
|
+
self.tracer = self._initialize_tracer(api_key, enable_tracing)
|
|
82
78
|
self.track = TrackManager(api_key)
|
|
83
|
-
self.workspace_id = workspace_id
|
|
84
79
|
|
|
85
80
|
def __getattr__(
|
|
86
81
|
self,
|
|
@@ -111,46 +106,30 @@ class PromptLayer:
|
|
|
111
106
|
raise AttributeError(f"module {__name__} has no attribute {name}")
|
|
112
107
|
|
|
113
108
|
def _create_track_request_callable(
|
|
114
|
-
self,
|
|
109
|
+
self,
|
|
110
|
+
*,
|
|
111
|
+
request_params,
|
|
112
|
+
tags,
|
|
113
|
+
input_variables,
|
|
114
|
+
group_id,
|
|
115
|
+
pl_run_span_id: str | None = None,
|
|
115
116
|
):
|
|
116
117
|
def _track_request(**body):
|
|
117
118
|
track_request_kwargs = self._prepare_track_request_kwargs(
|
|
118
|
-
request_params, tags, input_variables, group_id,
|
|
119
|
+
request_params, tags, input_variables, group_id, pl_run_span_id, **body
|
|
119
120
|
)
|
|
120
|
-
if self.tracer:
|
|
121
|
-
with self.tracer.start_as_current_span("track_request"):
|
|
122
|
-
return track_request(**track_request_kwargs)
|
|
123
121
|
return track_request(**track_request_kwargs)
|
|
124
122
|
|
|
125
123
|
return _track_request
|
|
126
124
|
|
|
127
|
-
def _fetch_prompt_blueprint(self, *, prompt_name, template_params):
|
|
128
|
-
if self.tracer:
|
|
129
|
-
with self.tracer.start_as_current_span("fetch_prompt_template") as span:
|
|
130
|
-
span.set_attribute("prompt_name", prompt_name)
|
|
131
|
-
span.set_attribute(
|
|
132
|
-
"function_input",
|
|
133
|
-
str(
|
|
134
|
-
{"prompt_name": prompt_name, "template_params": template_params}
|
|
135
|
-
),
|
|
136
|
-
)
|
|
137
|
-
result = self.templates.get(prompt_name, template_params)
|
|
138
|
-
span.set_attribute("function_output", str(result))
|
|
139
|
-
return result
|
|
140
|
-
return self.templates.get(prompt_name, template_params)
|
|
141
|
-
|
|
142
125
|
@staticmethod
|
|
143
|
-
def _initialize_tracer(
|
|
144
|
-
api_key: str = None, enable_tracing: bool = False, workspace_id: int = None
|
|
145
|
-
):
|
|
126
|
+
def _initialize_tracer(api_key: str = None, enable_tracing: bool = False):
|
|
146
127
|
if enable_tracing:
|
|
147
128
|
resource = Resource(
|
|
148
129
|
attributes={ResourceAttributes.SERVICE_NAME: "prompt-layer-library"}
|
|
149
130
|
)
|
|
150
131
|
tracer_provider = TracerProvider(resource=resource)
|
|
151
|
-
promptlayer_exporter = PromptLayerSpanExporter(
|
|
152
|
-
api_key=api_key, workspace_id=workspace_id
|
|
153
|
-
)
|
|
132
|
+
promptlayer_exporter = PromptLayerSpanExporter(api_key=api_key)
|
|
154
133
|
span_processor = BatchSpanProcessor(promptlayer_exporter)
|
|
155
134
|
tracer_provider.add_span_processor(span_processor)
|
|
156
135
|
trace.set_tracer_provider(tracer_provider)
|
|
@@ -158,26 +137,6 @@ class PromptLayer:
|
|
|
158
137
|
else:
|
|
159
138
|
return None
|
|
160
139
|
|
|
161
|
-
def _make_llm_request(self, request_params):
|
|
162
|
-
span_id = None
|
|
163
|
-
|
|
164
|
-
if self.tracer:
|
|
165
|
-
with self.tracer.start_as_current_span("llm_request") as span:
|
|
166
|
-
span.set_attribute("provider", request_params["provider"])
|
|
167
|
-
span.set_attribute("function_name", request_params["function_name"])
|
|
168
|
-
span.set_attribute("function_input", str(request_params))
|
|
169
|
-
span_id = hex(span.context.span_id)[2:].zfill(16)
|
|
170
|
-
response = request_params["request_function"](
|
|
171
|
-
request_params["prompt_blueprint"], **request_params["kwargs"]
|
|
172
|
-
)
|
|
173
|
-
span.set_attribute("function_output", str(response))
|
|
174
|
-
else:
|
|
175
|
-
response = request_params["request_function"](
|
|
176
|
-
request_params["prompt_blueprint"], **request_params["kwargs"]
|
|
177
|
-
)
|
|
178
|
-
|
|
179
|
-
return response, span_id
|
|
180
|
-
|
|
181
140
|
@staticmethod
|
|
182
141
|
def _prepare_get_prompt_template_params(
|
|
183
142
|
*, prompt_version, prompt_release_label, input_variables, metadata
|
|
@@ -220,7 +179,14 @@ class PromptLayer:
|
|
|
220
179
|
}
|
|
221
180
|
|
|
222
181
|
def _prepare_track_request_kwargs(
|
|
223
|
-
self,
|
|
182
|
+
self,
|
|
183
|
+
request_params,
|
|
184
|
+
tags,
|
|
185
|
+
input_variables,
|
|
186
|
+
group_id,
|
|
187
|
+
pl_run_span_id: str | None = None,
|
|
188
|
+
metadata: Dict[str, str] | None = None,
|
|
189
|
+
**body,
|
|
224
190
|
):
|
|
225
191
|
return {
|
|
226
192
|
"function_name": request_params["function_name"],
|
|
@@ -235,13 +201,13 @@ class PromptLayer:
|
|
|
235
201
|
datetime.timezone.utc
|
|
236
202
|
).timestamp(),
|
|
237
203
|
"api_key": self.api_key,
|
|
238
|
-
"metadata":
|
|
204
|
+
"metadata": metadata,
|
|
239
205
|
"prompt_id": request_params["prompt_blueprint"]["id"],
|
|
240
206
|
"prompt_version": request_params["prompt_blueprint"]["version"],
|
|
241
207
|
"prompt_input_variables": input_variables,
|
|
242
208
|
"group_id": group_id,
|
|
243
209
|
"return_prompt_blueprint": True,
|
|
244
|
-
"span_id":
|
|
210
|
+
"span_id": pl_run_span_id,
|
|
245
211
|
**body,
|
|
246
212
|
}
|
|
247
213
|
|
|
@@ -256,6 +222,7 @@ class PromptLayer:
|
|
|
256
222
|
metadata: Union[Dict[str, str], None] = None,
|
|
257
223
|
group_id: Union[int, None] = None,
|
|
258
224
|
stream: bool = False,
|
|
225
|
+
pl_run_span_id: str | None = None,
|
|
259
226
|
) -> Dict[str, Any]:
|
|
260
227
|
get_prompt_template_params = self._prepare_get_prompt_template_params(
|
|
261
228
|
prompt_version=prompt_version,
|
|
@@ -263,9 +230,7 @@ class PromptLayer:
|
|
|
263
230
|
input_variables=input_variables,
|
|
264
231
|
metadata=metadata,
|
|
265
232
|
)
|
|
266
|
-
prompt_blueprint = self.
|
|
267
|
-
prompt_name=prompt_name, template_params=get_prompt_template_params
|
|
268
|
-
)
|
|
233
|
+
prompt_blueprint = self.templates.get(prompt_name, get_prompt_template_params)
|
|
269
234
|
prompt_blueprint_model = self._validate_and_extract_model_from_prompt_blueprint(
|
|
270
235
|
prompt_blueprint=prompt_blueprint, prompt_name=prompt_name
|
|
271
236
|
)
|
|
@@ -276,13 +241,19 @@ class PromptLayer:
|
|
|
276
241
|
stream=stream,
|
|
277
242
|
)
|
|
278
243
|
|
|
279
|
-
response
|
|
244
|
+
response = llm_request_params["request_function"](
|
|
245
|
+
llm_request_params["prompt_blueprint"], **llm_request_params["kwargs"]
|
|
246
|
+
)
|
|
280
247
|
|
|
281
248
|
if stream:
|
|
282
249
|
return stream_response(
|
|
283
250
|
response,
|
|
284
251
|
self._create_track_request_callable(
|
|
285
|
-
llm_request_params,
|
|
252
|
+
request_params=llm_request_params,
|
|
253
|
+
tags=tags,
|
|
254
|
+
input_variables=input_variables,
|
|
255
|
+
group_id=group_id,
|
|
256
|
+
pl_run_span_id=pl_run_span_id,
|
|
286
257
|
),
|
|
287
258
|
llm_request_params["stream_function"],
|
|
288
259
|
)
|
|
@@ -292,7 +263,8 @@ class PromptLayer:
|
|
|
292
263
|
tags,
|
|
293
264
|
input_variables,
|
|
294
265
|
group_id,
|
|
295
|
-
|
|
266
|
+
pl_run_span_id,
|
|
267
|
+
metadata=metadata,
|
|
296
268
|
request_response=response.model_dump(),
|
|
297
269
|
)
|
|
298
270
|
|
|
@@ -303,17 +275,24 @@ class PromptLayer:
|
|
|
303
275
|
}
|
|
304
276
|
|
|
305
277
|
def _track_request_log(
|
|
306
|
-
self,
|
|
278
|
+
self,
|
|
279
|
+
request_params,
|
|
280
|
+
tags,
|
|
281
|
+
input_variables,
|
|
282
|
+
group_id,
|
|
283
|
+
pl_run_span_id: str | None = None,
|
|
284
|
+
metadata: Dict[str, str] | None = None,
|
|
285
|
+
**body,
|
|
307
286
|
):
|
|
308
287
|
track_request_kwargs = self._prepare_track_request_kwargs(
|
|
309
|
-
request_params,
|
|
288
|
+
request_params,
|
|
289
|
+
tags,
|
|
290
|
+
input_variables,
|
|
291
|
+
group_id,
|
|
292
|
+
pl_run_span_id,
|
|
293
|
+
metadata=metadata,
|
|
294
|
+
**body,
|
|
310
295
|
)
|
|
311
|
-
if self.tracer:
|
|
312
|
-
with self.tracer.start_as_current_span("track_request") as span:
|
|
313
|
-
span.set_attribute("function_input", str(track_request_kwargs))
|
|
314
|
-
result = track_request(**track_request_kwargs)
|
|
315
|
-
span.set_attribute("function_output", str(result))
|
|
316
|
-
return result
|
|
317
296
|
return track_request(**track_request_kwargs)
|
|
318
297
|
|
|
319
298
|
@staticmethod
|
|
@@ -364,12 +343,14 @@ class PromptLayer:
|
|
|
364
343
|
}
|
|
365
344
|
|
|
366
345
|
if self.tracer:
|
|
367
|
-
with self.tracer.start_as_current_span("PromptLayer
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
result = self._run_internal(
|
|
372
|
-
|
|
346
|
+
with self.tracer.start_as_current_span("PromptLayer Run") as span:
|
|
347
|
+
span.set_attribute("prompt_name", prompt_name)
|
|
348
|
+
span.set_attribute("function_input", str(_run_internal_kwargs))
|
|
349
|
+
pl_run_span_id = hex(span.context.span_id)[2:].zfill(16)
|
|
350
|
+
result = self._run_internal(
|
|
351
|
+
**_run_internal_kwargs, pl_run_span_id=pl_run_span_id
|
|
352
|
+
)
|
|
353
|
+
span.set_attribute("function_output", str(result))
|
|
373
354
|
return result
|
|
374
355
|
else:
|
|
375
356
|
return self._run_internal(**_run_internal_kwargs)
|
promptlayer/span_exporter.py
CHANGED
|
@@ -8,10 +8,9 @@ from promptlayer.utils import URL_API_PROMPTLAYER
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
class PromptLayerSpanExporter(SpanExporter):
|
|
11
|
-
def __init__(self, api_key: str = None
|
|
11
|
+
def __init__(self, api_key: str = None):
|
|
12
12
|
self.api_key = api_key
|
|
13
13
|
self.url = f"{URL_API_PROMPTLAYER}/spans-bulk"
|
|
14
|
-
self.workspace_id = workspace_id
|
|
15
14
|
|
|
16
15
|
def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
|
|
17
16
|
request_data = []
|
|
@@ -65,7 +64,6 @@ class PromptLayerSpanExporter(SpanExporter):
|
|
|
65
64
|
},
|
|
66
65
|
json={
|
|
67
66
|
"spans": request_data,
|
|
68
|
-
"workspace_id": self.workspace_id,
|
|
69
67
|
},
|
|
70
68
|
)
|
|
71
69
|
response.raise_for_status()
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
promptlayer/__init__.py,sha256=
|
|
1
|
+
promptlayer/__init__.py,sha256=NPhgcyisSfks_AFSRjhlScwwHQRlZSKiPBJS-Lm_grg,102
|
|
2
2
|
promptlayer/groups/__init__.py,sha256=-xs-2cn0nc0D_5YxZr3nC86iTdRVZmBhEpOKDJXE-sQ,224
|
|
3
3
|
promptlayer/groups/groups.py,sha256=yeO6T0TM3qB0ondZRiHhcH8G06YygrpFoM8b9RmoIao,165
|
|
4
|
-
promptlayer/promptlayer.py,sha256=
|
|
4
|
+
promptlayer/promptlayer.py,sha256=xqnwYO5p2JPA_mQbP-5l9yQlQ1bKrfms803CrJBSiFA,13824
|
|
5
5
|
promptlayer/promptlayer_base.py,sha256=sev-EZehRXJSZSmJtMkqmAUK1345pqbDY_lNjPP5MYA,7158
|
|
6
|
-
promptlayer/span_exporter.py,sha256=
|
|
6
|
+
promptlayer/span_exporter.py,sha256=zIJNsb3Fe6yb5wKLDmkoPF2wqFjk1p39E0jWHD2plzI,2658
|
|
7
7
|
promptlayer/templates.py,sha256=aY_-BCrL0AgIdYEUE28pi0AP_avTVAgwv5hgzrh75vo,717
|
|
8
8
|
promptlayer/track/__init__.py,sha256=VheO_Au0lffGlPKYYPQwkv8ci16wSXABCVSNRoFWu_w,945
|
|
9
9
|
promptlayer/track/track.py,sha256=XNEZT9yNiRBPp9vaDZo_f0dP_ldOu8q1qafpVfS5Ze8,1610
|
|
10
10
|
promptlayer/types/__init__.py,sha256=ulWSyCrk5hZ_PI-nKGpd6GPcRaK8lqP4wFl0LPNUYWk,61
|
|
11
11
|
promptlayer/types/prompt_template.py,sha256=QbxYSeIubrwp8KmDKdt9syAwzONFPh_So9yr4H73ANQ,4429
|
|
12
12
|
promptlayer/utils.py,sha256=-p0qapUvkZYJd_Dfat3c8LANXWU1JN0bJB91IyjB8iA,29656
|
|
13
|
-
promptlayer-1.0.
|
|
14
|
-
promptlayer-1.0.
|
|
15
|
-
promptlayer-1.0.
|
|
16
|
-
promptlayer-1.0.
|
|
13
|
+
promptlayer-1.0.16.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
14
|
+
promptlayer-1.0.16.dist-info/METADATA,sha256=HpWQPpYbuABRRj4feBBLYpZvviPcIe25q5S0fBKlzR8,4609
|
|
15
|
+
promptlayer-1.0.16.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
16
|
+
promptlayer-1.0.16.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|