prompty 0.1.7__py3-none-any.whl → 0.1.8__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
@@ -89,58 +89,92 @@ class Trace:
89
89
  return str(obj)
90
90
 
91
91
 
92
- def trace(func: Callable = None, *, description: str = None) -> Callable:
93
- if func is None:
94
- return partial(trace, description=description)
92
+ def _name(func: Callable, args):
93
+ if hasattr(func, "__qualname__"):
94
+ signature = f"{func.__module__}.{func.__qualname__}"
95
+ else:
96
+ signature = f"{func.__module__}.{func.__name__}"
97
+
98
+ # core invoker gets special treatment
99
+ core_invoker = signature == "prompty.core.Invoker.__call__"
100
+ if core_invoker:
101
+ name = type(args[0]).__name__
102
+ signature = f"{args[0].__module__}.{args[0].__class__.__name__}.invoke"
103
+ else:
104
+ name = func.__name__
105
+
106
+ return name, signature
107
+
108
+
109
+ def _inputs(func: Callable, args, kwargs) -> dict:
110
+ ba = inspect.signature(func).bind(*args, **kwargs)
111
+ ba.apply_defaults()
112
+
113
+ inputs = {k: Trace.to_dict(v) for k, v in ba.arguments.items() if k != "self"}
114
+
115
+ return inputs
95
116
 
117
+ def _results(result: Any) -> dict:
118
+ return {
119
+ "result": Trace.to_dict(result) if result is not None else "None",
120
+ }
121
+
122
+ def _trace_sync(func: Callable = None, *, description: str = None) -> Callable:
96
123
  description = description or ""
97
124
 
98
125
  @wraps(func)
99
126
  def wrapper(*args, **kwargs):
100
- if hasattr(func, "__qualname__"):
101
- signature = f"{func.__module__}.{func.__qualname__}"
102
- else:
103
- signature = f"{func.__module__}.{func.__name__}"
127
+ name, signature = _name(func, args)
128
+ Trace.start(name)
129
+ Trace.add("signature", signature)
130
+ if description and description != "":
131
+ Trace.add("description", description)
104
132
 
105
- # core invoker gets special treatment
106
- core_invoker = signature == "prompty.core.Invoker.__call__"
107
- if core_invoker:
108
- name = type(args[0]).__name__
109
- else:
110
- name = func.__name__
133
+ inputs = _inputs(func, args, kwargs)
134
+ Trace.add("inputs", inputs)
111
135
 
112
- Trace.start(name)
136
+ result = func(*args, **kwargs)
137
+ Trace.add("result", _results(result))
113
138
 
114
- if core_invoker:
115
- Trace.add(
116
- "signature",
117
- f"{args[0].__module__}.{args[0].__class__.__name__}.invoke",
118
- )
119
- else:
120
- Trace.add("signature", signature)
139
+ Trace.end()
121
140
 
122
- if len(description) > 0:
123
- Trace.add("description", description)
141
+ return result
142
+
143
+ return wrapper
124
144
 
125
- ba = inspect.signature(func).bind(*args, **kwargs)
126
- ba.apply_defaults()
145
+ def _trace_async(func: Callable = None, *, description: str = None) -> Callable:
146
+ description = description or ""
127
147
 
128
- inputs = {k: Trace.to_dict(v) for k, v in ba.arguments.items() if k != "self"}
148
+ @wraps(func)
149
+ async def wrapper(*args, **kwargs):
150
+ name, signature = _name(func, args)
151
+ Trace.start(name)
152
+ Trace.add("signature", signature)
153
+ if description and description != "":
154
+ Trace.add("description", description)
129
155
 
130
- Trace.add("input", Trace.to_dict(inputs))
131
- result = func(*args, **kwargs)
156
+ inputs = _inputs(func, args, kwargs)
157
+ Trace.add("inputs", inputs)
132
158
 
133
- Trace.add(
134
- "result",
135
- Trace.to_dict(result) if result is not None else "None",
136
- )
159
+ result = await func(*args, **kwargs)
160
+ Trace.add("result", _results(result))
137
161
 
138
162
  Trace.end()
139
163
 
140
164
  return result
141
-
165
+
142
166
  return wrapper
143
167
 
168
+ def trace(func: Callable = None, *, description: str = None) -> Callable:
169
+ if func is None:
170
+ return partial(trace, description=description)
171
+
172
+ wrapped_method = (
173
+ _trace_async if inspect.iscoroutinefunction(func) else _trace_sync
174
+ )
175
+
176
+ return wrapped_method(func, description=description)
177
+
144
178
 
145
179
  class PromptyTracer(Tracer):
146
180
  _stack: List[Dict[str, Any]] = []
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: prompty
3
- Version: 0.1.7
3
+ Version: 0.1.8
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
  License: MIT
@@ -1,6 +1,6 @@
1
- prompty-0.1.7.dist-info/METADATA,sha256=WnpHqfmMVpAyXSIjChwWCNo5lTB19gLTiYVEm0xCvCU,4665
2
- prompty-0.1.7.dist-info/WHEEL,sha256=rSwsxJWe3vzyR5HCwjWXQruDgschpei4h_giTm0dJVE,90
3
- prompty-0.1.7.dist-info/licenses/LICENSE,sha256=KWSC4z9cfML_t0xThoQYjzTdcZQj86Y_mhXdatzU-KM,1052
1
+ prompty-0.1.8.dist-info/METADATA,sha256=1sVPpxf3pjHAhCIJoXa-v02zF6P5w4aCdBcQZV3kEm4,4665
2
+ prompty-0.1.8.dist-info/WHEEL,sha256=rSwsxJWe3vzyR5HCwjWXQruDgschpei4h_giTm0dJVE,90
3
+ prompty-0.1.8.dist-info/licenses/LICENSE,sha256=KWSC4z9cfML_t0xThoQYjzTdcZQj86Y_mhXdatzU-KM,1052
4
4
  prompty/__init__.py,sha256=Msp8eiKdrDq0wyl6G5DFDH8r5BxM2_E60uzzL7_MJ5w,11183
5
5
  prompty/cli.py,sha256=_bx_l5v7OGhtAn4d_73b8tyfEw7OOkjCqGMQPu0YP5A,2489
6
6
  prompty/core.py,sha256=WYSvognjMUl08FT0_mkcqZfymb_guKcp3sK8_RO4Kq0,13528
@@ -8,5 +8,5 @@ prompty/executors.py,sha256=TankDTAEBTZkvnPfNUw2KNb1TnNuWhyY8TkWOogUXKs,3185
8
8
  prompty/parsers.py,sha256=4mmIn4SVNs8B0R1BufanqUJk8v4r0OEEo8yx6UOxQpA,4670
9
9
  prompty/processors.py,sha256=GmReygLx2XW1UuanlX71HG3rTZL86y0yAGyNdbGWkcg,2366
10
10
  prompty/renderers.py,sha256=RSHFQFx7AtKLUfsMLCXR0a56Mb7DL1NJNgjUqgg3IqU,776
11
- prompty/tracer.py,sha256=ac7b0M94RjeTqmMAypTlqq5kSnfxge4Oo8sJFEqVp9c,5497
12
- prompty-0.1.7.dist-info/RECORD,,
11
+ prompty/tracer.py,sha256=XMS4aJD_Tp76wm2UFB8amtXn7ioGmPBUy11LmklSUFQ,6490
12
+ prompty-0.1.8.dist-info/RECORD,,