springprompt 0.3.0__py3-none-any.whl

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.
@@ -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())