12 KiB
12 KiB
In [3]:
import boto3
def generate_conversation(messages,
):
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
messages=messages
)
return response["output"]["message"]["content"][0]["text"]
messages = [{
"role": "user",
"content": [{"text": "hello world"}]
}]
generate_conversation(messages)Out [3]:
'Hi! How can I help you today?'
In [4]:
messages = [{
"role": "user",
"content": [{"text": "What flavors are used in Dr. Pepper?"}]
}]
generate_conversation(messages)Out [4]:
"Dr Pepper's exact formula is a trade secret, but it's known to contain 23 different flavors. While the complete list isn't public, some commonly believed ingredients include:\n\n1. Cherry\n2. Cola\n3. Vanilla\n4. Amaretto/Almond\n5. Blackberry\n6. Caramel\n7. Pepper\n8. Root Beer\n9. Prune juice (though the company has denied this)\n10. Licorice\n\nThe actual combination and proportions of flavors remain confidential, and there may be other flavors not commonly known or speculated about. The unique blend of these flavors creates Dr Pepper's distinctive taste that sets it apart from other soft drinks."
In [5]:
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
messages=[{
"role": "user",
"content": [{"text": "hello world"}]
}],
)
responseOut [5]:
{'ResponseMetadata': {'RequestId': '67f892c6-6dcc-4296-a61c-d67449183ab9',
'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Thu, 14 Nov 2024 19:19:03 GMT',
'content-type': 'application/json',
'content-length': '209',
'connection': 'keep-alive',
'x-amzn-requestid': '67f892c6-6dcc-4296-a61c-d67449183ab9'},
'RetryAttempts': 0},
'output': {'message': {'role': 'assistant',
'content': [{'text': 'Hi! How can I help you today?'}]}},
'stopReason': 'end_turn',
'usage': {'inputTokens': 9, 'outputTokens': 12, 'totalTokens': 21},
'metrics': {'latencyMs': 643}}In [6]:
messages = [{
"role": "user",
"content": [{"text": "Generate a beautiful haiku"}]
},{
"role":"assistant",
"content": [{"text": "calming mountain air"}]
}]
generate_conversation(messages)Out [6]:
'\ncherry blossoms dance and drift\npeace follows the wind'
In [7]:
print("calming mountain air" + generate_conversation(messages))calming mountain air cherry blossoms dance in dew Spring calls in a dream
In [8]:
messages = [{
"role": "user",
"content": [{"text": f"Analyze the sentiment in this tweet: Just tried the new spicy pickles from @PickleCo, and my taste buds are doing a happy dance! 🌶️🥒 #pickleslove #spicyfood"}]
}]
generate_conversation(messages)Out [8]:
'This tweet has a positive sentiment. The use of enthusiastic language ("happy dance"), the positive context of enjoying the product, and the cheerful emoji usage (🌶️🥒) all indicate the user had a favorable experience with the spicy pickles. The inclusion of upbeat hashtags like #pickleslove also reinforces the positive tone.'In [9]:
messages = [
{"role": "user", "content": [{"text": "Unpopular opinion: Pickles are disgusting. Don't @ me"}]},
{"role": "assistant", "content": [{"text": "NEGATIVE"}]},
{"role": "user", "content": [{"text": "I think my love for pickles might be getting out of hand. I just bought a pickle-shaped pool float"}]},
{"role": "assistant", "content": [{"text": "POSITIVE"}]},
{"role": "user", "content": [{"text": "Seriously why would anyone ever eat a pickle? Those things are nasty!"}]},
{"role": "assistant", "content": [{"text": "NEGATIVE"}]},
{"role": "user", "content": [{"text": "Just tried the new spicy pickles from @PickleCo, and my taste buds are doing a happy dance! 🌶️🥒 #pickleslove #spicyfood"}]}
]
generate_conversation(messages)Out [9]:
'POSITIVE'