lucidicai 3.3.0__py3-none-any.whl → 3.4.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.
lucidicai/__init__.py CHANGED
@@ -34,11 +34,14 @@ from .core.errors import (
34
34
  FeatureFlagError,
35
35
  )
36
36
 
37
+ # Prompt object
38
+ from .api.resources.prompt import Prompt
39
+
37
40
  # Integrations
38
41
  from .integrations.livekit import setup_livekit
39
42
 
40
43
  # Version
41
- __version__ = "3.3.0"
44
+ __version__ = "3.4.0"
42
45
 
43
46
  # All exports
44
47
  __all__ = [
@@ -53,6 +56,8 @@ __all__ = [
53
56
  "InvalidOperationError",
54
57
  "PromptError",
55
58
  "FeatureFlagError",
59
+ # Prompt object
60
+ "Prompt",
56
61
  # Integrations
57
62
  "setup_livekit",
58
63
  # Version
@@ -1,58 +1,118 @@
1
1
  """Prompt resource API operations."""
2
2
  import logging
3
- from typing import Any, Dict, Optional
3
+ import time
4
+ from dataclasses import dataclass
5
+ from typing import Any, Dict, Optional, Tuple, TYPE_CHECKING
4
6
 
5
7
  from ..client import HttpClient
6
8
 
9
+ if TYPE_CHECKING:
10
+ from ...core.config import SDKConfig
11
+
7
12
  logger = logging.getLogger("Lucidic")
8
13
 
9
14
 
15
+ @dataclass
16
+ class Prompt:
17
+ """Represents a prompt retrieved from the Lucidic prompt database."""
18
+
19
+ raw_content: str
20
+ content: str
21
+ metadata: Dict[str, Any]
22
+
23
+ def __str__(self) -> str:
24
+ return self.content
25
+
26
+
10
27
  class PromptResource:
11
28
  """Handle prompt-related API operations."""
12
29
 
13
- def __init__(self, http: HttpClient, production: bool = False):
30
+ def __init__(self, http: HttpClient, config: "SDKConfig", production: bool = False):
14
31
  """Initialize prompt resource.
15
32
 
16
33
  Args:
17
34
  http: HTTP client instance
35
+ config: SDK configuration
18
36
  production: Whether to suppress errors in production mode
19
37
  """
20
38
  self.http = http
39
+ self._config = config
21
40
  self._production = production
41
+ self._cache: Dict[Tuple[str, str], Dict[str, Any]] = {}
42
+
43
+ def _is_cache_valid(self, cache_key: Tuple[str, str], cache_ttl: int) -> bool:
44
+ """Check if a cached prompt is still valid.
45
+
46
+ Args:
47
+ cache_key: The (prompt_name, label) tuple
48
+ cache_ttl: Cache TTL in seconds (-1 = indefinite, 0 = no cache)
49
+
50
+ Returns:
51
+ True if cache is valid, False otherwise
52
+ """
53
+ if cache_ttl == 0:
54
+ return False
55
+ if cache_key not in self._cache:
56
+ return False
57
+ if cache_ttl == -1:
58
+ return True
59
+ cached = self._cache[cache_key]
60
+ return (time.time() - cached["timestamp"]) < cache_ttl
22
61
 
23
62
  def get(
24
63
  self,
25
64
  prompt_name: str,
26
65
  variables: Optional[Dict[str, Any]] = None,
27
66
  label: str = "production",
28
- ) -> str:
67
+ cache_ttl: int = 0,
68
+ ) -> Prompt:
29
69
  """Get a prompt from the prompt database.
30
70
 
31
71
  Args:
32
72
  prompt_name: Name of the prompt.
33
73
  variables: Variables to interpolate into the prompt.
34
74
  label: Prompt version label (default: "production").
75
+ cache_ttl: Cache TTL in seconds. 0 = no cache, -1 = cache indefinitely,
76
+ positive value = seconds before refetching.
35
77
 
36
78
  Returns:
37
- The prompt content with variables interpolated.
79
+ A Prompt object with raw_content, content (with variables replaced),
80
+ and metadata. Use str(prompt) for backward-compatible string access.
38
81
  """
39
82
  try:
40
- response = self.http.get(
41
- "getprompt",
42
- {"prompt_name": prompt_name, "label": label},
43
- )
44
- prompt = response.get("prompt_content", "")
83
+ cache_key = (prompt_name, label)
84
+
85
+ # Check cache
86
+ if self._is_cache_valid(cache_key, cache_ttl):
87
+ raw_content = self._cache[cache_key]["content"]
88
+ metadata = self._cache[cache_key]["metadata"]
89
+ else:
90
+ response = self.http.get(
91
+ "getprompt",
92
+ {"prompt_name": prompt_name, "label": label, "agent_id": self._config.agent_id},
93
+ )
94
+ raw_content = response.get("prompt_content", "")
95
+ metadata = response.get("metadata", {})
96
+
97
+ # Store in cache if caching is enabled
98
+ if cache_ttl != 0:
99
+ self._cache[cache_key] = {
100
+ "content": raw_content,
101
+ "metadata": metadata,
102
+ "timestamp": time.time(),
103
+ }
45
104
 
46
105
  # Replace variables
106
+ content = raw_content
47
107
  if variables:
48
108
  for key, value in variables.items():
49
- prompt = prompt.replace(f"{{{key}}}", str(value))
109
+ content = content.replace(f"{{{{{key}}}}}", str(value))
50
110
 
51
- return prompt
111
+ return Prompt(raw_content=raw_content, content=content, metadata=metadata)
52
112
  except Exception as e:
53
113
  if self._production:
54
114
  logger.error(f"[PromptResource] Failed to get prompt: {e}")
55
- return ""
115
+ return Prompt(raw_content="", content="", metadata={})
56
116
  raise
57
117
 
58
118
  async def aget(
@@ -60,25 +120,43 @@ class PromptResource:
60
120
  prompt_name: str,
61
121
  variables: Optional[Dict[str, Any]] = None,
62
122
  label: str = "production",
63
- ) -> str:
123
+ cache_ttl: int = 0,
124
+ ) -> Prompt:
64
125
  """Get a prompt from the prompt database (asynchronous).
65
126
 
66
127
  See get() for full documentation.
67
128
  """
