From f4e0ae620462c98960f4ecb21e305f283a1429c5 Mon Sep 17 00:00:00 2001 From: Jeremy Howard Date: Tue, 15 Oct 2024 04:44:08 +1000 Subject: [PATCH] fixes #37 --- 00_core.ipynb | 149 ++++++++++++++++++++++++---------------------- claudette/core.py | 11 ++-- 2 files changed, 82 insertions(+), 78 deletions(-) diff --git a/00_core.ipynb b/00_core.ipynb index 63665cc..1178ad7 100644 --- a/00_core.ipynb +++ b/00_core.ipynb @@ -233,7 +233,7 @@ "\n", "
\n", "\n", - "- id: `msg_018EAtA4mLHFpYLUMUsHqeRL`\n", + "- id: `msg_01Q1vckoYvJbYqNiWVde4YAh`\n", "- content: `[{'text': \"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to talk about or any questions you have?\", 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20240620`\n", "- role: `assistant`\n", @@ -245,7 +245,7 @@ "
" ], "text/plain": [ - "Message(id='msg_018EAtA4mLHFpYLUMUsHqeRL', content=[TextBlock(text=\"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to talk about or any questions you have?\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 10; Out: 36; Cache create: 0; Cache read: 0; Total: 46)" + "Message(id='msg_01Q1vckoYvJbYqNiWVde4YAh', content=[TextBlock(text=\"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to talk about or any questions you have?\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 10; Out: 36; Cache create: 0; Cache read: 0; Total: 46)" ] }, "execution_count": null, @@ -425,7 +425,7 @@ "\n", "
\n", "\n", - "- id: `msg_018EAtA4mLHFpYLUMUsHqeRL`\n", + "- id: `msg_01Q1vckoYvJbYqNiWVde4YAh`\n", "- content: `[{'text': \"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to talk about or any questions you have?\", 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20240620`\n", "- role: `assistant`\n", @@ -437,7 +437,7 @@ "
" ], "text/plain": [ - "Message(id='msg_018EAtA4mLHFpYLUMUsHqeRL', content=[TextBlock(text=\"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to talk about or any questions you have?\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 10; Out: 36; Cache create: 0; Cache read: 0; Total: 46)" + "Message(id='msg_01Q1vckoYvJbYqNiWVde4YAh', content=[TextBlock(text=\"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to talk about or any questions you have?\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 10; Out: 36; Cache create: 0; Cache read: 0; Total: 46)" ] }, "execution_count": null, @@ -734,7 +734,7 @@ "\n", "
\n", "\n", - "- id: `msg_01LghdZYe8S92Kqv8tERZJZ4`\n", + "- id: `msg_01A8mhTieRqzSnxgdo4EZX3P`\n", "- content: `[{'text': \"Hello Jeremy! It's nice to meet you. Is there anything I can help you with today?\", 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20240620`\n", "- role: `assistant`\n", @@ -746,7 +746,7 @@ "
" ], "text/plain": [ - "Message(id='msg_01LghdZYe8S92Kqv8tERZJZ4', content=[TextBlock(text=\"Hello Jeremy! It's nice to meet you. Is there anything I can help you with today?\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 10; Out: 23; Cache create: 0; Cache read: 0; Total: 33)" + "Message(id='msg_01A8mhTieRqzSnxgdo4EZX3P', content=[TextBlock(text=\"Hello Jeremy! It's nice to meet you. Is there anything I can help you with today?\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 10; Out: 23; Cache create: 0; Cache read: 0; Total: 33)" ] }, "execution_count": null, @@ -839,23 +839,23 @@ { "data": { "text/markdown": [ - "Of course! You told me earlier that your name is Jeremy.\n", + "Hello! Of course, I'd be happy to remind you. You told me that your name is Jeremy.\n", "\n", "
\n", "\n", - "- id: `msg_012xRJ2q3AgS8FU9qfYgfg8s`\n", - "- content: `[{'text': 'Of course! You told me earlier that your name is Jeremy.', 'type': 'text'}]`\n", + "- id: `msg_016DyoiA47BLNTVSjQupLczR`\n", + "- content: `[{'text': \"Hello! Of course, I'd be happy to remind you. You told me that your name is Jeremy.\", 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20240620`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", - "- usage: `{'input_tokens': 58, 'output_tokens': 16}`\n", + "- usage: `{'input_tokens': 58, 'output_tokens': 25}`\n", "\n", "
" ], "text/plain": [ - "Message(id='msg_012xRJ2q3AgS8FU9qfYgfg8s', content=[TextBlock(text='Of course! You told me earlier that your name is Jeremy.', type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 58; Out: 16; Cache create: 0; Cache read: 0; Total: 74)" + "Message(id='msg_016DyoiA47BLNTVSjQupLczR', content=[TextBlock(text=\"Hello! Of course, I'd be happy to remind you. You told me that your name is Jeremy.\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 58; Out: 25; Cache create: 0; Cache read: 0; Total: 83)" ] }, "execution_count": null, @@ -1123,7 +1123,7 @@ "\n", "
\n", "\n", - "- id: `msg_01Vq8eg9CF89UnfycQVFtqmq`\n", + "- id: `msg_0149tYtBGuQ5JLagr7XtwRVg`\n", "- content: `[{'text': 'Hello! How can I assist you today?', 'type': 'text'}]`\n", "- model: `claude-3-haiku-20240307`\n", "- role: `assistant`\n", @@ -1135,7 +1135,7 @@ "
" ], "text/plain": [ - "Message(id='msg_01Vq8eg9CF89UnfycQVFtqmq', content=[TextBlock(text='Hello! How can I assist you today?', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 8; Out: 12; Cache create: 0; Cache read: 0; Total: 20)" + "Message(id='msg_0149tYtBGuQ5JLagr7XtwRVg', content=[TextBlock(text='Hello! How can I assist you today?', type='text')], model='claude-3-haiku-20240307', 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": null, @@ -1200,7 +1200,7 @@ "\n", "
\n", "\n", - "- id: `msg_01VCUby6sd4Et55uXQY4ZETt`\n", + "- id: `msg_01WhcJgftDJCdvSQM2PcwSBE`\n", "- content: `[{'text': 'According to Douglas Adams, \"The answer to the ultimate question of life, the universe, and everything is 42.\"', 'type': 'text'}]`\n", "- model: `claude-3-haiku-20240307`\n", "- role: `assistant`\n", @@ -1212,7 +1212,7 @@ "
" ], "text/plain": [ - "Message(id='msg_01VCUby6sd4Et55uXQY4ZETt', content=[TextBlock(text='According to Douglas Adams, \"The answer to the ultimate question of life, the universe, and everything is 42.\"', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 24; Out: 23; Cache create: 0; Cache read: 0; Total: 47)" + "Message(id='msg_01WhcJgftDJCdvSQM2PcwSBE', content=[TextBlock(text='According to Douglas Adams, \"The answer to the ultimate question of life, the universe, and everything is 42.\"', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 24; Out: 23; Cache create: 0; Cache read: 0; Total: 47)" ] }, "execution_count": null, @@ -1331,7 +1331,7 @@ "\n", "
\n", "\n", - "- id: `msg_01138y1DUMSYgpXj9LTD5H5f`\n", + "- id: `msg_013X453KDLgng8i54C9dRYbj`\n", "- content: `[{'text': '1, 2, 3, 4, ', 'type': 'text'}]`\n", "- model: `claude-3-haiku-20240307`\n", "- role: `assistant`\n", @@ -1343,7 +1343,7 @@ "
" ], "text/plain": [ - "Message(id='msg_01138y1DUMSYgpXj9LTD5H5f', content=[TextBlock(text='1, 2, 3, 4, ', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='stop_sequence', stop_sequence='5', type='message', usage=In: 15; Out: 14; Cache create: 0; Cache read: 0; Total: 29)" + "Message(id='msg_013X453KDLgng8i54C9dRYbj', content=[TextBlock(text='1, 2, 3, 4, ', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='stop_sequence', stop_sequence='5', type='message', usage=In: 15; Out: 14; Cache create: 0; Cache read: 0; Total: 29)" ] }, "execution_count": null, @@ -1410,7 +1410,7 @@ " 'temp': None,\n", " 'stream': None,\n", " 'stop': None,\n", - " 'result': Message(id='msg_01J2j6oTiLDUpjwVgcZXtCFq', content=[TextBlock(text='1, ', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='stop_sequence', stop_sequence='2', type='message', usage=In: 15; Out: 5; Cache create: 0; Cache read: 0; Total: 20),\n", + " 'result': Message(id='msg_01YQYQndsn4zGWoEAY3pyKi6', content=[TextBlock(text='1, ', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='stop_sequence', stop_sequence='2', type='message', usage=In: 15; Out: 5; Cache create: 0; Cache read: 0; Total: 20),\n", " 'use': In: 94; Out: 89; Cache create: 0; Cache read: 0; Total: 183,\n", " 'stop_reason': 'stop_sequence',\n", " 'stop_sequence': '2'}" @@ -1550,12 +1550,12 @@ { "data": { "text/markdown": [ - "ToolUseBlock(id='toolu_01PvSZwWLettTqoiVmPC1Tpv', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')\n", + "ToolUseBlock(id='toolu_0194WBTCEHsdetejQFmKgrXk', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')\n", "\n", "
\n", "\n", - "- id: `msg_01Nq1jdWSdEz5UKQbvrMGUJB`\n", - "- content: `[{'id': 'toolu_01PvSZwWLettTqoiVmPC1Tpv', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]`\n", + "- id: `msg_016A2WbFbdwTKvK9ibDJuA9X`\n", + "- content: `[{'id': 'toolu_0194WBTCEHsdetejQFmKgrXk', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]`\n", "- model: `claude-3-haiku-20240307`\n", "- role: `assistant`\n", "- stop_reason: `tool_use`\n", @@ -1566,7 +1566,7 @@ "
" ], "text/plain": [ - "Message(id='msg_01Nq1jdWSdEz5UKQbvrMGUJB', content=[ToolUseBlock(id='toolu_01PvSZwWLettTqoiVmPC1Tpv', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 493; Out: 53; Cache create: 0; Cache read: 0; Total: 546)" + "Message(id='msg_016A2WbFbdwTKvK9ibDJuA9X', content=[ToolUseBlock(id='toolu_0194WBTCEHsdetejQFmKgrXk', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 493; Out: 53; Cache create: 0; Cache read: 0; Total: 546)" ] }, "execution_count": null, @@ -1675,7 +1675,7 @@ "data": { "text/plain": [ "{'type': 'tool_result',\n", - " 'tool_use_id': 'toolu_01PvSZwWLettTqoiVmPC1Tpv',\n", + " 'tool_use_id': 'toolu_0194WBTCEHsdetejQFmKgrXk',\n", " 'content': 7063474}" ] }, @@ -1736,9 +1736,9 @@ "data": { "text/plain": [ "[{'role': 'assistant',\n", - " 'content': \"[ToolUseBlock(id='toolu_01PvSZwWLettTqoiVmPC1Tpv', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')]\"},\n", + " 'content': \"[ToolUseBlock(id='toolu_0194WBTCEHsdetejQFmKgrXk', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')]\"},\n", " {'role': 'user',\n", - " 'content': \"[{'type': 'tool_result', 'tool_use_id': 'toolu_01PvSZwWLettTqoiVmPC1Tpv', 'content': 7063474}]\"}]" + " 'content': \"[{'type': 'tool_result', 'tool_use_id': 'toolu_0194WBTCEHsdetejQFmKgrXk', 'content': 7063474}]\"}]" ] }, "execution_count": null, @@ -1860,11 +1860,10 @@ " stop=None, # Stop sequence\n", " tools:Optional[list]=None, # List of tools to make available to Claude\n", " tool_choice:Optional[dict]=None, # Optionally force use of some tool\n", - " pr=None, # prompt\n", " **kwargs):\n", " \"Make a call to Claude.\"\n", " if tools: kwargs['tools'] = [get_schema(o) for o in listify(tools)]\n", - " if tool_choice and pr: kwargs['tool_choice'] = mk_tool_choice(tool_choice)\n", + " if tool_choice: kwargs['tool_choice'] = mk_tool_choice(tool_choice)\n", " msgs = self._precall(msgs, prefill, stop, kwargs)\n", " if stream: return self._stream(msgs, prefill=prefill, max_tokens=maxtok, system=sp, temperature=temp, **kwargs)\n", " res = self.c.messages.create(model=self.model, messages=msgs, max_tokens=maxtok, system=sp, temperature=temp, **kwargs)\n", @@ -1890,12 +1889,12 @@ { "data": { "text/markdown": [ - "ToolUseBlock(id='toolu_01CbdsCCSftjkLHUPugoKuKv', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')\n", + "ToolUseBlock(id='toolu_01Lu6suErfUwcGK3AzKHJtLZ', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')\n", "\n", "
\n", "\n", - "- id: `msg_012mXKpcfVQbzs83ZdYdQuXS`\n", - "- content: `[{'id': 'toolu_01CbdsCCSftjkLHUPugoKuKv', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]`\n", + "- id: `msg_017Wu4jHECfPjsUoKSHgw2dF`\n", + "- content: `[{'id': 'toolu_01Lu6suErfUwcGK3AzKHJtLZ', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]`\n", "- model: `claude-3-haiku-20240307`\n", "- role: `assistant`\n", "- stop_reason: `tool_use`\n", @@ -1906,7 +1905,7 @@ "
" ], "text/plain": [ - "Message(id='msg_012mXKpcfVQbzs83ZdYdQuXS', content=[ToolUseBlock(id='toolu_01CbdsCCSftjkLHUPugoKuKv', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 489; Out: 57; Cache create: 0; Cache read: 0; Total: 546)" + "Message(id='msg_017Wu4jHECfPjsUoKSHgw2dF', content=[ToolUseBlock(id='toolu_01Lu6suErfUwcGK3AzKHJtLZ', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 489; Out: 57; Cache create: 0; Cache read: 0; Total: 546)" ] }, "execution_count": null, @@ -2027,13 +2026,12 @@ " cli:Optional[Client]=None, # Client to use (leave empty if passing `model`)\n", " sp='', # Optional system prompt\n", " tools:Optional[list]=None, # List of tools to make available to Claude\n", - " cont_pr:Optional[str]=None, # User prompt to continue an assistant response: assistant,[user:\"...\"],assistant\n", - " tool_choice:Optional[dict]=None): # Optionally force use of some tool\n", + " cont_pr:Optional[str]=None): # User prompt to continue an assistant response: assistant,[user:\"...\"],assistant\n", " \"Anthropic chat client.\"\n", " assert model or cli\n", " assert cont_pr != \"\", \"cont_pr may not be an empty string\"\n", " self.c = (cli or Client(model))\n", - " self.h,self.sp,self.tools,self.cont_pr,self.tool_choice = [],sp,tools,cont_pr,tool_choice\n", + " self.h,self.sp,self.tools,self.cont_pr = [],sp,tools,cont_pr\n", "\n", " @property\n", " def use(self): return self.c.use" @@ -2242,10 +2240,11 @@ " maxtok=4096, # Maximum tokens\n", " stream=False, # Stream response?\n", " prefill='', # Optional prefill to pass to Claude as start of its response\n", + " tool_choice:Optional[dict]=None, # Optionally force use of some tool\n", " **kw):\n", " self._append_pr(pr)\n", " res = self.c(self.h, stream=stream, prefill=prefill, sp=self.sp, temp=temp, maxtok=maxtok,\n", - " tools=self.tools, tool_choice=self.tool_choice, pr=pr,**kw)\n", + " tools=self.tools, tool_choice=tool_choice,**kw)\n", " if stream: return self._stream(res)\n", " self.h += mk_toolres(self.c.result, ns=self.tools, obj=self)\n", " return res" @@ -2272,19 +2271,19 @@ "\n", "
\n", "\n", - "- id: `msg_01DX8yPzTQQMX1ABfKjuuUuZ`\n", + "- id: `msg_01F3aKo8UyUA3D94DLbg1CPC`\n", "- content: `[{'text': 'Your name is Jeremy.', 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20240620`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", - "- usage: `{'input_tokens': 75, 'output_tokens': 8, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", + "- usage: `{'input_tokens': 153, 'output_tokens': 8, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", "\n", "
" ], "text/plain": [ - "Message(id='msg_01DX8yPzTQQMX1ABfKjuuUuZ', content=[TextBlock(text='Your name is Jeremy.', type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 75; Out: 8; Cache create: 0; Cache read: 0; Total: 83)" + "Message(id='msg_01F3aKo8UyUA3D94DLbg1CPC', content=[TextBlock(text='Your name is Jeremy.', type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 153; Out: 8; Cache create: 0; Cache read: 0; Total: 161)" ] }, "execution_count": null, @@ -2306,7 +2305,7 @@ { "data": { "text/plain": [ - "(In: 92; Out: 47; Cache create: 0; Cache read: 0; Total: 139, 0.000981)" + "(In: 243; Out: 109; Cache create: 0; Cache read: 0; Total: 352, 0.002364)" ] }, "execution_count": null, @@ -2350,31 +2349,31 @@ "\n", "42.\n", "\n", - "More seriously, there's no universally agreed upon answer. Common perspectives include:\n", + "More seriously, philosophers and thinkers have long debated this question. Common perspectives include:\n", "\n", - "• Finding personal meaning and fulfillment\n", - "• Loving and connecting with others \n", - "• Pursuing knowledge and growth\n", - "• Contributing positively to the world\n", - "• Experiencing joy and beauty\n", + "• Finding personal fulfillment and happiness\n", + "• Loving others and building relationships\n", + "• Making a positive impact on the world\n", + "• Pursuing knowledge and understanding\n", + "• Experiencing life's joys and challenges\n", "\n", - "Ultimately, many believe each person must define life's meaning for themselves.\n", + "Ultimately, many believe each person must define their own meaning.\n", "\n", "
\n", "\n", - "- id: `msg_01CyeNAfi2A1E9Hzu8DBxWe7`\n", - "- content: `[{'text': \"According to Douglas Adams,\\n\\n42.\\n\\nMore seriously, there's no universally agreed upon answer. Common perspectives include:\\n\\n• Finding personal meaning and fulfillment\\n• Loving and connecting with others \\n• Pursuing knowledge and growth\\n• Contributing positively to the world\\n• Experiencing joy and beauty\\n\\nUltimately, many believe each person must define life's meaning for themselves.\", 'type': 'text'}]`\n", + "- id: `msg_01Dehb6VbrWxX56JmsVgbaET`\n", + "- content: `[{'text': \"According to Douglas Adams,\\n\\n42.\\n\\nMore seriously, philosophers and thinkers have long debated this question. Common perspectives include:\\n\\n• Finding personal fulfillment and happiness\\n• Loving others and building relationships\\n• Making a positive impact on the world\\n• Pursuing knowledge and understanding\\n• Experiencing life's joys and challenges\\n\\nUltimately, many believe each person must define their own meaning.\", 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20240620`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", - "- usage: `{'input_tokens': 114, 'output_tokens': 78, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", + "- usage: `{'input_tokens': 192, 'output_tokens': 83, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", "\n", "
" ], "text/plain": [ - "Message(id='msg_01CyeNAfi2A1E9Hzu8DBxWe7', content=[TextBlock(text=\"According to Douglas Adams,\\n\\n42.\\n\\nMore seriously, there's no universally agreed upon answer. Common perspectives include:\\n\\n• Finding personal meaning and fulfillment\\n• Loving and connecting with others \\n• Pursuing knowledge and growth\\n• Contributing positively to the world\\n• Experiencing joy and beauty\\n\\nUltimately, many believe each person must define life's meaning for themselves.\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 114; Out: 78; Cache create: 0; Cache read: 0; Total: 192)" + "Message(id='msg_01Dehb6VbrWxX56JmsVgbaET', content=[TextBlock(text=\"According to Douglas Adams,\\n\\n42.\\n\\nMore seriously, philosophers and thinkers have long debated this question. Common perspectives include:\\n\\n• Finding personal fulfillment and happiness\\n• Loving others and building relationships\\n• Making a positive impact on the world\\n• Pursuing knowledge and understanding\\n• Experiencing life's joys and challenges\\n\\nUltimately, many believe each person must define their own meaning.\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 192; Out: 83; Cache create: 0; Cache read: 0; Total: 275)" ] }, "execution_count": null, @@ -2432,32 +2431,32 @@ "text/markdown": [ "[TextBlock(text=\"Continuing on the meaning of life:\n", "\n", - "• Achieving self-actualization\n", - "• Leaving a lasting legacy\n", - "• Serving a higher purpose or deity\n", - "• Maximizing happiness and minimizing suffering\n", - "• Embracing the absurdity of existence\n", - "• Living authentically and true to oneself\n", - "• Constantly evolving and adapting\n", - "• Finding balance and harmony\n", + "• Self-actualization and personal growth\n", + "• Serving a higher purpose or spiritual calling\n", + "• Creating art, music, or other forms of expression\n", + "• Advancing human knowledge through science and discovery\n", + "• Leaving a lasting legacy for future generations\n", + "• Finding balance between various aspects of life\n", + "• Embracing the absurdity and randomness of existence\n", + "• Cultivating mindfulness and living in the present moment\n", "\n", - "The search for meaning itself can be seen as part of life's purpose. It's a deeply personal journey that often evolves throughout one's lifetime.\", type='text')]\n", + "The search for meaning itself can be seen as part of life's purpose, with the journey being as important as any destination.\", type='text')]\n", "\n", "
\n", "\n", - "- id: `msg_014UpnCqdhPzjFQY3xKvMZPG`\n", - "- content: `[{'text': '[TextBlock(text=\"Continuing on the meaning of life:\\n\\n• Achieving self-actualization\\n• Leaving a lasting legacy\\n• Serving a higher purpose or deity\\n• Maximizing happiness and minimizing suffering\\n• Embracing the absurdity of existence\\n• Living authentically and true to oneself\\n• Constantly evolving and adapting\\n• Finding balance and harmony\\n\\nThe search for meaning itself can be seen as part of life\\'s purpose. It\\'s a deeply personal journey that often evolves throughout one\\'s lifetime.\", type=\\'text\\')]', 'type': 'text'}]`\n", + "- id: `msg_01MePtrG3DWnDzwvk2GDhmY2`\n", + "- content: `[{'text': '[TextBlock(text=\"Continuing on the meaning of life:\\n\\n• Self-actualization and personal growth\\n• Serving a higher purpose or spiritual calling\\n• Creating art, music, or other forms of expression\\n• Advancing human knowledge through science and discovery\\n• Leaving a lasting legacy for future generations\\n• Finding balance between various aspects of life\\n• Embracing the absurdity and randomness of existence\\n• Cultivating mindfulness and living in the present moment\\n\\nThe search for meaning itself can be seen as part of life\\'s purpose, with the journey being as important as any destination.\", type=\\'text\\')]', 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20240620`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", - "- usage: `{'input_tokens': 224, 'output_tokens': 121, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", + "- usage: `{'input_tokens': 307, 'output_tokens': 134, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", "\n", "
" ], "text/plain": [ - "Message(id='msg_014UpnCqdhPzjFQY3xKvMZPG', content=[TextBlock(text='[TextBlock(text=\"Continuing on the meaning of life:\\n\\n• Achieving self-actualization\\n• Leaving a lasting legacy\\n• Serving a higher purpose or deity\\n• Maximizing happiness and minimizing suffering\\n• Embracing the absurdity of existence\\n• Living authentically and true to oneself\\n• Constantly evolving and adapting\\n• Finding balance and harmony\\n\\nThe search for meaning itself can be seen as part of life\\'s purpose. It\\'s a deeply personal journey that often evolves throughout one\\'s lifetime.\", type=\\'text\\')]', type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 224; Out: 121; Cache create: 0; Cache read: 0; Total: 345)" + "Message(id='msg_01MePtrG3DWnDzwvk2GDhmY2', content=[TextBlock(text='[TextBlock(text=\"Continuing on the meaning of life:\\n\\n• Self-actualization and personal growth\\n• Serving a higher purpose or spiritual calling\\n• Creating art, music, or other forms of expression\\n• Advancing human knowledge through science and discovery\\n• Leaving a lasting legacy for future generations\\n• Finding balance between various aspects of life\\n• Embracing the absurdity and randomness of existence\\n• Cultivating mindfulness and living in the present moment\\n\\nThe search for meaning itself can be seen as part of life\\'s purpose, with the journey being as important as any destination.\", type=\\'text\\')]', type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 307; Out: 134; Cache create: 0; Cache read: 0; Total: 441)" ] }, "execution_count": null, @@ -2581,8 +2580,8 @@ "\n", "
\n", "\n", - "- id: `msg_01Nnh4jeazfzmWWQ7wbq8tzK`\n", - "- content: `[{'text': 'To answer this question, I can use the \"sums\" function to add these two numbers together. Let me do that for you.', 'type': 'text'}, {'id': 'toolu_014eFmrLb2WZQbTEi9i5TyCp', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]`\n", + "- id: `msg_012pSzK2jxiNzvDTSAvgoWVD`\n", + "- content: `[{'text': 'To answer this question, I can use the \"sums\" function to add these two numbers together. Let me do that for you.', 'type': 'text'}, {'id': 'toolu_01KfmvWrBPWj5FqVowCFzMAk', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]`\n", "- model: `claude-3-5-sonnet-20240620`\n", "- role: `assistant`\n", "- stop_reason: `tool_use`\n", @@ -2593,7 +2592,7 @@ "
" ], "text/plain": [ - "Message(id='msg_01Nnh4jeazfzmWWQ7wbq8tzK', content=[TextBlock(text='To answer this question, I can use the \"sums\" function to add these two numbers together. Let me do that for you.', type='text'), ToolUseBlock(id='toolu_014eFmrLb2WZQbTEi9i5TyCp', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 428; Out: 101; Cache create: 0; Cache read: 0; Total: 529)" + "Message(id='msg_012pSzK2jxiNzvDTSAvgoWVD', content=[TextBlock(text='To answer this question, I can use the \"sums\" function to add these two numbers together. Let me do that for you.', type='text'), ToolUseBlock(id='toolu_01KfmvWrBPWj5FqVowCFzMAk', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 428; Out: 101; Cache create: 0; Cache read: 0; Total: 529)" ] }, "execution_count": null, @@ -2607,6 +2606,14 @@ "r" ] }, + { + "cell_type": "markdown", + "id": "6486462c", + "metadata": {}, + "source": [ + "Now we need to send this result to Claude—calling the object with no parameters tells it to return the tool result to Claude:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -2623,23 +2630,21 @@ "\n", "This result was calculated using the \"sums\" function, which accurately added the two numbers together. The function took the first number (604,542) as parameter 'a' and the second number (6,458,932) as parameter 'b', and returned their sum.\n", "\n", - "Is there anything else you'd like to calculate or any other questions you have?\n", - "\n", "
\n", "\n", - "- id: `msg_01CXTUmb9jgHc6NaZRQtyim9`\n", - "- content: `[{'text': 'The sum of 604,542 and 6,458,932 is 7,063,474.\\n\\nTo break it down:\\n604,542 + 6,458,932 = 7,063,474\\n\\nThis result was calculated using the \"sums\" function, which accurately added the two numbers together. The function took the first number (604,542) as parameter \\'a\\' and the second number (6,458,932) as parameter \\'b\\', and returned their sum.\\n\\nIs there anything else you\\'d like to calculate or any other questions you have?', 'type': 'text'}]`\n", + "- id: `msg_01XZsMbG7yzXMBoLXZEHcKEX`\n", + "- content: `[{'text': 'The sum of 604,542 and 6,458,932 is 7,063,474.\\n\\nTo break it down:\\n604,542 + 6,458,932 = 7,063,474\\n\\nThis result was calculated using the \"sums\" function, which accurately added the two numbers together. The function took the first number (604,542) as parameter \\'a\\' and the second number (6,458,932) as parameter \\'b\\', and returned their sum.', 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20240620`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", - "- usage: `{'input_tokens': 589, 'output_tokens': 130, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", + "- usage: `{'input_tokens': 589, 'output_tokens': 113, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", "\n", "
" ], "text/plain": [ - "Message(id='msg_01CXTUmb9jgHc6NaZRQtyim9', content=[TextBlock(text='The sum of 604,542 and 6,458,932 is 7,063,474.\\n\\nTo break it down:\\n604,542 + 6,458,932 = 7,063,474\\n\\nThis result was calculated using the \"sums\" function, which accurately added the two numbers together. The function took the first number (604,542) as parameter \\'a\\' and the second number (6,458,932) as parameter \\'b\\', and returned their sum.\\n\\nIs there anything else you\\'d like to calculate or any other questions you have?', type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 589; Out: 130; Cache create: 0; Cache read: 0; Total: 719)" + "Message(id='msg_01XZsMbG7yzXMBoLXZEHcKEX', content=[TextBlock(text='The sum of 604,542 and 6,458,932 is 7,063,474.\\n\\nTo break it down:\\n604,542 + 6,458,932 = 7,063,474\\n\\nThis result was calculated using the \"sums\" function, which accurately added the two numbers together. The function took the first number (604,542) as parameter \\'a\\' and the second number (6,458,932) as parameter \\'b\\', and returned their sum.', type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 589; Out: 113; Cache create: 0; Cache read: 0; Total: 702)" ] }, "execution_count": null, diff --git a/claudette/core.py b/claudette/core.py index e9c05aa..76ff0e8 100644 --- a/claudette/core.py +++ b/claudette/core.py @@ -208,11 +208,10 @@ def __call__(self:Client, stop=None, # Stop sequence tools:Optional[list]=None, # List of tools to make available to Claude tool_choice:Optional[dict]=None, # Optionally force use of some tool - pr=None, # prompt **kwargs): "Make a call to Claude." if tools: kwargs['tools'] = [get_schema(o) for o in listify(tools)] - if tool_choice and pr: kwargs['tool_choice'] = mk_tool_choice(tool_choice) + if tool_choice: kwargs['tool_choice'] = mk_tool_choice(tool_choice) msgs = self._precall(msgs, prefill, stop, kwargs) if stream: return self._stream(msgs, prefill=prefill, max_tokens=maxtok, system=sp, temperature=temp, **kwargs) res = self.c.messages.create(model=self.model, messages=msgs, max_tokens=maxtok, system=sp, temperature=temp, **kwargs) @@ -242,13 +241,12 @@ def __init__(self, cli:Optional[Client]=None, # Client to use (leave empty if passing `model`) sp='', # Optional system prompt tools:Optional[list]=None, # List of tools to make available to Claude - cont_pr:Optional[str]=None, # User prompt to continue an assistant response: assistant,[user:"..."],assistant - tool_choice:Optional[dict]=None): # Optionally force use of some tool + cont_pr:Optional[str]=None): # User prompt to continue an assistant response: assistant,[user:"..."],assistant "Anthropic chat client." assert model or cli assert cont_pr != "", "cont_pr may not be an empty string" self.c = (cli or Client(model)) - self.h,self.sp,self.tools,self.cont_pr,self.tool_choice = [],sp,tools,cont_pr,tool_choice + self.h,self.sp,self.tools,self.cont_pr = [],sp,tools,cont_pr @property def use(self): return self.c.use @@ -302,10 +300,11 @@ def __call__(self:Chat, maxtok=4096, # Maximum tokens stream=False, # Stream response? prefill='', # Optional prefill to pass to Claude as start of its response + tool_choice:Optional[dict]=None, # Optionally force use of some tool **kw): self._append_pr(pr) res = self.c(self.h, stream=stream, prefill=prefill, sp=self.sp, temp=temp, maxtok=maxtok, - tools=self.tools, tool_choice=self.tool_choice, pr=pr,**kw) + tools=self.tools, tool_choice=tool_choice,**kw) if stream: return self._stream(res) self.h += mk_toolres(self.c.result, ns=self.tools, obj=self) return res