prompty 0.1.23__py2.py3-none-any.whl → 0.1.24__py2.py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
prompty/tracer.py CHANGED
@@ -1,6 +1,7 @@
1
1
  import os
2
2
  import json
3
3
  import inspect
4
+ import numbers
4
5
  import traceback
5
6
  import importlib
6
7
  import contextlib
@@ -139,7 +140,11 @@ def _trace_sync(
139
140
  {
140
141
  "exception": {
141
142
  "type": type(e),
142
- "traceback": traceback.format_tb(tb=e.__traceback__) if e.__traceback__ else None,
143
+ "traceback": (
144
+ traceback.format_tb(tb=e.__traceback__)
145
+ if e.__traceback__
146
+ else None
147
+ ),
143
148
  "message": str(e),
144
149
  "args": to_dict(e.args),
145
150
  }
@@ -179,7 +184,11 @@ def _trace_async(
179
184
  {
180
185
  "exception": {
181
186
  "type": type(e),
182
- "traceback": traceback.format_tb(tb=e.__traceback__) if e.__traceback__ else None,
187
+ "traceback": (
188
+ traceback.format_tb(tb=e.__traceback__)
189
+ if e.__traceback__
190
+ else None
191
+ ),
183
192
  "message": str(e),
184
193
  "args": to_dict(e.args),
185
194
  }
@@ -250,11 +259,10 @@ class PromptyTracer:
250
259
  # hoist usage to parent frame
251
260
  if "result" in frame and isinstance(frame["result"], dict):
252
261
  if "usage" in frame["result"]:
253
- if "__usage" in frame:
254
- for key, value in frame["result"]["usage"].items():
255
- frame["__usage"][key] += value
256
- else:
257
- frame["__usage"] = frame["result"]["usage"]
262
+ frame["__usage"] = self.hoist_item(
263
+ frame["result"]["usage"],
264
+ frame["__usage"] if "__usage" in frame else {},
265
+ )
258
266
 
259
267
  # streamed results may have usage as well
260
268
  if "result" in frame and isinstance(frame["result"], list):
@@ -264,48 +272,59 @@ class PromptyTracer:
264
272
  and "usage" in result
265
273
  and isinstance(result["usage"], dict)
266
274
  ):
267
- if "__usage" not in frame:
268
- frame["__usage"] = {}
269
- for key, value in result["usage"].items():
270
- if key not in frame["__usage"]:
271
- frame["__usage"][key] = value
272
- else:
273
- frame["__usage"][key] += value
275
+ frame["__usage"] = self.hoist_item(
276
+ result["usage"],
277
+ frame["__usage"] if "__usage" in frame else {},
278
+ )
274
279
 
275
280
  # add any usage frames from below
276
281
  if "__frames" in frame:
277
282
  for child in frame["__frames"]:
278
283
  if "__usage" in child:
279
- if "__usage" not in frame:
280
- frame["__usage"] = {}
281
- for key, value in child["__usage"].items():
282
- if key not in frame["__usage"]:
283
- frame["__usage"][key] = value
284
- else:
285
- frame["__usage"][key] += value
284
+ frame["__usage"] = self.hoist_item(
285
+ child["__usage"],
286
+ frame["__usage"] if "__usage" in frame else {},
287
+ )
286
288
 
287
289
  # if stack is empty, dump the frame
288
290
  if len(self.stack) == 0:
289
- trace_file = (
290
- self.output
291
- / f"{frame['name']}.{datetime.now().strftime('%Y%m%d.%H%M%S')}.tracy"
292
- )
293
-
294
- v = importlib.metadata.version("prompty")
295
- enriched_frame = {
296
- "runtime": "python",
297
- "version": v,
298
- "trace": frame,
299
- }
300
-
301
- with open(trace_file, "w") as f:
302
- json.dump(enriched_frame, f, indent=4)
291
+ self.write_trace(frame)
303
292
  # otherwise, append the frame to the parent
304
293
  else:
305
294
  if "__frames" not in self.stack[-1]:
306
295
  self.stack[-1]["__frames"] = []
307
296
  self.stack[-1]["__frames"].append(frame)
308
297
 
298
+ def hoist_item(self, src: Dict[str, Any], cur: Dict[str, Any]) -> None:
299
+ for key, value in src.items():
300
+ if value is None or isinstance(value, list) or isinstance(value, dict):
301
+ continue
302
+ try:
303
+ if key not in cur:
304
+ cur[key] = value
305
+ else:
306
+ cur[key] += value
307
+ except:
308
+ continue
309
+
310
+ return cur
311
+
312
+ def write_trace(self, frame: Dict[str, Any]) -> None:
313
+ trace_file = (
314
+ self.output
315
+ / f"{frame['name']}.{datetime.now().strftime('%Y%m%d.%H%M%S')}.tracy"
316
+ )
317
+
318
+ v = importlib.metadata.version("prompty")
319
+ enriched_frame = {
320
+ "runtime": "python",
321
+ "version": v,
322
+ "trace": frame,
323
+ }
324
+
325
+ with open(trace_file, "w") as f:
326
+ json.dump(enriched_frame, f, indent=4)
327
+
309
328
 
310
329
  @contextlib.contextmanager
311
330
  def console_tracer(name: str) -> Iterator[Callable[[str, Any], None]]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: prompty
3
- Version: 0.1.23
3
+ Version: 0.1.24
4
4
  Summary: Prompty is a new asset class and format for LLM prompts that aims to provide observability, understandability, and portability for developers. It includes spec, tooling, and a runtime. This Prompty runtime supports Python
5
5
  Author-Email: Seth Juarez <seth.juarez@microsoft.com>
6
6
  Requires-Dist: pyyaml>=6.0.1
@@ -22,7 +22,7 @@ Prompty is an asset class and format for LLM prompts designed to enhance observa
22
22
 
23
23
  The file format has a supporting toolchain with a VS Code extension and runtimes in multiple programming languages to simplify and accelerate your AI application development.
24
24
 
25
- The tooling comes together in three ways: the *prompty file asset*, the *VS Code extension tool*, and *runtimes* in multiple programming languges.
25
+ The tooling comes together in three ways: the *prompty file asset*, the *VS Code extension tool*, and *runtimes* in multiple programming languages.
26
26
 
27
27
  ## The Prompty File Format
28
28
  Prompty is a language agnostic prompt asset for creating prompts and engineering the responses. Learn more about the format [here](https://prompty.ai/docs/prompty-file-spec).
@@ -95,7 +95,7 @@ The Prompty runtime comes with a set of built-in invokers that can be used to ex
95
95
 
96
96
 
97
97
  ## Using Tracing in Prompty
98
- Prompty supports tracing to help you understand the execution of your prompts. This functionality is customizeable and can be used to trace the execution of your prompts in a way that makes sense to you. Prompty has two default traces built in: `console_tracer` and `PromptyTracer`. The `console_tracer` writes the trace to the console, and the `PromptyTracer` writes the trace to a JSON file. You can also create your own tracer by creating your own hook.
98
+ Prompty supports tracing to help you understand the execution of your prompts. This functionality is customizable and can be used to trace the execution of your prompts in a way that makes sense to you. Prompty has two default traces built in: `console_tracer` and `PromptyTracer`. The `console_tracer` writes the trace to the console, and the `PromptyTracer` writes the trace to a JSON file. You can also create your own tracer by creating your own hook.
99
99
 
100
100
  ```python
101
101
  import prompty
@@ -117,7 +117,7 @@ print(response)
117
117
  ```
118
118
 
119
119
  You can also bring your own tracer by your own tracing hook. The `console_tracer` is the simplest example of a tracer. It writes the trace to the console.
120
- This is what it loks like:
120
+ This is what it looks like:
121
121
 
122
122
  ```python
123
123
  @contextlib.contextmanager
@@ -212,4 +212,4 @@ prompty -s path/to/prompty/file -e .env
212
212
  This will execute the prompt and print the response to the console. If there are any environment variables the CLI should take into account, you can pass those in via the `-e` flag. It also has default tracing enabled.
213
213
 
214
214
  ## Contributing
215
- We welcome contributions to the Prompty project! This community led project is open to all contributors. The project cvan be found on [GitHub](https://github.com/Microsoft/prompty).
215
+ We welcome contributions to the Prompty project! This community led project is open to all contributors. The project can be found on [GitHub](https://github.com/Microsoft/prompty).
@@ -1,7 +1,7 @@
1
- prompty-0.1.23.dist-info/METADATA,sha256=jxIN-iLH2JzTWPDha6eHMX61oKf-Do0raKMpC4NAXrQ,9037
2
- prompty-0.1.23.dist-info/WHEEL,sha256=CuZGaXTwoRLAOVv0AcE3bCTxO5ejVuBEJkUBe9C-kvk,94
3
- prompty-0.1.23.dist-info/entry_points.txt,sha256=9y1lKPWUpPWRJzUslcVH-gMwbNoa2PzjyoZsKYLQqyw,45
4
- prompty-0.1.23.dist-info/licenses/LICENSE,sha256=KWSC4z9cfML_t0xThoQYjzTdcZQj86Y_mhXdatzU-KM,1052
1
+ prompty-0.1.24.dist-info/METADATA,sha256=Za-iNjmfbvfVGUVDesnmA55OTFhsRMEO86UU0Ry_jMw,9037
2
+ prompty-0.1.24.dist-info/WHEEL,sha256=CuZGaXTwoRLAOVv0AcE3bCTxO5ejVuBEJkUBe9C-kvk,94
3
+ prompty-0.1.24.dist-info/entry_points.txt,sha256=9y1lKPWUpPWRJzUslcVH-gMwbNoa2PzjyoZsKYLQqyw,45
4
+ prompty-0.1.24.dist-info/licenses/LICENSE,sha256=KWSC4z9cfML_t0xThoQYjzTdcZQj86Y_mhXdatzU-KM,1052
5
5
  prompty/__init__.py,sha256=XTUgJ3xT7HYJieuWW5PBItey0BWneg3G7iBBjIeNJZU,11628
6
6
  prompty/azure/__init__.py,sha256=ptGajCh68s_tugPv45Y4GJCyBToNFCExUzUh9yIBIfo,292
7
7
  prompty/azure/executor.py,sha256=4BiVgDcoDaJ2QcSd3O1LIm_3Fh-ln6wjqO2ZoVYjslc,4700
@@ -16,5 +16,5 @@ prompty/renderers.py,sha256=RSHFQFx7AtKLUfsMLCXR0a56Mb7DL1NJNgjUqgg3IqU,776
16
16
  prompty/serverless/__init__.py,sha256=NPqoFATEMQ96G8OQkVcGxUWU4llIQCwxfJePPo8YFY8,279
17
17
  prompty/serverless/executor.py,sha256=jbTnYE2aq8oS5PWcZ96NhhjL-gU1a_tlUoB449QqHaY,4648
18
18
  prompty/serverless/processor.py,sha256=pft1XGbPzo0MzQMbAt1VxsLsvRrjQO3B8MXEE2PfSA0,1982
19
- prompty/tracer.py,sha256=WNI7V9ySGrOywY_StGt3swM7BuT-XtaPTSVSs4DteSs,10709
20
- prompty-0.1.23.dist-info/RECORD,,
19
+ prompty/tracer.py,sha256=qYivi1z-xkXiIikM45uG8yFlEoXwaxoP2Xog_7yjz5Y,10990
20
+ prompty-0.1.24.dist-info/RECORD,,