diff --git a/02_async.ipynb b/02_async.ipynb index c196614..db7829a 100644 --- a/02_async.ipynb +++ b/02_async.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "efe78920", "metadata": {}, "outputs": [], @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "033c76fd", "metadata": {}, "outputs": [], @@ -49,7 +49,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "13866a72", "metadata": {}, "outputs": [], @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "70b53a51", "metadata": {}, "outputs": [], @@ -79,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "6ec40731", "metadata": {}, "outputs": [ @@ -105,7 +105,7 @@ "Message(id='msg_01UBPA1yCoPZdk4vDbCwdXGm', content=[TextBlock(text=\"Hi Jeremy! Nice to meet you. I'm Claude, an AI assistant created by Anthropic. How can I help you today?\", type='text')], model='claude-3-5-sonnet-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 10; Out: 31; Cache create: 0; Cache read: 0; Total: 41)" ] }, - "execution_count": 5, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "3b873aaf", "metadata": {}, "outputs": [], @@ -133,7 +133,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "d01e9ccf", "metadata": {}, "outputs": [], @@ -143,7 +143,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "0181f7b3", "metadata": {}, "outputs": [ @@ -153,7 +153,7 @@ "In: 10; Out: 31; Cache create: 0; Cache read: 0; Total: 41" ] }, - "execution_count": 8, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -165,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "6520a355", "metadata": {}, "outputs": [], @@ -181,7 +181,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "835638bb", "metadata": {}, "outputs": [], @@ -212,7 +212,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "881b5e78", "metadata": {}, "outputs": [ @@ -222,7 +222,7 @@ "In: 0; Out: 0; Cache create: 0; Cache read: 0; Total: 0" ] }, - "execution_count": 11, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -234,7 +234,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "c1220856", "metadata": {}, "outputs": [ @@ -260,7 +260,7 @@ "Message(id='msg_01TC5wq1bS1ZcJMopq8bZ4o2', content=[TextBlock(text='Hello! How can I help you today?', type='text')], model='claude-3-5-sonnet-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 8; Out: 12; Cache create: 0; Cache read: 0; Total: 20)" ] }, - "execution_count": 12, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -272,7 +272,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "ae9f7e06", "metadata": {}, "outputs": [ @@ -282,7 +282,7 @@ "In: 8; Out: 12; Cache create: 0; Cache read: 0; Total: 20" ] }, - "execution_count": 13, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -293,7 +293,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "7f479429", "metadata": {}, "outputs": [ @@ -319,7 +319,7 @@ "Message(id='msg_012ZvrAoNyBherFi2q4fDRWq', content=[TextBlock(text=\"According to Douglas Adams, it's 42. More seriously, there's no universal answer - it's deeply personal. Common perspectives include: finding happiness, creating meaning through relationships and achievements, pursuing knowledge, helping others, or following spiritual/religious beliefs.\", type='text')], model='claude-3-5-sonnet-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 24; Out: 50; Cache create: 0; Cache read: 0; Total: 74)" ] }, - "execution_count": 14, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -332,7 +332,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "c0230a8c", "metadata": {}, "outputs": [ @@ -350,7 +350,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "beb25f2a", "metadata": {}, "outputs": [ @@ -360,7 +360,7 @@ "In: 40; Out: 74; Cache create: 0; Cache read: 0; Total: 114" ] }, - "execution_count": 16, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -371,7 +371,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "db1c75ef", "metadata": {}, "outputs": [ @@ -389,7 +389,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "e36eddc9", "metadata": {}, "outputs": [ @@ -399,7 +399,7 @@ "In: 64; Out: 124; Cache create: 0; Cache read: 0; Total: 188" ] }, - "execution_count": 18, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -410,7 +410,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "id": "046e8cc3", "metadata": {}, "outputs": [], @@ -426,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "d51f2bdf", "metadata": {}, "outputs": [], @@ -438,7 +438,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "bff81d52", "metadata": {}, "outputs": [], @@ -449,7 +449,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "9f7292b0", "metadata": {}, "outputs": [ @@ -466,7 +466,7 @@ "'The sum of 604542 and 6458932 is 7063474.'" ] }, - "execution_count": 22, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -481,7 +481,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "id": "8bae7694", "metadata": {}, "outputs": [], @@ -504,11 +504,13 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "id": "b68060dc", "metadata": {}, "outputs": [], "source": [ + "#|export\n", + "\n", "@patch\n", "@delegates(Client.__call__)\n", "async def structured(self:AsyncClient,\n", @@ -529,7 +531,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "id": "30a71b2e", "metadata": {}, "outputs": [ @@ -546,7 +548,7 @@ "[7063474]" ] }, - "execution_count": 25, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -565,7 +567,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "id": "a77d1edb", "metadata": {}, "outputs": [], @@ -584,7 +586,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "id": "04b837c5", "metadata": {}, "outputs": [ @@ -594,7 +596,7 @@ "(In: 0; Out: 0; Cache create: 0; Cache read: 0; Total: 0, [])" ] }, - "execution_count": 27, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -607,7 +609,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "id": "42a05df9", "metadata": {}, "outputs": [], @@ -621,7 +623,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "id": "8e5ab3be", "metadata": {}, "outputs": [], @@ -636,7 +638,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "id": "bec85e37", "metadata": {}, "outputs": [], @@ -659,7 +661,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "id": "40073f42", "metadata": {}, "outputs": [ @@ -685,7 +687,7 @@ "Message(id='msg_01BNxuSzZGanZupYuJxFDTgi', content=[TextBlock(text='Your name is Jeremy.', type='text')], model='claude-3-5-sonnet-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 41; Out: 8; Cache create: 0; Cache read: 0; Total: 49)" ] }, - "execution_count": 31, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -697,7 +699,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "id": "20a32de0", "metadata": {}, "outputs": [ @@ -723,7 +725,7 @@ "Message(id='msg_012WB8YcoMyPk2Uuh69eUxnF', content=[TextBlock(text='According to Douglas Adams, 42. But more seriously: to find purpose, create meaning, love, grow, and make a positive impact during our time here.', type='text')], model='claude-3-5-sonnet-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 69; Out: 31; Cache create: 0; Cache read: 0; Total: 100)" ] }, - "execution_count": 32, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -736,7 +738,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "id": "529104ec", "metadata": {}, "outputs": [ @@ -755,7 +757,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "id": "ee6535cf", "metadata": {}, "outputs": [ @@ -781,7 +783,7 @@ "Message(id='msg_013c9kT2obX52nLMf9PmWWHH', content=[TextBlock(text='7,063,474', type='text')], model='claude-3-5-sonnet-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 24; Out: 9; Cache create: 0; Cache read: 0; Total: 33)" ] }, - "execution_count": 40, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -795,7 +797,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": null, "id": "2dc85163", "metadata": {}, "outputs": [ @@ -821,7 +823,7 @@ "Message(id='msg_01F6Jms2nhe8VEDN16ZUGomv', content=[TextBlock(text='The sum of 604,542 and 6,458,932 is 7,063,474.', type='text')], model='claude-3-5-sonnet-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 31; Out: 27; Cache create: 0; Cache read: 0; Total: 58)" ] }, - "execution_count": 41, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -835,7 +837,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "id": "5d35d564", "metadata": {}, "outputs": [], @@ -846,7 +848,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "id": "0c0eed5d", "metadata": {}, "outputs": [ @@ -872,7 +874,7 @@ "Message(id='msg_01MLGBSn2QYJoKnthg7W2Tkj', content=[TextBlock(text='In this adorable puppy photo, there are purple/lavender colored flowers (appears to be asters or similar daisy-like flowers) in the background.', type='text')], model='claude-3-5-sonnet-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 110; Out: 37; Cache create: 0; Cache read: 0; Total: 147)" ] }, - "execution_count": 43, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -893,7 +895,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "id": "1e9ee5c1", "metadata": {}, "outputs": [], @@ -915,21 +917,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "python3", "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.7" } }, "nbformat": 4, diff --git a/claudette/_modidx.py b/claudette/_modidx.py index f3c0d12..cb8d7cb 100644 --- a/claudette/_modidx.py +++ b/claudette/_modidx.py @@ -13,7 +13,8 @@ 'claudette.asink.AsyncClient': ('async.html#asyncclient', 'claudette/asink.py'), 'claudette.asink.AsyncClient.__call__': ('async.html#asyncclient.__call__', 'claudette/asink.py'), 'claudette.asink.AsyncClient.__init__': ('async.html#asyncclient.__init__', 'claudette/asink.py'), - 'claudette.asink.AsyncClient._stream': ('async.html#asyncclient._stream', 'claudette/asink.py')}, + 'claudette.asink.AsyncClient._stream': ('async.html#asyncclient._stream', 'claudette/asink.py'), + 'claudette.asink.AsyncClient.structured': ('async.html#asyncclient.structured', 'claudette/asink.py')}, 'claudette.core': { 'claudette.core.Chat': ('core.html#chat', 'claudette/core.py'), 'claudette.core.Chat.__call__': ('core.html#chat.__call__', 'claudette/core.py'), 'claudette.core.Chat.__init__': ('core.html#chat.__init__', 'claudette/core.py'), diff --git a/claudette/asink.py b/claudette/asink.py index 9f90883..1cd5559 100644 --- a/claudette/asink.py +++ b/claudette/asink.py @@ -54,6 +54,24 @@ async def __call__(self:AsyncClient, model=self.model, messages=msgs, max_tokens=maxtok, system=sp, temperature=temp, **kwargs) return self._log(res, prefill, msgs, maxtok, sp, temp, stream=stream, stop=stop, **kwargs) +# %% ../02_async.ipynb +@patch +@delegates(Client.__call__) +async def structured(self:AsyncClient, + msgs:list, # List of messages in the dialog + tools:Optional[list]=None, # List of tools to make available to Claude + obj:Optional=None, # Class to search for tools + ns:Optional[abc.Mapping]=None, # Namespace to search for tools + **kwargs): + "Return the value of all tool calls (generally used for structured outputs)" + tools = listify(tools) + if ns is None: ns=mk_ns(*tools) + if obj is not None: ns = mk_ns(obj) + res = await self(msgs, tools=tools, tool_choice=tools,**kwargs) + cts = getattr(res, 'content', []) + tcs = [call_func(o.name, o.input, ns=ns) for o in cts if isinstance(o,ToolUseBlock)] + return tcs + # %% ../02_async.ipynb @delegates() class AsyncChat(Chat):