Skip to content

Commit

Permalink
Merge pull request #17 from xl0/logs2
Browse files Browse the repository at this point in the history
Save all messages in client.log[]
  • Loading branch information
jph00 authored Aug 19, 2024
2 parents f13c6e5 + 48b42a1 commit 95758d8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 74 deletions.
75 changes: 61 additions & 14 deletions 00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@
{
"data": {
"text/plain": [
"In: 20; Out: 46; Total: 66"
"In: 20; Out: 74; Total: 94"
]
},
"execution_count": null,
Expand Down Expand Up @@ -682,12 +682,12 @@
{
"data": {
"text/markdown": [
"Hello Jeremy! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to chat about or any questions you have?\n",
"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?\n",
"\n",
"<details>\n",
"\n",
"- id: msg_01TDkKPNLXoSK8DCPuyP37hX\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 chat about or any questions you have?\", 'type': 'text'}]\n",
"- id: msg_017sTJJVEKEgfbXZmcRSuwnK\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",
"- stop_reason: end_turn\n",
Expand All @@ -698,7 +698,7 @@
"</details>"
],
"text/plain": [
"Message(id='msg_01TDkKPNLXoSK8DCPuyP37hX', 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 chat 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_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)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -760,7 +760,7 @@
"text/plain": [
"[{'role': 'user', 'content': \"I'm Jeremy\"},\n",
" {'role': 'assistant',\n",
" '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 chat about or any questions you have?\", type='text')]},\n",
" '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')]},\n",
" {'role': 'user', 'content': 'I forgot my name. Can you remind me please?'}]"
]
},
Expand Down Expand Up @@ -791,23 +791,23 @@
{
"data": {
"text/markdown": [
"Of course! You just introduced yourself as Jeremy. That's your name.\n",
"Of course! You just told me that your name is Jeremy.\n",
"\n",
"<details>\n",
"\n",
"- id: msg_014avFgGrLozDLLtVPy6BerE\n",
"- content: [{'text': \"Of course! You just introduced yourself as Jeremy. That's your name.\", 'type': 'text'}]\n",
"- id: msg_0162tTPWXhr32HRm7WtbC7gG\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",
"- stop_reason: end_turn\n",
"- stop_sequence: None\n",
"- type: message\n",
"- usage: {'input_tokens': 60, 'output_tokens': 18}\n",
"- usage: {'input_tokens': 60, 'output_tokens': 16}\n",
"\n",
"</details>"
],
"text/plain": [
"Message(id='msg_014avFgGrLozDLLtVPy6BerE', content=[TextBlock(text=\"Of course! You just introduced yourself as Jeremy. That's your name.\", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 60; Out: 18; Total: 78)"
"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)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -836,9 +836,10 @@
"source": [
"#| exports\n",
"class Client:\n",
" def __init__(self, model, cli=None):\n",
" def __init__(self, model, cli=None, log=False):\n",
" \"Basic Anthropic messages client.\"\n",
" self.model,self.use = model,usage()\n",
" self.log = [] if log else None\n",
" self.c = (cli or Anthropic())"
]
},
Expand Down Expand Up @@ -946,7 +947,11 @@
" with self.c.messages.stream(model=self.model, messages=mk_msgs(msgs), **kwargs) as s:\n",
" if prefill: yield(prefill)\n",
" yield from s.text_stream\n",
" self._r(s.get_final_message(), prefill)"
" self._r(s.get_final_message(), prefill)\n",
" if self.log is not None: self.log.append({\n",
" \"msgs\": msgs, \"prefill\": prefill, **kwargs,\n",
" \"result\": self.result, \"use\": self.use, \"stop_reason\": self.stop_reason, \"stop_sequence\": self.stop_sequence\n",
" })"
]
},
{
Expand Down Expand Up @@ -987,6 +992,10 @@
" res = self.c.messages.create(\n",
" model=self.model, messages=msgs, max_tokens=maxtok, system=sp, temperature=temp, **kwargs)\n",
" self._r(res, prefill)\n",
" if self.log is not None: self.log.append({\n",
" \"msgs\": msgs, \"maxtok\": maxtok, \"sp\": sp, \"temp\": temp, \"prefill\": prefill, \"stream\": stream, \"stop\": stop, **kwargs,\n",
" \"result\": res, \"use\": self.use, \"stop_reason\": self.stop_reason, \"stop_sequence\": self.stop_sequence\n",
" })\n",
" return self.result"
]
},
Expand Down Expand Up @@ -1016,7 +1025,7 @@
}
],
"source": [
"c = Client(model)\n",
"c = Client(model, log=True)\n",
"c.use"
]
},
Expand Down Expand Up @@ -1303,6 +1312,44 @@
"print(c.stop_reason, c.stop_sequence)"
]
},
{
"cell_type": "markdown",
"id": "2108776e",
"metadata": {},
"source": [
"You can check the logs:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17122b92",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'msgs': [{'role': 'user', 'content': 'Count from 1 to 10'}],\n",
" 'prefill': '',\n",
" 'max_tokens': 4096,\n",
" '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",
" 'use': In: 94; Out: 89; Total: 183,\n",
" 'stop_reason': 'stop_sequence',\n",
" 'stop_sequence': '2'}"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.log[-1]"
]
},
{
"cell_type": "markdown",
"id": "1a7cdbc6",
Expand Down
26 changes: 0 additions & 26 deletions claudette/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,4 @@
'claudette.core.mk_toolres': ('core.html#mk_toolres', 'claudette/core.py'),
'claudette.core.text_msg': ('core.html#text_msg', 'claudette/core.py'),
'claudette.core.usage': ('core.html#usage', 'claudette/core.py')},
'claudette.core2': { 'claudette.core2.Chat': ('core.html#chat', 'claudette/core2.py'),
'claudette.core2.Chat.__call__': ('core.html#chat.__call__', 'claudette/core2.py'),
'claudette.core2.Chat.__init__': ('core.html#chat.__init__', 'claudette/core2.py'),
'claudette.core2.Chat._stream': ('core.html#chat._stream', 'claudette/core2.py'),
'claudette.core2.Chat.use': ('core.html#chat.use', 'claudette/core2.py'),
'claudette.core2.Client': ('core.html#client', 'claudette/core2.py'),
'claudette.core2.Client.__call__': ('core.html#client.__call__', 'claudette/core2.py'),
'claudette.core2.Client.__init__': ('core.html#client.__init__', 'claudette/core2.py'),
'claudette.core2.Client._r': ('core.html#client._r', 'claudette/core2.py'),
'claudette.core2.Client._stream': ('core.html#client._stream', 'claudette/core2.py'),
'claudette.core2.Message._repr_markdown_': ('core.html#message._repr_markdown_', 'claudette/core2.py'),
'claudette.core2.Usage.__add__': ('core.html#usage.__add__', 'claudette/core2.py'),
'claudette.core2.Usage.__repr__': ('core.html#usage.__repr__', 'claudette/core2.py'),
'claudette.core2.Usage.total': ('core.html#usage.total', 'claudette/core2.py'),
'claudette.core2._mk_content': ('core.html#_mk_content', 'claudette/core2.py'),
'claudette.core2._mk_ns': ('core.html#_mk_ns', 'claudette/core2.py'),
'claudette.core2.call_func': ('core.html#call_func', 'claudette/core2.py'),
'claudette.core2.contents': ('core.html#contents', 'claudette/core2.py'),
'claudette.core2.find_block': ('core.html#find_block', 'claudette/core2.py'),
'claudette.core2.img_msg': ('core.html#img_msg', 'claudette/core2.py'),
'claudette.core2.mk_msg': ('core.html#mk_msg', 'claudette/core2.py'),
'claudette.core2.mk_msgs': ('core.html#mk_msgs', 'claudette/core2.py'),
'claudette.core2.mk_tool_choice': ('core.html#mk_tool_choice', 'claudette/core2.py'),
'claudette.core2.mk_toolres': ('core.html#mk_toolres', 'claudette/core2.py'),
'claudette.core2.text_msg': ('core.html#text_msg', 'claudette/core2.py'),
'claudette.core2.usage': ('core.html#usage', 'claudette/core2.py')},
'claudette.toolloop': {'claudette.toolloop.Chat.toolloop': ('toolloop.html#chat.toolloop', 'claudette/toolloop.py')}}}
Loading

0 comments on commit 95758d8

Please sign in to comment.