Skip to content

Commit 2b80d61

Browse files
authored
Cache invalidate (#312)
1 parent 12d9ccb commit 2b80d61

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

promptlayer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717
from .promptlayer import AsyncPromptLayer, PromptLayer
1818

19-
__version__ = "1.4.0"
19+
__version__ = "1.4.1"
2020
__all__ = [
2121
"PromptLayer",
2222
"AsyncPromptLayer",

promptlayer/promptlayer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ def __init__(
8585
)
8686
self.track = TrackManager(api_key, self.base_url, self.throw_on_error)
8787

88+
def invalidate(self, prompt_name: Optional[str] = None) -> None:
89+
"""Invalidate SDK template cache for a prompt or entirely."""
90+
self.templates.invalidate(prompt_name)
91+
8892
def __getattr__(
8993
self,
9094
name: Union[Literal["openai"], Literal["anthropic"], Literal["prompts"]],
@@ -468,6 +472,10 @@ def __init__(
468472
)
469473
self.track = AsyncTrackManager(api_key, self.base_url, self.throw_on_error)
470474

475+
def invalidate(self, prompt_name: Optional[str] = None) -> None:
476+
"""Invalidate SDK template cache for a prompt or entirely."""
477+
self.templates.invalidate(prompt_name)
478+
471479
def __getattr__(self, name: Union[Literal["openai"], Literal["anthropic"], Literal["prompts"]]):
472480
if name == "openai":
473481
import openai as openai_module

promptlayer/templates.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Union
2+
from typing import Optional, Union
33

44
from promptlayer import exceptions as _exceptions
55
from promptlayer.span_exporter import set_prompt_span_attributes
@@ -112,6 +112,14 @@ def publish(self, body: PublishPromptTemplate):
112112
self._cache.invalidate(prompt_name)
113113
return result
114114

115+
def invalidate(self, prompt_name: Optional[str] = None) -> None:
116+
if not self._cache:
117+
return
118+
if prompt_name:
119+
self._cache.invalidate(prompt_name)
120+
else:
121+
self._cache.clear()
122+
115123
def all(self, page: int = 1, per_page: int = 30, label: str = None):
116124
return get_all_prompt_templates(self.api_key, self.base_url, self.throw_on_error, page, per_page, label)
117125

@@ -188,3 +196,11 @@ async def _aget_with_cache(self, prompt_name, params):
188196

189197
async def all(self, page: int = 1, per_page: int = 30, label: str = None):
190198
return await aget_all_prompt_templates(self.api_key, self.base_url, self.throw_on_error, page, per_page, label)
199+
200+
def invalidate(self, prompt_name: Optional[str] = None) -> None:
201+
if not self._cache:
202+
return
203+
if prompt_name:
204+
self._cache.invalidate(prompt_name)
205+
else:
206+
self._cache.clear()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "promptlayer"
3-
version = "1.4.0"
3+
version = "1.4.1"
44
description = "PromptLayer is a platform for prompt engineering and tracks your LLM requests."
55
authors = ["Magniv <hello@magniv.io>"]
66
license = "Apache-2.0"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from promptlayer import AsyncPromptLayer, PromptLayer
2+
3+
4+
def test_promptlayer_client_invalidate_prompt_name(promptlayer_api_key, base_url):
5+
client = PromptLayer(api_key=promptlayer_api_key, base_url=base_url, cache_ttl_seconds=60)
6+
cache = client.templates._cache
7+
8+
alpha_key = cache.make_key("alpha")
9+
beta_key = cache.make_key("beta")
10+
cache.put(alpha_key, {"prompt_template": {"type": "chat", "messages": []}})
11+
cache.put(beta_key, {"prompt_template": {"type": "chat", "messages": []}})
12+
13+
client.invalidate("alpha")
14+
15+
alpha_cached, _ = cache.get(alpha_key)
16+
beta_cached, _ = cache.get(beta_key)
17+
assert alpha_cached is None
18+
assert beta_cached is not None
19+
20+
21+
def test_promptlayer_client_invalidate_all(promptlayer_api_key, base_url):
22+
client = PromptLayer(api_key=promptlayer_api_key, base_url=base_url, cache_ttl_seconds=60)
23+
cache = client.templates._cache
24+
25+
first_key = cache.make_key("first")
26+
second_key = cache.make_key("second")
27+
cache.put(first_key, {"prompt_template": {"type": "chat", "messages": []}})
28+
cache.put(second_key, {"prompt_template": {"type": "chat", "messages": []}})
29+
30+
client.invalidate()
31+
32+
first_cached, _ = cache.get(first_key)
33+
second_cached, _ = cache.get(second_key)
34+
assert first_cached is None
35+
assert second_cached is None
36+
37+
38+
def test_async_promptlayer_client_invalidate(promptlayer_api_key, base_url):
39+
client = AsyncPromptLayer(api_key=promptlayer_api_key, base_url=base_url, cache_ttl_seconds=60)
40+
cache = client.templates._cache
41+
42+
key = cache.make_key("async-template")
43+
cache.put(key, {"prompt_template": {"type": "chat", "messages": []}})
44+
45+
client.invalidate("async-template")
46+
47+
cached, _ = cache.get(key)
48+
assert cached is None

0 commit comments

Comments
 (0)