68
129
  try:
69
- response = await self.http.aget(
70
- "getprompt",
71
- {"prompt_name": prompt_name, "label": label},
72
- )
73
- prompt = response.get("prompt_content", "")
130
+ cache_key = (prompt_name, label)
131
+
132
+ # Check cache
133
+ if self._is_cache_valid(cache_key, cache_ttl):
134
+ raw_content = self._cache[cache_key]["content"]
135
+ metadata = self._cache[cache_key]["metadata"]
136
+ else:
137
+ response = await self.http.aget(
138
+ "getprompt",
139
+ {"prompt_name": prompt_name, "label": label, "agent_id": self._config.agent_id},
140
+ )
141
+ raw_content = response.get("prompt_content", "")
142
+ metadata = response.get("metadata", {})
143
+
144
+ # Store in cache if caching is enabled
145
+ if cache_ttl != 0:
146
+ self._cache[cache_key] = {
147
+ "content": raw_content,
148
+ "metadata": metadata,
149
+ "timestamp": time.time(),
150
+ }
74
151
 
152
+ content = raw_content
75
153
  if variables:
76
154
  for key, value in variables.items():
77
- prompt = prompt.replace(f"{{{key}}}", str(value))
155
+ content = content.replace(f"{{{{{key}}}}}", str(value))
78
156
 
79
- return prompt
157
+ return Prompt(raw_content=raw_content, content=content, metadata=metadata)
80
158
  except Exception as e:
81
159
  if self._production:
82
160
  logger.error(f"[PromptResource] Failed to get prompt: {e}")
83
- return ""
161
+ return Prompt(raw_content="", content="", metadata={})
84
162
  raise
lucidicai/client.py CHANGED
@@ -155,7 +155,7 @@ class LucidicAI:
155
155
  "events": EventResource(self._http, self._production),
156
156
  "datasets": DatasetResource(self._http, self._config.agent_id, self._production),
157
157
  "experiments": ExperimentResource(self._http, self._config.agent_id, self._production),
158
- "prompts": PromptResource(self._http, self._production),
158
+ "prompts": PromptResource(self._http, self._config, self._production),
159
159
  "feature_flags": FeatureFlagResource(self._http, self._config.agent_id, self._production),
160
160
  "evals": EvalsResource(self._http, self._production),
161
161
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lucidicai
3
- Version: 3.3.0
3
+ Version: 3.4.0
4
4
  Summary: Lucidic AI Python SDK
5
5
  Author: Andy Liang
6
6
  Author-email: andy@lucidic.ai
@@ -1,6 +1,6 @@
1
- lucidicai/__init__.py,sha256=gdk3Y-cq-RaTbhBcBE77saBwHSbIvqaScU9gNoTi7_g,1284
1
+ lucidicai/__init__.py,sha256=F60cdLYiZ2P2xh5B1GXzIUFckj-HUwGZimMOAfepXUk,1376
2
2
  lucidicai/action.py,sha256=sPRd1hTIVXDqnvG9ZXWEipUFh0bsXcE0Fm7RVqmVccM,237
