Skip to content

Instantly share code, notes, and snippets.

@giordafrancis
Created March 4, 2025 00:29
Show Gist options
  • Select an option

  • Save giordafrancis/8669e32ea73354ccc8844f31910aaaa8 to your computer and use it in GitHub Desktop.

Select an option

Save giordafrancis/8669e32ea73354ccc8844f31910aaaa8 to your computer and use it in GitHub Desktop.
My Dialog
Display the source blob
Display the rendered blob
Raw
{"cells": [{"cell_type": "code", "metadata": {}, "source": ["from fastcore.utils import *"], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Note to solveit: Hi, I am Francisco, and I am working with you today! When writing code for me, please follow fast.ai coding style: avoid comments; place single line if/loop/etc constructs on the same line; use concise variable names where appropriate. Follow the style you will see in my messages, as we work on this dialogue together. Are you ready?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Hi Francisco! I'm ready to help and will follow the fast.ai coding style you described - keeping things concise and clean. "]}, {"cell_type": "code", "metadata": {}, "source": ["import httpx\n", "from bs4 import BeautifulSoup as bs\n", "from toolslm.md_hier import *"], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["## Jeremy Rag\n", "\n", "Main idea for Jeremy Rag: Extract relevant pieces of information for your task. Curate these and pass the relevant context to your llm. \n", "Below I will try to reach for the relevant documentation for the Claudette library. Claudette is a python sdk wrapper around the Anthropic python sdk. Using toolslm library I'm able to extract the relevant markdown docs headings and ask the llm to provide areas that will satisfy my query."], "attachments": {}}, {"cell_type": "code", "metadata": {}, "source": ["url = \"https://raw.githubusercontent.com/AnswerDotAI/claudette/refs/heads/main/README.md\"\n", "page = httpx.get(url).text.replace(r\"\\*\", '*')\n", "text = bs(page, 'html.parser').get_text()\n", "text[:1000]"], "outputs": [{"data": {"text/plain": ["'# claudette\\n\\n\\n\\n\\n> **NB**: If you are reading this in GitHub\u2019s readme, we recommend you\\n> instead read the much more nicely formatted [documentation\\n> format](https://claudette.answer.ai/) of this tutorial.\\n\\n*Claudette* is a wrapper for Anthropic\u2019s [Python\\nSDK](https://github.com/anthropics/anthropic-sdk-python).\\n\\nThe SDK works well, but it is quite low level \u2013 it leaves the developer\\nto do a lot of stuff manually. That\u2019s a lot of extra work and\\nboilerplate! Claudette automates pretty much everything that can be\\nautomated, whilst providing full control. Amongst the features provided:\\n\\n- A [`Chat`](https://claudette.answer.ai/core.html#chat) class that\\n creates stateful dialogs\\n- Support for *prefill*, which tells Claude what to use as the first few\\n words of its response\\n- Convenient image support\\n- Simple and convenient support for Claude\u2019s new Tool Use API.\\n\\nYou\u2019ll need to set the `ANTHROPIC_API_KEY` environment variable to the\\nkey provided to you by Anthropic in order to use this l'"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["hdgs = create_heading_dict(text)\n", "hdict = markdown_to_dict(text)"], "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["hdgs"], "outputs": [{"data": {"text/plain": ["{'claudette': {'Install': {}, 'Getting started': {}},\n", " \"os.environ['ANTHROPIC_LOG'] = 'debug'\": {'Chat': {'Async': {}},\n", " 'Prompt caching': {},\n", " 'Tool use': {},\n", " 'Structured data': {},\n", " 'Images': {},\n", " 'Other model providers': {'Amazon Bedrock': {}, 'Google Vertex': {}},\n", " 'Extensions': {}}}"], "text/markdown": ["```json\n", "{ 'claudette': {'Getting started': {}, 'Install': {}},\n", " \"os.environ['ANTHROPIC_LOG'] = 'debug'\": { 'Chat': {'Async': {}},\n", " 'Extensions': {},\n", " 'Images': {},\n", " 'Other model providers': { 'Amazon Bedrock': { },\n", " 'Google Vertex': { }},\n", " 'Prompt caching': {},\n", " 'Structured data': {},\n", " 'Tool use': {}}}\n", "```"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "I want to create a demo of using the claudette api and. The API overview is in $`hdgs`. What might be some good examples that we could do together now? I want to include a demo with the Client class, Chat class and tools, including tooloop."]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Looking at the API overview in `hdgs`, we could create a progressive demo that showcases the key features. Let's break this down into steps:\n", "\n", "1. Should we start with a basic Client/Chat example to show the core functionality? We could create a simple interactive chat that demonstrates stateful dialogs.\n", "\n", "2. After that, we could explore the Tool use capabilities - perhaps create a practical example where Claude needs to:\n", " - Parse some data\n", " - Do some calculations\n", " - Make decisions based on the results\n", "\n", "Would you like to start with the basic Chat example first? We can then build up to the more complex tool use cases once we're comfortable with the fundamentals.\n", "\n", "What specific type of task would interest you for the demo?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "using $`hdict` extract the relevant documentation for core functions. No code for now just refer back to the headings required and provide a plan of attack. My aim is to learn how to use this library on a pratical way."]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Looking at `hdict`, let's plan our exploration of claudette through these key areas:\n", "\n", "1. Basic Setup\n", "- \"Getting started\" section for initial configuration\n", "- Setting up the environment with `ANTHROPIC_API_KEY`\n", "\n", "2. Core Client and Chat Functionality\n", "- \"Chat\" section, which appears to be the main interface\n", "- Understanding stateful dialogs\n", "- Async capabilities if needed\n", "\n", "3. Advanced Features\n", "- \"Tool use\" section for integrating computational tools\n", "- \"Structured data\" for handling data formats\n", "- \"Images\" if we want to work with visual content\n", "\n", "Would you like me to dig deeper into any of these sections from `hdict` first? We can start with whichever area interests you most for your practical learning."]}, {"cell_type": "code", "metadata": {}, "source": ["print(hdict.keys())"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["dict_keys(['claudette', 'claudette.Install', 'claudette.Getting started', 'osenvironANTHROPICLOG debug', 'osenvironANTHROPICLOG debug.Chat', 'osenvironANTHROPICLOG debug.Chat.Async', 'osenvironANTHROPICLOG debug.Prompt caching', 'osenvironANTHROPICLOG debug.Tool use', 'osenvironANTHROPICLOG debug.Structured data', 'osenvironANTHROPICLOG debug.Images', 'osenvironANTHROPICLOG debug.Other model providers', 'osenvironANTHROPICLOG debug.Other model providers.Amazon Bedrock', 'osenvironANTHROPICLOG debug.Other model providers.Google Vertex', 'osenvironANTHROPICLOG debug.Extensions'])\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["chat_docs = hdict['osenvironANTHROPICLOG debug.Chat']\n", "tool_docs = hdict['osenvironANTHROPICLOG debug.Tool use']\n", "struct_docs = hdict['osenvironANTHROPICLOG debug.Structured data']"], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Let's look at authenticaion with the anthropic api. Looking at hgds what headings might suggest an approach to this. Suggest code to extract this secction."]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Looking at `hdgs`, the \"Getting started\" section should contain the authentication information. Let's extract that section:\n", "\n", "```python\n", "getting_started = hdict['claudette.Getting started']\n", "```\n", "\n", "Would you like me to show you what this section contains about API authentication? Then we can discuss the best way to set up your environment for using the Anthropic API."]}, {"cell_type": "code", "metadata": {}, "source": ["docs_getting_started = hdict[\"claudette.Getting started\"]\n", "docs_claudette = hdict[\"claudette\"]"], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["## OAUT"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "ok please provide me an example on how to auth based on $`docs_getting_started` and $`docs_claudette`. Any keys will be set as an env variable in this session."]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Looking at `docs_claudette`, I can see that you need to set the `ANTHROPIC_API_KEY` environment variable with your Anthropic API key. Here's how you can do this in your session:\n", "\n", "```python\n", "import os\n", "os.environ['ANTHROPIC_API_KEY'] = 'your-api-key-here'\n", "```\n", "\n", "Would you like me to help you verify that the authentication is working once you've set your API key? We can try a simple test with the Chat class."]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "provide a simple example to test auth"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Based on the documentation, we can test the authentication with a simple Chat example. Here's a minimal test:\n", "\n", "```python\n", "from claudette import *\n", "\n", "# Get available models\n", "print(\"Available models:\", models)\n", "\n", "# Create a simple chat to test authentication\n", "chat = Chat(models[1]) # Using claude-3-sonnet model\n", "r = chat(\"Hi, this is a test message\")\n", "print(\"Response received:\", r)\n", "```\n", "\n", "This will:\n", "1. Import claudette\n", "2. Show available models (which requires valid auth)\n", "3. Create a basic chat instance\n", "4. Send a test message\n", "\n", "If authentication fails, you'll get an error. If it succeeds, you'll see the model's response. Would you like to try this?"]}, {"cell_type": "code", "metadata": {}, "source": ["from claudette import *\n", "\n", "# Get available models\n", "print(\"Available models:\", models)\n", "\n", "# Create a simple chat to test authentication\n", "chat = Chat(models[1]) # Using claude-3-sonnet model\n", "r = chat(\"Hi, this is a test message\")\n", "r"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Available models: ['claude-3-opus-20240229', 'claude-3-7-sonnet-20250219', 'claude-3-5-sonnet-20241022', 'claude-3-haiku-20240307', 'claude-3-5-haiku-20241022']\n"]}, {"data": {"text/plain": ["Message(id='msg_01K4njvdLwnWsB9PCpJt7njo', content=[TextBlock(citations=None, text=\"Hello! I see that you're sending a test message. I'm here and ready to assist you with any questions, tasks, or conversations you'd like to have. Is there something specific you'd like to discuss or ask about today?\", type='text')], model='claude-3-7-sonnet-20250219', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 14; Out: 51; Cache create: 0; Cache read: 0; Total: 65)"], "text/markdown": ["Hello! I see that you're sending a test message. I'm here and ready to assist you with any questions, tasks, or conversations you'd like to have. Is there something specific you'd like to discuss or ask about today?\n", "\n", "<details>\n", "\n", "- id: `msg_01K4njvdLwnWsB9PCpJt7njo`\n", "- content: `[{'citations': None, 'text': \"Hello! I see that you're sending a test message. I'm here and ready to assist you with any questions, tasks, or conversations you'd like to have. Is there something specific you'd like to discuss or ask about today?\", 'type': 'text'}]`\n", "- model: `claude-3-7-sonnet-20250219`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 14, 'output_tokens': 51}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["## Plan of Action\n", "\n", "\n", "1. Basic Chat interaction:\n", " - Found in the \"Chat\" section\n", " - Shows how to create a stateful dialog\n", " - Demonstrates basic message exchange\n", " - Includes the prefill feature for controlling responses\n", "\n", "\n", "2. Tool Use features:\n", " - Detailed in the \"Tool use\" section\n", " - Shows function definition with docments\n", " - Demonstrates automatic tool calling\n", " - Includes the toolloop feature for multiple operations\n", "\n", "3. Structured Data:\n", " - Found in \"Structured data\" section\n", " - Shows how to use Client.structured\n", " - Includes examples with custom classes"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["## Basic Chat Features\n", "\n", "1. Create a chat instance\n", "2. Send an initial message\n", "3. Ask a follow-up question that refers to the previous message\n", "4. Includes the prefill feature for controlling responses"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Now Looking at the docs_getting_started and docs_claudette, What examples are coverd by the Chat class?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Looking at the docs, the Chat class covers several key examples:\n", "\n", "1. Basic stateful dialog:\n", " - Creating a chat instance with a model\n", " - Sending messages and getting responses\n", " - Maintaining conversation context\n", "\n", "2. Prefill feature:\n", " - Controlling how Claude starts its responses\n", " - Specifying the first few words of the response\n", "\n", "3. System prompts:\n", " - Setting behavior using `sp` parameter\n", " - Controlling how the assistant responds\n", "\n", "Would you like to explore any of these specific features? We can start with whichever interests you most and look at practical examples."]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Lets focus on 1. Basic stateful dialog. Covering an example for 1. I want you like to create a simple example to demonstrate this We can:\n", "1. Create a chat instance\n", "2. Send an initial message\n", "3. Ask a follow-up question that refers to the previous message\n", "\n", "Let's start with step 1 - creating a chat instance. Provide me an example with code that satisfies this."]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "I'll help you create a chat instance. From the documentation, here's how to create a basic chat instance:\n", "\n", "```python\n", "from claudette import *\n", "\n", "# Create a chat instance with the Sonnet model\n", "chat = Chat(models[1], sp=\"You are a helpful and concise assistant.\")\n", "```\n", "\n", "The `models[1]` selects Claude-3.5-Sonnet, and I've included a simple system prompt (`sp`).\n", "\n", "Would you like me to explain any part of this code before we move on to sending the initial message?"]}, {"cell_type": "code", "metadata": {}, "source": ["from claudette import *\n", "\n", "# Create a chat instance with the Sonnet model\n", "chat = Chat(models[1], sp=\"You are a helpful and concise assistant.\")"], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "is the sp here just a tone/ context setting for the conversation?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Yes, exactly! The `sp` (system prompt) parameter sets the overall context and behavior for the conversation. It's like giving initial instructions to the assistant about how it should behave throughout the conversation.\n", "\n", "In this case, `\"You are a helpful and concise assistant.\"` tells Claude to:\n", "- Be helpful in its responses\n", "- Keep answers concise and to the point\n", "\n", "You can customize this system prompt to set different tones or behaviors. For example, you could make it more formal, technical, or specify other behavioral traits.\n", "\n", "Would you like to try a different system prompt, or shall we move on to sending our first message to test the chat?"]}, {"cell_type": "code", "metadata": {}, "source": ["# Create a chat instance with the Sonnet model\n", "chat_sarky = Chat(models[1], sp=\"You are a concise assistant, but you will reply all query with sarcasm and sassiness\")"], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "ok now i want you to provide me the code for the same prompt -> What's the cosine similarity of two vectors. I want to use this prompt on the two different chats ive created."]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "I'll help you compare responses from both chat instances using the same question:\n", "\n", "```python\n", "prompt = \"What's the cosine similarity of two vectors?\"\n", "\n", "# Test with the helpful assistant\n", "print(\"Helpful assistant response:\")\n", "r1 = chat(prompt)\n", "print(\"\\nSarcastic assistant response:\")\n", "r2 = chat_sarky(prompt)\n", "```\n", "\n", "Would you like to run this code and see how the different system prompts affect the responses? Then we can follow up with more specific questions about cosine similarity to demonstrate the stateful nature of the chat."]}, {"cell_type": "code", "metadata": {}, "source": ["prompt = \"What's the cosine similarity of two vectors?\"\n", "# Test with the helpful assistant\n", "print(\"Helpful assistant response:\")\n", "chat(prompt)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Helpful assistant response:\n"]}, {"data": {"text/plain": ["Message(id='msg_01FpxZPCUkh8VPL3rYjK4fTv', content=[TextBlock(citations=None, text='The cosine similarity of two vectors is a measure of the cosine of the angle between them. It ranges from -1 (exactly opposite) to 1 (exactly the same), with 0 indicating orthogonality (no correlation).\\n\\nThe formula is:\\ncos(\u03b8) = (A\u00b7B)/(||A||\u00b7||B||)\\n\\nWhere:\\n- A\u00b7B is the dot product of vectors A and B\\n- ||A|| and ||B|| are the magnitudes (Euclidean norms) of vectors A and B\\n\\nCosine similarity focuses on orientation rather than magnitude, making it useful for text analysis, recommendation systems, and other applications where the angle between vectors is more important than their sizes.', type='text')], model='claude-3-7-sonnet-20250219', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 26; Out: 161; Cache create: 0; Cache read: 0; Total: 187)"], "text/markdown": ["The cosine similarity of two vectors is a measure of the cosine of the angle between them. It ranges from -1 (exactly opposite) to 1 (exactly the same), with 0 indicating orthogonality (no correlation).\n", "\n", "The formula is:\n", "cos(\u03b8) = (A\u00b7B)/(||A||\u00b7||B||)\n", "\n", "Where:\n", "- A\u00b7B is the dot product of vectors A and B\n", "- ||A|| and ||B|| are the magnitudes (Euclidean norms) of vectors A and B\n", "\n", "Cosine similarity focuses on orientation rather than magnitude, making it useful for text analysis, recommendation systems, and other applications where the angle between vectors is more important than their sizes.\n", "\n", "<details>\n", "\n", "- id: `msg_01FpxZPCUkh8VPL3rYjK4fTv`\n", "- content: `[{'citations': None, 'text': 'The cosine similarity of two vectors is a measure of the cosine of the angle between them. It ranges from -1 (exactly opposite) to 1 (exactly the same), with 0 indicating orthogonality (no correlation).\\n\\nThe formula is:\\ncos(\u03b8) = (A\u00b7B)/(||A||\u00b7||B||)\\n\\nWhere:\\n- A\u00b7B is the dot product of vectors A and B\\n- ||A|| and ||B|| are the magnitudes (Euclidean norms) of vectors A and B\\n\\nCosine similarity focuses on orientation rather than magnitude, making it useful for text analysis, recommendation systems, and other applications where the angle between vectors is more important than their sizes.', 'type': 'text'}]`\n", "- model: `claude-3-7-sonnet-20250219`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 26, 'output_tokens': 161}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["print(\"\\nSarcastic assistant response:\")\n", "chat_sarky(prompt)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "Sarcastic assistant response:\n"]}, {"data": {"text/plain": ["Message(id='msg_018Wf7JVffKbyvivd6HptDBK', content=[TextBlock(citations=None, text=\"Oh, just the dot product of two vectors divided by the product of their magnitudes. You know, that super obscure formula that's definitely not in every basic linear algebra textbook ever written. It measures how similar vectors are directionally - 1 means identical direction, 0 means perpendicular, -1 means opposite. Shocking stuff, really.\", type='text')], model='claude-3-7-sonnet-20250219', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 37; Out: 79; Cache create: 0; Cache read: 0; Total: 116)"], "text/markdown": ["Oh, just the dot product of two vectors divided by the product of their magnitudes. You know, that super obscure formula that's definitely not in every basic linear algebra textbook ever written. It measures how similar vectors are directionally - 1 means identical direction, 0 means perpendicular, -1 means opposite. Shocking stuff, really.\n", "\n", "<details>\n", "\n", "- id: `msg_018Wf7JVffKbyvivd6HptDBK`\n", "- content: `[{'citations': None, 'text': \"Oh, just the dot product of two vectors divided by the product of their magnitudes. You know, that super obscure formula that's definitely not in every basic linear algebra textbook ever written. It measures how similar vectors are directionally - 1 means identical direction, 0 means perpendicular, -1 means opposite. Shocking stuff, really.\", 'type': 'text'}]`\n", "- model: `claude-3-7-sonnet-20250219`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 37, 'output_tokens': 79}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["# Follow up with helpful assistant\r\n", "print(\"Helpful assistant response:\")\r\n", "chat(\"Why do I hear so much about embeddings and cosine similarity and other distances?\")"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Helpful assistant response:\n"]}, {"data": {"text/plain": ["Message(id='msg_015gvwv6tdnqHuc956Bqr9UV', content=[TextBlock(citations=None, text=\"Embeddings and similarity measures like cosine similarity are frequently discussed because they're fundamental to modern AI and machine learning:\\n\\n1. **Embeddings** transform complex data (text, images, etc.) into numerical vectors that capture semantic meaning in a way computers can process.\\n\\n2. **Cosine similarity** is popular for comparing embeddings because:\\n - It's efficient to compute\\n - It's invariant to vector magnitude (focusing on direction/meaning)\\n - It works well in high-dimensional spaces\\n\\n3. **Other distance metrics** (Euclidean, Manhattan, etc.) serve different needs depending on the application.\\n\\nThese concepts are central to many applications you encounter daily:\\n- Search engines\\n- Recommendation systems\\n- Language models\\n- Image recognition\\n- Information retrieval\\n\\nAs AI becomes more prevalent, these foundational techniques appear more frequently in technical discussions.\", type='text')], model='claude-3-7-sonnet-20250219', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 207; Out: 190; Cache create: 0; Cache read: 0; Total: 397)"], "text/markdown": ["Embeddings and similarity measures like cosine similarity are frequently discussed because they're fundamental to modern AI and machine learning:\n", "\n", "1. **Embeddings** transform complex data (text, images, etc.) into numerical vectors that capture semantic meaning in a way computers can process.\n", "\n", "2. **Cosine similarity** is popular for comparing embeddings because:\n", " - It's efficient to compute\n", " - It's invariant to vector magnitude (focusing on direction/meaning)\n", " - It works well in high-dimensional spaces\n", "\n", "3. **Other distance metrics** (Euclidean, Manhattan, etc.) serve different needs depending on the application.\n", "\n", "These concepts are central to many applications you encounter daily:\n", "- Search engines\n", "- Recommendation systems\n", "- Language models\n", "- Image recognition\n", "- Information retrieval\n", "\n", "As AI becomes more prevalent, these foundational techniques appear more frequently in technical discussions.\n", "\n", "<details>\n", "\n", "- id: `msg_015gvwv6tdnqHuc956Bqr9UV`\n", "- content: `[{'citations': None, 'text': \"Embeddings and similarity measures like cosine similarity are frequently discussed because they're fundamental to modern AI and machine learning:\\n\\n1. **Embeddings** transform complex data (text, images, etc.) into numerical vectors that capture semantic meaning in a way computers can process.\\n\\n2. **Cosine similarity** is popular for comparing embeddings because:\\n - It's efficient to compute\\n - It's invariant to vector magnitude (focusing on direction/meaning)\\n - It works well in high-dimensional spaces\\n\\n3. **Other distance metrics** (Euclidean, Manhattan, etc.) serve different needs depending on the application.\\n\\nThese concepts are central to many applications you encounter daily:\\n- Search engines\\n- Recommendation systems\\n- Language models\\n- Image recognition\\n- Information retrieval\\n\\nAs AI becomes more prevalent, these foundational techniques appear more frequently in technical discussions.\", 'type': 'text'}]`\n", "- model: `claude-3-7-sonnet-20250219`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 207, 'output_tokens': 190}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["# Follow up with sarcastic assistant\r\n", "print(\"\\nSarcastic assistant response:\")\r\n", "chat_sarky(\"Why do I hear so much about embeddings and cosine similarity and other distances?\")"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "Sarcastic assistant response:\n"]}, {"data": {"text/plain": ["Message(id='msg_013Fmg3zaky6xZNZQed4Nm9X', content=[TextBlock(citations=None, text='*eye roll* \\n\\nGee, I wonder why mathematical concepts that measure how similar things are might be useful in a world obsessed with finding patterns and relationships in data? \\n\\nEmbeddings just transform complex things like words or images into vectors that computers can actually work with. And cosine similarity? Only the backbone of recommendation systems, search engines, and pretty much any AI that needs to know if things are \"similar.\"\\n\\nBut sure, let\\'s act surprised that measuring distances between mathematical representations of things is somehow relevant in modern computing. Totally bizarre concept.', type='text')], model='claude-3-7-sonnet-20250219', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 136; Out: 120; Cache create: 0; Cache read: 0; Total: 256)"], "text/markdown": ["*eye roll* \n", "\n", "Gee, I wonder why mathematical concepts that measure how similar things are might be useful in a world obsessed with finding patterns and relationships in data? \n", "\n", "Embeddings just transform complex things like words or images into vectors that computers can actually work with. And cosine similarity? Only the backbone of recommendation systems, search engines, and pretty much any AI that needs to know if things are \"similar.\"\n", "\n", "But sure, let's act surprised that measuring distances between mathematical representations of things is somehow relevant in modern computing. Totally bizarre concept.\n", "\n", "<details>\n", "\n", "- id: `msg_013Fmg3zaky6xZNZQed4Nm9X`\n", "- content: `[{'citations': None, 'text': '*eye roll* \\n\\nGee, I wonder why mathematical concepts that measure how similar things are might be useful in a world obsessed with finding patterns and relationships in data? \\n\\nEmbeddings just transform complex things like words or images into vectors that computers can actually work with. And cosine similarity? Only the backbone of recommendation systems, search engines, and pretty much any AI that needs to know if things are \"similar.\"\\n\\nBut sure, let\\'s act surprised that measuring distances between mathematical representations of things is somehow relevant in modern computing. Totally bizarre concept.', 'type': 'text'}]`\n", "- model: `claude-3-7-sonnet-20250219`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 136, 'output_tokens': 120}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Lets use prefil now to test some output constraints. Provide one example where i want the output of the reply of what are the different metrics but now in Portuguese. Also can I reuse my different chats before to set a prefil or a new chat object needs to be created? \n", "\n", "If so provide an example for both chats with the prefil. Again do i need new vars for these?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "You can use prefill with your existing chat instances - no need to create new ones. Here's how to use prefill with both chats:\n", "\n", "```python\n", "prompt = \"What are the different distance metrics used in machine learning?\"\n", "prefill = \"Em portugu\u00eas, as m\u00e9tricas principais s\u00e3o:\" # In Portuguese, the main metrics are:\n", "\n", "# Using existing helpful chat\n", "print(\"Helpful assistant (Portuguese):\")\n", "r1 = chat(prompt, prefill=prefill)\n", "\n", "# Using existing sarcastic chat\n", "print(\"\\nSarcastic assistant (Portuguese):\")\n", "r2 = chat_sarky(prompt, prefill=prefill)\n", "```\n", "\n", "The prefill parameter tells Claude to start its response with those exact words. Would you like to try this and see how both assistants handle the Portuguese prefill while maintaining their different personalities?"]}, {"cell_type": "code", "metadata": {}, "source": ["prompt = \"What are the different distance metrics used in machine learning?\"\r\n", "prefill = \"Em portugu\u00eas, as m\u00e9tricas principais s\u00e3o:\" # In Portuguese, the main metrics are:\r\n", "\r\n", "# Using existing helpful chat\r\n", "print(\"Helpful assistant (with Portuguese prefill):\")\r\n", "chat(prompt, prefill=prefill)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Helpful assistant (with Portuguese prefill):\n"]}, {"data": {"text/plain": ["Message(id='msg_01LdJRQhKYbnLEXKEpVK4Mj2', content=[TextBlock(citations=None, text='Em portugu\u00eas, as m\u00e9tricas principais s\u00e3o: \\n\\n# Common Distance Metrics in Machine Learning\\n\\n1. **Euclidean Distance**\\n - The straight-line distance between points\\n - Sensitive to scale differences\\n - Formula: \u221a(\u03a3(xi - yi)\u00b2)\\n\\n2. **Manhattan Distance (L1 Norm)**\\n - Sum of absolute differences along dimensions\\n - Less sensitive to outliers than Euclidean\\n - Formula: \u03a3|xi - yi|\\n\\n3. **Cosine Similarity/Distance**\\n - Measures angle between vectors, ignoring magnitude\\n - Effective for high-dimensional data like text\\n - Formula: 1 - (A\u00b7B)/(||A||\u00b7||B||)\\n\\n4. **Mahalanobis Distance**\\n - Accounts for correlations between variables\\n - Scale-invariant\\n - Useful for clustered data\\n\\n5. **Hamming Distance**\\n - Counts positions where symbols differ\\n - Used for categorical data and error detection\\n\\n6. **Jaccard Distance**\\n - Measures dissimilarity between sets\\n - Formula: 1 - (A\u2229B)/(A\u222aB)\\n\\n7. **Minkowski Distance**\\n - Generalization of Euclidean and Manhattan\\n - Formula: (\u03a3|xi - yi|\u1d56)^(1/p)\\n\\nEach metric has specific use cases depending on data type, dimensionality, and the problem being solved.', type='text')], model='claude-3-7-sonnet-20250219', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 421; Out: 341; Cache create: 0; Cache read: 0; Total: 762)"], "text/markdown": ["Em portugu\u00eas, as m\u00e9tricas principais s\u00e3o: \n", "\n", "# Common Distance Metrics in Machine Learning\n", "\n", "1. **Euclidean Distance**\n", " - The straight-line distance between points\n", " - Sensitive to scale differences\n", " - Formula: \u221a(\u03a3(xi - yi)\u00b2)\n", "\n", "2. **Manhattan Distance (L1 Norm)**\n", " - Sum of absolute differences along dimensions\n", " - Less sensitive to outliers than Euclidean\n", " - Formula: \u03a3|xi - yi|\n", "\n", "3. **Cosine Similarity/Distance**\n", " - Measures angle between vectors, ignoring magnitude\n", " - Effective for high-dimensional data like text\n", " - Formula: 1 - (A\u00b7B)/(||A||\u00b7||B||)\n", "\n", "4. **Mahalanobis Distance**\n", " - Accounts for correlations between variables\n", " - Scale-invariant\n", " - Useful for clustered data\n", "\n", "5. **Hamming Distance**\n", " - Counts positions where symbols differ\n", " - Used for categorical data and error detection\n", "\n", "6. **Jaccard Distance**\n", " - Measures dissimilarity between sets\n", " - Formula: 1 - (A\u2229B)/(A\u222aB)\n", "\n", "7. **Minkowski Distance**\n", " - Generalization of Euclidean and Manhattan\n", " - Formula: (\u03a3|xi - yi|\u1d56)^(1/p)\n", "\n", "Each metric has specific use cases depending on data type, dimensionality, and the problem being solved.\n", "\n", "<details>\n", "\n", "- id: `msg_01LdJRQhKYbnLEXKEpVK4Mj2`\n", "- content: `[{'citations': None, 'text': 'Em portugu\u00eas, as m\u00e9tricas principais s\u00e3o: \\n\\n# Common Distance Metrics in Machine Learning\\n\\n1. **Euclidean Distance**\\n - The straight-line distance between points\\n - Sensitive to scale differences\\n - Formula: \u221a(\u03a3(xi - yi)\u00b2)\\n\\n2. **Manhattan Distance (L1 Norm)**\\n - Sum of absolute differences along dimensions\\n - Less sensitive to outliers than Euclidean\\n - Formula: \u03a3|xi - yi|\\n\\n3. **Cosine Similarity/Distance**\\n - Measures angle between vectors, ignoring magnitude\\n - Effective for high-dimensional data like text\\n - Formula: 1 - (A\u00b7B)/(||A||\u00b7||B||)\\n\\n4. **Mahalanobis Distance**\\n - Accounts for correlations between variables\\n - Scale-invariant\\n - Useful for clustered data\\n\\n5. **Hamming Distance**\\n - Counts positions where symbols differ\\n - Used for categorical data and error detection\\n\\n6. **Jaccard Distance**\\n - Measures dissimilarity between sets\\n - Formula: 1 - (A\u2229B)/(A\u222aB)\\n\\n7. **Minkowski Distance**\\n - Generalization of Euclidean and Manhattan\\n - Formula: (\u03a3|xi - yi|\u1d56)^(1/p)\\n\\nEach metric has specific use cases depending on data type, dimensionality, and the problem being solved.', 'type': 'text'}]`\n", "- model: `claude-3-7-sonnet-20250219`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 421, 'output_tokens': 341}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["# Using existing sarcastic chat\n", "print(\"\\nSarcastic assistant (with Portuguese prefill):\")\n", "chat_sarky(prompt, prefill=prefill)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\n", "Sarcastic assistant (with Portuguese prefill):\n"]}, {"data": {"text/plain": ["Message(id='msg_01Lk4QbRL4tXVEADWyN6CXgn', content=[TextBlock(citations=None, text='Em portugu\u00eas, as m\u00e9tricas principais s\u00e3o: dist\u00e2ncia euclidiana, similaridade de cosseno, dist\u00e2ncia de Manhattan, dist\u00e2ncia de Mahalanobis, dist\u00e2ncia de Hamming, e diverg\u00eancia KL.', type='text')], model='claude-3-7-sonnet-20250219', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 280; Out: 44; Cache create: 0; Cache read: 0; Total: 324)"], "text/markdown": ["Em portugu\u00eas, as m\u00e9tricas principais s\u00e3o: dist\u00e2ncia euclidiana, similaridade de cosseno, dist\u00e2ncia de Manhattan, dist\u00e2ncia de Mahalanobis, dist\u00e2ncia de Hamming, e diverg\u00eancia KL.\n", "\n", "<details>\n", "\n", "- id: `msg_01Lk4QbRL4tXVEADWyN6CXgn`\n", "- content: `[{'citations': None, 'text': 'Em portugu\u00eas, as m\u00e9tricas principais s\u00e3o: dist\u00e2ncia euclidiana, similaridade de cosseno, dist\u00e2ncia de Manhattan, dist\u00e2ncia de Mahalanobis, dist\u00e2ncia de Hamming, e diverg\u00eancia KL.', 'type': 'text'}]`\n", "- model: `claude-3-7-sonnet-20250219`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 280, 'output_tokens': 44}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["chat_sarky.sp #insteresting sarcasm is lost in Portuguese..."], "outputs": [{"data": {"text/plain": ["'You are a concise assistant, but you will reply all query with sarcasm and sassiness'"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["## solveit RAG (aka Jono RAG)"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["Can you provide me with a quick start guide for the claudette lirbrary?"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Using Claudete.core please provide an example of a role user and assitant iteraction of 3 messages each? Using the client object and a list of strings wach representing user assistant pairs"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Here's an example of creating a dialog with 3 pairs of user-assistant interactions using the Client class:\n", "\n", "```python\n", "from claudette import *\n", "\n", "# Create client instance\n", "cli = Client(models[1]) # Using Claude-3.5-Sonnet\n", "\n", "# Create list of message pairs\n", "messages = [\n", " \"Hi, I'm learning about machine learning\", # user\n", " \"Hello! That's great. What specific area interests you?\", # assistant\n", " \"I'm interested in neural networks\", # user\n", " \"Neural networks are fascinating! Any particular aspect you'd like to explore?\", # assistant\n", " \"Can you explain backpropagation?\", # user\n", " \"Backpropagation is the key algorithm for training neural networks. Would you like me to explain how it works?\" # assistant\n", "]\n", "\n", "# Create the messages using mk_msgs helper\n", "dialog = mk_msgs(messages)\n", "\n", "# Would you like me to show you how to use this dialog with the client?\n", "```\n", "\n", "Would you like to see how to use this dialog with the client, or would you like me to explain any part of the code first?"]}, {"cell_type": "code", "metadata": {}, "source": ["# Create client instance\n", "\n", "# Create client instance\n", "c = Client(models[-1]) \n", "\n", "# Create list of message pairs\n", "messages = [\n", " \"Hi, I'm learning about machine learning\", # user\n", " \"Hello! That's great. What specific area interests you?\", # assistant\n", " \"I'm interested in neural networks\", # user\n", " \"Neural networks are fascinating! Any particular aspect you'd like to explore?\", # assistant\n", " \"Can you explain backpropagation?\", # user\n", " \"Backpropagation is the key algorithm for training neural networks. Would you like me to explain how it works?\" # assistant\n", "]\n", "\n", "# Create the messages using mk_msgs helper\n", "dialog = mk_msgs(messages)\n", "\n", "dialog"], "outputs": [{"data": {"text/plain": ["[{'role': 'user', 'content': \"Hi, I'm learning about machine learning\"},\n", " {'role': 'assistant',\n", " 'content': \"Hello! That's great. What specific area interests you?\"},\n", " {'role': 'user', 'content': \"I'm interested in neural networks\"},\n", " {'role': 'assistant',\n", " 'content': \"Neural networks are fascinating! Any particular aspect you'd like to explore?\"},\n", " {'role': 'user', 'content': 'Can you explain backpropagation?'},\n", " {'role': 'assistant',\n", " 'content': 'Backpropagation is the key algorithm for training neural networks. Would you like me to explain how it works?'}]"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["mk_msgs??"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0mmk_msgs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcache_last_ckpt_only\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mapi\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'openai'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mSource:\u001b[0m \n", "\u001b[0;34m@\u001b[0m\u001b[0mdelegates\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmk_msgs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;32mdef\u001b[0m \u001b[0mmk_msgs_anthropic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcache_last_ckpt_only\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m\"Create a list of Anthropic compatible messages.\"\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmsgs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpartial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmk_msgs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mapi\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"anthropic\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcache_last_ckpt_only\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mmsgs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0m_remove_cache_ckpts\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mm\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmsgs\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmsgs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_add_cache_control\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsgs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcache\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmsgs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mFile:\u001b[0m /usr/local/lib/python3.11/site-packages/msglm/core.py\n", "\u001b[0;31mType:\u001b[0m function\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c.cache"], "outputs": [{"data": {"text/plain": ["False"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c(msgs=dialog, prefill=\"I'm from Tallin which is the\")"], "outputs": [{"data": {"text/plain": ["Message(id='msg_01KGadYfe7TwUPbPXo92GiXt', content=[TextBlock(citations=None, text=\"I'm from Tallin which is theTallinn is the capital and largest city of Estonia, located on the northern coast of the country along the Baltic Sea. It's known for its well-preserved medieval Old Town, which is a UNESCO World Heritage site, and its modern, tech-savvy environment. Estonia is often referred to as a digital society and is known for innovations in e-government and technology. Would you like to know more about Tallinn or Estonia?\", type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 108; Out: 92; Cache create: 0; Cache read: 0; Total: 200)"], "text/markdown": ["I'm from Tallin which is theTallinn is the capital and largest city of Estonia, located on the northern coast of the country along the Baltic Sea. It's known for its well-preserved medieval Old Town, which is a UNESCO World Heritage site, and its modern, tech-savvy environment. Estonia is often referred to as a digital society and is known for innovations in e-government and technology. Would you like to know more about Tallinn or Estonia?\n", "\n", "<details>\n", "\n", "- id: `msg_01KGadYfe7TwUPbPXo92GiXt`\n", "- content: `[{'citations': None, 'text': \"I'm from Tallin which is theTallinn is the capital and largest city of Estonia, located on the northern coast of the country along the Baltic Sea. It's known for its well-preserved medieval Old Town, which is a UNESCO World Heritage site, and its modern, tech-savvy environment. Estonia is often referred to as a digital society and is known for innovations in e-government and technology. Would you like to know more about Tallinn or Estonia?\", 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 108, 'output_tokens': 92}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c(msgs=dialog)"], "outputs": [{"data": {"text/plain": ["Message(id='msg_01P7QwHKMtjFwBhhoj1LRJ3k', content=[TextBlock(citations=None, text=\"\\n\\nHere's a basic overview:\\n1. During forward propagation, the network makes a prediction\\n2. It calculates the error between the prediction and the actual target\\n3. Backpropagation calculates how much each weight contributed to the error\\n4. The weights are then adjusted to minimize the error in future predictions\\n\\nThe core idea is to use the chain rule of calculus to efficiently compute gradients and update the network's weights.\\n\\nWould you like me to go into more technical detail about how the gradient calculations work?\", type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 93; Out: 115; Cache create: 0; Cache read: 0; Total: 208)"], "text/markdown": ["Here's a basic overview:\n", "1. During forward propagation, the network makes a prediction\n", "2. It calculates the error between the prediction and the actual target\n", "3. Backpropagation calculates how much each weight contributed to the error\n", "4. The weights are then adjusted to minimize the error in future predictions\n", "\n", "The core idea is to use the chain rule of calculus to efficiently compute gradients and update the network's weights.\n", "\n", "Would you like me to go into more technical detail about how the gradient calculations work?\n", "\n", "<details>\n", "\n", "- id: `msg_01P7QwHKMtjFwBhhoj1LRJ3k`\n", "- content: `[{'citations': None, 'text': \"\\n\\nHere's a basic overview:\\n1. During forward propagation, the network makes a prediction\\n2. It calculates the error between the prediction and the actual target\\n3. Backpropagation calculates how much each weight contributed to the error\\n4. The weights are then adjusted to minimize the error in future predictions\\n\\nThe core idea is to use the chain rule of calculus to efficiently compute gradients and update the network's weights.\\n\\nWould you like me to go into more technical detail about how the gradient calculations work?\", 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 93, 'output_tokens': 115}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c(msgs=mk_msgs(messages+['Please make an haiku on how backpropogation works']))"], "outputs": [{"data": {"text/plain": ["Message(id='msg_01Jh3VzJu2f58mUCoxj7rJBu', content=[TextBlock(citations=None, text=\"Here's a haiku about backpropagation:\\n\\nErrors cascade back\\nWeights adjust, learning unfolds\\nNetwork grows wiser\\n\\nThe haiku captures the essence of backpropagation: how the error is propagated backwards through the network, allowing the weights to be adjusted to minimize the error, ultimately improving the network's performance.\", type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 112; Out: 76; Cache create: 0; Cache read: 0; Total: 188)"], "text/markdown": ["Here's a haiku about backpropagation:\n", "\n", "Errors cascade back\n", "Weights adjust, learning unfolds\n", "Network grows wiser\n", "\n", "The haiku captures the essence of backpropagation: how the error is propagated backwards through the network, allowing the weights to be adjusted to minimize the error, ultimately improving the network's performance.\n", "\n", "<details>\n", "\n", "- id: `msg_01Jh3VzJu2f58mUCoxj7rJBu`\n", "- content: `[{'citations': None, 'text': \"Here's a haiku about backpropagation:\\n\\nErrors cascade back\\nWeights adjust, learning unfolds\\nNetwork grows wiser\\n\\nThe haiku captures the essence of backpropagation: how the error is propagated backwards through the network, allowing the weights to be adjusted to minimize the error, ultimately improving the network's performance.\", 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 112, 'output_tokens': 76}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c.model = models[0]\n", "c(msgs=messages + ['Please make an haiku on how backpropogation works'], prefill=\"I'm from Tallin, none of that matters now and so here is your haiku:\\n\")"], "outputs": [{"data": {"text/plain": ["Message(id='msg_01ST8UANNoaNmBNMcQFrBVyo', content=[TextBlock(citations=None, text=\"I'm from Tallin, none of that matters now and so here is your haiku:\\n \\n\\nForward pass computes\\nBackward pass adjusts weights\\nGradient descent\\n\\nThe haiku captures the essence of backpropagation:\\n\\n1. Forward pass: The input data is fed through the network, and the output is computed.\\n\\n2. Backward pass: The error between the predicted output and the actual output is calculated, and this error is propagated back through the network.\\n\\n3. Gradient descent: The weights of the connections between neurons are adjusted based on the error to minimize the loss function.\\n\\nThis process is repeated iteratively until the network converges to a state where it can make accurate predictions.\", type='text')], model='claude-3-opus-20240229', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 132; Out: 134; Cache create: 0; Cache read: 0; Total: 266)"], "text/markdown": ["I'm from Tallin, none of that matters now and so here is your haiku:\n", " \n", "\n", "Forward pass computes\n", "Backward pass adjusts weights\n", "Gradient descent\n", "\n", "The haiku captures the essence of backpropagation:\n", "\n", "1. Forward pass: The input data is fed through the network, and the output is computed.\n", "\n", "2. Backward pass: The error between the predicted output and the actual output is calculated, and this error is propagated back through the network.\n", "\n", "3. Gradient descent: The weights of the connections between neurons are adjusted based on the error to minimize the loss function.\n", "\n", "This process is repeated iteratively until the network converges to a state where it can make accurate predictions.\n", "\n", "<details>\n", "\n", "- id: `msg_01ST8UANNoaNmBNMcQFrBVyo`\n", "- content: `[{'citations': None, 'text': \"I'm from Tallin, none of that matters now and so here is your haiku:\\n \\n\\nForward pass computes\\nBackward pass adjusts weights\\nGradient descent\\n\\nThe haiku captures the essence of backpropagation:\\n\\n1. Forward pass: The input data is fed through the network, and the output is computed.\\n\\n2. Backward pass: The error between the predicted output and the actual output is calculated, and this error is propagated back through the network.\\n\\n3. Gradient descent: The weights of the connections between neurons are adjusted based on the error to minimize the loss function.\\n\\nThis process is repeated iteratively until the network converges to a state where it can make accurate predictions.\", 'type': 'text'}]`\n", "- model: `claude-3-opus-20240229`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 132, 'output_tokens': 134}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["under the hood _precall is used to make the message user/assistant schema and add prefill if there"], "attachments": {}}, {"cell_type": "code", "metadata": {}, "source": ["c._precall??"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31mSignature:\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_precall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsgs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprefill\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m <no docstring>\n", "\u001b[0;31mSource:\u001b[0m \n", "\u001b[0;34m@\u001b[0m\u001b[0mpatch\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;32mdef\u001b[0m \u001b[0m_precall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mClient\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsgs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprefill\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mpref\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mprefill\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mprefill\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsgs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mmsgs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mmsgs\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mstop\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstop\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mstop\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"stop_sequences\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstop\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmsgs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmk_msgs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsgs\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mpref\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcache\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcache_last_ckpt_only\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcache\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmsgs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mFile:\u001b[0m /usr/local/lib/python3.11/site-packages/claudette/core.py\n", "\u001b[0;31mType:\u001b[0m method\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["mk_msgs??"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0mmk_msgs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcache_last_ckpt_only\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mapi\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'openai'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mSource:\u001b[0m \n", "\u001b[0;34m@\u001b[0m\u001b[0mdelegates\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmk_msgs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;32mdef\u001b[0m \u001b[0mmk_msgs_anthropic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcache_last_ckpt_only\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m\"Create a list of Anthropic compatible messages.\"\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmsgs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpartial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmk_msgs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mapi\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"anthropic\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcache_last_ckpt_only\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mmsgs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0m_remove_cache_ckpts\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mm\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmsgs\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmsgs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_add_cache_control\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsgs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcache\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmsgs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mFile:\u001b[0m /usr/local/lib/python3.11/site-packages/msglm/core.py\n", "\u001b[0;31mType:\u001b[0m function\n"]}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["mk_msgs makes to compatible Anthropic messages schema"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["## Structured Data:\n", "\n", " - Found in \"Structured data\" section\n", " - Shows how to use Client.structured\n", " - Includes examples with custom classes"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Can you provide me a way to use an struture data output based on Claudette quick start?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Based on the Claudette quick start guide, here's how to use structured data output:\n", "\n", "```python\n", "from claudette import *\n", "\n", "# Create a client\n", "cli = Client(models[1]) # Using Claude-3.5-Sonnet\n", "\n", "# Define a class for the structured output\n", "class President:\n", " \"\"\"Information about a president of the United States\"\"\"\n", " def __init__(self, \n", " first: str, # first name\n", " last: str, # last name\n", " years_in_office: str, # format: \"{start_year}-{end_year}\"\n", " birthplace: str, # name of city\n", " birth_year: int # year of birth\n", " ):\n", " self.first = first\n", " self.last = last\n", " self.years_in_office = years_in_office\n", " self.birthplace = birthplace\n", " self.birth_year = birth_year\n", " \n", " def __repr__(self):\n", " return f\"President(first='{self.first}', last='{self.last}', years_in_office='{self.years_in_office}', birthplace='{self.birthplace}', birth_year={self.birth_year})\"\n", "```\n", "\n", "Would you like me to show you how to use this class with the `structured` method to get information about a president?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Use this class in an example"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Here's how to use the President class with the structured method:\n", "\n", "```python\n", "# Using the President class with structured output\n", "result = cli.structured(\"Provide key information about the 16th President of the United States\", President)\n", "print(result)\n", "```\n", "\n", "This will:\n", "1. Send a prompt asking for information about the 16th President (Abraham Lincoln)\n", "2. Claude will analyze the request and fill in the structured data\n", "3. Return a President object with the requested information\n", "\n", "Would you like to try running this example?"]}, {"cell_type": "code", "metadata": {}, "source": ["# Create a client\n", "cli = Client(models[-1]) # Using Claude-3.5-Sonnet\n", "\n", "# Define a class for the structured output\n", "class President:\n", " \"\"\"Information about a president of the United States\"\"\"\n", " def __init__(self, \n", " first: str, # first name\n", " last: str, # last name\n", " years_in_office: str, # format: \"{start_year}-{end_year}\"\n", " birthplace: str, # name of city\n", " birth_year: int, # year of birth\n", " president_num: int # presidency number, Geroge wash. being number 1\n", " ):\n", " self.first = first\n", " self.last = last\n", " self.years_in_office = years_in_office\n", " self.birthplace = birthplace\n", " self.birth_year = birth_year\n", " self.president_num = president_num\n", " \n", " def __repr__(self):\n", " return f\"\"\"President(first='{self.first}', last='{self.last}', years_in_office='{self.years_in_office}', \n", " birthplace='{self.birthplace}', birth_year={self.birth_year}) \n", " {self.president_num}th president of the U.S.\"\"\"\n", "\n", " # Using the President class with structured output\n", "result = cli.structured(\"Provide key information about John Adams Presidency\", President)\n", "\n", "result"], "outputs": [{"data": {"text/plain": ["[President(first='John', last='Adams', years_in_office='1797-1801', \n", " birthplace='Braintree', birth_year=1735) \n", " 2th president of the U.S.]"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["## Tool use\n", "\n", "Tool Use features:\n", " - Detailed in the \"Tool use\" section\n", " - Shows function definition with docments\n", " - Demonstrates automatic tool calling\n", " - Includes the toolloop feature for multiple operations"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["Score: 1.00 -- <a href=\"https://raw.githubusercontent.com/AnswerDotAI/claudette/refs/heads/main/README.md\" target=\"_blank\">Quick start guide and overview</a>\n", "\n", "---\n", "\n", "# claudette\n", "\n", "## Tool use\n", "\n", "[Tool use](https://docs.anthropic.com/claude/docs/tool-use) lets Claude\n", "use external tools.\n", "\n", "We use [docments](https://fastcore.fast.ai/docments.html) to make\n", "defining Python functions as ergonomic as possible. Each parameter (and\n", "the return value) should have a type, and a docments comment with the\n", "description of what it is. As an example we\u2019ll write a simple function\n", "that adds numbers together, and will tell us when it\u2019s being called:\n", "\n", "``` python\n", "def sums(\n", " a:int, # First thing to sum\n", " b:int=1 # Second thing to sum\n", ") -> int: # The sum of the inputs\n", " \"Adds a + b.\"\n", " print(f\"Finding the sum of {a} and {b}\")\n", " return a + b\n", "```\n", "\n", "Sometimes Claude will say something like \u201caccording to the `sums` tool\n", "the answer is\u201d \u2013 generally we\u2019d rather it just tells the user the\n", "answer, so we can use a system prompt to help with this:\n", "\n", "``` python\n", "sp = \"Never mention what tools you use.\"\n", "```\n", "\n", "We\u2019ll get Claude to add up some long numbers:\n", "\n", "``` python\n", "a,b = 604542,6458932\n", "pr = f\"What is {a}+{b}?\"\n", "pr\n", "```\n", "\n", " 'What is 604542+6458932?'\n", "\n", "To use tools, pass a list of them to\n", "[`Chat`](https://claudette.answer.ai/core.html#chat):\n", "\n", "``` python\n", "chat = Chat(model, sp=sp, tools=[sums])\n", "```\n", "\n", "To force Claude to always answer using a tool, set `tool_choice` to that\n", "function name. When Claude needs to use a tool, it doesn\u2019t return the\n", "answer, but instead returns a `tool_use` message, which means we have to\n", "call the named tool with the provided parameters.\n", "\n", "``` python\n", "r = chat(pr, tool_choice='sums')\n", "r\n", "```\n", "\n", " Finding the sum of 604542 and 6458932\n", "\n", "ToolUseBlock(id=\u2018toolu_014ip2xWyEq8RnAccVT4SySt\u2019, input={\u2018a\u2019: 604542,\n", "\u2018b\u2019: 6458932}, name=\u2018sums\u2019, type=\u2018tool_use\u2019)\n", "\n", "<details>\n", "\n", "- id: `msg_014xrPyotyiBmFSctkp1LZHk`\n", "- content:\n", " `[{'id': 'toolu_014ip2xWyEq8RnAccVT4SySt', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]`\n", "- model: `claude-3-5-sonnet-20241022`\n", "- role: `assistant`\n", "- stop_reason: `tool_use`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage:\n", " `{'input_tokens': 442, 'output_tokens': 53, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", "\n", "</details>\n", "\n", "Claudette handles all that for us \u2013 we just call it again, and it all\n", "happens automatically:\n", "\n", "``` python\n", "chat()\n", "```\n", "\n", "The sum of 604542 and 6458932 is 7063474.\n", "\n", "<details>\n", "\n", "- id: `msg_01151puJxG8Fa6k6QSmzwKQA`\n", "- content:\n", " `[{'text': 'The sum of 604542 and 6458932 is 7063474.', 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage:\n", " `{'input_tokens': 524, 'output_tokens': 23, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", "\n", "</details>\n", "\n", "You can see how many tokens have been used at any time by checking the\n", "`use` property. Note that (as of May 2024) tool use in Claude uses a\n", "*lot* of tokens, since it automatically adds a large system prompt.\n", "\n", "``` python\n", "chat.use\n", "```\n", "\n", " In: 966; Out: 76; Cache create: 0; Cache read: 0; Total: 1042\n", "\n", "We can do everything needed to use tools in a single step, by using\n", "[`Chat.toolloop`](https://claudette.answer.ai/toolloop.html#chat.toolloop).\n", "This can even call multiple tools as needed solve a problem. For\n", "example, let\u2019s define a tool to handle multiplication:\n", "\n", "``` python\n", "def mults(\n", " a:int, # First thing to multiply\n", " b:int=1 # Second thing to multiply\n", ") -> int: # The product of the inputs\n", " \"Multiplies a * b.\"\n", " print(f\"Finding the product of {a} and {b}\")\n", " return a * b\n", "```\n", "\n", "Now with a single call we can calculate `(a+b)*2` \u2013 by passing\n", "`show_trace` we can see each response from Claude in the process:\n", "\n", "``` python\n", "chat = Chat(model, sp=sp, tools=[sums,mults])\n", "pr = f'Calculate ({a}+{b})*2'\n", "pr\n", "```\n", "\n", " 'Calculate (604542+6458932)*2'\n", "\n", "``` python\n", "chat.toolloop(pr, trace_func=print)\n", "```\n", "\n", " Finding the sum of 604542 and 6458932\n", " [{'role': 'user', 'content': [{'type': 'text', 'text': 'Calculate (604542+6458932)*2'}]}, {'role': 'assistant', 'content': [TextBlock(text=\"I'll help you break this down into steps:\\n\\nFirst, let's add those numbers:\", type='text'), ToolUseBlock(id='toolu_01St5UKxYUU4DKC96p2PjgcD', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01St5UKxYUU4DKC96p2PjgcD', 'content': '7063474'}]}]\n", " Finding the product of 7063474 and 2\n", " [{'role': 'assistant', 'content': [TextBlock(text=\"Now, let's multiply this result by 2:\", type='text'), ToolUseBlock(id='toolu_01FpmRG4ZskKEWN1gFZzx49s', input={'a': 7063474, 'b': 2}, name='mults', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01FpmRG4ZskKEWN1gFZzx49s', 'content': '14126948'}]}]\n", " [{'role': 'assistant', 'content': [TextBlock(text='The final result is 14,126,948.', type='text')]}]\n", "\n", "The final result is 14,126,948.\n", "\n", "<details>\n", "\n", "- id: `msg_0162teyBcJHriUzZXMPz4r5d`\n", "- content:\n", " `[{'text': 'The final result is 14,126,948.', 'type': 'text'}]`\n", "- model: `claude-3-5-sonnet-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage:\n", " `{'input_tokens': 741, 'output_tokens': 15, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0}`\n", "\n", "</details>\n", "\n", "\n", "----\n", "\n", "Library: Claudette - Docs (Claudette is a Python library that wraps Anthropic's Claude API to provide a higher-level interface for creating AI applications. It automates common patterns while maintaining full control, offering features like stateful chat, prefill support, image handling, and streamlined tool use.); Document: README (Quick start guide and overview)"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Can you provide me the code to replicate the tool use case presented and sums and mults tools passed to the client?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Here's the code to replicate the tool use case with both the sums and mults tools:\n", "\n", "```python\n", "from claudette import *\n", "\n", "# Create the tools with docments style\n", "def sums(\n", " a: int, # First thing to sum\n", " b: int = 1 # Second thing to sum\n", ") -> int: # The sum of the inputs\n", " \"Adds a + b.\"\n", " print(f\"Finding the sum of {a} and {b}\")\n", " return a + b\n", "\n", "def mults(\n", " a: int, # First thing to multiply\n", " b: int = 1 # Second thing to multiply\n", ") -> int: # The product of the inputs\n", " \"Multiplies a * b.\"\n", " print(f\"Finding the product of {a} and {b}\")\n", " return a * b\n", "\n", "# Set system prompt to avoid mentioning tools\n", "sp = \"Never mention what tools you use.\"\n", "\n", "# Create a chat instance with both tools\n", "chat = Chat(models[1], sp=sp, tools=[sums, mults])\n", "\n", "# Example prompt\n", "a, b = 604542, 6458932\n", "prompt = f\"Calculate ({a}+{b})*2\"\n", "```\n", "\n", "Would you like me to show how to use this with toolloop to get the result?"]}, {"cell_type": "code", "metadata": {}, "source": ["# Define the tools\n", "def sums(\n", " a:int, # First thing to sum\n", " b:int=1 # Second thing to sum\n", ") -> int: # The sum of the inputs\n", " \"Adds a + b.\"\n", " print(f\"Finding the sum of {a} and {b}\")\n", " return a + b\n", "\n", "def mults(\n", " a:int, # First thing to multiply\n", " b:int=1 # Second thing to multiply\n", ") -> int: # The product of the inputs\n", " \"Multiplies a * b.\"\n", " print(f\"Finding the product of {a} and {b}\")\n", " return a * b\n", "\n", "# Create chat with tools\n", "sp = \"You must use the `sums` and `mults` functions where appropriate, but never mention what tools you use.\"\n", "chat = Chat(models[1], sp=sp, tools=[sums, mults])"], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["Lets explore the below:\n", "\n", "1. Single tool calls\n", "2. Multiple tool calls with toolloop\n", "3. Force specific tool usage with tool_choice"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Provide an example of how a single tools call would look like?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Here's an example of a single tool call using the `sums` function:\n", "\n", "```python\n", "# Simple addition query\n", "prompt = \"What is 123 + 456?\"\n", "\n", "# Send the query to chat\n", "r = chat(prompt)\n", "\n", "# The response will automatically use the sums tool and show the result\n", "```\n", "\n", "Would you like to try this example? We can also try forcing it to use a specific tool using `tool_choice` if you'd prefer to see that instead.\n", "\n", "Note: The chat will automatically determine when to use the appropriate tool based on the query, and Claudette handles all the tool calling mechanics behind the scenes."]}, {"cell_type": "code", "metadata": {}, "source": ["# Single tool call example\n", "a,b = 42,58\n", "prompt = f\"What is {a} + {b}?\"\n", "r = chat(prompt)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Finding the sum of 42 and 58\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["# force it\n", "\n", "r = chat(prompt, tool_choice='sums')"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Finding the sum of 42 and 58\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["r"], "outputs": [{"data": {"text/plain": ["Message(id='msg_01Ljr7oKp7UQhnsy4Zba5b18', content=[ToolUseBlock(id='toolu_01TaVwp8QXKnu9heYoGjPzDq', input={'a': 42, 'b': 58}, name='sums', type='tool_use')], model='claude-3-7-sonnet-20250219', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 670; Out: 50; Cache create: 0; Cache read: 0; Total: 720)"], "text/markdown": ["ToolUseBlock(id='toolu_01TaVwp8QXKnu9heYoGjPzDq', input={'a': 42, 'b': 58}, name='sums', type='tool_use')\n", "\n", "<details>\n", "\n", "- id: `msg_01Ljr7oKp7UQhnsy4Zba5b18`\n", "- content: `[{'id': 'toolu_01TaVwp8QXKnu9heYoGjPzDq', 'input': {'a': 42, 'b': 58}, 'name': 'sums', 'type': 'tool_use'}]`\n", "- model: `claude-3-7-sonnet-20250219`\n", "- role: `assistant`\n", "- stop_reason: `tool_use`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 670, 'output_tokens': 50}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "how to i get the result of r? also what is the ToolUseBlock output provided?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "To get the result after seeing a ToolUseBlock, you need to call chat() again with no arguments. The ToolUseBlock is Claudette's way of showing that Claude has decided to use a tool - it contains:\n", "\n", "1. The tool name ('sums')\n", "2. The input parameters (a=42, b=58)\n", "3. The type of operation ('tool_use')\n", "\n", "Here's how to get the final result:\n", "\n", "```python\n", "# First get the tool use response\n", "r = chat(prompt, tool_choice='sums')\n", "\n", "# Then call chat() again to get the actual result\n", "result = chat()\n", "```\n", "\n", "Would you like to try this and see the final result? "]}, {"cell_type": "code", "metadata": {}, "source": ["chat()"], "outputs": [{"data": {"text/plain": ["Message(id='msg_012hEWYR7u9Rb8youPDXEB5f', content=[TextBlock(citations=None, text='The answer is 100.', type='text')], model='claude-3-7-sonnet-20250219', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 747; Out: 10; Cache create: 0; Cache read: 0; Total: 757)"], "text/markdown": ["The answer is 100.\n", "\n", "<details>\n", "\n", "- id: `msg_012hEWYR7u9Rb8youPDXEB5f`\n", "- content: `[{'citations': None, 'text': 'The answer is 100.', 'type': 'text'}]`\n", "- model: `claude-3-7-sonnet-20250219`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 747, 'output_tokens': 10}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "In claudette source I've seena ToolUseBlock class being created when a tool is requested to be used by claude. What is a ToolUseBlock class??"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "A ToolUseBlock is a special message type in Claudette that represents Claude's request to use a tool. When Claude decides it needs to use a tool, it returns a ToolUseBlock containing:\n", "\n", "1. id: A unique identifier for this tool use request\n", "2. input: The parameters Claude wants to pass to the tool\n", "3. name: Which tool Claude wants to use\n", "4. type: Always 'tool_use' to identify this as a tool request\n", "\n", "For example, in our previous output:\n", "```python\n", "ToolUseBlock(\n", " id='toolu_01DsQN6UNVxQhGZqwFTfYH6E', \n", " input={'a': 42, 'b': 58}, \n", " name='sums', \n", " type='tool_use'\n", ")\n", "```\n", "\n", "This tells Claudette:\n", "- Use the 'sums' tool\n", "- With parameters a=42 and b=58\n", "\n", "Would you like to see how this works with different tools or parameters?"]}, {"cell_type": "code", "metadata": {}, "source": ["chat.use"], "outputs": [{"data": {"text/plain": ["In: 2614; Out: 147; Cache create: 0; Cache read: 0; Total: 2761"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["## ToolLoop"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "In the context if a tool request what is a toolloop? A for loop of several tools being used in one request?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Yes, toolloop is Claudette's way of handling multiple tool calls in sequence for a single request. For example, when you need to:\n", "\n", "1. Use one tool's output as input to another tool\n", "2. Perform multiple calculations in sequence\n", "3. Let Claude decide which tools to use and in what order\n", "\n", "From the documentation, here's what toolloop enables:\n", "```python\n", "# Example calculation that needs multiple tools: (a+b)*2\n", "a,b = 42,58\n", "prompt = f\"Calculate ({a}+{b})*2\"\n", "\n", "# toolloop will handle the sequence automatically:\n", "# 1. First use sums to add the numbers\n", "# 2. Then use mults to multiply by 2\n", "result = chat.toolloop(prompt)\n", "```"]}, {"cell_type": "code", "metadata": {}, "source": ["chat = Chat(models[-1], sp=sp, tools=[sums,mults])\n", "pr = f'Calculate ({a}+{b})*2'\n", "pr"], "outputs": [{"data": {"text/plain": ["'Calculate (42+58)*2'"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["chat.tools?"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31mType:\u001b[0m list\n", "\u001b[0;31mString form:\u001b[0m [<function sums at 0x77e073bb8ae0>, <function mults at 0x77e073bb84a0>]\n", "\u001b[0;31mLength:\u001b[0m 2\n", "\u001b[0;31mDocstring:\u001b[0m \n", "Built-in mutable sequence.\n", "\n", "If no argument is given, the constructor creates a new empty list.\n", "The argument must be an iterable if specified.\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["chat.toolloop?"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0mchat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtoolloop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mpr\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmax_steps\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mtrace_func\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mbuilt\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;32min\u001b[0m \u001b[0mfunction\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcont_func\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mbuilt\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;32min\u001b[0m \u001b[0mfunction\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m<\u001b[0m\u001b[0mfunction\u001b[0m \u001b[0mnoop\u001b[0m \u001b[0mat\u001b[0m \u001b[0;36m0x77e14a88df80\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mtemp\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmaxtok\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m4096\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mstream\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mprefill\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mtool_choice\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mdict\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m Add prompt `pr` to dialog and get a response from Claude, automatically following up with `tool_use` messages\n", "\u001b[0;31mFile:\u001b[0m /usr/local/lib/python3.11/site-packages/claudette/toolloop.py\n", "\u001b[0;31mType:\u001b[0m method\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["chat.toolloop(pr)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Finding the sum of 42 and 58\n", "Finding the product of 100 and 2\n"]}, {"data": {"text/plain": ["Message(id='msg_01FU9dhTS6Yd9c3hpLwaSR2D', content=[TextBlock(citations=None, text='The result is 200.', type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 1125; Out: 10; Cache create: 0; Cache read: 0; Total: 1135)"], "text/markdown": ["The result is 200.\n", "\n", "<details>\n", "\n", "- id: `msg_01FU9dhTS6Yd9c3hpLwaSR2D`\n", "- content: `[{'citations': None, 'text': 'The result is 200.', 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 1125, 'output_tokens': 10}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["chat.toolloop(pr, trace_func=print)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Finding the sum of 42 and 58\n", "[{'role': 'user', 'content': 'Calculate (42+58)*2'}, {'role': 'assistant', 'content': [TextBlock(citations=None, text=\"I'll solve this step by step:\", type='text'), ToolUseBlock(id='toolu_01HwXQo5wFFnagAemVEh4Mmy', input={'a': 42, 'b': 58}, name='sums', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01HwXQo5wFFnagAemVEh4Mmy', 'content': '100'}]}]\n", "Finding the product of 100 and 2\n", "[{'role': 'assistant', 'content': [ToolUseBlock(id='toolu_01NRibfQFMM781XK9BEmGAeo', input={'a': 100, 'b': 2}, name='mults', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01NRibfQFMM781XK9BEmGAeo', 'content': '200'}]}]\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='The result is 200.', type='text')]}]\n"]}, {"data": {"text/plain": ["Message(id='msg_013zqg9eYmWZot3WS1dgP3v7', content=[TextBlock(citations=None, text='The result is 200.', type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 933; Out: 10; Cache create: 0; Cache read: 0; Total: 943)"], "text/markdown": ["The result is 200.\n", "\n", "<details>\n", "\n", "- id: `msg_013zqg9eYmWZot3WS1dgP3v7`\n", "- content: `[{'citations': None, 'text': 'The result is 200.', 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 933, 'output_tokens': 10}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["mk_toolres??"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0mmk_toolres\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mcollections\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mabc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMapping\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mns\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mcollections\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mabc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMapping\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mSource:\u001b[0m \n", "\u001b[0;32mdef\u001b[0m \u001b[0mmk_toolres\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mabc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMapping\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;31m# Tool use request response from Claude\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mns\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mabc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMapping\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;31m# Namespace to search for tools\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mOptional\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m \u001b[0;31m# Class to search for tools\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m\"Create a `tool_result` message from response `r`.\"\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'content'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mmk_msg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mns\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mglobals\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mobj\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mns\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmk_ns\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mtcs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mmk_funcres\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcall_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mns\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mo\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcts\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mToolUseBlock\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtcs\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mres\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmk_msg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtcs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mres\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mFile:\u001b[0m /usr/local/lib/python3.11/site-packages/claudette/core.py\n", "\u001b[0;31mType:\u001b[0m function\n"]}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "In the context of python what is a namespace? is this a regualr concept in programming?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "In Python, a namespace is a mapping from names to objects. It's essentially a dictionary that holds variable and function names as keys, with their corresponding values or references.\n", "\n", "For example, when you define a function or variable, its name is added to the current namespace:\n", "\n", "```python\n", "x = 10 # Added to the current namespace\n", "def my_func(): # 'my_func' is added to the namespace\n", " pass\n", "```\n", "\n", "Namespaces are a fundamental concept in programming, particularly in Python, and serve several important purposes:\n", "\n", "1. **Avoiding name conflicts**: Different modules can use the same name for different objects\n", "2. **Organization**: Related functionality can be grouped together\n", "3. **Scope management**: Variables defined in different scopes live in different namespaces\n", "\n", "In the Claudette code you saw, `ns` (namespace) is used to look up tool functions. When you pass `ns=globals()`, it's searching the global namespace for the tool functions. This allows Claudette to find and call the right function when Claude requests it.\n", "\n", "Would you like me to explain more about how namespaces are used in the toolloop functionality?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "What is a abc.Mapping type?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "In Python, `abc.Mapping` is an abstract base class from the `collections.abc` module that defines the interface for mapping types (like dictionaries).\n", "\n", "`abc.Mapping` represents any object that behaves like a read-only dictionary, with the following key characteristics:\n", "\n", "1. It's a collection of key-value pairs\n", "2. Keys are unique\n", "3. You can look up values by their keys\n", "4. You can iterate through the keys\n", "\n", "For example, Python's built-in `dict` type is a concrete implementation of the `abc.Mapping` interface.\n", "\n", "In the Claudette code, when a parameter is typed as `abc.Mapping`, it means the function expects an object that behaves like a dictionary - it could be a regular `dict`, an `OrderedDict`, or any custom class that implements the mapping interface.\n", "\n", "This provides flexibility while ensuring the object has the necessary dictionary-like methods that the function will use.\n", "\n", "Would you like to know more about abstract base classes or how Claudette uses mapping types?"]}, {"cell_type": "code", "metadata": {}, "source": [], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["## The ReAct (Reasoning and Acting) framework pattern"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "in the context of LLMs what is the react framework?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "The ReAct (Reasoning and Acting) framework is a pattern for LLMs to solve complex tasks by alternating between:\n", "\n", "1. Reasoning: The LLM thinks about what needs to be done and plans next steps\n", "2. Acting: The LLM takes actions (like using tools) based on its reasoning\n", "\n", "This framework helps LLMs break down complex problems into manageable steps and use tools effectively. Would you like to try another example that demonstrates this pattern?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Could you provide anther xplanation on what is the ReAct framework? Now match it to the experimentation I've carried out so far with the Claudette library."]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "The ReAct framework is a problem-solving approach for LLMs that combines reasoning with action-taking. In your experiments with Claudette, this pattern is clearly visible:\n", "\n", "When you used toolloop with the calculation example `(42+58)*2`, Claude demonstrated ReAct by:\n", "\n", "1. **Reasoning**: \"I'll help you calculate (42+58)*2 step by step: First, I'll add 42 and 58\"\n", "2. **Acting**: Used the `sums` tool with inputs 42 and 58\n", "3. **Reasoning**: \"Now, I'll multiply the result by 2\"\n", "4. **Acting**: Used the `mults` tool with inputs 100 and 2\n", "5. **Reasoning**: \"The final result is 200. To break it down: 1. 42 + 58 = 100, 2. 100 * 2 = 200\"\n", "\n", "The toolloop functionality in Claudette implements this ReAct pattern automatically:\n", "- It enables Claude to think through problems step-by-step\n", "- It allows Claude to use tools when needed\n", "- It handles the back-and-forth between reasoning and tool use\n", "- It provides the final synthesized answer\n", "\n", "This is particularly powerful for complex problems that require multiple steps or different tools to solve completely."]}, {"cell_type": "markdown", "metadata": {}, "source": ["I want to be using toolloop wit the Claudette api, this for a more complex source"], "attachments": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "I want to use the customer order example used in the Anthropic api. Please breakdown the example into steps. Please dont provide any code for now"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "# Customer Order Example Breakdown\n", "\n", "The customer order example demonstrates a more complex ReAct pattern with multiple tools. Here's the breakdown:\n", "\n", "## Setup Components\n", "1. A database of customers (dictionary with customer IDs as keys)\n", "2. A database of orders (dictionary with order IDs as keys)\n", "3. A relationship between customers and their orders\n", "\n", "## Tools Defined\n", "1. `get_customer_info` - Retrieves customer details including their orders\n", "2. `get_order_details` - Gets specific information about a single order\n", "3. `cancel_order` - Changes an order's status to \"Cancelled\"\n", "\n", "## Process Flow for Cancelling All Orders\n", "1. The system receives a request: \"Please cancel all orders for customer C1\"\n", "2. Claude reasons that it needs customer information first\n", "3. Claude calls `get_customer_info` with the customer ID\n", "4. Claude receives customer data including a list of orders\n", "5. Claude reasons through each order that needs cancellation\n", "6. For each order, Claude calls `cancel_order` with the order ID\n", "7. Claude verifies the cancellation was successful\n", "8. Claude provides a final summary of actions taken\n", "\n", "This example shows how Claude can handle a multi-step process requiring:\n", "- Information gathering\n", "- Decision making\n", "- Multiple tool calls\n", "- Tracking state between steps\n", "- Providing a coherent summary\n", "\n", "Would you like me to explain any particular part of this process in more detail?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Ok lets set up some Mock data first. Steps 1-3 for setup componesnts. Please provide mock data with your output"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Let's set up the mock data for our customer order system. Here's the data structure:\n", "\n", "```python\n", "# 1. Database of orders (dictionary with order IDs as keys)\n", "orders = {\n", " \"O1\": dict(id=\"O1\", product=\"Widget A\", quantity=2, price=19.99, status=\"Shipped\"),\n", " \"O2\": dict(id=\"O2\", product=\"Gadget B\", quantity=1, price=49.99, status=\"Processing\"),\n", " \"O3\": dict(id=\"O3\", product=\"Gadget B\", quantity=2, price=49.99, status=\"Shipped\")\n", "}\n", "\n", "# 2. Database of customers (dictionary with customer IDs as keys)\n", "# 3. Relationship between customers and their orders\n", "customers = {\n", " \"C1\": dict(name=\"John Doe\", email=\"john@example.com\", phone=\"123-456-7890\",\n", " orders=[orders['O1'], orders['O2']]),\n", " \"C2\": dict(name=\"Jane Smith\", email=\"jane@example.com\", phone=\"987-654-3210\",\n", " orders=[orders['O3']])\n", "}\n", "```\n", "\n", "This creates:\n", "- 3 orders with different products, quantities, prices and statuses\n", "- 2 customers, each linked to their respective orders\n", "- A relationship where customer C1 has orders O1 and O2, while C2 has order O3\n", "\n", "Would you like to proceed with defining the tools next?"]}, {"cell_type": "code", "metadata": {}, "source": ["# 1. Database of orders (dictionary with order IDs as keys)\n", "orders = {\n", " \"O1\": dict(id=\"O1\", product=\"Widget A\", quantity=2, price=19.99, status=\"Shipped\"),\n", " \"O2\": dict(id=\"O2\", product=\"Gadget B\", quantity=1, price=49.99, status=\"Processing\"),\n", " \"O3\": dict(id=\"O3\", product=\"Gadget B\", quantity=2, price=49.99, status=\"Shipped\")\n", "}\n", "\n", "# 2. Database of customers (dictionary with customer IDs as keys)\n", "# 3. Relationship between customers and their orders\n", "customers = {\n", " \"C1\": dict(name=\"John Doe\", email=\"john@example.com\", phone=\"123-456-7890\",\n", " orders=[orders['O1'], orders['O2']]),\n", " \"C2\": dict(name=\"Jane Smith\", email=\"jane@example.com\", phone=\"987-654-3210\",\n", " orders=[orders['O3']])\n", "}"], "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["orders"], "outputs": [{"data": {"text/plain": ["{'O1': {'id': 'O1',\n", " 'product': 'Widget A',\n", " 'quantity': 2,\n", " 'price': 19.99,\n", " 'status': 'Shipped'},\n", " 'O2': {'id': 'O2',\n", " 'product': 'Gadget B',\n", " 'quantity': 1,\n", " 'price': 49.99,\n", " 'status': 'Processing'},\n", " 'O3': {'id': 'O3',\n", " 'product': 'Gadget B',\n", " 'quantity': 2,\n", " 'price': 49.99,\n", " 'status': 'Shipped'}}"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["customers"], "outputs": [{"data": {"text/plain": ["{'C1': {'name': 'John Doe',\n", " 'email': 'john@example.com',\n", " 'phone': '123-456-7890',\n", " 'orders': [{'id': 'O1',\n", " 'product': 'Widget A',\n", " 'quantity': 2,\n", " 'price': 19.99,\n", " 'status': 'Shipped'},\n", " {'id': 'O2',\n", " 'product': 'Gadget B',\n", " 'quantity': 1,\n", " 'price': 49.99,\n", " 'status': 'Processing'}]},\n", " 'C2': {'name': 'Jane Smith',\n", " 'email': 'jane@example.com',\n", " 'phone': '987-654-3210',\n", " 'orders': [{'id': 'O3',\n", " 'product': 'Gadget B',\n", " 'quantity': 2,\n", " 'price': 49.99,\n", " 'status': 'Shipped'}]}}"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Let's proceed with the tool defenition. Please provide the relevant code for this"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Here are the tool definitions for our customer order system:\n", "\n", "```python\n", "def get_customer_info(\n", " customer_id: str # ID of the customer\n", "): # Customer's name, email, phone number, and list of orders\n", " \"Retrieves a customer's information and their orders based on the customer ID\"\n", " print(f'- Retrieving customer {customer_id}')\n", " return customers.get(customer_id, \"Customer not found\")\n", "\n", "def get_order_details(\n", " order_id: str # ID of the order\n", "): # Order's ID, product name, quantity, price, and order status\n", " \"Retrieves the details of a specific order based on the order ID\"\n", " print(f'- Retrieving order {order_id}')\n", " return orders.get(order_id, \"Order not found\")\n", "\n", "def cancel_order(\n", " order_id: str # ID of the order to cancel\n", ") -> bool: # True if the cancellation is successful\n", " \"Cancels an order based on the provided order ID\"\n", " print(f'- Cancelling order {order_id}')\n", " if order_id not in orders: return False\n", " orders[order_id]['status'] = 'Cancelled'\n", " return True\n", "```\n", "\n", "These tools provide the following functionality:\n", "1. `get_customer_info` - Looks up customer data by ID\n", "2. `get_order_details` - Looks up order data by ID\n", "3. `cancel_order` - Changes an order's status to \"Cancelled\"\n", "\n", "Each function includes type annotations and docstrings to help Claude understand how to use them.\n", "\n", "Would you like to create the Chat instance with these tools next?"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Prompt**\n", "\n", "Please evaluate my Chat instance below. \n", "\n", "```python\n", "c = Chat(model= models[-1], tools=[get_customer_info, get_order_details, cancel_order])\n", "```"]}, {"cell_type": "markdown", "metadata": {}, "source": ["**AI Response**\n", "\n", "Your Chat instance looks good! Let's break down what you've done:\n", "\n", "```python\n", "c = Chat(model=models[-1], tools=[get_customer_info, get_order_details, cancel_order])\n", "```\n", "\n", "This code:\n", "\n", "1. Creates a new Chat instance using Claude\n", "2. Uses `models[-1]` which selects the last model in the models list (likely Claude-3-Haiku based on earlier examples)\n", "3. Provides all three tools we defined (get_customer_info, get_order_details, and cancel_order)\n", "\n", "This setup will allow Claude to:\n", "- Access customer information\n", "- Look up order details\n", "- Cancel orders when needed\n", "\n", "The Chat instance is now ready to handle queries about customers and orders, and can execute multi-step processes like canceling all orders for a customer.\n", "\n", "Would you like to test this setup with a specific query?"]}, {"cell_type": "code", "metadata": {}, "source": ["def get_customer_info(\n", " customer_id: str # ID of the customer\n", "): # Customer's name, email, phone number, and list of orders\n", " \"Retrieves a customer's information and their orders based on the customer ID\"\n", " print(f'- Retrieving customer {customer_id}')\n", " return customers.get(customer_id, \"Customer not found\")\n", "\n", "def get_order_details(\n", " order_id: str # ID of the order\n", "): # Order's ID, product name, quantity, price, and order status\n", " \"Retrieves the details of a specific order based on the order ID\"\n", " print(f'- Retrieving order {order_id}')\n", " return orders.get(order_id, \"Order not found\")\n", "\n", "def cancel_order(\n", " order_id: str # ID of the order to cancel\n", ") -> bool: # True if the cancellation is successful\n", " \"Cancels an order based on the provided order ID\"\n", " print(f'- Cancelling order {order_id}')\n", " if order_id not in orders: return False\n", " orders[order_id]['status'] = 'Cancelled'\n", " return True"], "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["p = \"Can you provide me all the infor for custoemr C1?\"#\n", "c = Chat(model= models[-1], tools=[get_customer_info, get_order_details, cancel_order])\n", "c.toolloop(p)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["- Retrieving customer C1\n"]}, {"data": {"text/plain": ["Message(id='msg_01WahEuzz8w3Eu7zxQfVHRdA', content=[TextBlock(citations=None, text=\"Here's the detailed information for customer C1:\\n\\nCustomer Details:\\n- Name: John Doe\\n- Email: john@example.com\\n- Phone: 123-456-7890\\n\\nOrder History:\\n1. Order ID: O1\\n - Product: Widget A\\n - Quantity: 2\\n - Price: $19.99\\n - Status: Shipped\\n\\n2. Order ID: O2\\n - Product: Gadget B\\n - Quantity: 1\\n - Price: $49.99\\n - Status: Processing\\n\\nIs there anything specific you would like to know about this customer or their orders?\", type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 744; Out: 145; Cache create: 0; Cache read: 0; Total: 889)"], "text/markdown": ["Here's the detailed information for customer C1:\n", "\n", "Customer Details:\n", "- Name: John Doe\n", "- Email: john@example.com\n", "- Phone: 123-456-7890\n", "\n", "Order History:\n", "1. Order ID: O1\n", " - Product: Widget A\n", " - Quantity: 2\n", " - Price: &#36;19.99\n", " - Status: Shipped\n", "\n", "2. Order ID: O2\n", " - Product: Gadget B\n", " - Quantity: 1\n", " - Price: &#36;49.99\n", " - Status: Processing\n", "\n", "Is there anything specific you would like to know about this customer or their orders?\n", "\n", "<details>\n", "\n", "- id: `msg_01WahEuzz8w3Eu7zxQfVHRdA`\n", "- content: `[{'citations': None, 'text': \"Here's the detailed information for customer C1:\\n\\nCustomer Details:\\n- Name: John Doe\\n- Email: john@example.com\\n- Phone: 123-456-7890\\n\\nOrder History:\\n1. Order ID: O1\\n - Product: Widget A\\n - Quantity: 2\\n - Price: $19.99\\n - Status: Shipped\\n\\n2. Order ID: O2\\n - Product: Gadget B\\n - Quantity: 1\\n - Price: $49.99\\n - Status: Processing\\n\\nIs there anything specific you would like to know about this customer or their orders?\", 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 744, 'output_tokens': 145}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["p = \"Please cancel order o1 for customer c1\"\n", "c.toolloop(p)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["- Cancelling order O1\n"]}, {"data": {"text/plain": ["Message(id='msg_01GWJciNUHEhoLKhgWKUJBaB', content=[TextBlock(citations=None, text='The order O1 has been successfully cancelled. The system returned \"True\", which indicates that the cancellation was processed without any issues.\\n\\nWould you like me to retrieve the updated order details or customer information to confirm the cancellation?', type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 992; Out: 52; Cache create: 0; Cache read: 0; Total: 1044)"], "text/markdown": ["The order O1 has been successfully cancelled. The system returned \"True\", which indicates that the cancellation was processed without any issues.\n", "\n", "Would you like me to retrieve the updated order details or customer information to confirm the cancellation?\n", "\n", "<details>\n", "\n", "- id: `msg_01GWJciNUHEhoLKhgWKUJBaB`\n", "- content: `[{'citations': None, 'text': 'The order O1 has been successfully cancelled. The system returned \"True\", which indicates that the cancellation was processed without any issues.\\n\\nWould you like me to retrieve the updated order details or customer information to confirm the cancellation?', 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 992, 'output_tokens': 52}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c.toolloop(\"provide me all orders for cusotmer c1\")"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["- Retrieving customer C1\n"]}, {"data": {"text/plain": ["Message(id='msg_01DeNy6YpG9hYA1RWkhmmpqa', content=[TextBlock(citations=None, text='Here are all the orders for customer C1 (John Doe):\\n\\n1. Order ID: O1\\n - Product: Widget A\\n - Quantity: 2\\n - Price: $19.99\\n - Status: Cancelled\\n\\n2. Order ID: O2\\n - Product: Gadget B\\n - Quantity: 1\\n - Price: $49.99\\n - Status: Processing\\n\\nWould you like me to provide more details about these orders or help you with anything else?', type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 1854; Out: 117; Cache create: 0; Cache read: 0; Total: 1971)"], "text/markdown": ["Here are all the orders for customer C1 (John Doe):\n", "\n", "1. Order ID: O1\n", " - Product: Widget A\n", " - Quantity: 2\n", " - Price: &#36;19.99\n", " - Status: Cancelled\n", "\n", "2. Order ID: O2\n", " - Product: Gadget B\n", " - Quantity: 1\n", " - Price: &#36;49.99\n", " - Status: Processing\n", "\n", "Would you like me to provide more details about these orders or help you with anything else?\n", "\n", "<details>\n", "\n", "- id: `msg_01DeNy6YpG9hYA1RWkhmmpqa`\n", "- content: `[{'citations': None, 'text': 'Here are all the orders for customer C1 (John Doe):\\n\\n1. Order ID: O1\\n - Product: Widget A\\n - Quantity: 2\\n - Price: $19.99\\n - Status: Cancelled\\n\\n2. Order ID: O2\\n - Product: Gadget B\\n - Quantity: 1\\n - Price: $49.99\\n - Status: Processing\\n\\nWould you like me to provide more details about these orders or help you with anything else?', 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 1854, 'output_tokens': 117}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["orders"], "outputs": [{"data": {"text/plain": ["{'O1': {'id': 'O1',\n", " 'product': 'Widget A',\n", " 'quantity': 2,\n", " 'price': 19.99,\n", " 'status': 'Cancelled'},\n", " 'O2': {'id': 'O2',\n", " 'product': 'Gadget B',\n", " 'quantity': 1,\n", " 'price': 49.99,\n", " 'status': 'Processing'},\n", " 'O3': {'id': 'O3',\n", " 'product': 'Gadget B',\n", " 'quantity': 2,\n", " 'price': 49.99,\n", " 'status': 'Shipped'}}"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c.toolloop(\"cancel order o1\", trace_func=print)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["- Cancelling order O1\n", "[{'role': 'user', 'content': 'cancel order o1'}, {'role': 'assistant', 'content': [TextBlock(citations=None, text=\"I'll attempt to cancel order O1 again:\", type='text'), ToolUseBlock(id='toolu_01PKXUKPQub4zKoiVuBTtfZs', input={'order_id': 'O1'}, name='cancel_order', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01PKXUKPQub4zKoiVuBTtfZs', 'content': 'True'}]}]\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='The system returned \"True\", but as mentioned in the previous response, this order was already cancelled. The status remains unchanged. \\n\\nWould you like me to:\\n1. Verify the current status of the order?\\n2. Check details of the other order (O2)?\\n3. Assist you with something else?', type='text')]}]\n"]}, {"data": {"text/plain": ["Message(id='msg_016at6U9o6xG7r15W4CUFiKL', content=[TextBlock(citations=None, text='The system returned \"True\", but as mentioned in the previous response, this order was already cancelled. The status remains unchanged. \\n\\nWould you like me to:\\n1. Verify the current status of the order?\\n2. Check details of the other order (O2)?\\n3. Assist you with something else?', type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 2227; Out: 72; Cache create: 0; Cache read: 0; Total: 2299)"], "text/markdown": ["The system returned \"True\", but as mentioned in the previous response, this order was already cancelled. The status remains unchanged. \n", "\n", "Would you like me to:\n", "1. Verify the current status of the order?\n", "2. Check details of the other order (O2)?\n", "3. Assist you with something else?\n", "\n", "<details>\n", "\n", "- id: `msg_016at6U9o6xG7r15W4CUFiKL`\n", "- content: `[{'citations': None, 'text': 'The system returned \"True\", but as mentioned in the previous response, this order was already cancelled. The status remains unchanged. \\n\\nWould you like me to:\\n1. Verify the current status of the order?\\n2. Check details of the other order (O2)?\\n3. Assist you with something else?', 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 2227, 'output_tokens': 72}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c.toolloop(\"cancel order o3\", trace_func=print)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["- Cancelling order O3\n", "[{'role': 'user', 'content': 'cancel order o3'}, {'role': 'assistant', 'content': [TextBlock(citations=None, text=\"I'll attempt to cancel order O3:\", type='text'), ToolUseBlock(id='toolu_014tKQwvqHvLbaXhiqzkA1NR', input={'order_id': 'O3'}, name='cancel_order', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_014tKQwvqHvLbaXhiqzkA1NR', 'content': 'True'}]}]\n", "- Retrieving order O3\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='The system returned \"True\" for the cancellation of order O3. However, to verify the details, I\\'ll try to retrieve the order information:', type='text'), ToolUseBlock(id='toolu_011rq8jaH6EBsz8Q942P2qCX', input={'order_id': 'O3'}, name='get_order_details', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_011rq8jaH6EBsz8Q942P2qCX', 'content': \"{'id': 'O3', 'product': 'Gadget B', 'quantity': 2, 'price': 49.99, 'status': 'Cancelled'}\"}]}]\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='Order O3 details:\\n- Product: Gadget B\\n- Quantity: 2\\n- Price: $49.99\\n- Status: Cancelled\\n\\nThe order has been successfully cancelled. Is there anything else I can help you with?', type='text')]}]\n"]}, {"data": {"text/plain": ["Message(id='msg_01GfVjUJMA66VHiUVHg4ZhaM', content=[TextBlock(citations=None, text='Order O3 details:\\n- Product: Gadget B\\n- Quantity: 2\\n- Price: $49.99\\n- Status: Cancelled\\n\\nThe order has been successfully cancelled. Is there anything else I can help you with?', type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 2530; Out: 57; Cache create: 0; Cache read: 0; Total: 2587)"], "text/markdown": ["Order O3 details:\n", "- Product: Gadget B\n", "- Quantity: 2\n", "- Price: &#36;49.99\n", "- Status: Cancelled\n", "\n", "The order has been successfully cancelled. Is there anything else I can help you with?\n", "\n", "<details>\n", "\n", "- id: `msg_01GfVjUJMA66VHiUVHg4ZhaM`\n", "- content: `[{'citations': None, 'text': 'Order O3 details:\\n- Product: Gadget B\\n- Quantity: 2\\n- Price: $49.99\\n- Status: Cancelled\\n\\nThe order has been successfully cancelled. Is there anything else I can help you with?', 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 2530, 'output_tokens': 57}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c.toolloop(\"cancel order o5\", trace_func=print)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["- Cancelling order O5\n", "[{'role': 'user', 'content': 'cancel order o5'}, {'role': 'assistant', 'content': [TextBlock(citations=None, text=\"I'll attempt to cancel order O5:\", type='text'), ToolUseBlock(id='toolu_01JRdd1BTe7ZjarzGZtSH5Hb', input={'order_id': 'O5'}, name='cancel_order', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01JRdd1BTe7ZjarzGZtSH5Hb', 'content': 'False'}]}]\n", "- Retrieving order O5\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='The system returned \"False\", which typically indicates that the order could not be cancelled. This could be due to several reasons:\\n1. The order may not exist\\n2. The order might have already been completed or shipped\\n3. There might be a system issue preventing the cancellation\\n\\nLet me try to retrieve the order details to confirm:', type='text'), ToolUseBlock(id='toolu_01SEcnSu4iFu69QxMvmayv7c', input={'order_id': 'O5'}, name='get_order_details', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01SEcnSu4iFu69QxMvmayv7c', 'content': 'Order not found'}]}]\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='The system indicates that order O5 does not exist. This explains why the cancellation failed.\\n\\nWould you like me to help you with:\\n1. Verifying the correct order number\\n2. Checking orders for a specific customer\\n3. Assisting you with something else?', type='text')]}]\n"]}, {"data": {"text/plain": ["Message(id='msg_019otyTkR626nBnr7mMQJsuZ', content=[TextBlock(citations=None, text='The system indicates that order O5 does not exist. This explains why the cancellation failed.\\n\\nWould you like me to help you with:\\n1. Verifying the correct order number\\n2. Checking orders for a specific customer\\n3. Assisting you with something else?', type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 2816; Out: 65; Cache create: 0; Cache read: 0; Total: 2881)"], "text/markdown": ["The system indicates that order O5 does not exist. This explains why the cancellation failed.\n", "\n", "Would you like me to help you with:\n", "1. Verifying the correct order number\n", "2. Checking orders for a specific customer\n", "3. Assisting you with something else?\n", "\n", "<details>\n", "\n", "- id: `msg_019otyTkR626nBnr7mMQJsuZ`\n", "- content: `[{'citations': None, 'text': 'The system indicates that order O5 does not exist. This explains why the cancellation failed.\\n\\nWould you like me to help you with:\\n1. Verifying the correct order number\\n2. Checking orders for a specific customer\\n3. Assisting you with something else?', 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 2816, 'output_tokens': 65}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["{'O1': {'id': 'O1',\n", " 'product': 'Widget A',\n", " 'quantity': 2,\n", " 'price': 19.99,\n", " 'status': 'Shipped'},\n", " 'O2': {'id': 'O2',\n", " 'product': 'Gadget B',\n", " 'quantity': 1,\n", " 'price': 49.99,\n", " 'status': 'Processing'},\n", " 'O3': {'id': 'O3',\n", " 'product': 'Gadget B',\n", " 'quantity': 2,\n", " 'price': 49.99,\n", " 'status': 'Shipped'}}\n", "\n", "{'C1': {'name': 'John Doe',\n", " 'email': 'john@example.com',\n", " 'phone': '123-456-7890',\n", " 'orders': [{'id': 'O1',\n", " 'product': 'Widget A',\n", " 'quantity': 2,\n", " 'price': 19.99,\n", " 'status': 'Shipped'},\n", " {'id': 'O2',\n", " 'product': 'Gadget B',\n", " 'quantity': 1,\n", " 'price': 49.99,\n", " 'status': 'Processing'}]},\n", " 'C2': {'name': 'Jane Smith',\n", " 'email': 'jane@example.com',\n", " 'phone': '987-654-3210',\n", " 'orders': [{'id': 'O3',\n", " 'product': 'Gadget B',\n", " 'quantity': 2,\n", " 'price': 49.99,\n", " 'status': 'Shipped'}]}}"], "outputs": [{"data": {"text/plain": ["{'C1': {'name': 'John Doe',\n", " 'email': 'john@example.com',\n", " 'phone': '123-456-7890',\n", " 'orders': [{'id': 'O1',\n", " 'product': 'Widget A',\n", " 'quantity': 2,\n", " 'price': 19.99,\n", " 'status': 'Shipped'},\n", " {'id': 'O2',\n", " 'product': 'Gadget B',\n", " 'quantity': 1,\n", " 'price': 49.99,\n", " 'status': 'Processing'}]},\n", " 'C2': {'name': 'Jane Smith',\n", " 'email': 'jane@example.com',\n", " 'phone': '987-654-3210',\n", " 'orders': [{'id': 'O3',\n", " 'product': 'Gadget B',\n", " 'quantity': 2,\n", " 'price': 49.99,\n", " 'status': 'Shipped'}]}}"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c.toolloop(\"cancel all orders for customer c1\", trace_func=print)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["- Retrieving customer C1\n", "[{'role': 'user', 'content': 'cancel all orders for customer c1'}, {'role': 'assistant', 'content': [TextBlock(citations=None, text=\"I'll first retrieve the customer's orders and then attempt to cancel each of them:\", type='text'), ToolUseBlock(id='toolu_01S5aHHDWpnSuRzsBDzfNZwu', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01S5aHHDWpnSuRzsBDzfNZwu', 'content': \"{'name': 'John Doe', 'email': 'john@example.com', 'phone': '123-456-7890', 'orders': [{'id': 'O1', 'product': 'Widget A', 'quantity': 2, 'price': 19.99, 'status': 'Cancelled'}, {'id': 'O2', 'product': 'Gadget B', 'quantity': 1, 'price': 49.99, 'status': 'Processing'}]}\"}]}]\n", "- Cancelling order O1\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text=\"I'll now attempt to cancel each order:\\n\\n1. Cancelling O1:\", type='text'), ToolUseBlock(id='toolu_01DxyRqwcceyrjLSK8QX3scV', input={'order_id': 'O1'}, name='cancel_order', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01DxyRqwcceyrjLSK8QX3scV', 'content': 'True'}]}]\n", "- Cancelling order O2\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='2. Cancelling O2:', type='text'), ToolUseBlock(id='toolu_016tBug8kwGPgbEQ2VBjPp5R', input={'order_id': 'O2'}, name='cancel_order', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_016tBug8kwGPgbEQ2VBjPp5R', 'content': 'True'}]}]\n", "- Retrieving customer C1\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='Let me verify the final status of the orders:', type='text'), ToolUseBlock(id='toolu_01VGTY5tdPs5sWZ6yvAoLfH6', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01VGTY5tdPs5sWZ6yvAoLfH6', 'content': \"{'name': 'John Doe', 'email': 'john@example.com', 'phone': '123-456-7890', 'orders': [{'id': 'O1', 'product': 'Widget A', 'quantity': 2, 'price': 19.99, 'status': 'Cancelled'}, {'id': 'O2', 'product': 'Gadget B', 'quantity': 1, 'price': 49.99, 'status': 'Cancelled'}]}\"}]}]\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='Summary:\\n- Order O1 was already cancelled, so no change was needed.\\n- Order O2 has now been cancelled.\\n\\nAll orders for customer C1 (John Doe) have been cancelled. Is there anything else I can help you with?', type='text')]}]\n"]}, {"data": {"text/plain": ["Message(id='msg_01EL4XLU2KjB6tP8s37i7KJr', content=[TextBlock(citations=None, text='Summary:\\n- Order O1 was already cancelled, so no change was needed.\\n- Order O2 has now been cancelled.\\n\\nAll orders for customer C1 (John Doe) have been cancelled. Is there anything else I can help you with?', type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 3476; Out: 60; Cache create: 0; Cache read: 0; Total: 3536)"], "text/markdown": ["Summary:\n", "- Order O1 was already cancelled, so no change was needed.\n", "- Order O2 has now been cancelled.\n", "\n", "All orders for customer C1 (John Doe) have been cancelled. Is there anything else I can help you with?\n", "\n", "<details>\n", "\n", "- id: `msg_01EL4XLU2KjB6tP8s37i7KJr`\n", "- content: `[{'citations': None, 'text': 'Summary:\\n- Order O1 was already cancelled, so no change was needed.\\n- Order O2 has now been cancelled.\\n\\nAll orders for customer C1 (John Doe) have been cancelled. Is there anything else I can help you with?', 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 3476, 'output_tokens': 60}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": ["c.toolloop(\"Does c1 has any more orders with status like processing?\", trace_func=print)"], "outputs": [{"name": "stdout", "output_type": "stream", "text": ["- Retrieving customer C1\n", "[{'role': 'user', 'content': 'Does c1 has any more orders with status like processing?'}, {'role': 'assistant', 'content': [TextBlock(citations=None, text=\"I'll retrieve the customer information for C1 to check the status of their orders:\", type='text'), ToolUseBlock(id='toolu_01TyNaEJD5HJqSgyjpuz6wrU', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01TyNaEJD5HJqSgyjpuz6wrU', 'content': \"{'name': 'John Doe', 'email': 'john@example.com', 'phone': '123-456-7890', 'orders': [{'id': 'O1', 'product': 'Widget A', 'quantity': 2, 'price': 19.99, 'status': 'Cancelled'}, {'id': 'O2', 'product': 'Gadget B', 'quantity': 1, 'price': 49.99, 'status': 'Cancelled'}]}\"}]}]\n", "[{'role': 'assistant', 'content': [TextBlock(citations=None, text='No, customer C1 does not have any orders with a \"Processing\" status. Both of their orders (O1 and O2) are currently in \"Cancelled\" status.\\n\\nIs there anything else you would like to know about this customer or their orders?', type='text')]}]\n"]}, {"data": {"text/plain": ["Message(id='msg_01BfY7tRuiPxeX5ExQaRd4Hn', content=[TextBlock(citations=None, text='No, customer C1 does not have any orders with a \"Processing\" status. Both of their orders (O1 and O2) are currently in \"Cancelled\" status.\\n\\nIs there anything else you would like to know about this customer or their orders?', type='text')], model='claude-3-5-haiku-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 3766; Out: 60; Cache create: 0; Cache read: 0; Total: 3826)"], "text/markdown": ["No, customer C1 does not have any orders with a \"Processing\" status. Both of their orders (O1 and O2) are currently in \"Cancelled\" status.\n", "\n", "Is there anything else you would like to know about this customer or their orders?\n", "\n", "<details>\n", "\n", "- id: `msg_01BfY7tRuiPxeX5ExQaRd4Hn`\n", "- content: `[{'citations': None, 'text': 'No, customer C1 does not have any orders with a \"Processing\" status. Both of their orders (O1 and O2) are currently in \"Cancelled\" status.\\n\\nIs there anything else you would like to know about this customer or their orders?', 'type': 'text'}]`\n", "- model: `claude-3-5-haiku-20241022`\n", "- role: `assistant`\n", "- stop_reason: `end_turn`\n", "- stop_sequence: `None`\n", "- type: `message`\n", "- usage: `{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 3766, 'output_tokens': 60}`\n", "\n", "</details>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["### Summary\n", "\n", "## RAGs\n", "The contrast between automated and manual context curation is really cool. While Jeremy's approach demands more effort, the human-handpicked selection of relevant info creates a more targeted context for the model, which I find particularly compelling.\n", "\n", "## Claudette\n", "Prefill seems the more reliable control mechanism compared to system prompts, which the model often ignores. The `mk_msgs` function handles message formatting, making the user/assistant dialog structure more intuitive. Structure output functionality provides a clean way to coerce model responses into specific data formats\u2014potentially opening doors for XML/JSON like integrations with tools. \n", "\n", "## ReAct Pattern\n", "The Reasoning-Action framework shows the model's cognitive process, providing visibility into how language models approach problem-solving. This makes things way more transparent, allowing us to trace the model's decision-making journey.\n", "\n", "## ToolLoop\n", "This feature stands out as a powerful for RAG prototyping. By defining expected tools and observing the model's patterns, I gained vauable insights into its query handling capabilities."], "attachments": {}}], "metadata": {"kernelspec": {"display_name": "python3", "language": "python", "name": "python3"}}, "nbformat": 4, "nbformat_minor": 4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment