@@ -162,12 +162,14 @@ def mk_msgs(msgs: list, *args, api:str="openai", **kw) -> list:
162162mk_msgs_openai = partial (mk_msgs , api = "openai" )
163163
164164# %% ../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 ."
167167 if not cache : return msg
168168 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
171173 return msg
172174
173175def _remove_cache_ckpts (msg ):
@@ -178,18 +180,18 @@ def _remove_cache_ckpts(msg):
178180 return msg
179181
180182@delegates (mk_msg )
181- def mk_msg_anthropic (* args , cache = False , ** kwargs ):
183+ def mk_msg_anthropic (* args , cache = False , ttl = None , ** kwargs ):
182184 "Create an Anthropic compatible message."
183185 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 )
185187
186188@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 ):
188190 "Create a list of Anthropic compatible messages."
189191 msgs = partial (mk_msgs , api = "anthropic" )(* args , ** kwargs )
190192 if cache_last_ckpt_only : msgs = [_remove_cache_ckpts (m ) for m in msgs ]
191193 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 )
193195 return msgs
194196
195197# %% ../nbs/00_core.ipynb
0 commit comments