springprompt 0.3.0__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.
- springprompt-0.3.0/.gitignore +4 -0
- springprompt-0.3.0/LICENSE +21 -0
- springprompt-0.3.0/PKG-INFO +142 -0
- springprompt-0.3.0/README.md +129 -0
- springprompt-0.3.0/pyproject.toml +34 -0
- springprompt-0.3.0/src/springprompt/__init__.py +201 -0
- springprompt-0.3.0/src/springprompt/__main__.py +123 -0
- springprompt-0.3.0/src/springprompt/_client.py +491 -0
- springprompt-0.3.0/src/springprompt/_hooks.py +463 -0
- springprompt-0.3.0/src/springprompt/_init.py +382 -0
- springprompt-0.3.0/src/springprompt/_scrub.py +306 -0
- springprompt-0.3.0/tests/__init__.py +0 -0
- springprompt-0.3.0/tests/conftest.py +57 -0
- springprompt-0.3.0/tests/fakes.py +266 -0
- springprompt-0.3.0/tests/test_bench.py +83 -0
- springprompt-0.3.0/tests/test_capture.py +474 -0
- springprompt-0.3.0/tests/test_cli.py +60 -0
- springprompt-0.3.0/tests/test_init.py +227 -0
- springprompt-0.3.0/tests/test_scrub.py +179 -0
- springprompt-0.3.0/tests/test_sdks.py +92 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Incremento Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: springprompt
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Copies a sample of your app's LLM calls to Spring Prompt, which tests them on other models.
|
|
5
|
+
Project-URL: Homepage, https://springprompt.com
|
|
6
|
+
Author: Spring Prompt
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# springprompt
|
|
15
|
+
|
|
16
|
+
Copies a sample of your app's LLM calls to Spring Prompt, which tests them on other models and
|
|
17
|
+
tells you where you could be better, faster or cheaper.
|
|
18
|
+
|
|
19
|
+
## Quick start: npx springprompt init
|
|
20
|
+
|
|
21
|
+
In your app's folder:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
pip install springprompt
|
|
25
|
+
python -m springprompt init # or: npx springprompt init
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
It opens Spring Prompt in your browser. Sign in, pick or make a project, check the code matches
|
|
29
|
+
your terminal and confirm "Connect this computer". The terminal then writes a new key to `.env`
|
|
30
|
+
(making sure `.env` is in `.gitignore`), never into your code, prints the two lines to add and
|
|
31
|
+
where, and offers to send a test call. Options: `--project <id>` picks the project in advance,
|
|
32
|
+
`--url` (or `SPRINGPROMPT_URL`) points at another Spring Prompt, `--no-browser` only prints the
|
|
33
|
+
address, and `--yes` asks no questions.
|
|
34
|
+
|
|
35
|
+
## The two lines
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
import springprompt
|
|
39
|
+
springprompt.init()
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Set `SPRINGPROMPT_KEY` (from your Spring Prompt set-up page) and that is all. Without a key the
|
|
43
|
+
package does nothing at all: nothing is patched and nothing is sent.
|
|
44
|
+
|
|
45
|
+
Your calls still go straight to the provider. The package keeps a copy of the request and the
|
|
46
|
+
answer and sends it from a background thread, so it never changes, slows or breaks a call. If
|
|
47
|
+
something goes wrong inside the package three times, it takes itself off and says so once.
|
|
48
|
+
|
|
49
|
+
Check the set-up:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
python -m springprompt init # sign in in the browser; the key goes in .env
|
|
53
|
+
python -m springprompt test # sends one made-up call; it shows on your set-up page
|
|
54
|
+
python -m springprompt status # key set or not, the address, which libraries are hooked
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Python 3.9 or later. No dependencies. MIT licence.
|
|
58
|
+
|
|
59
|
+
## Options
|
|
60
|
+
|
|
61
|
+
`springprompt.init(key=None, *, url=None, hosts=(), mask=(), redact=None, env=None, release=None, share=None, debug=False)`
|
|
62
|
+
|
|
63
|
+
| Option | What it does |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `key` | The ingest key. Default: `SPRINGPROMPT_KEY`. |
|
|
66
|
+
| `url` | Where to send. Default: `SPRINGPROMPT_URL`, else `https://springprompt.com`. |
|
|
67
|
+
| `hosts` | Extra addresses to watch, such as a LiteLLM proxy: `["llm.internal"]`. |
|
|
68
|
+
| `mask` | Any of `"email"`, `"phone"`, `"card"` (13 to 19 digits passing the Luhn check), `"ip"`. |
|
|
69
|
+
| `redact` | Your own function, given each body as text, returning it cleaned. If it raises, that call is not sent. |
|
|
70
|
+
| `env` | Default: `SPRINGPROMPT_ENV`, else `production`. |
|
|
71
|
+
| `release` | Default: `SPRINGPROMPT_RELEASE` or your host's commit variable (Railway, Vercel, Render, Heroku, `GIT_COMMIT`). |
|
|
72
|
+
| `share` | Starting share of calls sent after the first 20 of each prompt (default 0.1). Spring Prompt may change it. |
|
|
73
|
+
| `debug` | Print what the package is doing on stderr. |
|
|
74
|
+
|
|
75
|
+
Calling `init()` again does nothing.
|
|
76
|
+
|
|
77
|
+
Name a prompt so its calls group together, with a block or a decorator (works with `async def`
|
|
78
|
+
too):
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
with springprompt.prompt("ticket-triage"):
|
|
82
|
+
client.chat.completions.create(...)
|
|
83
|
+
|
|
84
|
+
@springprompt.prompt("ticket-triage")
|
|
85
|
+
def triage(ticket): ...
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Without a name, calls are grouped by the file and function that made them.
|
|
89
|
+
|
|
90
|
+
## What is sent
|
|
91
|
+
|
|
92
|
+
For each sampled call: the request body and the answer (the raw stream for streamed answers),
|
|
93
|
+
the host and path, the status, how long it took, the file and function that made the call, and
|
|
94
|
+
the prompt name if you set one. Also counts of calls seen, errors and calls skipped.
|
|
95
|
+
|
|
96
|
+
The first 20 calls of each prompt are sent; after that about one in ten.
|
|
97
|
+
|
|
98
|
+
## What is never sent
|
|
99
|
+
|
|
100
|
+
- Headers of any kind (so never your provider key), and query strings.
|
|
101
|
+
- Calls with images, audio or files in the request, and bodies over 256 KB. These are only
|
|
102
|
+
counted.
|
|
103
|
+
- Key-like text in the bodies (OpenAI, Anthropic, Google, GitHub, Slack, AWS keys, JWTs, bearer
|
|
104
|
+
tokens): replaced with `[key removed]` before anything leaves your app.
|
|
105
|
+
- Anything your `mask` or `redact` takes out.
|
|
106
|
+
|
|
107
|
+
## Tools and the prompt cache
|
|
108
|
+
|
|
109
|
+
Calls that use tools need nothing extra: the tool definitions are in the request and the
|
|
110
|
+
model's tool calls are in the answer, and both are sent as they are. Spring Prompt tests the
|
|
111
|
+
next step the model takes on other models, with the same tools on offer, and never runs a
|
|
112
|
+
tool. A tool that takes an image's address or a file as an argument is not media, and its
|
|
113
|
+
calls are sent (since 0.2.0; before, such a call was only counted).
|
|
114
|
+
|
|
115
|
+
Cost takes your provider's prompt cache into account. The token counts, including how many
|
|
116
|
+
were read from the cache, are read from the usage in each answer. A streamed OpenAI answer
|
|
117
|
+
only has usage if your app asks for it (`stream_options: {"include_usage": true}`).
|
|
118
|
+
|
|
119
|
+
## Serverless
|
|
120
|
+
|
|
121
|
+
The package sends every 5 seconds from a background thread, and again when the process exits.
|
|
122
|
+
On AWS Lambda, Cloud Functions and the like the process can freeze before that, so call
|
|
123
|
+
`springprompt.flush()` at the end of each handler (it waits up to 2 seconds).
|
|
124
|
+
`springprompt.shutdown()` sends what is left and stops watching.
|
|
125
|
+
|
|
126
|
+
Forked workers (Gunicorn, Celery, multiprocessing) are fine: each child starts with an empty
|
|
127
|
+
queue and its own thread.
|
|
128
|
+
|
|
129
|
+
## Supported
|
|
130
|
+
|
|
131
|
+
- The OpenAI and Anthropic SDKs, sync and async, streamed or not. The Google Gen AI and
|
|
132
|
+
Mistral SDKs use the same libraries (`httpx` or `httpx2`) and are caught the same way.
|
|
133
|
+
- `httpx` and `httpx2` used directly.
|
|
134
|
+
- `requests` (streamed answers, `stream=True`, are counted but not copied).
|
|
135
|
+
|
|
136
|
+
Watched by default: OpenAI, Anthropic, Google Gemini API, OpenRouter, Mistral, DeepSeek, xAI,
|
|
137
|
+
Groq, Together, Fireworks, Cerebras and Azure OpenAI (`*.openai.azure.com`).
|
|
138
|
+
|
|
139
|
+
Not yet supported: `aiohttp`, `urllib`, gRPC.
|
|
140
|
+
|
|
141
|
+
Not yet tried: LiteLLM, LangChain, Azure OpenAI with its own SDK settings. They use the
|
|
142
|
+
libraries above, so they should work; tell us if they do not. AWS Bedrock is out of scope.
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# springprompt
|
|
2
|
+
|
|
3
|
+
Copies a sample of your app's LLM calls to Spring Prompt, which tests them on other models and
|
|
4
|
+
tells you where you could be better, faster or cheaper.
|
|
5
|
+
|
|
6
|
+
## Quick start: npx springprompt init
|
|
7
|
+
|
|
8
|
+
In your app's folder:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
pip install springprompt
|
|
12
|
+
python -m springprompt init # or: npx springprompt init
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
It opens Spring Prompt in your browser. Sign in, pick or make a project, check the code matches
|
|
16
|
+
your terminal and confirm "Connect this computer". The terminal then writes a new key to `.env`
|
|
17
|
+
(making sure `.env` is in `.gitignore`), never into your code, prints the two lines to add and
|
|
18
|
+
where, and offers to send a test call. Options: `--project <id>` picks the project in advance,
|
|
19
|
+
`--url` (or `SPRINGPROMPT_URL`) points at another Spring Prompt, `--no-browser` only prints the
|
|
20
|
+
address, and `--yes` asks no questions.
|
|
21
|
+
|
|
22
|
+
## The two lines
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
import springprompt
|
|
26
|
+
springprompt.init()
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Set `SPRINGPROMPT_KEY` (from your Spring Prompt set-up page) and that is all. Without a key the
|
|
30
|
+
package does nothing at all: nothing is patched and nothing is sent.
|
|
31
|
+
|
|
32
|
+
Your calls still go straight to the provider. The package keeps a copy of the request and the
|
|
33
|
+
answer and sends it from a background thread, so it never changes, slows or breaks a call. If
|
|
34
|
+
something goes wrong inside the package three times, it takes itself off and says so once.
|
|
35
|
+
|
|
36
|
+
Check the set-up:
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
python -m springprompt init # sign in in the browser; the key goes in .env
|
|
40
|
+
python -m springprompt test # sends one made-up call; it shows on your set-up page
|
|
41
|
+
python -m springprompt status # key set or not, the address, which libraries are hooked
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Python 3.9 or later. No dependencies. MIT licence.
|
|
45
|
+
|
|
46
|
+
## Options
|
|
47
|
+
|
|
48
|
+
`springprompt.init(key=None, *, url=None, hosts=(), mask=(), redact=None, env=None, release=None, share=None, debug=False)`
|
|
49
|
+
|
|
50
|
+
| Option | What it does |
|
|
51
|
+
| --- | --- |
|
|
52
|
+
| `key` | The ingest key. Default: `SPRINGPROMPT_KEY`. |
|
|
53
|
+
| `url` | Where to send. Default: `SPRINGPROMPT_URL`, else `https://springprompt.com`. |
|
|
54
|
+
| `hosts` | Extra addresses to watch, such as a LiteLLM proxy: `["llm.internal"]`. |
|
|
55
|
+
| `mask` | Any of `"email"`, `"phone"`, `"card"` (13 to 19 digits passing the Luhn check), `"ip"`. |
|
|
56
|
+
| `redact` | Your own function, given each body as text, returning it cleaned. If it raises, that call is not sent. |
|
|
57
|
+
| `env` | Default: `SPRINGPROMPT_ENV`, else `production`. |
|
|
58
|
+
| `release` | Default: `SPRINGPROMPT_RELEASE` or your host's commit variable (Railway, Vercel, Render, Heroku, `GIT_COMMIT`). |
|
|
59
|
+
| `share` | Starting share of calls sent after the first 20 of each prompt (default 0.1). Spring Prompt may change it. |
|
|
60
|
+
| `debug` | Print what the package is doing on stderr. |
|
|
61
|
+
|
|
62
|
+
Calling `init()` again does nothing.
|
|
63
|
+
|
|
64
|
+
Name a prompt so its calls group together, with a block or a decorator (works with `async def`
|
|
65
|
+
too):
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
with springprompt.prompt("ticket-triage"):
|
|
69
|
+
client.chat.completions.create(...)
|
|
70
|
+
|
|
71
|
+
@springprompt.prompt("ticket-triage")
|
|
72
|
+
def triage(ticket): ...
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Without a name, calls are grouped by the file and function that made them.
|
|
76
|
+
|
|
77
|
+
## What is sent
|
|
78
|
+
|
|
79
|
+
For each sampled call: the request body and the answer (the raw stream for streamed answers),
|
|
80
|
+
the host and path, the status, how long it took, the file and function that made the call, and
|
|
81
|
+
the prompt name if you set one. Also counts of calls seen, errors and calls skipped.
|
|
82
|
+
|
|
83
|
+
The first 20 calls of each prompt are sent; after that about one in ten.
|
|
84
|
+
|
|
85
|
+
## What is never sent
|
|
86
|
+
|
|
87
|
+
- Headers of any kind (so never your provider key), and query strings.
|
|
88
|
+
- Calls with images, audio or files in the request, and bodies over 256 KB. These are only
|
|
89
|
+
counted.
|
|
90
|
+
- Key-like text in the bodies (OpenAI, Anthropic, Google, GitHub, Slack, AWS keys, JWTs, bearer
|
|
91
|
+
tokens): replaced with `[key removed]` before anything leaves your app.
|
|
92
|
+
- Anything your `mask` or `redact` takes out.
|
|
93
|
+
|
|
94
|
+
## Tools and the prompt cache
|
|
95
|
+
|
|
96
|
+
Calls that use tools need nothing extra: the tool definitions are in the request and the
|
|
97
|
+
model's tool calls are in the answer, and both are sent as they are. Spring Prompt tests the
|
|
98
|
+
next step the model takes on other models, with the same tools on offer, and never runs a
|
|
99
|
+
tool. A tool that takes an image's address or a file as an argument is not media, and its
|
|
100
|
+
calls are sent (since 0.2.0; before, such a call was only counted).
|
|
101
|
+
|
|
102
|
+
Cost takes your provider's prompt cache into account. The token counts, including how many
|
|
103
|
+
were read from the cache, are read from the usage in each answer. A streamed OpenAI answer
|
|
104
|
+
only has usage if your app asks for it (`stream_options: {"include_usage": true}`).
|
|
105
|
+
|
|
106
|
+
## Serverless
|
|
107
|
+
|
|
108
|
+
The package sends every 5 seconds from a background thread, and again when the process exits.
|
|
109
|
+
On AWS Lambda, Cloud Functions and the like the process can freeze before that, so call
|
|
110
|
+
`springprompt.flush()` at the end of each handler (it waits up to 2 seconds).
|
|
111
|
+
`springprompt.shutdown()` sends what is left and stops watching.
|
|
112
|
+
|
|
113
|
+
Forked workers (Gunicorn, Celery, multiprocessing) are fine: each child starts with an empty
|
|
114
|
+
queue and its own thread.
|
|
115
|
+
|
|
116
|
+
## Supported
|
|
117
|
+
|
|
118
|
+
- The OpenAI and Anthropic SDKs, sync and async, streamed or not. The Google Gen AI and
|
|
119
|
+
Mistral SDKs use the same libraries (`httpx` or `httpx2`) and are caught the same way.
|
|
120
|
+
- `httpx` and `httpx2` used directly.
|
|
121
|
+
- `requests` (streamed answers, `stream=True`, are counted but not copied).
|
|
122
|
+
|
|
123
|
+
Watched by default: OpenAI, Anthropic, Google Gemini API, OpenRouter, Mistral, DeepSeek, xAI,
|
|
124
|
+
Groq, Together, Fireworks, Cerebras and Azure OpenAI (`*.openai.azure.com`).
|
|
125
|
+
|
|
126
|
+
Not yet supported: `aiohttp`, `urllib`, gRPC.
|
|
127
|
+
|
|
128
|
+
Not yet tried: LiteLLM, LangChain, Azure OpenAI with its own SDK settings. They use the
|
|
129
|
+
libraries above, so they should work; tell us if they do not. AWS Bedrock is out of scope.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "springprompt"
|
|
7
|
+
version = "0.3.0"
|
|
8
|
+
description = "Copies a sample of your app's LLM calls to Spring Prompt, which tests them on other models."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
authors = [{ name = "Spring Prompt" }]
|
|
14
|
+
dependencies = []
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://springprompt.com"
|
|
22
|
+
|
|
23
|
+
[tool.hatch.build.targets.wheel]
|
|
24
|
+
packages = ["src/springprompt"]
|
|
25
|
+
|
|
26
|
+
[tool.ruff]
|
|
27
|
+
line-length = 100
|
|
28
|
+
target-version = "py39"
|
|
29
|
+
|
|
30
|
+
[tool.ruff.lint]
|
|
31
|
+
select = ["E", "F", "I", "UP", "B"]
|
|
32
|
+
|
|
33
|
+
[tool.pytest.ini_options]
|
|
34
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""Spring Prompt: copy a sample of your app's LLM calls so Spring Prompt can test them on
|
|
2
|
+
other models.
|
|
3
|
+
|
|
4
|
+
import springprompt
|
|
5
|
+
springprompt.init()
|
|
6
|
+
|
|
7
|
+
Without `SPRINGPROMPT_KEY` (or `init(key=...)`) nothing happens at all. Calls still go
|
|
8
|
+
straight to the provider; this package only keeps a copy and sends it in the background.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import atexit
|
|
14
|
+
import functools
|
|
15
|
+
import inspect
|
|
16
|
+
import os
|
|
17
|
+
import threading
|
|
18
|
+
from collections.abc import Iterable
|
|
19
|
+
from typing import Any, Callable
|
|
20
|
+
|
|
21
|
+
from . import _client, _hooks
|
|
22
|
+
|
|
23
|
+
__version__ = _client.VERSION
|
|
24
|
+
__all__ = ["init", "prompt", "flush", "shutdown", "__version__"]
|
|
25
|
+
|
|
26
|
+
_lock = threading.Lock()
|
|
27
|
+
_registered = False
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def init(
|
|
31
|
+
key: str | None = None,
|
|
32
|
+
*,
|
|
33
|
+
url: str | None = None,
|
|
34
|
+
hosts: Iterable[str] = (),
|
|
35
|
+
mask: Iterable[str] = (),
|
|
36
|
+
redact: Callable[[str], str] | None = None,
|
|
37
|
+
env: str | None = None,
|
|
38
|
+
release: str | None = None,
|
|
39
|
+
share: float | None = None,
|
|
40
|
+
debug: bool = False,
|
|
41
|
+
) -> bool:
|
|
42
|
+
"""Start watching LLM calls. Safe to call more than once; only the first call counts.
|
|
43
|
+
|
|
44
|
+
Returns True when watching, False when there is no key (and so nothing was changed).
|
|
45
|
+
"""
|
|
46
|
+
global _registered
|
|
47
|
+
with _lock:
|
|
48
|
+
if _hooks.state.client is not None:
|
|
49
|
+
return True
|
|
50
|
+
key = key or os.environ.get("SPRINGPROMPT_KEY") or ""
|
|
51
|
+
key = key.strip()
|
|
52
|
+
if not key:
|
|
53
|
+
return False
|
|
54
|
+
try:
|
|
55
|
+
client = _client.Client(
|
|
56
|
+
key,
|
|
57
|
+
url or os.environ.get("SPRINGPROMPT_URL") or _client.DEFAULT_URL,
|
|
58
|
+
hosts=tuple(hosts or ()),
|
|
59
|
+
mask=tuple(mask or ()),
|
|
60
|
+
redact=redact,
|
|
61
|
+
env=env,
|
|
62
|
+
release=release,
|
|
63
|
+
share=share,
|
|
64
|
+
debug=debug,
|
|
65
|
+
)
|
|
66
|
+
_hooks.state.client = client
|
|
67
|
+
_hooks.state.failures = 0
|
|
68
|
+
_hooks.hook_all()
|
|
69
|
+
if not _registered:
|
|
70
|
+
_registered = True
|
|
71
|
+
atexit.register(_at_exit)
|
|
72
|
+
if hasattr(os, "register_at_fork"):
|
|
73
|
+
os.register_at_fork(after_in_child=_after_fork)
|
|
74
|
+
client.log(f"watching; hooked: {', '.join(hooked()) or 'none yet'}")
|
|
75
|
+
except Exception as error: # never break the app's start-up
|
|
76
|
+
_hooks.unhook_all()
|
|
77
|
+
_hooks.state.client = None
|
|
78
|
+
_warn(f"could not start ({type(error).__name__}: {error})")
|
|
79
|
+
return False
|
|
80
|
+
return True
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def flush(timeout: float = 2.0) -> bool:
|
|
84
|
+
"""Send what is waiting now. Call at the end of a serverless handler."""
|
|
85
|
+
client = _hooks.state.client
|
|
86
|
+
if client is None:
|
|
87
|
+
return True
|
|
88
|
+
try:
|
|
89
|
+
return client.flush(timeout)
|
|
90
|
+
except Exception:
|
|
91
|
+
return False
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def shutdown(timeout: float = 2.0) -> None:
|
|
95
|
+
"""Send what is waiting, then stop watching."""
|
|
96
|
+
with _lock:
|
|
97
|
+
client = _hooks.state.client
|
|
98
|
+
_hooks.unhook_all()
|
|
99
|
+
_hooks.state.client = None
|
|
100
|
+
if client is not None:
|
|
101
|
+
try:
|
|
102
|
+
client.close(timeout)
|
|
103
|
+
except Exception:
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def hooked() -> list:
|
|
108
|
+
"""The HTTP libraries currently hooked."""
|
|
109
|
+
return [name for name in _hooks.LIBRARIES if name in _hooks.state.hooked]
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class prompt:
|
|
113
|
+
"""Name the calls made inside it, as a context manager or a decorator.
|
|
114
|
+
|
|
115
|
+
with springprompt.prompt("ticket-triage"):
|
|
116
|
+
client.chat.completions.create(...)
|
|
117
|
+
|
|
118
|
+
@springprompt.prompt("ticket-triage")
|
|
119
|
+
def triage(ticket): ...
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
def __init__(self, name: str) -> None:
|
|
123
|
+
self.name = str(name) if name else None
|
|
124
|
+
self._tokens: list = []
|
|
125
|
+
|
|
126
|
+
def __enter__(self) -> prompt:
|
|
127
|
+
self._tokens.append(_hooks.current_name.set(self.name))
|
|
128
|
+
return self
|
|
129
|
+
|
|
130
|
+
def __exit__(self, *exc: Any) -> None:
|
|
131
|
+
if self._tokens:
|
|
132
|
+
_hooks.current_name.reset(self._tokens.pop())
|
|
133
|
+
|
|
134
|
+
async def __aenter__(self) -> prompt:
|
|
135
|
+
return self.__enter__()
|
|
136
|
+
|
|
137
|
+
async def __aexit__(self, *exc: Any) -> None:
|
|
138
|
+
self.__exit__(*exc)
|
|
139
|
+
|
|
140
|
+
def __call__(self, fn: Callable[..., Any]) -> Callable[..., Any]:
|
|
141
|
+
name = self.name
|
|
142
|
+
if inspect.iscoroutinefunction(fn):
|
|
143
|
+
|
|
144
|
+
@functools.wraps(fn)
|
|
145
|
+
async def run_async(*args: Any, **kwargs: Any) -> Any:
|
|
146
|
+
token = _hooks.current_name.set(name)
|
|
147
|
+
try:
|
|
148
|
+
return await fn(*args, **kwargs)
|
|
149
|
+
finally:
|
|
150
|
+
_hooks.current_name.reset(token)
|
|
151
|
+
|
|
152
|
+
return run_async
|
|
153
|
+
|
|
154
|
+
@functools.wraps(fn)
|
|
155
|
+
def run(*args: Any, **kwargs: Any) -> Any:
|
|
156
|
+
token = _hooks.current_name.set(name)
|
|
157
|
+
try:
|
|
158
|
+
return fn(*args, **kwargs)
|
|
159
|
+
finally:
|
|
160
|
+
_hooks.current_name.reset(token)
|
|
161
|
+
|
|
162
|
+
return run
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _at_exit() -> None:
|
|
166
|
+
client = _hooks.state.client
|
|
167
|
+
if client is not None:
|
|
168
|
+
try:
|
|
169
|
+
client.flush(2.0)
|
|
170
|
+
except Exception:
|
|
171
|
+
pass
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _after_fork() -> None:
|
|
175
|
+
client = _hooks.state.client
|
|
176
|
+
if client is not None:
|
|
177
|
+
try:
|
|
178
|
+
client.after_fork()
|
|
179
|
+
except Exception:
|
|
180
|
+
pass
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _warn(message: str) -> None:
|
|
184
|
+
import sys
|
|
185
|
+
|
|
186
|
+
try:
|
|
187
|
+
sys.stderr.write(f"springprompt: {message}\n")
|
|
188
|
+
except Exception:
|
|
189
|
+
pass
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _reset() -> None:
|
|
193
|
+
"""For tests: forget everything, as if init() had never been called."""
|
|
194
|
+
with _lock:
|
|
195
|
+
client = _hooks.state.client
|
|
196
|
+
_hooks.unhook_all()
|
|
197
|
+
_hooks.state.client = None
|
|
198
|
+
_hooks.state.failures = 0
|
|
199
|
+
if client is not None:
|
|
200
|
+
client.stopped = True
|
|
201
|
+
client.close(timeout=0)
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""`python -m springprompt init`, `test` and `status`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
import time
|
|
9
|
+
import uuid
|
|
10
|
+
|
|
11
|
+
from . import _client
|
|
12
|
+
|
|
13
|
+
USAGE = """usage: python -m springprompt init | test | status
|
|
14
|
+
|
|
15
|
+
init connect this app to a Spring Prompt Live project: sign in in the browser, and
|
|
16
|
+
the key goes in .env (python -m springprompt init --help for options)
|
|
17
|
+
test send one made-up call to check the key works
|
|
18
|
+
status show whether a key is set, where calls go, and which libraries are hooked"""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _test_call() -> dict:
|
|
22
|
+
request = {
|
|
23
|
+
"model": "gpt-test",
|
|
24
|
+
"messages": [{"role": "user", "content": "Say hello to Spring Prompt."}],
|
|
25
|
+
}
|
|
26
|
+
answer = {
|
|
27
|
+
"id": "chatcmpl-test",
|
|
28
|
+
"object": "chat.completion",
|
|
29
|
+
"created": int(time.time()),
|
|
30
|
+
"model": "gpt-test",
|
|
31
|
+
"choices": [
|
|
32
|
+
{
|
|
33
|
+
"index": 0,
|
|
34
|
+
"message": {"role": "assistant", "content": "Hello, Spring Prompt."},
|
|
35
|
+
"finish_reason": "stop",
|
|
36
|
+
}
|
|
37
|
+
],
|
|
38
|
+
"usage": {"prompt_tokens": 8, "completion_tokens": 4, "total_tokens": 12},
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
"id": uuid.uuid4().hex,
|
|
42
|
+
"at": _client.iso(time.time()),
|
|
43
|
+
"host": "api.openai.com",
|
|
44
|
+
"path": "/v1/chat/completions",
|
|
45
|
+
"request": json.dumps(request),
|
|
46
|
+
"answer": json.dumps(answer),
|
|
47
|
+
"status": 200,
|
|
48
|
+
"ms": 420,
|
|
49
|
+
"where": "springprompt test",
|
|
50
|
+
"share": 1.0,
|
|
51
|
+
"test": True,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _url() -> str:
|
|
56
|
+
return (os.environ.get("SPRINGPROMPT_URL") or _client.DEFAULT_URL).rstrip("/")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test(key: str | None = None, url: str | None = None) -> int:
|
|
60
|
+
"""Send one made-up call; `key` and `url` default to the environment's."""
|
|
61
|
+
key = (key or os.environ.get("SPRINGPROMPT_KEY") or "").strip()
|
|
62
|
+
url = (url or _url()).rstrip("/")
|
|
63
|
+
if not key:
|
|
64
|
+
print("No key: set SPRINGPROMPT_KEY first.")
|
|
65
|
+
return 1
|
|
66
|
+
payload = {"sdk": _client.SDK, "env": os.environ.get("SPRINGPROMPT_ENV") or "production"}
|
|
67
|
+
release = _client.release_from_env()
|
|
68
|
+
if release:
|
|
69
|
+
payload["release"] = release
|
|
70
|
+
payload["calls"] = [_test_call()]
|
|
71
|
+
reply = _client.post(url, key, payload)
|
|
72
|
+
if 200 <= reply.status < 300:
|
|
73
|
+
print("Sent. Open your Spring Prompt set-up page to see it arrive.")
|
|
74
|
+
return 0
|
|
75
|
+
if reply.status == 401:
|
|
76
|
+
print("Key refused (401): check SPRINGPROMPT_KEY.")
|
|
77
|
+
elif reply.status == 403:
|
|
78
|
+
print("Key removed (403): make a new key on your Spring Prompt set-up page.")
|
|
79
|
+
elif reply.status == 0:
|
|
80
|
+
print(f"Could not reach {url}: {reply.error}")
|
|
81
|
+
else:
|
|
82
|
+
print(f"Not sent: {url} answered {reply.status}.")
|
|
83
|
+
return 1
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def status() -> int:
|
|
87
|
+
import springprompt
|
|
88
|
+
|
|
89
|
+
key_set = bool((os.environ.get("SPRINGPROMPT_KEY") or "").strip())
|
|
90
|
+
print(f"Key: {'set' if key_set else 'not set'}")
|
|
91
|
+
print(f"URL: {_url()}")
|
|
92
|
+
if not key_set:
|
|
93
|
+
print("Hooked: nothing (no key, so the package does nothing)")
|
|
94
|
+
return 0
|
|
95
|
+
springprompt.init()
|
|
96
|
+
for name in ("httpx", "httpx2", "requests"):
|
|
97
|
+
try:
|
|
98
|
+
__import__(name)
|
|
99
|
+
except ImportError:
|
|
100
|
+
pass
|
|
101
|
+
print(
|
|
102
|
+
f"Hooked: {', '.join(springprompt.hooked()) or 'nothing (no supported library installed)'}"
|
|
103
|
+
)
|
|
104
|
+
springprompt._reset()
|
|
105
|
+
return 0
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def main(argv: list[str] | None = None) -> int:
|
|
109
|
+
args = sys.argv[1:] if argv is None else argv
|
|
110
|
+
if args[:1] == ["init"]:
|
|
111
|
+
from . import _init
|
|
112
|
+
|
|
113
|
+
return _init.run(args[1:])
|
|
114
|
+
if args == ["test"]:
|
|
115
|
+
return test()
|
|
116
|
+
if args == ["status"]:
|
|
117
|
+
return status()
|
|
118
|
+
print(USAGE)
|
|
119
|
+
return 0 if args in (["help"], ["--help"], ["-h"]) else 2
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
if __name__ == "__main__":
|
|
123
|
+
sys.exit(main())
|