Skip to content
Merged
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
11 changes: 5 additions & 6 deletions lisette/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ def _call(self:Chat, msg=None, prefill=None, temp=None, think=None, search=None,
return
yield res
if tcs := _filter_srvtools(m.tool_calls):
tool_results=[_lite_call_func(tc, self.tool_schemas, self.ns, tc_res=self.tc_res, tc_res_eval=self.tc_res_eval) for tc in tcs]
_f = partial(_lite_call_func, tool_schemas=self.tool_schemas, ns=self.ns, tc_res=self.tc_res, tc_res_eval=self.tc_res_eval)
tool_results=list(parallel(_f, tcs, threadpool=True, n_workers=len(tcs)))
self.hist+=tool_results
for r in tool_results: yield r
if step>=max_steps: prompt,tool_choice,search = final_prompt,'none',False
Expand Down Expand Up @@ -565,11 +566,9 @@ async def _call(self, msg=None, prefill=None, temp=None, think=None, search=None
yield res

if tcs := _filter_srvtools(m.tool_calls):
tool_results = []
for tc in tcs:
result = await _alite_call_func(tc, self.tool_schemas, self.ns, tc_res=self.tc_res, tc_res_eval=self.tc_res_eval)
tool_results.append(result)
yield result
tool_results = await asyncio.gather(*[
_alite_call_func(tc, self.tool_schemas, self.ns, tc_res=self.tc_res, tc_res_eval=self.tc_res_eval) for tc in tcs])
for r in tool_results: yield r
self.hist+=tool_results
if step>=max_steps-1: prompt,tool_choice,search = final_prompt,'none',False
else: prompt = None
Expand Down
51 changes: 29 additions & 22 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3188,7 +3188,8 @@
" return\n",
" yield res\n",
" if tcs := _filter_srvtools(m.tool_calls):\n",
" tool_results=[_lite_call_func(tc, self.tool_schemas, self.ns, tc_res=self.tc_res, tc_res_eval=self.tc_res_eval) for tc in tcs]\n",
" _f = partial(_lite_call_func, tool_schemas=self.tool_schemas, ns=self.ns, tc_res=self.tc_res, tc_res_eval=self.tc_res_eval)\n",
" tool_results=list(parallel(_f, tcs, threadpool=True, n_workers=len(tcs)))\n",
" self.hist+=tool_results\n",
" for r in tool_results: yield r\n",
" if step>=max_steps: prompt,tool_choice,search = final_prompt,'none',False\n",
Expand Down Expand Up @@ -6970,6 +6971,14 @@
" self.value = stream_chunk_builder(chunks)"
]
},
{
"cell_type": "markdown",
"id": "eaba5ab0",
"metadata": {},
"source": [
"Parallel tool execution in `AsyncChat` works with both sync and async tool functions. Async tools run concurrently via `asyncio.gather`, while sync tools are automatically offloaded to threads via `asyncio.to_thread` in `call_func_async` (toolslm). For sync `Chat`, tools run in parallel via `fastcore.parallel` with threads."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -7008,11 +7017,9 @@
" yield res\n",
"\n",
" if tcs := _filter_srvtools(m.tool_calls):\n",
" tool_results = []\n",
" for tc in tcs:\n",
" result = await _alite_call_func(tc, self.tool_schemas, self.ns, tc_res=self.tc_res, tc_res_eval=self.tc_res_eval)\n",
" tool_results.append(result)\n",
" yield result\n",
" tool_results = await asyncio.gather(*[\n",
" _alite_call_func(tc, self.tool_schemas, self.ns, tc_res=self.tc_res, tc_res_eval=self.tc_res_eval) for tc in tcs])\n",
" for r in tool_results: yield r\n",
" self.hist+=tool_results\n",
" if step>=max_steps-1: prompt,tool_choice,search = final_prompt,'none',False\n",
" else: prompt = None\n",
Expand Down Expand Up @@ -7280,7 +7287,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'tool_call_id': 'call_0m1TlhBYToydcXPlW8f96w', 'role': 'tool', 'name': 'async_add', 'content': '12'}\n",
"{'tool_call_id': 'call_MSNO_m5kQEOqpQ9OxvAJMw', 'role': 'tool', 'name': 'async_add', 'content': '12'}\n",
"The sum of 5 and 7 is 12."
]
}
Expand Down Expand Up @@ -7815,7 +7822,7 @@
"\n",
"```json\n",
"{\n",
" \"id\": \"call_fOcbSPulTlmYozc2_RrHzg\",\n",
" \"id\": \"call_GtCm8ia9SXSTtWSwi_BMPg\",\n",
" \"call\": {\n",
" \"function\": \"async_add\",\n",
" \"arguments\": {\n",
Expand Down Expand Up @@ -7920,7 +7927,7 @@
"\n",
"```json\n",
"{\n",
" \"id\": \"call_40WscurDQgSt587zftLsLw\",\n",
" \"id\": \"call_hW89leCuShusWWIWrg_byA\",\n",
" \"call\": {\n",
" \"function\": \"simple_add\",\n",
" \"arguments\": {\n",
Expand All @@ -7941,7 +7948,7 @@
"\n",
"```json\n",
"{\n",
" \"id\": \"call__4jsgn_ZQnOVYnOGUozCQQ\",\n",
" \"id\": \"call_40WscurDQgSt587zftLsLw\",\n",
" \"call\": {\n",
" \"function\": \"simple_add\",\n",
" \"arguments\": {\n",
Expand Down Expand Up @@ -7988,7 +7995,7 @@
{
"data": {
"text/plain": [
"Message(content=None, role='assistant', tool_calls=[{'provider_specific_fields': {'thought_signature': 'EjQKMgG+Pvb7flzsNemEsWPXNGRFnhyvmQoZhKthF7xruwK0UZR21vDR1SwlKs3KsoGnhcN5'}, 'function': {'arguments': '{\"b\": 5, \"a\": 10}', 'name': 'simple_add'}, 'id': 'call_40WscurDQgSt587zftLsLw', 'type': 'function'}, {'function': {'arguments': '{\"b\": 1, \"a\": 2}', 'name': 'simple_add'}, 'id': 'call__4jsgn_ZQnOVYnOGUozCQQ', 'type': 'function'}], function_call=None, provider_specific_fields=None)"
"Message(content=None, role='assistant', tool_calls=[{'provider_specific_fields': {'thought_signature': 'EjQKMgG+Pvb7flzsNemEsWPXNGRFnhyvmQoZhKthF7xruwK0UZR21vDR1SwlKs3KsoGnhcN5'}, 'function': {'arguments': '{\"b\": 5, \"a\": 10}', 'name': 'simple_add'}, 'id': 'call_hW89leCuShusWWIWrg_byA', 'type': 'function'}, {'function': {'arguments': '{\"b\": 1, \"a\": 2}', 'name': 'simple_add'}, 'id': 'call_40WscurDQgSt587zftLsLw', 'type': 'function'}], function_call=None, provider_specific_fields=None)"
]
},
"execution_count": null,
Expand All @@ -8009,7 +8016,7 @@
{
"data": {
"text/plain": [
"{'tool_call_id': 'call_40WscurDQgSt587zftLsLw',\n",
"{'tool_call_id': 'call_hW89leCuShusWWIWrg_byA',\n",
" 'role': 'tool',\n",
" 'name': 'simple_add',\n",
" 'content': '15'}"
Expand All @@ -8033,7 +8040,7 @@
{
"data": {
"text/plain": [
"{'tool_call_id': 'call__4jsgn_ZQnOVYnOGUozCQQ',\n",
"{'tool_call_id': 'call_40WscurDQgSt587zftLsLw',\n",
" 'role': 'tool',\n",
" 'name': 'simple_add',\n",
" 'content': '3'}"
Expand Down Expand Up @@ -8261,23 +8268,23 @@
"text/markdown": [
"Perfect! I successfully completed both steps:\n",
"\n",
"1. **Retrieved person data**: I called `get_person` which returned a person named Alice who is 30 years old.\n",
"1. **Retrieved person data**: I called `get_person` which returned information about Alice, who is 30 years old.\n",
"\n",
"2. **Greeted the person**: I then passed Alice's data to `greet_person`, which generated the greeting: \"Hello Alice, you are 30 years old!\"\n",
"\n",
"The task has been completed successfully. The person's information was retrieved and used to create a personalized greeting.\n",
"The task has been completed successfully. The person's data was retrieved and used to create a personalized greeting.\n",
"\n",
"<details>\n",
"\n",
"- id: `chatcmpl-xxx`\n",
"- model: `claude-sonnet-4-5-20250929`\n",
"- finish_reason: `stop`\n",
"- usage: `Usage(completion_tokens=103, prompt_tokens=1077, total_tokens=1180, completion_tokens_details=None, prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=None, cached_tokens=0, text_tokens=None, image_tokens=None, cache_creation_tokens=0, cache_creation_token_details=CacheCreationTokenDetails(ephemeral_5m_input_tokens=0, ephemeral_1h_input_tokens=0)), cache_creation_input_tokens=0, cache_read_input_tokens=0)`\n",
"- usage: `Usage(completion_tokens=103, prompt_tokens=1083, total_tokens=1186, completion_tokens_details=None, prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=None, cached_tokens=0, text_tokens=None, image_tokens=None, cache_creation_tokens=0, cache_creation_token_details=CacheCreationTokenDetails(ephemeral_5m_input_tokens=0, ephemeral_1h_input_tokens=0)), cache_creation_input_tokens=0, cache_read_input_tokens=0)`\n",
"\n",
"</details>"
],
"text/plain": [
"ModelResponse(id='chatcmpl-xxx', created=1000000000, model='claude-sonnet-4-5-20250929', object='chat.completion', system_fingerprint=None, choices=[Choices(finish_reason='stop', index=0, message=Message(content='Perfect! I successfully completed both steps:\\n\\n1. **Retrieved person data**: I called `get_person` which returned a person named Alice who is 30 years old.\\n\\n2. **Greeted the person**: I then passed Alice\\'s data to `greet_person`, which generated the greeting: \"Hello Alice, you are 30 years old!\"\\n\\nThe task has been completed successfully. The person\\'s information was retrieved and used to create a personalized greeting.', role='assistant', tool_calls=None, function_call=None, provider_specific_fields={'citations': None, 'thinking_blocks': None}))], usage=Usage(completion_tokens=103, prompt_tokens=1077, total_tokens=1180, completion_tokens_details=None, prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=None, cached_tokens=0, text_tokens=None, image_tokens=None, cache_creation_tokens=0, cache_creation_token_details=CacheCreationTokenDetails(ephemeral_5m_input_tokens=0, ephemeral_1h_input_tokens=0)), cache_creation_input_tokens=0, cache_read_input_tokens=0))"
"ModelResponse(id='chatcmpl-xxx', created=1000000000, model='claude-sonnet-4-5-20250929', object='chat.completion', system_fingerprint=None, choices=[Choices(finish_reason='stop', index=0, message=Message(content='Perfect! I successfully completed both steps:\\n\\n1. **Retrieved person data**: I called `get_person` which returned information about Alice, who is 30 years old.\\n\\n2. **Greeted the person**: I then passed Alice\\'s data to `greet_person`, which generated the greeting: \"Hello Alice, you are 30 years old!\"\\n\\nThe task has been completed successfully. The person\\'s data was retrieved and used to create a personalized greeting.', role='assistant', tool_calls=None, function_call=None, provider_specific_fields={'citations': None, 'thinking_blocks': None}))], usage=Usage(completion_tokens=103, prompt_tokens=1083, total_tokens=1186, completion_tokens_details=None, prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=None, cached_tokens=0, text_tokens=None, image_tokens=None, cache_creation_tokens=0, cache_creation_token_details=CacheCreationTokenDetails(ephemeral_5m_input_tokens=0, ephemeral_1h_input_tokens=0)), cache_creation_input_tokens=0, cache_read_input_tokens=0))"
]
},
"execution_count": null,
Expand All @@ -8299,8 +8306,8 @@
{
"data": {
"text/plain": [
"{'toolu_CJswoICfSSOHoXmP5q3dng': {'name': 'Alice', 'age': 30},\n",
" 'toolu_y00Y1q22SjWXNKJskulOiQ': 'Hello Alice, you are 30 years old!'}"
"{'toolu_Ydn_OYFHSPSfDvMg9_YOfw': {'name': 'Alice', 'age': 30},\n",
" 'toolu_CJswoICfSSOHoXmP5q3dng': 'Hello Alice, you are 30 years old!'}"
]
},
"execution_count": null,
Expand All @@ -8323,12 +8330,12 @@
"text/plain": [
"[[{'index': 1,\n",
" 'function': {'arguments': '{}', 'name': 'get_person'},\n",
" 'id': 'toolu_CJswoICfSSOHoXmP5q3dng',\n",
" 'id': 'toolu_Ydn_OYFHSPSfDvMg9_YOfw',\n",
" 'type': 'function'}],\n",
" [{'index': 1,\n",
" 'function': {'arguments': '{\"person\": \"$`toolu_CJswoICfSSOHoXmP5q3dng`\"}',\n",
" 'function': {'arguments': '{\"person\": \"$`toolu_Ydn_OYFHSPSfDvMg9_YOfw`\"}',\n",
" 'name': 'greet_person'},\n",
" 'id': 'toolu_y00Y1q22SjWXNKJskulOiQ',\n",
" 'id': 'toolu_CJswoICfSSOHoXmP5q3dng',\n",
" 'type': 'function'}]]"
]
},
Expand Down