@@ -162,12 +162,14 @@ def mk_msgs(msgs: list, *args, api:str="openai", **kw) -> list:
162
162
mk_msgs_openai = partial (mk_msgs , api = "openai" )
163
163
164
164
# %% ../nbs/00_core.ipynb
165
- def _add_cache_control (msg , cache = False ):
166
- "cache `msg`."
165
+ def _add_cache_control (msg , cache = False , ttl = None ):
166
+ "cache `msg` with optional ttl ."
167
167
if not cache : return msg
168
168
if isinstance (msg ["content" ], str ): msg ["content" ] = [{"type" : "text" , "text" : msg ["content" ]}]
169
- if isinstance (msg ["content" ][- 1 ], dict ): msg ["content" ][- 1 ]["cache_control" ] = {"type" : "ephemeral" }
170
- elif isinstance (msg ["content" ][- 1 ], abc .Mapping ): msg ["content" ][- 1 ].cache_control = {"type" : "ephemeral" }
169
+ cache_control = {"type" : "ephemeral" }
170
+ if ttl is not None : cache_control ["ttl" ] = ttl
171
+ if isinstance (msg ["content" ][- 1 ], dict ): msg ["content" ][- 1 ]["cache_control" ] = cache_control
172
+ elif isinstance (msg ["content" ][- 1 ], abc .Mapping ): msg ["content" ][- 1 ].cache_control = cache_control
171
173
return msg
172
174
173
175
def _remove_cache_ckpts (msg ):
@@ -178,18 +180,18 @@ def _remove_cache_ckpts(msg):
178
180
return msg
179
181
180
182
@delegates (mk_msg )
181
- def mk_msg_anthropic (* args , cache = False , ** kwargs ):
183
+ def mk_msg_anthropic (* args , cache = False , ttl = None , ** kwargs ):
182
184
"Create an Anthropic compatible message."
183
185
msg = partial (mk_msg , api = "anthropic" )(* args , ** kwargs )
184
- return _add_cache_control (msg , cache = cache )
186
+ return _add_cache_control (msg , cache = cache , ttl = ttl )
185
187
186
188
@delegates (mk_msgs )
187
- def mk_msgs_anthropic (* args , cache = False , cache_last_ckpt_only = False , ** kwargs ):
189
+ def mk_msgs_anthropic (* args , cache = False , ttl = None , cache_last_ckpt_only = False , ** kwargs ):
188
190
"Create a list of Anthropic compatible messages."
189
191
msgs = partial (mk_msgs , api = "anthropic" )(* args , ** kwargs )
190
192
if cache_last_ckpt_only : msgs = [_remove_cache_ckpts (m ) for m in msgs ]
191
193
if not msgs : return msgs
192
- msgs [- 1 ] = _add_cache_control (msgs [- 1 ], cache = cache )
194
+ msgs [- 1 ] = _add_cache_control (msgs [- 1 ], cache = cache , ttl = ttl )
193
195
return msgs
194
196
195
197
# %% ../nbs/00_core.ipynb
0 commit comments