From 38830b944df8c3314ed1bb9beed1b980162a71bc Mon Sep 17 00:00:00 2001 From: Jeremy Howard Date: Sun, 28 Apr 2024 11:07:48 +1000 Subject: [PATCH] intro source --- 00_core.ipynb | 449 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 370 insertions(+), 79 deletions(-) diff --git a/00_core.ipynb b/00_core.ipynb index 4d720f5..7bd2b9f 100644 --- a/00_core.ipynb +++ b/00_core.ipynb @@ -60,7 +60,7 @@ "from collections import abc\n", "\n", "from anthropic import Anthropic\n", - "from anthropic.types import Usage, TextBlock\n", + "from anthropic.types import Usage, TextBlock, Message\n", "from anthropic.types.beta.tools import ToolsBetaMessage, tool_use_block\n", "from inspect import Parameter\n", "from io import BytesIO\n", @@ -77,9 +77,19 @@ "id": "0fff8869", "metadata": {}, "outputs": [], + "source": [ + "#| exports\n", + "models = 'claude-3-opus-20240229','claude-3-sonnet-20240229','claude-3-haiku-20240307'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e993496", + "metadata": {}, + "outputs": [], "source": [ "#| export\n", - "models = 'claude-3-opus-20240229','claude-3-sonnet-20240229','claude-3-haiku-20240307'\n", "empty = Parameter.empty" ] }, @@ -90,7 +100,7 @@ "metadata": {}, "outputs": [], "source": [ - "model = models[1]" + "model = models[-1]" ] }, { @@ -101,6 +111,16 @@ "## Client" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d7a372d5", + "metadata": {}, + "outputs": [], + "source": [ + "cli = Anthropic()" + ] + }, { "cell_type": "code", "execution_count": null, @@ -119,21 +139,52 @@ { "cell_type": "code", "execution_count": null, - "id": "8cfb240d", + "id": "edb7312e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'role': 'user', 'content': \"I'm Jeremy\"}" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#| exports\n", - "def mk_msgs(msgs, **kw):\n", - " \"Helper to set 'assistant' role on alternate messages\"\n", - " if isinstance(msgs,str): msgs=[msgs]\n", - " return [mk_msg(o, ('user','assistant')[i%2], **kw) for i,o in enumerate(msgs)]" + "prompt = \"I'm Jeremy\"\n", + "m = mk_msg(prompt)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "057917c1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Message(id='msg_01LzzKUnamfWzMEYfb7Lcfxu', content=[TextBlock(text=\"It's nice to meet you Jeremy! As an AI assistant, I don't have a personal identity, but I'm happy to chat and try my best to help you with any questions or tasks you may have. Please feel free to ask me anything.\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=10, output_tokens=54))" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r = cli.messages.create(messages=[m], model=model, max_tokens=100)\n", + "r" ] }, { "cell_type": "code", "execution_count": null, - "id": "6260b8ba", + "id": "544b1326", "metadata": {}, "outputs": [], "source": [ @@ -146,7 +197,28 @@ { "cell_type": "code", "execution_count": null, - "id": "660fbb78", + "id": "73e42331", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "TextBlock(text=\"It's nice to meet you Jeremy! As an AI assistant, I don't have a personal identity, but I'm happy to chat and try my best to help you with any questions or tasks you may have. Please feel free to ask me anything.\", type='text')" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "find_block(r)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ca5dea18", "metadata": {}, "outputs": [], "source": [ @@ -159,96 +231,124 @@ { "cell_type": "code", "execution_count": null, - "id": "a38a4733", + "id": "e9d8bb16", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "\"It's nice to meet you Jeremy! As an AI assistant, I don't have a personal identity, but I'm happy to chat and try my best to help you with any questions or tasks you may have. Please feel free to ask me anything.\"" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "cli = Anthropic()" + "contents(r)" ] }, { "cell_type": "code", "execution_count": null, - "id": "fcd9de2e", + "id": "8cfb240d", "metadata": {}, "outputs": [], "source": [ - "#|export\n", - "@patch\n", - "def __add__(self:Usage, b):\n", - " return Usage(input_tokens=self.input_tokens+b.input_tokens, output_tokens=self.output_tokens+b.output_tokens)\n", - "\n", - "@patch(as_prop=True)\n", - "def total(self:Usage): return self.input_tokens+self.output_tokens" + "#| exports\n", + "def mk_msgs(msgs, **kw):\n", + " \"Helper to set 'assistant' role on alternate messages\"\n", + " if isinstance(msgs,str): msgs=[msgs]\n", + " return [mk_msg(o, ('user','assistant')[i%2], **kw) for i,o in enumerate(msgs)]" ] }, { "cell_type": "code", "execution_count": null, - "id": "47d694ab", + "id": "1e8f3beb", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[{'role': 'user', 'content': \"I'm Jeremy\"},\n", + " {'role': 'assistant',\n", + " 'content': [TextBlock(text=\"It's nice to meet you Jeremy! As an AI assistant, I don't have a personal identity, but I'm happy to chat and try my best to help you with any questions or tasks you may have. Please feel free to ask me anything.\", type='text')]},\n", + " {'role': 'user', 'content': 'I forgot my name. Can you remind me please?'}]" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "@patch\n", - "def __repr__(self:Usage): return f'In: {self.input_tokens}; Out: {self.output_tokens}; Total: {self.total}'\n", - "\n", - "@patch\n", - "def _repr_html_(self:ToolsBetaMessage):\n", - " det = '\\n
  • '.join(f'{k}: {v}' for k,v in self.dict().items())\n", - " return f\"\"\"{contents(self)}\n", - "
    • \n", - "{det}\n", - "
  • \"\"\"" + "msgs = mk_msgs([prompt, r, 'I forgot my name. Can you remind me please?'])\n", + "msgs" ] }, { "cell_type": "code", "execution_count": null, - "id": "01186452", + "id": "d3b33c41", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Message(id='msg_01FidCYYvZsh8scXhxK7JiYR', content=[TextBlock(text=\"I'm afraid I don't actually know your name - you introduced yourself to me as Jeremy, but if you've forgotten your own name, I don't have a way to remind you of it. As an AI, I don't have personal information about you stored. I can only respond based on what you tell me directly in our conversation.\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=78, output_tokens=72))" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#| exports\n", - "class Client:\n", - " def __init__(self, model, cli=None):\n", - " \"Basic Anthropic messages client\"\n", - " self.model,self.use = model,Usage(input_tokens=0,output_tokens=0)\n", - " self.c = (cli or Anthropic())" + "r = cli.messages.create(messages=msgs, model=model, max_tokens=200)\n", + "r" ] }, { "cell_type": "code", "execution_count": null, - "id": "950f4e5d", + "id": "9fecbf43", "metadata": {}, "outputs": [], "source": [ - "c = Client(models[-1])" + "@patch\n", + "def _repr_html_(self:(ToolsBetaMessage,Message)):\n", + " det = '\\n
  • '.join(f'{k}: {v}' for k,v in self.dict().items())\n", + " return f\"\"\"{contents(self)}\n", + "
    • \n", + "{det}\n", + "
  • \"\"\"" ] }, { "cell_type": "code", "execution_count": null, - "id": "0d027017", + "id": "c6f7c428", "metadata": {}, "outputs": [ { "data": { "text/html": [ - "Hello! How can I assist you today?\n", + "I'm afraid I don't actually know your name - you introduced yourself to me as Jeremy, but if you've forgotten your own name, I don't have a way to remind you of it. As an AI, I don't have personal information about you stored. I can only respond based on what you tell me directly in our conversation.\n", "
    " ], "text/plain": [ - "ToolsBetaMessage(id='msg_018oPRxexjQpNTZYJG8mBzRm', 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; Total: 20)" + "Message(id='msg_01FidCYYvZsh8scXhxK7JiYR', content=[TextBlock(text=\"I'm afraid I don't actually know your name - you introduced yourself to me as Jeremy, but if you've forgotten your own name, I don't have a way to remind you of it. As an AI, I don't have personal information about you stored. I can only respond based on what you tell me directly in our conversation.\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=78, output_tokens=72))" ] }, "execution_count": null, @@ -257,14 +357,205 @@ } ], "source": [ - "a = c.c.beta.tools.messages.create(model=c.model, messages=mk_msgs('Hi'), max_tokens=100)\n", - "a" + "r" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a5870416", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Usage(input_tokens=78, output_tokens=72)" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r.usage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "47007768", + "metadata": {}, + "outputs": [], + "source": [ + "#| exports\n", + "def usage(inp=0, out=0):\n", + " \"Slightly more concise version of `Usage`\"\n", + " return Usage(input_tokens=inp, output_tokens=out)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "04b88b6a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Usage(input_tokens=5, output_tokens=0)" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "usage(5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22dc3680", + "metadata": {}, + "outputs": [], + "source": [ + "#| exports\n", + "@patch(as_prop=True)\n", + "def total(self:Usage): return self.input_tokens+self.output_tokens" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6120e315", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "usage(5,1).total" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6473320f", + "metadata": {}, + "outputs": [], + "source": [ + "#| exports\n", + "@patch\n", + "def __repr__(self:Usage): return f'In: {self.input_tokens}; Out: {self.output_tokens}; Total: {self.total}'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3339d94e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "In: 78; Out: 72; Total: 150" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r.usage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ea0bf9a5", + "metadata": {}, + "outputs": [], + "source": [ + "#| exports\n", + "@patch\n", + "def __add__(self:Usage, b):\n", + " return usage(self.input_tokens+b.input_tokens, self.output_tokens+b.output_tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "47864e07", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "In: 156; Out: 144; Total: 300" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r.usage+r.usage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e757c12c", + "metadata": {}, + "outputs": [], + "source": [ + "#| exports\n", + "class Client:\n", + " def __init__(self, model, cli=None):\n", + " \"Basic Anthropic messages client\"\n", + " self.model,self.use = model,Usage(input_tokens=0,output_tokens=0)\n", + " self.c = (cli or Anthropic())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "193ca8fa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "In: 0; Out: 0; Total: 0" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c = Client(model)\n", + "c.use" ] }, { "cell_type": "code", "execution_count": null, - "id": "65e3488c", + "id": "09a2c487", "metadata": {}, "outputs": [], "source": [ @@ -280,13 +571,13 @@ { "cell_type": "code", "execution_count": null, - "id": "a9e6201c", + "id": "768f91ab", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "In: 8; Out: 12; Total: 20" + "In: 78; Out: 72; Total: 150" ] }, "execution_count": null, @@ -295,14 +586,14 @@ } ], "source": [ - "c._r(a)\n", + "c._r(r)\n", "c.use" ] }, { "cell_type": "code", "execution_count": null, - "id": "2727fdbd", + "id": "3ecaa024", "metadata": {}, "outputs": [], "source": [ @@ -318,7 +609,7 @@ { "cell_type": "code", "execution_count": null, - "id": "ea76ae84", + "id": "9e3655e8", "metadata": {}, "outputs": [ { @@ -352,7 +643,7 @@ { "cell_type": "code", "execution_count": null, - "id": "ef039c26", + "id": "1cf22e90", "metadata": {}, "outputs": [ { @@ -437,7 +728,7 @@ { "cell_type": "code", "execution_count": null, - "id": "2b76b837", + "id": "d61b4419", "metadata": {}, "outputs": [], "source": [ @@ -452,7 +743,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3fc55ebe", + "id": "8b967131", "metadata": {}, "outputs": [ { @@ -473,7 +764,7 @@ { "cell_type": "code", "execution_count": null, - "id": "5761d788", + "id": "de6874d6", "metadata": {}, "outputs": [], "source": [ @@ -493,7 +784,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4cb8cd92", + "id": "e1223493", "metadata": {}, "outputs": [ { @@ -534,7 +825,7 @@ { "cell_type": "code", "execution_count": null, - "id": "edc3217b", + "id": "1e18ba87", "metadata": {}, "outputs": [], "source": [ @@ -551,7 +842,7 @@ { "cell_type": "code", "execution_count": null, - "id": "19f8273e", + "id": "f6cb0cac", "metadata": {}, "outputs": [ { @@ -689,7 +980,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8de0b2ee", + "id": "db1ba49e", "metadata": {}, "outputs": [], "source": [ @@ -702,7 +993,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4e936629", + "id": "21cb4247", "metadata": {}, "outputs": [], "source": [ @@ -730,7 +1021,7 @@ { "cell_type": "code", "execution_count": null, - "id": "07c50c88", + "id": "1d981fb8", "metadata": {}, "outputs": [], "source": [ @@ -773,7 +1064,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4adfe345", + "id": "1a8702f9", "metadata": {}, "outputs": [], "source": [ @@ -791,7 +1082,7 @@ { "cell_type": "code", "execution_count": null, - "id": "961c2299", + "id": "56b675e4", "metadata": {}, "outputs": [ { @@ -846,7 +1137,7 @@ }, { "cell_type": "markdown", - "id": "a5e190b8", + "id": "94b4a199", "metadata": {}, "source": [ "## Chat" @@ -855,7 +1146,7 @@ { "cell_type": "code", "execution_count": null, - "id": "025c0546", + "id": "d9a03565", "metadata": {}, "outputs": [], "source": [ @@ -879,17 +1170,17 @@ { "cell_type": "code", "execution_count": null, - "id": "262cb0a5", + "id": "14477abd", "metadata": {}, "outputs": [], "source": [ - "c = Chat(models[-1])" + "c = Chat(model)" ] }, { "cell_type": "code", "execution_count": null, - "id": "c3d6882c", + "id": "3923cfd5", "metadata": {}, "outputs": [ { @@ -911,7 +1202,7 @@ { "cell_type": "code", "execution_count": null, - "id": "e05b5baf", + "id": "1ea37384", "metadata": {}, "outputs": [], "source": [ @@ -923,7 +1214,7 @@ { "cell_type": "code", "execution_count": null, - "id": "74d82105", + "id": "6252d3d2", "metadata": {}, "outputs": [ { @@ -958,7 +1249,7 @@ { "cell_type": "code", "execution_count": null, - "id": "7a045192", + "id": "a07999da", "metadata": {}, "outputs": [ {