promptlayer 1.0.14__tar.gz → 1.0.16__tar.gz

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.

Potentially problematic release.


This version of promptlayer might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: promptlayer
3
- Version: 1.0.14
3
+ Version: 1.0.16
4
4
  Summary: PromptLayer is a platform for prompt engineering and tracks your LLM requests.
5
5
  License: Apache-2.0
6
6
  Author: Magniv
@@ -1,4 +1,4 @@
1
1
  from .promptlayer import PromptLayer
2
2
 
3
- __version__ = "1.0.14"
3
+ __version__ = "1.0.16"
4
4
  __all__ = ["PromptLayer", "__version__"]
@@ -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, workspace_id)
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, request_params, tags, input_variables, group_id, span_id
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, span_id, **body
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, request_params, tags, input_variables, group_id, span_id, **body
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": request_params.get("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": 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._fetch_prompt_blueprint(
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, span_id = self._make_llm_request(llm_request_params)
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, tags, input_variables, group_id, span_id
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
- span_id,
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, request_params, tags, input_variables, group_id, span_id, **body
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, tags, input_variables, group_id, span_id, **body
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.run") as main_span:
368
- main_span.set_attribute("prompt_name", prompt_name)
369
- main_span.set_attribute("stream", stream)
370
- main_span.set_attribute("function_input", str(_run_internal_kwargs))
371
- result = self._run_internal(**_run_internal_kwargs)
372
- main_span.set_attribute("function_output", str(result))
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)
@@ -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, workspace_id: int = 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,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "promptlayer"
3
- version = "1.0.14"
3
+ version = "1.0.16"
4
4
  description = "PromptLayer is a platform for prompt engineering and tracks your LLM requests."
5
5
  authors = ["Magniv <hello@magniv.io>"]
6
6
  license = "Apache-2.0"
File without changes
File without changes