prompty 0.1.12__tar.gz → 0.1.13__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- prompty-0.1.13/PKG-INFO +216 -0
- prompty-0.1.13/README.md +197 -0
- {prompty-0.1.12 → prompty-0.1.13}/pyproject.toml +2 -1
- prompty-0.1.12/PKG-INFO +0 -17
- {prompty-0.1.12 → prompty-0.1.13}/LICENSE +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/__init__.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/azure/__init__.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/azure/executor.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/azure/processor.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/cli.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/core.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/openai/__init__.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/openai/executor.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/openai/processor.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/parsers.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/renderers.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/serverless/__init__.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/serverless/executor.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/serverless/processor.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/prompty/tracer.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/fake_azure_executor.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/fake_serverless_executor.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/1contoso.md +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/2contoso.md +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/3contoso.md +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/4contoso.md +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/basic.prompty.md +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/camping.jpg +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/context.prompty.md +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/contoso_multi.md +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/faithfulness.prompty.md +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/generated/groundedness.prompty.md +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/hello_world-goodbye_world-hello_again.embedding.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/hello_world.embedding.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/__init__.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/basic.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/basic.prompty.execution.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/basic_json_output.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/camping.jpg +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/chat.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/context.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/context.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/context.prompty.execution.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/embedding.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/embedding.prompty.execution.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/evaluation.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/faithfulness.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/faithfulness.prompty.execution.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/fake.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/funcfile.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/funcfile.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/functions.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/functions.prompty.execution.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/groundedness.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/groundedness.prompty.execution.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/prompty.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/serverless.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/serverless.prompty.execution.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/serverless_stream.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/serverless_stream.prompty.execution.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/streaming.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/streaming.prompty.execution.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/sub/__init__.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/sub/basic.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/sub/sub/__init__.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/sub/sub/basic.prompty +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/sub/sub/prompty.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/sub/sub/test.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompts/test.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/prompty.json +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/test_common.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/test_execute.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/test_factory_invoker.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/test_path_exec.py +0 -0
- {prompty-0.1.12 → prompty-0.1.13}/tests/test_tracing.py +0 -0
prompty-0.1.13/PKG-INFO
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: prompty
|
3
|
+
Version: 0.1.13
|
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
|
+
Author-Email: Seth Juarez <seth.juarez@microsoft.com>
|
6
|
+
Requires-Dist: pyyaml>=6.0.1
|
7
|
+
Requires-Dist: pydantic>=2.8.2
|
8
|
+
Requires-Dist: jinja2>=3.1.4
|
9
|
+
Requires-Dist: python-dotenv>=1.0.1
|
10
|
+
Requires-Dist: click>=8.1.7
|
11
|
+
Requires-Dist: azure-identity>=1.17.1; extra == "azure"
|
12
|
+
Requires-Dist: openai>=1.35.10; extra == "azure"
|
13
|
+
Requires-Dist: openai>=1.35.10; extra == "openai"
|
14
|
+
Requires-Dist: azure-ai-inference>=1.0.0b3; extra == "serverless"
|
15
|
+
Provides-Extra: azure
|
16
|
+
Provides-Extra: openai
|
17
|
+
Provides-Extra: serverless
|
18
|
+
Description-Content-Type: text/markdown
|
19
|
+
|
20
|
+
|
21
|
+
Prompty is an asset class and format for LLM prompts designed to enhance observability, understandability, and portability for developers. The primary goal is to accelerate the developer inner loop of prompt engineering and prompt source management in a cross-language and cross-platform implementation.
|
22
|
+
|
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
|
+
|
25
|
+
The tooling comes together in three ways: the *prompty file asset*, the *VS Code extension tool*, and *runtimes* in multiple programming languges.
|
26
|
+
|
27
|
+
## The Prompty File Format
|
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).
|
29
|
+
|
30
|
+
Examples prompty file:
|
31
|
+
```markdown
|
32
|
+
---
|
33
|
+
name: Basic Prompt
|
34
|
+
description: A basic prompt that uses the GPT-3 chat API to answer questions
|
35
|
+
authors:
|
36
|
+
- sethjuarez
|
37
|
+
- jietong
|
38
|
+
model:
|
39
|
+
api: chat
|
40
|
+
configuration:
|
41
|
+
api_version: 2023-12-01-preview
|
42
|
+
azure_deployment: gpt-35-turbo
|
43
|
+
azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT}
|
44
|
+
azure_deployment: ${env:AZURE_OPENAI_DEPLOYMENT:gpt-35-turbo}
|
45
|
+
sample:
|
46
|
+
firstName: Jane
|
47
|
+
lastName: Doe
|
48
|
+
question: What is the meaning of life?
|
49
|
+
---
|
50
|
+
system:
|
51
|
+
You are an AI assistant who helps people find information.
|
52
|
+
As the assistant, you answer questions briefly, succinctly,
|
53
|
+
and in a personable manner using markdown and even add some personal flair with appropriate emojis.
|
54
|
+
|
55
|
+
# Customer
|
56
|
+
You are helping {{firstName}} {{lastName}} to find answers to their questions.
|
57
|
+
Use their name to address them in your responses.
|
58
|
+
|
59
|
+
user:
|
60
|
+
{{question}}
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
## The Prompty VS Code Extension
|
65
|
+
Run Prompty files directly in VS Code. This Visual Studio Code extension offers an intuitive prompt playground within VS Code to streamline the prompt engineering process. You can find the Prompty extension in the Visual Studio Code Marketplace.
|
66
|
+
|
67
|
+
Download the [VS Code extension here](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.prompty).
|
68
|
+
|
69
|
+
|
70
|
+
## Using this Prompty Runtime
|
71
|
+
The Python runtime is a simple way to run your prompts in Python. The runtime is available as a Python package and can be installed using pip. Depending on the type of prompt you are running, you may need to install additional dependencies. The runtime is designed to be extensible and can be customized to fit your needs.
|
72
|
+
|
73
|
+
```bash
|
74
|
+
pip install prompty[azure]
|
75
|
+
```
|
76
|
+
|
77
|
+
Simple usage example:
|
78
|
+
|
79
|
+
```python
|
80
|
+
import prompty
|
81
|
+
# import invoker
|
82
|
+
import prompty.azure
|
83
|
+
|
84
|
+
# execute the prompt
|
85
|
+
response = prompty.execute("path/to/prompty/file")
|
86
|
+
|
87
|
+
print(response)
|
88
|
+
```
|
89
|
+
|
90
|
+
## Available Invokers
|
91
|
+
The Prompty runtime comes with a set of built-in invokers that can be used to execute prompts. These include:
|
92
|
+
|
93
|
+
- `azure`: Invokes the Azure OpenAI API
|
94
|
+
- `openai`: Invokes the OpenAI API
|
95
|
+
- `serverless`: Invokes serverless models (like the ones on GitHub) using the [Azure AI Inference client library](https://learn.microsoft.com/en-us/python/api/overview/azure/ai-inference-readme?view=azure-python-preview) (currently only key based authentication is supported with more managed identity support coming soon)
|
96
|
+
|
97
|
+
|
98
|
+
## Using Tracing in Prompty
|
99
|
+
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.
|
100
|
+
|
101
|
+
```python
|
102
|
+
import prompty
|
103
|
+
# import invoker
|
104
|
+
import prompty.azure
|
105
|
+
from prompty.tracer import trace, Tracer, console_tracer, PromptyTracer
|
106
|
+
|
107
|
+
# add console tracer
|
108
|
+
Tracer.add("console", console_tracer)
|
109
|
+
|
110
|
+
# add PromptyTracer
|
111
|
+
json_tracer = PromptyTracer(output_dir="path/to/output")
|
112
|
+
Tracer.add("console", json_tracer.tracer)
|
113
|
+
|
114
|
+
# execute the prompt
|
115
|
+
response = prompty.execute("path/to/prompty/file")
|
116
|
+
|
117
|
+
print(response)
|
118
|
+
```
|
119
|
+
|
120
|
+
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.
|
121
|
+
This is what it loks like:
|
122
|
+
|
123
|
+
```python
|
124
|
+
@contextlib.contextmanager
|
125
|
+
def console_tracer(name: str) -> Iterator[Callable[[str, Any], None]]:
|
126
|
+
try:
|
127
|
+
print(f"Starting {name}")
|
128
|
+
yield lambda key, value: print(f"{key}:\n{json.dumps(value, indent=4)}")
|
129
|
+
finally:
|
130
|
+
print(f"Ending {name}")
|
131
|
+
|
132
|
+
```
|
133
|
+
|
134
|
+
It uses a context manager to define the start and end of the trace so you can do whatever setup and teardown you need. The `yield` statement returns a function that you can use to write the trace. The `console_tracer` writes the trace to the console using the `print` function.
|
135
|
+
|
136
|
+
The `PromptyTracer` is a more complex example of a tracer. This tracer manages its internal state using a full class. Here's an example of the class based approach that writes each function trace to a JSON file:
|
137
|
+
|
138
|
+
```python
|
139
|
+
class SimplePromptyTracer:
|
140
|
+
def __init__(self, output_dir: str):
|
141
|
+
self.output_dir = output_dir
|
142
|
+
self.tracer = self._tracer
|
143
|
+
|
144
|
+
@contextlib.contextmanager
|
145
|
+
def tracer(self, name: str) -> Iterator[Callable[[str, Any], None]]:
|
146
|
+
trace = {}
|
147
|
+
try:
|
148
|
+
yield lambda key, value: trace.update({key: value})
|
149
|
+
finally:
|
150
|
+
with open(os.path.join(self.output_dir, f"{name}.json"), "w") as f:
|
151
|
+
json.dump(trace, f, indent=4)
|
152
|
+
```
|
153
|
+
|
154
|
+
The tracing mechanism is supported for all of the prompty runtime internals and can be used to trace the execution of the prompt along with all of the paramters. There is also a `@trace` decorator that can be used to trace the execution of any function external to the runtime. This is provided as a facility to trace the execution of the prompt and whatever supporting code you have.
|
155
|
+
|
156
|
+
```python
|
157
|
+
import prompty
|
158
|
+
# import invoker
|
159
|
+
import prompty.azure
|
160
|
+
from prompty.tracer import trace, Tracer, PromptyTracer
|
161
|
+
|
162
|
+
json_tracer = PromptyTracer(output_dir="path/to/output")
|
163
|
+
Tracer.add("PromptyTracer", json_tracer.tracer)
|
164
|
+
|
165
|
+
@trace
|
166
|
+
def get_customer(customerId):
|
167
|
+
return {"id": customerId, "firstName": "Sally", "lastName": "Davis"}
|
168
|
+
|
169
|
+
@trace
|
170
|
+
def get_response(customerId, prompt):
|
171
|
+
customer = get_customer(customerId)
|
172
|
+
|
173
|
+
result = prompty.execute(
|
174
|
+
prompt,
|
175
|
+
inputs={"question": question, "customer": customer},
|
176
|
+
)
|
177
|
+
return {"question": question, "answer": result}
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
In this case, whenever this code is executed, a `.ptrace` file will be created in the `path/to/output` directory. This file will contain the trace of the execution of the `get_response` function, the execution of the `get_customer` function, and the prompty internals that generated the response.
|
182
|
+
|
183
|
+
## OpenTelemetry Tracing
|
184
|
+
You can add OpenTelemetry tracing to your application using the same hook mechanism. In your application, you might create something like `trace_span` to trace the execution of your prompts:
|
185
|
+
|
186
|
+
```python
|
187
|
+
from opentelemetry import trace as oteltrace
|
188
|
+
|
189
|
+
_tracer = "prompty"
|
190
|
+
|
191
|
+
@contextlib.contextmanager
|
192
|
+
def trace_span(name: str):
|
193
|
+
tracer = oteltrace.get_tracer(_tracer)
|
194
|
+
with tracer.start_as_current_span(name) as span:
|
195
|
+
yield lambda key, value: span.set_attribute(
|
196
|
+
key, json.dumps(value).replace("\n", "")
|
197
|
+
)
|
198
|
+
|
199
|
+
# adding this hook to the prompty runtime
|
200
|
+
Tracer.add("OpenTelemetry", trace_span)
|
201
|
+
|
202
|
+
```
|
203
|
+
|
204
|
+
This will produce spans during the execution of the prompt that can be sent to an OpenTelemetry collector for further analysis.
|
205
|
+
|
206
|
+
## CLI
|
207
|
+
The Prompty runtime also comes with a CLI tool that allows you to run prompts from the command line. The CLI tool is installed with the Python package.
|
208
|
+
|
209
|
+
```bash
|
210
|
+
prompty -s path/to/prompty/file
|
211
|
+
```
|
212
|
+
|
213
|
+
This will execute the prompt and print the response to the console. It also has default tracing enabled.
|
214
|
+
|
215
|
+
## Contributing
|
216
|
+
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).
|
prompty-0.1.13/README.md
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
|
2
|
+
Prompty is an asset class and format for LLM prompts designed to enhance observability, understandability, and portability for developers. The primary goal is to accelerate the developer inner loop of prompt engineering and prompt source management in a cross-language and cross-platform implementation.
|
3
|
+
|
4
|
+
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.
|
5
|
+
|
6
|
+
The tooling comes together in three ways: the *prompty file asset*, the *VS Code extension tool*, and *runtimes* in multiple programming languges.
|
7
|
+
|
8
|
+
## The Prompty File Format
|
9
|
+
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).
|
10
|
+
|
11
|
+
Examples prompty file:
|
12
|
+
```markdown
|
13
|
+
---
|
14
|
+
name: Basic Prompt
|
15
|
+
description: A basic prompt that uses the GPT-3 chat API to answer questions
|
16
|
+
authors:
|
17
|
+
- sethjuarez
|
18
|
+
- jietong
|
19
|
+
model:
|
20
|
+
api: chat
|
21
|
+
configuration:
|
22
|
+
api_version: 2023-12-01-preview
|
23
|
+
azure_deployment: gpt-35-turbo
|
24
|
+
azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT}
|
25
|
+
azure_deployment: ${env:AZURE_OPENAI_DEPLOYMENT:gpt-35-turbo}
|
26
|
+
sample:
|
27
|
+
firstName: Jane
|
28
|
+
lastName: Doe
|
29
|
+
question: What is the meaning of life?
|
30
|
+
---
|
31
|
+
system:
|
32
|
+
You are an AI assistant who helps people find information.
|
33
|
+
As the assistant, you answer questions briefly, succinctly,
|
34
|
+
and in a personable manner using markdown and even add some personal flair with appropriate emojis.
|
35
|
+
|
36
|
+
# Customer
|
37
|
+
You are helping {{firstName}} {{lastName}} to find answers to their questions.
|
38
|
+
Use their name to address them in your responses.
|
39
|
+
|
40
|
+
user:
|
41
|
+
{{question}}
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
## The Prompty VS Code Extension
|
46
|
+
Run Prompty files directly in VS Code. This Visual Studio Code extension offers an intuitive prompt playground within VS Code to streamline the prompt engineering process. You can find the Prompty extension in the Visual Studio Code Marketplace.
|
47
|
+
|
48
|
+
Download the [VS Code extension here](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.prompty).
|
49
|
+
|
50
|
+
|
51
|
+
## Using this Prompty Runtime
|
52
|
+
The Python runtime is a simple way to run your prompts in Python. The runtime is available as a Python package and can be installed using pip. Depending on the type of prompt you are running, you may need to install additional dependencies. The runtime is designed to be extensible and can be customized to fit your needs.
|
53
|
+
|
54
|
+
```bash
|
55
|
+
pip install prompty[azure]
|
56
|
+
```
|
57
|
+
|
58
|
+
Simple usage example:
|
59
|
+
|
60
|
+
```python
|
61
|
+
import prompty
|
62
|
+
# import invoker
|
63
|
+
import prompty.azure
|
64
|
+
|
65
|
+
# execute the prompt
|
66
|
+
response = prompty.execute("path/to/prompty/file")
|
67
|
+
|
68
|
+
print(response)
|
69
|
+
```
|
70
|
+
|
71
|
+
## Available Invokers
|
72
|
+
The Prompty runtime comes with a set of built-in invokers that can be used to execute prompts. These include:
|
73
|
+
|
74
|
+
- `azure`: Invokes the Azure OpenAI API
|
75
|
+
- `openai`: Invokes the OpenAI API
|
76
|
+
- `serverless`: Invokes serverless models (like the ones on GitHub) using the [Azure AI Inference client library](https://learn.microsoft.com/en-us/python/api/overview/azure/ai-inference-readme?view=azure-python-preview) (currently only key based authentication is supported with more managed identity support coming soon)
|
77
|
+
|
78
|
+
|
79
|
+
## Using Tracing in Prompty
|
80
|
+
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.
|
81
|
+
|
82
|
+
```python
|
83
|
+
import prompty
|
84
|
+
# import invoker
|
85
|
+
import prompty.azure
|
86
|
+
from prompty.tracer import trace, Tracer, console_tracer, PromptyTracer
|
87
|
+
|
88
|
+
# add console tracer
|
89
|
+
Tracer.add("console", console_tracer)
|
90
|
+
|
91
|
+
# add PromptyTracer
|
92
|
+
json_tracer = PromptyTracer(output_dir="path/to/output")
|
93
|
+
Tracer.add("console", json_tracer.tracer)
|
94
|
+
|
95
|
+
# execute the prompt
|
96
|
+
response = prompty.execute("path/to/prompty/file")
|
97
|
+
|
98
|
+
print(response)
|
99
|
+
```
|
100
|
+
|
101
|
+
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.
|
102
|
+
This is what it loks like:
|
103
|
+
|
104
|
+
```python
|
105
|
+
@contextlib.contextmanager
|
106
|
+
def console_tracer(name: str) -> Iterator[Callable[[str, Any], None]]:
|
107
|
+
try:
|
108
|
+
print(f"Starting {name}")
|
109
|
+
yield lambda key, value: print(f"{key}:\n{json.dumps(value, indent=4)}")
|
110
|
+
finally:
|
111
|
+
print(f"Ending {name}")
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
It uses a context manager to define the start and end of the trace so you can do whatever setup and teardown you need. The `yield` statement returns a function that you can use to write the trace. The `console_tracer` writes the trace to the console using the `print` function.
|
116
|
+
|
117
|
+
The `PromptyTracer` is a more complex example of a tracer. This tracer manages its internal state using a full class. Here's an example of the class based approach that writes each function trace to a JSON file:
|
118
|
+
|
119
|
+
```python
|
120
|
+
class SimplePromptyTracer:
|
121
|
+
def __init__(self, output_dir: str):
|
122
|
+
self.output_dir = output_dir
|
123
|
+
self.tracer = self._tracer
|
124
|
+
|
125
|
+
@contextlib.contextmanager
|
126
|
+
def tracer(self, name: str) -> Iterator[Callable[[str, Any], None]]:
|
127
|
+
trace = {}
|
128
|
+
try:
|
129
|
+
yield lambda key, value: trace.update({key: value})
|
130
|
+
finally:
|
131
|
+
with open(os.path.join(self.output_dir, f"{name}.json"), "w") as f:
|
132
|
+
json.dump(trace, f, indent=4)
|
133
|
+
```
|
134
|
+
|
135
|
+
The tracing mechanism is supported for all of the prompty runtime internals and can be used to trace the execution of the prompt along with all of the paramters. There is also a `@trace` decorator that can be used to trace the execution of any function external to the runtime. This is provided as a facility to trace the execution of the prompt and whatever supporting code you have.
|
136
|
+
|
137
|
+
```python
|
138
|
+
import prompty
|
139
|
+
# import invoker
|
140
|
+
import prompty.azure
|
141
|
+
from prompty.tracer import trace, Tracer, PromptyTracer
|
142
|
+
|
143
|
+
json_tracer = PromptyTracer(output_dir="path/to/output")
|
144
|
+
Tracer.add("PromptyTracer", json_tracer.tracer)
|
145
|
+
|
146
|
+
@trace
|
147
|
+
def get_customer(customerId):
|
148
|
+
return {"id": customerId, "firstName": "Sally", "lastName": "Davis"}
|
149
|
+
|
150
|
+
@trace
|
151
|
+
def get_response(customerId, prompt):
|
152
|
+
customer = get_customer(customerId)
|
153
|
+
|
154
|
+
result = prompty.execute(
|
155
|
+
prompt,
|
156
|
+
inputs={"question": question, "customer": customer},
|
157
|
+
)
|
158
|
+
return {"question": question, "answer": result}
|
159
|
+
|
160
|
+
```
|
161
|
+
|
162
|
+
In this case, whenever this code is executed, a `.ptrace` file will be created in the `path/to/output` directory. This file will contain the trace of the execution of the `get_response` function, the execution of the `get_customer` function, and the prompty internals that generated the response.
|
163
|
+
|
164
|
+
## OpenTelemetry Tracing
|
165
|
+
You can add OpenTelemetry tracing to your application using the same hook mechanism. In your application, you might create something like `trace_span` to trace the execution of your prompts:
|
166
|
+
|
167
|
+
```python
|
168
|
+
from opentelemetry import trace as oteltrace
|
169
|
+
|
170
|
+
_tracer = "prompty"
|
171
|
+
|
172
|
+
@contextlib.contextmanager
|
173
|
+
def trace_span(name: str):
|
174
|
+
tracer = oteltrace.get_tracer(_tracer)
|
175
|
+
with tracer.start_as_current_span(name) as span:
|
176
|
+
yield lambda key, value: span.set_attribute(
|
177
|
+
key, json.dumps(value).replace("\n", "")
|
178
|
+
)
|
179
|
+
|
180
|
+
# adding this hook to the prompty runtime
|
181
|
+
Tracer.add("OpenTelemetry", trace_span)
|
182
|
+
|
183
|
+
```
|
184
|
+
|
185
|
+
This will produce spans during the execution of the prompt that can be sent to an OpenTelemetry collector for further analysis.
|
186
|
+
|
187
|
+
## CLI
|
188
|
+
The Prompty runtime also comes with a CLI tool that allows you to run prompts from the command line. The CLI tool is installed with the Python package.
|
189
|
+
|
190
|
+
```bash
|
191
|
+
prompty -s path/to/prompty/file
|
192
|
+
```
|
193
|
+
|
194
|
+
This will execute the prompt and print the response to the console. It also has default tracing enabled.
|
195
|
+
|
196
|
+
## Contributing
|
197
|
+
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).
|
@@ -1,6 +1,7 @@
|
|
1
1
|
[project]
|
2
2
|
name = "prompty"
|
3
3
|
dynamic = []
|
4
|
+
readme = "README.md"
|
4
5
|
description = "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
6
|
authors = [
|
6
7
|
{ name = "Seth Juarez", email = "seth.juarez@microsoft.com" },
|
@@ -12,7 +13,7 @@ dependencies = [
|
|
12
13
|
"python-dotenv>=1.0.1",
|
13
14
|
"click>=8.1.7",
|
14
15
|
]
|
15
|
-
version = "0.1.
|
16
|
+
version = "0.1.13"
|
16
17
|
|
17
18
|
[project.optional-dependencies]
|
18
19
|
azure = [
|
prompty-0.1.12/PKG-INFO
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: prompty
|
3
|
-
Version: 0.1.12
|
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
|
-
Author-Email: Seth Juarez <seth.juarez@microsoft.com>
|
6
|
-
Requires-Dist: pyyaml>=6.0.1
|
7
|
-
Requires-Dist: pydantic>=2.8.2
|
8
|
-
Requires-Dist: jinja2>=3.1.4
|
9
|
-
Requires-Dist: python-dotenv>=1.0.1
|
10
|
-
Requires-Dist: click>=8.1.7
|
11
|
-
Requires-Dist: azure-identity>=1.17.1; extra == "azure"
|
12
|
-
Requires-Dist: openai>=1.35.10; extra == "azure"
|
13
|
-
Requires-Dist: openai>=1.35.10; extra == "openai"
|
14
|
-
Requires-Dist: azure-ai-inference>=1.0.0b3; extra == "serverless"
|
15
|
-
Provides-Extra: azure
|
16
|
-
Provides-Extra: openai
|
17
|
-
Provides-Extra: serverless
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{prompty-0.1.12 → prompty-0.1.13}/tests/hello_world-goodbye_world-hello_again.embedding.json
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|