|
| 1 | +--- |
| 2 | +linkTitle: LLM cache |
| 3 | +title: LLM Cache |
| 4 | +type: integration |
| 5 | +--- |
| 6 | + |
| 7 | + |
| 8 | +## SemanticCache |
| 9 | + |
| 10 | +<a id="semantic-cache-api"></a> |
| 11 | + |
| 12 | +### `class SemanticCache(name='llmcache', distance_threshold=0.1, ttl=None, vectorizer=None, filterable_fields=None, redis_client=None, redis_url='redis://localhost:6379', connection_kwargs={}, overwrite=False, **kwargs)` |
| 13 | + |
| 14 | +Bases: `BaseLLMCache` |
| 15 | + |
| 16 | +Semantic Cache for Large Language Models. |
| 17 | + |
| 18 | +Semantic Cache for Large Language Models. |
| 19 | + |
| 20 | +* **Parameters:** |
| 21 | + * **name** (*str* *,* *optional*) – The name of the semantic cache search index. |
| 22 | + Defaults to “llmcache”. |
| 23 | + * **distance_threshold** (*float* *,* *optional*) – Semantic threshold for the |
| 24 | + cache. Defaults to 0.1. |
| 25 | + * **ttl** (*Optional* *[* *int* *]* *,* *optional*) – The time-to-live for records cached |
| 26 | + in Redis. Defaults to None. |
| 27 | + * **vectorizer** (*Optional* *[* *BaseVectorizer* *]* *,* *optional*) – The vectorizer for the cache. |
| 28 | + Defaults to HFTextVectorizer. |
| 29 | + * **filterable_fields** (*Optional* *[* *List* *[* *Dict* *[* *str* *,* *Any* *]* *]* *]*) – An optional list of RedisVL fields |
| 30 | + that can be used to customize cache retrieval with filters. |
| 31 | + * **redis_client** (*Optional* *[* *Redis* *]* *,* *optional*) – A redis client connection instance. |
| 32 | + Defaults to None. |
| 33 | + * **redis_url** (*str* *,* *optional*) – The redis url. Defaults to redis://localhost:6379. |
| 34 | + * **connection_kwargs** (*Dict* *[* *str* *,* *Any* *]*) – The connection arguments |
| 35 | + for the redis client. Defaults to empty {}. |
| 36 | + * **overwrite** (*bool*) – Whether or not to force overwrite the schema for |
| 37 | + the semantic cache index. Defaults to false. |
| 38 | +* **Raises:** |
| 39 | + * **TypeError** – If an invalid vectorizer is provided. |
| 40 | + * **TypeError** – If the TTL value is not an int. |
| 41 | + * **ValueError** – If the threshold is not between 0 and 1. |
| 42 | + * **ValueError** – If existing schema does not match new schema and overwrite is False. |
| 43 | + |
| 44 | +#### `async acheck(prompt=None, vector=None, num_results=1, return_fields=None, filter_expression=None, distance_threshold=None)` |
| 45 | + |
| 46 | +Async check the semantic cache for results similar to the specified prompt |
| 47 | +or vector. |
| 48 | + |
| 49 | +This method searches the cache using vector similarity with |
| 50 | +either a raw text prompt (converted to a vector) or a provided vector as |
| 51 | +input. It checks for semantically similar prompts and fetches the cached |
| 52 | +LLM responses. |
| 53 | + |
| 54 | +* **Parameters:** |
| 55 | + * **prompt** (*Optional* *[* *str* *]* *,* *optional*) – The text prompt to search for in |
| 56 | + the cache. |
| 57 | + * **vector** (*Optional* *[* *List* *[* *float* *]* *]* *,* *optional*) – The vector representation |
| 58 | + of the prompt to search for in the cache. |
| 59 | + * **num_results** (*int* *,* *optional*) – The number of cached results to return. |
| 60 | + Defaults to 1. |
| 61 | + * **return_fields** (*Optional* *[* *List* *[* *str* *]* *]* *,* *optional*) – The fields to include |
| 62 | + in each returned result. If None, defaults to all available |
| 63 | + fields in the cached entry. |
| 64 | + * **filter_expression** (*Optional* *[*[*FilterExpression*]({{< relref "filter/#filterexpression" >}}) *]*) – Optional filter expression |
| 65 | + that can be used to filter cache results. Defaults to None and |
| 66 | + the full cache will be searched. |
| 67 | + * **distance_threshold** (*Optional* *[* *float* *]*) – The threshold for semantic |
| 68 | + vector distance. |
| 69 | +* **Returns:** |
| 70 | + A list of dicts containing the requested |
| 71 | + : return fields for each similar cached response. |
| 72 | +* **Return type:** |
| 73 | + List[Dict[str, Any]] |
| 74 | +* **Raises:** |
| 75 | + * **ValueError** – If neither a prompt nor a vector is specified. |
| 76 | + * **ValueError** – if ‘vector’ has incorrect dimensions. |
| 77 | + * **TypeError** – If return_fields is not a list when provided. |
| 78 | + |
| 79 | +```python |
| 80 | +response = await cache.acheck( |
| 81 | + prompt="What is the captial city of France?" |
| 82 | +) |
| 83 | +``` |
| 84 | + |
| 85 | +#### `async adrop(ids=None, keys=None)` |
| 86 | + |
| 87 | +Async expire specific entries from the cache by id or specific |
| 88 | +Redis key. |
| 89 | + |
| 90 | +* **Parameters:** |
| 91 | + * **ids** (*Optional* *[* *str* *]*) – The document ID or IDs to remove from the cache. |
| 92 | + * **keys** (*Optional* *[* *str* *]*) – The Redis keys to remove from the cache. |
| 93 | +* **Return type:** |
| 94 | + None |
| 95 | + |
| 96 | +#### `async astore(prompt, response, vector=None, metadata=None, filters=None, ttl=None)` |
| 97 | + |
| 98 | +Async stores the specified key-value pair in the cache along with metadata. |
| 99 | + |
| 100 | +* **Parameters:** |
| 101 | + * **prompt** (*str*) – The user prompt to cache. |
| 102 | + * **response** (*str*) – The LLM response to cache. |
| 103 | + * **vector** (*Optional* *[* *List* *[* *float* *]* *]* *,* *optional*) – The prompt vector to |
| 104 | + cache. Defaults to None, and the prompt vector is generated on |
| 105 | + demand. |
| 106 | + * **metadata** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *optional*) – The optional metadata to cache |
| 107 | + alongside the prompt and response. Defaults to None. |
| 108 | + * **filters** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]*) – The optional tag to assign to the cache entry. |
| 109 | + Defaults to None. |
| 110 | + * **ttl** (*Optional* *[* *int* *]*) – The optional TTL override to use on this individual cache |
| 111 | + entry. Defaults to the global TTL setting. |
| 112 | +* **Returns:** |
| 113 | + The Redis key for the entries added to the semantic cache. |
| 114 | +* **Return type:** |
| 115 | + str |
| 116 | +* **Raises:** |
| 117 | + * **ValueError** – If neither prompt nor vector is specified. |
| 118 | + * **ValueError** – if vector has incorrect dimensions. |
| 119 | + * **TypeError** – If provided metadata is not a dictionary. |
| 120 | + |
| 121 | +```python |
| 122 | +key = await cache.astore( |
| 123 | + prompt="What is the captial city of France?", |
| 124 | + response="Paris", |
| 125 | + metadata={"city": "Paris", "country": "France"} |
| 126 | +) |
| 127 | +``` |
| 128 | + |
| 129 | +#### `async aupdate(key, **kwargs)` |
| 130 | + |
| 131 | +Async update specific fields within an existing cache entry. If no fields |
| 132 | +are passed, then only the document TTL is refreshed. |
| 133 | + |
| 134 | +* **Parameters:** |
| 135 | + **key** (*str*) – the key of the document to update using kwargs. |
| 136 | +* **Raises:** |
| 137 | + * **ValueError if an incorrect mapping is provided as a kwarg.** – |
| 138 | + * **TypeError if metadata is provided and not** **of** **type dict.** – |
| 139 | +* **Return type:** |
| 140 | + None |
| 141 | + |
| 142 | +```python |
| 143 | +key = await cache.astore('this is a prompt', 'this is a response') |
| 144 | +await cache.aupdate( |
| 145 | + key, |
| 146 | + metadata={"hit_count": 1, "model_name": "Llama-2-7b"} |
| 147 | +) |
| 148 | +``` |
| 149 | + |
| 150 | +#### `check(prompt=None, vector=None, num_results=1, return_fields=None, filter_expression=None, distance_threshold=None)` |
| 151 | + |
| 152 | +Checks the semantic cache for results similar to the specified prompt |
| 153 | +or vector. |
| 154 | + |
| 155 | +This method searches the cache using vector similarity with |
| 156 | +either a raw text prompt (converted to a vector) or a provided vector as |
| 157 | +input. It checks for semantically similar prompts and fetches the cached |
| 158 | +LLM responses. |
| 159 | + |
| 160 | +* **Parameters:** |
| 161 | + * **prompt** (*Optional* *[* *str* *]* *,* *optional*) – The text prompt to search for in |
| 162 | + the cache. |
| 163 | + * **vector** (*Optional* *[* *List* *[* *float* *]* *]* *,* *optional*) – The vector representation |
| 164 | + of the prompt to search for in the cache. |
| 165 | + * **num_results** (*int* *,* *optional*) – The number of cached results to return. |
| 166 | + Defaults to 1. |
| 167 | + * **return_fields** (*Optional* *[* *List* *[* *str* *]* *]* *,* *optional*) – The fields to include |
| 168 | + in each returned result. If None, defaults to all available |
| 169 | + fields in the cached entry. |
| 170 | + * **filter_expression** (*Optional* *[*[*FilterExpression*]({{< relref "filter/#filterexpression" >}}) *]*) – Optional filter expression |
| 171 | + that can be used to filter cache results. Defaults to None and |
| 172 | + the full cache will be searched. |
| 173 | + * **distance_threshold** (*Optional* *[* *float* *]*) – The threshold for semantic |
| 174 | + vector distance. |
| 175 | +* **Returns:** |
| 176 | + A list of dicts containing the requested |
| 177 | + : return fields for each similar cached response. |
| 178 | +* **Return type:** |
| 179 | + List[Dict[str, Any]] |
| 180 | +* **Raises:** |
| 181 | + * **ValueError** – If neither a prompt nor a vector is specified. |
| 182 | + * **ValueError** – if ‘vector’ has incorrect dimensions. |
| 183 | + * **TypeError** – If return_fields is not a list when provided. |
| 184 | + |
| 185 | +```python |
| 186 | +response = cache.check( |
| 187 | + prompt="What is the captial city of France?" |
| 188 | +) |
| 189 | +``` |
| 190 | + |
| 191 | +#### `clear()` |
| 192 | + |
| 193 | +Clear the cache of all keys while preserving the index. |
| 194 | + |
| 195 | +* **Return type:** |
| 196 | + None |
| 197 | + |
| 198 | +#### `delete()` |
| 199 | + |
| 200 | +Clear the semantic cache of all keys and remove the underlying search |
| 201 | +index. |
| 202 | + |
| 203 | +* **Return type:** |
| 204 | + None |
| 205 | + |
| 206 | +#### `drop(ids=None, keys=None)` |
| 207 | + |
| 208 | +Manually expire specific entries from the cache by id or specific |
| 209 | +Redis key. |
| 210 | + |
| 211 | +* **Parameters:** |
| 212 | + * **ids** (*Optional* *[* *str* *]*) – The document ID or IDs to remove from the cache. |
| 213 | + * **keys** (*Optional* *[* *str* *]*) – The Redis keys to remove from the cache. |
| 214 | +* **Return type:** |
| 215 | + None |
| 216 | + |
| 217 | +#### `set_threshold(distance_threshold)` |
| 218 | + |
| 219 | +Sets the semantic distance threshold for the cache. |
| 220 | + |
| 221 | +* **Parameters:** |
| 222 | + **distance_threshold** (*float*) – The semantic distance threshold for |
| 223 | + the cache. |
| 224 | +* **Raises:** |
| 225 | + **ValueError** – If the threshold is not between 0 and 1. |
| 226 | +* **Return type:** |
| 227 | + None |
| 228 | + |
| 229 | +#### `set_ttl(ttl=None)` |
| 230 | + |
| 231 | +Set the default TTL, in seconds, for entries in the cache. |
| 232 | + |
| 233 | +* **Parameters:** |
| 234 | + **ttl** (*Optional* *[* *int* *]* *,* *optional*) – The optional time-to-live expiration |
| 235 | + for the cache, in seconds. |
| 236 | +* **Raises:** |
| 237 | + **ValueError** – If the time-to-live value is not an integer. |
| 238 | + |
| 239 | +#### `store(prompt, response, vector=None, metadata=None, filters=None, ttl=None)` |
| 240 | + |
| 241 | +Stores the specified key-value pair in the cache along with metadata. |
| 242 | + |
| 243 | +* **Parameters:** |
| 244 | + * **prompt** (*str*) – The user prompt to cache. |
| 245 | + * **response** (*str*) – The LLM response to cache. |
| 246 | + * **vector** (*Optional* *[* *List* *[* *float* *]* *]* *,* *optional*) – The prompt vector to |
| 247 | + cache. Defaults to None, and the prompt vector is generated on |
| 248 | + demand. |
| 249 | + * **metadata** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *optional*) – The optional metadata to cache |
| 250 | + alongside the prompt and response. Defaults to None. |
| 251 | + * **filters** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]*) – The optional tag to assign to the cache entry. |
| 252 | + Defaults to None. |
| 253 | + * **ttl** (*Optional* *[* *int* *]*) – The optional TTL override to use on this individual cache |
| 254 | + entry. Defaults to the global TTL setting. |
| 255 | +* **Returns:** |
| 256 | + The Redis key for the entries added to the semantic cache. |
| 257 | +* **Return type:** |
| 258 | + str |
| 259 | +* **Raises:** |
| 260 | + * **ValueError** – If neither prompt nor vector is specified. |
| 261 | + * **ValueError** – if vector has incorrect dimensions. |
| 262 | + * **TypeError** – If provided metadata is not a dictionary. |
| 263 | + |
| 264 | +```python |
| 265 | +key = cache.store( |
| 266 | + prompt="What is the captial city of France?", |
| 267 | + response="Paris", |
| 268 | + metadata={"city": "Paris", "country": "France"} |
| 269 | +) |
| 270 | +``` |
| 271 | + |
| 272 | +#### `update(key, **kwargs)` |
| 273 | + |
| 274 | +Update specific fields within an existing cache entry. If no fields |
| 275 | +are passed, then only the document TTL is refreshed. |
| 276 | + |
| 277 | +* **Parameters:** |
| 278 | + **key** (*str*) – the key of the document to update using kwargs. |
| 279 | +* **Raises:** |
| 280 | + * **ValueError if an incorrect mapping is provided as a kwarg.** – |
| 281 | + * **TypeError if metadata is provided and not** **of** **type dict.** – |
| 282 | +* **Return type:** |
| 283 | + None |
| 284 | + |
| 285 | +```python |
| 286 | +key = cache.store('this is a prompt', 'this is a response') |
| 287 | +cache.update(key, metadata={"hit_count": 1, "model_name": "Llama-2-7b"}) |
| 288 | +) |
| 289 | +``` |
| 290 | + |
| 291 | +#### `property aindex: `[`AsyncSearchIndex`]({{< relref "searchindex/#asyncsearchindex" >}})` | None` |
| 292 | + |
| 293 | +The underlying AsyncSearchIndex for the cache. |
| 294 | + |
| 295 | +* **Returns:** |
| 296 | + The async search index. |
| 297 | +* **Return type:** |
| 298 | + [AsyncSearchIndex]({{< relref "searchindex/#asyncsearchindex" >}}) |
| 299 | + |
| 300 | +#### `property distance_threshold: float` |
| 301 | + |
| 302 | +The semantic distance threshold for the cache. |
| 303 | + |
| 304 | +* **Returns:** |
| 305 | + The semantic distance threshold. |
| 306 | +* **Return type:** |
| 307 | + float |
| 308 | + |
| 309 | +#### `property index: `[`SearchIndex`]({{< relref "searchindex/#searchindex" >}})` ` |
| 310 | + |
| 311 | +The underlying SearchIndex for the cache. |
| 312 | + |
| 313 | +* **Returns:** |
| 314 | + The search index. |
| 315 | +* **Return type:** |
| 316 | + [SearchIndex]({{< relref "searchindex/#searchindex" >}}) |
| 317 | + |
| 318 | +#### `property ttl: int | None` |
| 319 | + |
| 320 | +The default TTL, in seconds, for entries in the cache. |
0 commit comments