Skip to content
Open
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
22 changes: 22 additions & 0 deletions cachy.jsonl

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lisette/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
'lisette.core._lite_call_func': ('core.html#_lite_call_func', 'lisette/core.py'),
'lisette.core._mk_content': ('core.html#_mk_content', 'lisette/core.py'),
'lisette.core._mk_prefill': ('core.html#_mk_prefill', 'lisette/core.py'),
'lisette.core._skip_tools_cache': ('core.html#_skip_tools_cache', 'lisette/core.py'),
'lisette.core._trunc_str': ('core.html#_trunc_str', 'lisette/core.py'),
'lisette.core.adisplay_stream': ('core.html#adisplay_stream', 'lisette/core.py'),
'lisette.core.astream_with_complete': ('core.html#astream_with_complete', 'lisette/core.py'),
Expand Down
26 changes: 21 additions & 5 deletions lisette/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,22 @@ def fmt2hist(outp:str)->list:
hist.append(tcr[1])
return hist

# %% ../nbs/00_core.ipynb
def _skip_tools_cache(msgs, cache_idxs):
"Skip tool use blocks and tool results in and shift cache indices"
res = []
for idx in cache_idxs:
try:
while msgs[idx].get('tool_calls', []) or msgs[idx]['role'] == 'tool': idx -= 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some cache_idxs can end up being shifted back to the same idx. For example if last 4 messages are tool blocks / results and user sets cahce_idxs=[-4,-3,-2,-1] then it will be cache_idxs=[-5,-5,-5,-5].

We can keep shifting them until all idxs are unique (if possible otherwise collision is unavoidable) but this might complicate the logic a bit?

except IndexError: continue
res.append(idx)
return res

# %% ../nbs/00_core.ipynb
def mk_msgs(
msgs, # List of messages (each: str, bytes, list, or dict w 'role' and 'content' fields)
cache=False, # Enable Anthropic caching
cache_idxs=[-1], # Cache breakpoint idxs
ttl=None, # Cache TTL: '5m' (default) or '1h'
):
"Create a list of LiteLLM compatible messages."
Expand All @@ -164,9 +176,7 @@ def mk_msgs(
for m in msgs:
res.append(msg:=remove_cache_ckpts(mk_msg(m, role=role)))
role = 'assistant' if msg['role'] in ('user','function', 'tool') else 'user'
if cache:
res[-1] = _add_cache_control(res[-1], ttl)
res[-2] = _add_cache_control(res[-2], ttl)
if cache: L(_skip_tools_cache(res, cache_idxs)).map(lambda idx: _add_cache_control(res[idx], ttl))
return res

# %% ../nbs/00_core.ipynb
Expand Down Expand Up @@ -228,11 +238,12 @@ def __init__(
hist:list=None, # Chat history
ns:Optional[dict]=None, # Custom namespace for tool calling
cache=False, # Anthropic prompt caching
cache_idxs:list=[-1], # Anthropic cache breakpoint idxs, use `0` for sys prompt if provided
ttl=None, # Anthropic prompt caching ttl
):
"LiteLLM chat client."
self.model = model
hist,tools = mk_msgs(hist,cache,ttl),listify(tools)
hist,tools = mk_msgs(hist,cache,cache_idxs,ttl),listify(tools)
if ns is None and tools: ns = mk_ns(tools)
elif ns is None: ns = globals()
self.tool_schemas = [lite_mk_func(t) for t in tools] if tools else None
Expand All @@ -241,7 +252,12 @@ def __init__(
def _prep_msg(self, msg=None, prefill=None):
"Prepare the messages list for the API call"
sp = [{"role": "system", "content": self.sp}] if self.sp else []
if msg: self.hist = mk_msgs(self.hist+[msg], self.cache, self.ttl)
if sp:
if 0 in self.cache_idxs: sp[0] = _add_cache_control(sp[0])
cache_idxs = L(self.cache_idxs).filter().map(lambda o: o-1 if o>0 else o)
else:
cache_idxs = self.cache_idxs
if msg: self.hist = mk_msgs(self.hist+[msg], self.cache, cache_idxs, self.ttl)
pf = [{"role":"assistant","content":prefill}] if prefill else []
return sp + self.hist + pf

Expand Down
Loading