Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion promptlayer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from .promptlayer import AsyncPromptLayer, PromptLayer

__version__ = "1.4.0"
__version__ = "1.4.1"
__all__ = [
"PromptLayer",
"AsyncPromptLayer",
Expand Down
8 changes: 8 additions & 0 deletions promptlayer/promptlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def __init__(
)
self.track = TrackManager(api_key, self.base_url, self.throw_on_error)

def invalidate(self, prompt_name: Optional[str] = None) -> None:
"""Invalidate SDK template cache for a prompt or entirely."""
self.templates.invalidate(prompt_name)

def __getattr__(
self,
name: Union[Literal["openai"], Literal["anthropic"], Literal["prompts"]],
Expand Down Expand Up @@ -468,6 +472,10 @@ def __init__(
)
self.track = AsyncTrackManager(api_key, self.base_url, self.throw_on_error)

def invalidate(self, prompt_name: Optional[str] = None) -> None:
"""Invalidate SDK template cache for a prompt or entirely."""
self.templates.invalidate(prompt_name)

def __getattr__(self, name: Union[Literal["openai"], Literal["anthropic"], Literal["prompts"]]):
if name == "openai":
import openai as openai_module
Expand Down
18 changes: 17 additions & 1 deletion promptlayer/templates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Union
from typing import Optional, Union

from promptlayer import exceptions as _exceptions
from promptlayer.span_exporter import set_prompt_span_attributes
Expand Down Expand Up @@ -112,6 +112,14 @@ def publish(self, body: PublishPromptTemplate):
self._cache.invalidate(prompt_name)
return result

def invalidate(self, prompt_name: Optional[str] = None) -> None:
if not self._cache:
return
if prompt_name:
self._cache.invalidate(prompt_name)
else:
self._cache.clear()

def all(self, page: int = 1, per_page: int = 30, label: str = None):
return get_all_prompt_templates(self.api_key, self.base_url, self.throw_on_error, page, per_page, label)

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

async def all(self, page: int = 1, per_page: int = 30, label: str = None):
return await aget_all_prompt_templates(self.api_key, self.base_url, self.throw_on_error, page, per_page, label)

def invalidate(self, prompt_name: Optional[str] = None) -> None:
if not self._cache:
return
if prompt_name:
self._cache.invalidate(prompt_name)
else:
self._cache.clear()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "promptlayer"
version = "1.4.0"
version = "1.4.1"
description = "PromptLayer is a platform for prompt engineering and tracks your LLM requests."
authors = ["Magniv <hello@magniv.io>"]
license = "Apache-2.0"
Expand Down
48 changes: 48 additions & 0 deletions tests/test_client_cache_invalidation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from promptlayer import AsyncPromptLayer, PromptLayer


def test_promptlayer_client_invalidate_prompt_name(promptlayer_api_key, base_url):
client = PromptLayer(api_key=promptlayer_api_key, base_url=base_url, cache_ttl_seconds=60)
cache = client.templates._cache

alpha_key = cache.make_key("alpha")
beta_key = cache.make_key("beta")
cache.put(alpha_key, {"prompt_template": {"type": "chat", "messages": []}})
cache.put(beta_key, {"prompt_template": {"type": "chat", "messages": []}})

client.invalidate("alpha")

alpha_cached, _ = cache.get(alpha_key)
beta_cached, _ = cache.get(beta_key)
assert alpha_cached is None
assert beta_cached is not None


def test_promptlayer_client_invalidate_all(promptlayer_api_key, base_url):
client = PromptLayer(api_key=promptlayer_api_key, base_url=base_url, cache_ttl_seconds=60)
cache = client.templates._cache

first_key = cache.make_key("first")
second_key = cache.make_key("second")
cache.put(first_key, {"prompt_template": {"type": "chat", "messages": []}})
cache.put(second_key, {"prompt_template": {"type": "chat", "messages": []}})

client.invalidate()

first_cached, _ = cache.get(first_key)
second_cached, _ = cache.get(second_key)
assert first_cached is None
assert second_cached is None


def test_async_promptlayer_client_invalidate(promptlayer_api_key, base_url):
client = AsyncPromptLayer(api_key=promptlayer_api_key, base_url=base_url, cache_ttl_seconds=60)
cache = client.templates._cache

key = cache.make_key("async-template")
cache.put(key, {"prompt_template": {"type": "chat", "messages": []}})

client.invalidate("async-template")

cached, _ = cache.get(key)
assert cached is None
Loading