Skip to content

Instantly share code, notes, and snippets.

@jaeyow
Last active May 29, 2024 23:46
Show Gist options
  • Save jaeyow/7346e6b6bdee96ad274483ae95acf976 to your computer and use it in GitHub Desktop.
Save jaeyow/7346e6b6bdee96ad274483ae95acf976 to your computer and use it in GitHub Desktop.
Reliable AI Development with Instructor
from enum import Enum
from pydantic import BaseModel
from pydantic import Field
class EmailCategory(str, Enum):
IT_Support = "IT Support"
HR = "HR"
Personal = "Personal"
class Email(BaseModel):
"""
Classify the email message and more
"""
category: EmailCategory = Field(..., description="The category of the email, either IT Support, HR, or Personal.")
summary: str = Field(None, description="The summary of the email message.")
sender: str = Field(None, description="The name of the sender of the email.")
recipient: str = Field(None, description="The name of the recipient of the email.")
message: str = Field(..., description="The content of the email message. Please correct grammar and spelling.")
sentiment: str = Field(None, description="The sentiment of the email message (either positive, negative, neutral).")
class EmailClassifier:
def __init__(self, client):
self.client = client
def classify(self, message: str) -> Email:
user = self.client.messages.create(
messages=[{"role": "user", "content": f"Classify message: {message}"}],
response_model=Email,
max_retries=3,
)
print(user.model_dump_json(indent=2))
email_classifier = EmailClassifier(instructor_client)
# now let's classify the sample emails and see how they are categorized + other tasks
emails = []
for email in sample_emails[0:10]:
response = email_classifier.classify(email["message"])
if response:
emails.append(response)
print(response.model_dump_json(indent=2))
import os
import anthropic
import instructor
client = anthropic.AnthropicBedrock(
aws_access_key=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
aws_session_token=os.getenv("AWS_SESSION_TOKEN"),
aws_region=os.getenv("AWS_DEFAULT_REGION"),
)
# choose one of the Claude 3 models
# ("anthropic.claude-3-sonnet-20240229-v1:0")
# ("anthropic.claude-3-opus-20240229-v1:0")
# ("anthropic.claude-3-haiku-20240307-v1:0")
instructor_client = instructor.from_anthropic(
client,
max_tokens=1000,
model="anthropic.claude-3-haiku-20240307-v1:0",
)
# asked ChatGPT to generate 15 emails from three topics: IT Support, HR, and Personal. Here are the generated emails:
sample_emails = [
{
"topic": "IT Support",
"message": "Hi Emma & Jean, You are request for IT support has been received. Our team is currently working on resolving the issue. We'll update you once it's fixed. Thanks for your patience."
},
{
"topic": "HR",
"message": "Dear Liam, Congratulations! Your performance has bean exceptional this quarter. Keep up the great work!"
},
{
"topic": "Personal",
"message": "Hey Olivia, Just wanted to check in and see how you're doing. Let me know if there's anything I can do to help."
},
{
"topic": "IT Support",
"message": "Hello Noah, We've noticed some unusual activity on your account. Please reset your password immediately for security purposes."
},
{
"topic": "HR",
"message": "Hi Ava, Just a reminder that tomorrow is the deadline for submitting your leave request for next month. Please make sure to do so if needed. Thanks!"
},
{
"topic": "Personal",
"message": "Hey Elijah, I came across this article and thought you might find it interesting. Check it out when you have a moment: [Link]."
},
{
"topic": "IT Support",
"message": "Dear Charlotte, Our records indicate that your antivirus software is due for an update. Please install the latest version to ensure your device's security."
},
{
"topic": "HR",
"message": "Hello Oliver, We're pleased to inform you that your application for the position of [Job Title] has been successful. Welcome to the team!"
},
{
"topic": "Personal",
"message": "Hi Sophia, How was your weekend? I hope you had a chance to relax and recharge."
},
{
"topic": "IT Support",
"message": "Hi Mason, We're currently experiencing some technical difficulties with the server. Our team is working on a fix, and we'll keep you updated on the progress."
},
{
"topic": "HR",
"message": "Dear Isabella, Please remember to complete your mandatory training modules by the end of the week. Let us know if you encounter any issues accessing them."
},
{
"topic": "Personal",
"message": "Hey Lucas, I heard about your recent accomplishment. Congratulations! I'm so proud of you."
},
{
"topic": "IT Support",
"message": "Hello Mia, Your device seems to be running low on storage space. Consider deleting unnecessary files or transferring them to an external drive."
},
{
"topic": "HR",
"message": "Hi James, Just a heads up that the company picnic is scheduled for next Friday. We hope to see you there for some fun and relaxation!"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment