Skip to content

Commit

Permalink
fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Aug 27, 2024
1 parent 4066436 commit 3c6565a
Show file tree
Hide file tree
Showing 7 changed files with 996 additions and 74 deletions.
116 changes: 81 additions & 35 deletions 00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
{
"data": {
"text/plain": [
"Message(id='msg_01RjWdAktoZPidk5mZoXRRZL', 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=Usage(input_tokens=10, output_tokens=23))"
"Message(id='msg_01GJ5kFRLnNYQLaUo62pDdaD', 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 know or discuss?\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=10, output_tokens=32))"
]
},
"execution_count": null,
Expand Down Expand Up @@ -282,7 +282,7 @@
{
"data": {
"text/plain": [
"TextBlock(text=\"Hello Jeremy! It's nice to meet you. Is there anything I can help you with today?\", type='text')"
"TextBlock(text=\"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to know or discuss?\", type='text')"
]
},
"execution_count": null,
Expand Down Expand Up @@ -326,7 +326,7 @@
{
"data": {
"text/plain": [
"\"Hello Jeremy! It's nice to meet you. Is there anything I can help you with today?\""
"\"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to know or discuss?\""
]
},
"execution_count": null,
Expand Down Expand Up @@ -375,23 +375,23 @@
{
"data": {
"text/markdown": [
"Hello Jeremy! It's nice to meet you. Is there anything I can help you with today?\n",
"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to know or discuss?\n",
"\n",
"<details>\n",
"\n",
"- id: msg_01RjWdAktoZPidk5mZoXRRZL\n",
"- content: [{'text': \"Hello Jeremy! It's nice to meet you. Is there anything I can help you with today?\", 'type': 'text'}]\n",
"- id: msg_01GJ5kFRLnNYQLaUo62pDdaD\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 know or discuss?\", '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': 10, 'output_tokens': 23}\n",
"- usage: {'input_tokens': 10, 'output_tokens': 32}\n",
"\n",
"</details>"
],
"text/plain": [
"Message(id='msg_01RjWdAktoZPidk5mZoXRRZL', 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=Usage(input_tokens=10, output_tokens=23))"
"Message(id='msg_01GJ5kFRLnNYQLaUo62pDdaD', 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 know or discuss?\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=10, output_tokens=32))"
]
},
"execution_count": null,
Expand Down Expand Up @@ -422,7 +422,7 @@
{
"data": {
"text/plain": [
"Usage(input_tokens=10, output_tokens=23)"
"Usage(input_tokens=10, output_tokens=32)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -548,7 +548,7 @@
{
"data": {
"text/plain": [
"In: 10; Out: 23; Total: 33"
"In: 10; Out: 32; Total: 42"
]
},
"execution_count": null,
Expand Down Expand Up @@ -591,7 +591,7 @@
{
"data": {
"text/plain": [
"In: 20; Out: 74; Total: 94"
"In: 20; Out: 64; Total: 84"
]
},
"execution_count": null,
Expand Down Expand Up @@ -686,7 +686,7 @@
"\n",
"<details>\n",
"\n",
"- id: msg_017sTJJVEKEgfbXZmcRSuwnK\n",
"- id: msg_017wm9FhcSxcDXNk7kguzcWv\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",
Expand All @@ -698,7 +698,7 @@
"</details>"
],
"text/plain": [
"Message(id='msg_017sTJJVEKEgfbXZmcRSuwnK', 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; Total: 46)"
"Message(id='msg_017wm9FhcSxcDXNk7kguzcWv', 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; Total: 46)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -795,7 +795,7 @@
"\n",
"<details>\n",
"\n",
"- id: msg_0162tTPWXhr32HRm7WtbC7gG\n",
"- id: msg_017bNPBtZc73veTHZWDqDBJE\n",
"- content: [{'text': 'Of course! You just told me that your name is Jeremy.', 'type': 'text'}]\n",
"- model: claude-3-5-sonnet-20240620\n",
"- role: assistant\n",
Expand All @@ -807,7 +807,7 @@
"</details>"
],
"text/plain": [
"Message(id='msg_0162tTPWXhr32HRm7WtbC7gG', content=[TextBlock(text='Of course! You just 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: 60; Out: 16; Total: 76)"
"Message(id='msg_017bNPBtZc73veTHZWDqDBJE', content=[TextBlock(text='Of course! You just 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: 60; Out: 16; Total: 76)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -840,7 +840,7 @@
" \"Basic Anthropic messages client.\"\n",
" self.model,self.use = model,usage()\n",
" self.log = [] if log else None\n",
" self.c = (cli or Anthropic())"
" self.c = (cli or Anthropic(default_headers={'anthropic-beta': 'prompt-caching-2024-07-31'}))"
]
},
{
Expand Down Expand Up @@ -1052,19 +1052,19 @@
"\n",
"<details>\n",
"\n",
"- id: msg_01Ds4j94G5j51jVdY5aJKPsc\n",
"- id: msg_012nxrD3nA5rWCtRWmuU6ks2\n",
"- content: [{'text': 'Hello! How can I assist you today?', 'type': 'text'}]\n",
"- model: claude-3-haiku-20240307\n",
"- role: assistant\n",
"- stop_reason: end_turn\n",
"- stop_sequence: None\n",
"- type: message\n",
"- usage: {'input_tokens': 8, 'output_tokens': 12}\n",
"- usage: {'input_tokens': 8, 'output_tokens': 12, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}\n",
"\n",
"</details>"
],
"text/plain": [
"Message(id='msg_01Ds4j94G5j51jVdY5aJKPsc', 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_012nxrD3nA5rWCtRWmuU6ks2', 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)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1129,19 +1129,19 @@
"\n",
"<details>\n",
"\n",
"- id: msg_01RQ9JXwWi8ncwhwTJBtncKB\n",
"- id: msg_017bd3ZEU48vRxx8pmJkf1yD\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",
"- stop_reason: end_turn\n",
"- stop_sequence: None\n",
"- type: message\n",
"- usage: {'input_tokens': 24, 'output_tokens': 23}\n",
"- usage: {'input_tokens': 24, 'output_tokens': 23, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}\n",
"\n",
"</details>"
],
"text/plain": [
"Message(id='msg_01RQ9JXwWi8ncwhwTJBtncKB', 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; Total: 47)"
"Message(id='msg_017bd3ZEU48vRxx8pmJkf1yD', 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; Total: 47)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1260,19 +1260,19 @@
"\n",
"<details>\n",
"\n",
"- id: msg_01YGeu3qQNoNwafG66kB8V51\n",
"- id: msg_013CFS8Lo2BJj5X5FZWVaAr8\n",
"- content: [{'text': '1, 2, 3, 4, ', 'type': 'text'}]\n",
"- model: claude-3-haiku-20240307\n",
"- role: assistant\n",
"- stop_reason: stop_sequence\n",
"- stop_sequence: 5\n",
"- type: message\n",
"- usage: {'input_tokens': 15, 'output_tokens': 14}\n",
"- usage: {'input_tokens': 15, 'output_tokens': 14, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}\n",
"\n",
"</details>"
],
"text/plain": [
"Message(id='msg_01YGeu3qQNoNwafG66kB8V51', 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; Total: 29)"
"Message(id='msg_013CFS8Lo2BJj5X5FZWVaAr8', 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; Total: 29)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1335,7 +1335,7 @@
" 'system': '',\n",
" 'temperature': 0,\n",
" 'stop_sequences': ['2', 'yellow'],\n",
" 'result': Message(id='msg_01YZbPipXLvUEuCzBPESRxvS', 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; Total: 20),\n",
" 'result': Message(id='msg_013QcLR8dEx9tX2DZvMaFK8U', 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; Total: 20),\n",
" 'use': In: 94; Out: 89; Total: 183,\n",
" 'stop_reason': 'stop_sequence',\n",
" 'stop_sequence': '2'}"
Expand Down Expand Up @@ -2375,6 +2375,28 @@
"img = fn.read_bytes()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9fd16299",
"metadata": {},
"outputs": [],
"source": [
"#| exports\n",
"def _add_cache(d, cache):\n",
" \"Optionally add cache control\"\n",
" if cache: d[\"cache_control\"] = {\"type\": \"ephemeral\"}\n",
" return d"
]
},
{
"cell_type": "markdown",
"id": "026fb70e",
"metadata": {},
"source": [
"Claude supports context caching by adding a `cache_control` header, so we provide an option to enable that."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -2383,12 +2405,12 @@
"outputs": [],
"source": [
"#| exports\n",
"def img_msg(data:bytes)->dict:\n",
"def img_msg(data:bytes, cache:False)->dict:\n",
" \"Convert image `data` into an encoded `dict`\"\n",
" img = base64.b64encode(data).decode(\"utf-8\")\n",
" mtype = mimetypes.types_map['.'+imghdr.what(None, h=data)]\n",
" r = dict(type=\"base64\", media_type=mtype, data=img)\n",
" return {\"type\": \"image\", \"source\": r}"
" return _add_cache({\"type\": \"image\", \"source\": r}, cache)"
]
},
{
Expand All @@ -2407,9 +2429,9 @@
"outputs": [],
"source": [
"#| exports\n",
"def text_msg(s:str)->dict:\n",
"def text_msg(s:str, cache:False)->dict:\n",
" \"Convert `s` to a text message\"\n",
" return {\"type\": \"text\", \"text\": s}"
" return _add_cache({\"type\": \"text\", \"text\": s}, cache)"
]
},
{
Expand Down Expand Up @@ -2476,10 +2498,10 @@
"outputs": [],
"source": [
"#| exports\n",
"def _mk_content(src):\n",
"def _mk_content(src, cache=False):\n",
" \"Create appropriate content data structure based on type of content\"\n",
" if isinstance(src,str): return text_msg(src)\n",
" if isinstance(src,bytes): return img_msg(src)\n",
" if isinstance(src,str): return text_msg(src, cache=cache)\n",
" if isinstance(src,bytes): return img_msg(src, cache=cache)\n",
" return src"
]
},
Expand Down Expand Up @@ -2522,15 +2544,39 @@
"#| exports\n",
"def mk_msg(content, # A string, list, or dict containing the contents of the message\n",
" role='user', # Must be 'user' or 'assistant'\n",
" cache=False,\n",
" **kw):\n",
" \"Helper to create a `dict` appropriate for a Claude message. `kw` are added as key/value pairs to the message\"\n",
" if hasattr(content, 'content'): content,role = content.content,content.role\n",
" if isinstance(content, abc.Mapping): content=content['content']\n",
" if isinstance(content, abc.Mapping): content=content.get('content', content)\n",
" if not isinstance(content, list): content=[content]\n",
" content = [_mk_content(o) for o in content] if content else '.'\n",
" content = [_mk_content(o, cache if islast else False) for islast,o in loop_last(content)] if content else '.'\n",
" return dict(role=role, content=content, **kw)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19a9d3e1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'role': 'user',\n",
" 'content': [{'type': 'text', 'text': 'hi'},\n",
" {'type': 'text', 'text': 'there', 'cache_control': {'type': 'ephemeral'}}]}"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mk_msg(['hi', 'there'], cache=True)"
]
},
{
"cell_type": "markdown",
"id": "339cbd22",
Expand Down
Loading

0 comments on commit 3c6565a

Please sign in to comment.