Building an AI mother with Claude 3.5 Sonnet

claude 3.5. sonnet Jul 18, 2024
 


Building an AI Mother with Claude 3.5 Sonnet and ElevenLabs

In this tutorial, we'll create a fun AI project where we build an AI that behaves like a loving but nagging mother. We'll use Claude 3.5 Sonnet and ElevenLabs to make this happen. Plus, our AI will be able to read emails!

This is a shortened version of the original lesson available in our member area. If you want to download the complete video and source code including Elevenlabs integration, consider enrolling in a free trial.

Step 1: Installing Dependencies

First, we need to install some libraries. Open your terminal or command prompt and run this command:

 pip install anthropic elevenlabs pygame


Step 2: Setting Up the AI's Personality

We want our AI to act like a typical nagging mother. To do this, we need to set up a special instruction, called a system prompt, for our AI.

Create a new file called app.py and add the following code:

import anthropic

client = anthropic.Anthropic()
system_prompt = """
  You are an AI designed to emulate the behavior of a stereotypical nagging mother.
  ...
"""


Step 3: Creating a Continuous Conversation

Now, let's make our AI respond to us in a loop. We'll also ensure it remembers what we've discussed by saving the conversation in a variable.

conversation = ""

while True:
    user_input = input('> ')
    conversation += user_input
    message = client.messages.create(
        model="claude-3-5-sonnet-20240620",
        max_tokens=1000,
        temperature=0,
        system=system_prompt,
        messages=[{
            "role": "user",
            "content": [{
                "type": "text",
                "text": f"Conversation so far: {conversation}"
            }]
        }]
    )
    mothers_answer = message.content[0].text
    conversation += mothers_answer
    print(mothers_answer)

You can try running this code now, but remember to set the ANTHROPIC_API_KEY as an environment variable first. You get the key at https://console.anthropic.com/settings/keys.

 

 

Step 4: Adding Email Access

Our AI mother will be able to read emails. Claude 3.5 Sonnet can decide when to call a function to check emails.

Defining the Email Function

Let's write a function that gets emails. If we want to filter emails by the sender's name, we can do that too.
For now, this is just a placeholder method. If you want to learn how to replace this with working code, such as fetching emails from Gmail, please check out the course inside.

def get_emails(sender_name=None):
    emails = [
        {
            "From": "[email protected]",
            "Content": "What about dinner tonight?"
        },
        {
            "From": "[email protected]",
            "Content": "Do you want to go to the cinema with me on Wednesday?"
        }
    ]

    if sender_name:
        filtered_emails = [
            email for email in emails if sender_name.lower() in email["From"].lower()
        ]
        return filtered_emails
    else:
        return emails

 

Describing the Email Tool

We need to tell Claude 3.5 Sonnet about our email function. We'll do this by defining a tool.

def get_emails_tool_definition():
    return {
        "name": "get_emails",
        "description": "Retrieve emails from specific senders.",
        "input_schema": {
            "type": "object",
            "properties": {
                "sender_name": {
                    "type": "string",
                    "description": "The name of the sender",
                }
            },
        },
    }

 

Step 5: Updating the Call to Claude 3.5 Sonnet

Let's extend our call to Claude 3.5 Sonnet to include the email tool we just defined. Add the tools parameter to the call to Claude.

... 
tools=[get_emails_tool_definition()],
...

 

Step 6: Handling Email Requests 

First, we want to move the call to Claude into a separate function to prevent code duplication.

def call_claude(conversation):
    message = client.messages.create(
        model="claude-3-5-sonnet-20240620",
        max_tokens=1000,
        temperature=0,
        system=system_prompt,
        messages=[{
            "role": "user",
            "content": [{
                "type": "text",
                "text": f"Conversation so far: {conversation}"
            }]
        }]
    )
    return message

 

Finally, we'll make our AI check for email requests. If Claude 3.5 Sonnet decides an email check is needed, we'll call our get_emails function and update the conversation.

while True:
    user_input = input('> ')
    conversation += user_input
    response = call_claude(conversation)
    mothers_answer = response.content[0].text
    conversation += mothers_answer
    print(mothers_answer)
    # say(mothers_answer)
    
    if response.stop_reason == 'tool_use':
        emails = get_emails()
        conversation += f"\n\nResponse from get_emails: {emails}\n\n"
        mail_response = call_claude(conversation)
        print(mail_response.content[0].text)
        # say(mail_response.content[0].text)

 


Conclusion

Congratulations! You have successfully built an AI mother using Claude 3.5 Sonnet and ElevenLabs. This AI can chat continuously and even read emails. This project demonstrates how fun and interactive AI can be with the power of Claude 3.5 Sonnet.

If you want to download the complete video and source code including Elevenlabs integration, consider enrolling in a free trial.

Learn To Build Real-world AI

Unlock 100+ AI Videos & Source Code Now