Skip to content

Commit fe1a7f4

Browse files
committed
fixes #83
1 parent adf2b40 commit fe1a7f4

File tree

9 files changed

+557
-529
lines changed

9 files changed

+557
-529
lines changed

00_core.ipynb

Lines changed: 200 additions & 206 deletions
Large diffs are not rendered by default.

01_toolloop.ipynb

Lines changed: 328 additions & 311 deletions
Large diffs are not rendered by default.

02_async.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,8 @@
800800
"@patch\n",
801801
"async def _stream(self:AsyncChat, res):\n",
802802
" async for o in res: yield o\n",
803-
" self.h += mk_toolres(self.c.result, ns=self.tools, obj=self)"
803+
" self.last = mk_toolres(self.c.result, ns=self.tools, obj=self)\n",
804+
" self.h += self.last"
804805
]
805806
},
806807
{
@@ -840,7 +841,8 @@
840841
" await self._append_pr(pr)\n",
841842
" res = await self.c(self.h, stream=stream, prefill=prefill, sp=self.sp, temp=temp, maxtok=maxtok, maxthinktok=maxthinktok, tools=self.tools, tool_choice=tool_choice, **kw)\n",
842843
" if stream: return self._stream(res)\n",
843-
" self.h += await mk_toolres_async(self.c.result, ns=self.ns)\n",
844+
" self.last = await mk_toolres_async(self.c.result, ns=self.ns)\n",
845+
" self.h += self.last\n",
844846
" return res"
845847
]
846848
},

claudette/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.1.12"
1+
__version__ = "0.2.0"
22
from .core import *
33
from .toolloop import *
44
from .asink import *

claudette/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
'claudette.core._convert': ('core.html#_convert', 'claudette/core.py'),
4646
'claudette.core._dgetattr': ('core.html#_dgetattr', 'claudette/core.py'),
4747
'claudette.core._is_builtin': ('core.html#_is_builtin', 'claudette/core.py'),
48+
'claudette.core._type': ('core.html#_type', 'claudette/core.py'),
4849
'claudette.core.blks2cited_txt': ('core.html#blks2cited_txt', 'claudette/core.py'),
4950
'claudette.core.can_set_system_prompt': ('core.html#can_set_system_prompt', 'claudette/core.py'),
5051
'claudette.core.can_set_temperature': ('core.html#can_set_temperature', 'claudette/core.py'),

claudette/asink.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def __init__(self,
114114
@patch
115115
async def _stream(self:AsyncChat, res):
116116
async for o in res: yield o
117-
self.h += mk_toolres(self.c.result, ns=self.tools, obj=self)
117+
self.last = mk_toolres(self.c.result, ns=self.tools, obj=self)
118+
self.h += self.last
118119

119120
# %% ../02_async.ipynb
120121
@patch
@@ -138,5 +139,6 @@ async def __call__(self:AsyncChat,
138139
await self._append_pr(pr)
139140
res = await self.c(self.h, stream=stream, prefill=prefill, sp=self.sp, temp=temp, maxtok=maxtok, maxthinktok=maxthinktok, tools=self.tools, tool_choice=tool_choice, **kw)
140141
if stream: return self._stream(res)
141-
self.h += await mk_toolres_async(self.c.result, ns=self.ns)
142+
self.last = await mk_toolres_async(self.c.result, ns=self.ns)
143+
self.h += self.last
142144
return res

claudette/core.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,16 @@ def can_set_temperature(m): return m in has_temperature_models
9292
def can_use_extended_thinking(m): return m in has_extended_thinking_models
9393

9494
# %% ../00_core.ipynb
95+
def _type(x):
96+
try: return x.type
97+
except AttributeError: return x.get('type')
98+
9599
def find_block(r:abc.Mapping, # The message to look in
96-
blk_type:type=TextBlock # The type of block to find
100+
blk_type:type|str=TextBlock # The type of block to find
97101
):
98102
"Find the first block of type `blk_type` in `r.content`."
99-
return first(o for o in r.content if isinstance(o,blk_type))
103+
f = (lambda x:_type(x)==blk_type) if isinstance(blk_type,str) else (lambda x:isinstance(x,blk_type))
104+
return first(o for o in r.content if f(o))
100105

101106
# %% ../00_core.ipynb
102107
@patch
@@ -410,7 +415,8 @@ def cost(self: Chat) -> float: return self.c.cost
410415
@patch
411416
def _stream(self:Chat, res):
412417
yield from res
413-
self.h += mk_toolres(self.c.result, ns=self.ns) #, obj=self)
418+
self.last = mk_toolres(self.c.result, ns=self.ns) #, obj=self)
419+
self.h += self.last
414420

415421
# %% ../00_core.ipynb
416422
@patch
@@ -445,7 +451,8 @@ def __call__(self:Chat,
445451
self._append_pr(pr)
446452
res = self.c(self.h, stream=stream, prefill=prefill, sp=self.sp, temp=temp, maxtok=maxtok, maxthinktok=maxthinktok, tools=self.tools, tool_choice=tool_choice,**kw)
447453
if stream: return self._stream(res)
448-
self.h += mk_toolres(self.c.result, ns=self.ns)
454+
self.last = mk_toolres(self.c.result, ns=self.ns)
455+
self.h += self.last
449456
return res
450457

451458
# %% ../00_core.ipynb

claudette/toolloop.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .core import *
88
from fastcore.utils import *
99
from fastcore.meta import delegates
10+
from functools import wraps
1011

1112
from anthropic.types import TextBlock, Message, ToolUseBlock
1213

@@ -28,12 +29,14 @@ def __iter__(a):
2829
init_n = len(self.h)
2930
r = self(pr, **kwargs)
3031
yield r
32+
if len(self.last)>1: yield self.last[1]
3133
for i in range(max_steps-1):
3234
if self.c.stop_reason!='tool_use': break
3335
r = self(final_prompt if i==max_steps-2 else None, **kwargs)
3436
yield r
37+
if len(self.last)>1: yield self.last[1]
3538
if not cont_func(*self.h[-3:]): break
36-
a.value = self.h[init_n:]
39+
a.value = self.h[init_n+1:]
3740
return _Loop()
3841

3942
# %% ../01_toolloop.ipynb
@@ -56,10 +59,12 @@ async def __aiter__(a):
5659
init_n = len(self.h)
5760
r = await self(pr, **kwargs)
5861
yield r
62+
if len(self.last)>1: yield self.last[1]
5963
for i in range(max_steps-1):
6064
if self.c.stop_reason != 'tool_use': break
6165
r = await self(final_prompt if i==max_steps-2 else None, **kwargs)
6266
yield r
67+
if len(self.last)>1: yield self.last[1]
6368
if not cont_func(*self.h[-3:]): break
64-
a.value = self.h[init_n:]
69+
a.value = self.h[init_n+1:]
6570
return _Loop()

settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[DEFAULT]
22
repo = claudette
33
lib_name = claudette
4-
version = 0.1.12
4+
version = 0.2.0
55
min_python = 3.9
66
license = apache2
77
black_formatting = False

0 commit comments

Comments
 (0)