3
- lucidicai/client.py,sha256=LC9KPhZgTnt5klntHoyCQWWpRUE-X58lM80uAaWBNiU,15090
3
+ lucidicai/client.py,sha256=BGKP91_Oj5kHQU0osYf1T_BWakL8KIhs0AgUc5X99sU,15104
4
4
  lucidicai/constants.py,sha256=zN8O7TjoRHRlaGa9CZUWppS73rhzKGwaEkF9XMTV0Cg,1160
5
5
  lucidicai/context.py,sha256=ruEXAndSv0gQ-YEXLlC4Fx6NNbaylfp_dZxbpwmLZSA,4622
6
6
  lucidicai/dataset.py,sha256=wu25X02JyWkht_yQabgQpGZFfzbNTxG6tf5k9ol8Amo,4005
@@ -26,7 +26,7 @@ lucidicai/api/resources/evals.py,sha256=_3nLE6dMLht844mWw7kl_hctjv5JIuC6MP06YWUg
26
26
  lucidicai/api/resources/event.py,sha256=GTIU5sIbLNTWAHk4rB120xWTRkhnraz9JNfamEygyNo,14267
27
27
  lucidicai/api/resources/experiment.py,sha256=fOIKJ5d89bHJBVZ3wjbhY_6XF3kLHz9TE3BVPA5pNpA,3563
28
28
  lucidicai/api/resources/feature_flag.py,sha256=ii412DIkZCEAhrXdGydcpQKveqGlFq4NlgdmWQnU83c,2259
29
- lucidicai/api/resources/prompt.py,sha256=tdMVTaLc3DDRbd_R8Xd5mkvpdwQONfr8OwkJRTE0atE,2495
29
+ lucidicai/api/resources/prompt.py,sha256=KAvpgWuLzo4HhSHy1vSBApNNplNAfMNyjYjv0fsurHM,5637
30
30
  lucidicai/api/resources/session.py,sha256=jW_bftHdunhLHl_3-k0nqB5FrtLhlFeCF0tMFE82nNw,20761
31
31
  lucidicai/core/__init__.py,sha256=b0YQkd8190Y_GgwUcmf0tOiSLARd7L4kq4jwfhhGAyI,39
32
32
  lucidicai/core/config.py,sha256=q4h-yR35Ay_3znL7vavri6ScfeM69RjHShNNzjoQthc,10194
@@ -93,7 +93,7 @@ lucidicai/utils/images.py,sha256=z8mlIKgFfrIbuk-l4L2rB62uw_uPO79sHPXPY7eLu2A,128
93
93
  lucidicai/utils/logger.py,sha256=R3B3gSee64F6UVHUrShihBq_O7W7bgfrBiVDXTO3Isg,4777
94
94
  lucidicai/utils/queue.py,sha256=8DQwnGw7pINEJ0dNSkB0PhdPW-iBQQ-YZg23poe4umE,17323
95
95
  lucidicai/utils/serialization.py,sha256=KdOREZd7XBxFBAZ86DePMfYPzSVyKr4RcgUa82aFxrs,820
96
- lucidicai-3.3.0.dist-info/METADATA,sha256=p8D3cXfEzfEC4HnYYz68keBQwzNDOkSqifh8H13m1t4,902
97
- lucidicai-3.3.0.dist-info/WHEEL,sha256=Xo9-1PvkuimrydujYJAjF7pCkriuXBpUPEjma1nZyJ0,92
98
- lucidicai-3.3.0.dist-info/top_level.txt,sha256=vSSdM3lclF4I5tyVC0xxUk8eIRnnYXMe1hW-eO91HUo,10
99
- lucidicai-3.3.0.dist-info/RECORD,,
96
+ lucidicai-3.4.0.dist-info/METADATA,sha256=8-0BGkaSBc5YCjeVpDV6QFTCgZiCWDnzeY3Y00RcvOg,902
97
+ lucidicai-3.4.0.dist-info/WHEEL,sha256=Xo9-1PvkuimrydujYJAjF7pCkriuXBpUPEjma1nZyJ0,92
98
+ lucidicai-3.4.0.dist-info/top_level.txt,sha256=vSSdM3lclF4I5tyVC0xxUk8eIRnnYXMe1hW-eO91HUo,10
99
+ lucidicai-3.4.0.dist-info/RECORD,,