486 KiB
486 KiB
In [1]:
import boto3
def generate_conversation(messages,
system_prompts=[],
inference_config={},
additional_model_fields={},
):
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,
system=system_prompts,
inferenceConfig=inference_config,
additionalModelRequestFields=additional_model_fields
)
return response["output"]["message"]["content"][0]["text"]
messages = [{
"role": "user",
"content": [{"text": "hello world"}]
}]
generate_conversation(messages)Out [1]:
'Hi! How can I help you today?'
In [2]:
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
messages = [{
"role": "user",
"content": [{"text": "Write me a poem"}]
}]
inference_config={"maxTokens":100}
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
inferenceConfig=inference_config,
)In [3]:
response['stopReason']Out [3]:
'max_tokens'
In [4]:
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
messages = [{
"role": "user",
"content": [{"text": "Write me a poem"}]
}]
inference_config={"maxTokens":200}
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
inferenceConfig=inference_config,
)In [5]:
response["usage"]["outputTokens"]Out [5]:
148
In [6]:
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
messages = [{
"role": "user",
"content": [{"text": "Come up with a name for an alien planet. Respond with a single word."}]
}]
def demonstrate_temperature():
temperatures = [0, 1]
for temperature in temperatures:
print(f"Prompting Claude three times with temperature of {temperature}")
print("================")
for i in range(3):
inference_config={"temperature":temperature}
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
inferenceConfig=inference_config,
)
print(f"Response {i+1}: {response['output']['message']['content'][0]['text']}")
In [7]:
demonstrate_temperature()Prompting Claude three times with temperature of 0 ================ Response 1: Kestrax Response 2: Kestrax Response 3: Kestrax Prompting Claude three times with temperature of 1 ================ Response 1: Kestralia Response 2: Keylora Response 3: Xylora
In [18]:
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
messages = [{
"role": "user",
"content": [{"text": "generate a poem that uses all letters of the alphabet"}]
}]
def generate_random_letters_3_times():
for i in range(3):
inference_config={"stopSequences":["b","c"]}
response = bedrock_client.converse(
modelId=model_id,
messages=messages,
inferenceConfig=inference_config,
)
if response['stopReason'] == 'stop_sequence':
print(f"Response {i+1} of {response['output']['message']['content'][0]['text']} stopped")In [19]:
generate_random_letters_3_times()Response 1 of Here's an alpha stopped Response 2 of Here's a poem that uses all letters of the alpha stopped Response 3 of Here's an alpha stopped
In [ ]:
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
messages = [{
"role": "user",
"content": [{"text": "Hey there, how are you?!"}]
}]
inference_config={"maxTokens":100}
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
system=[{"text": "You are a helpful foreign language tutor that always responds in French."}],
messages=messages,
inferenceConfig=inference_config,
)In [ ]:
response{'ResponseMetadata': {'RequestId': '93198670-67d6-4c5b-ae20-2fc75717dd4f',
'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Tue, 12 Nov 2024 22:43:36 GMT',
'content-type': 'application/json',
'content-length': '311',
'connection': 'keep-alive',
'x-amzn-requestid': '93198670-67d6-4c5b-ae20-2fc75717dd4f'},
'RetryAttempts': 0},
'output': {'message': {'role': 'assistant',
'content': [{'text': "Bonjour ! Je vais très bien, merci ! Et vous, comment allez-vous ? \n\n(Hello! I'm doing very well, thank you! And how are you?)"}]}},
'stopReason': 'end_turn',
'usage': {'inputTokens': 29, 'outputTokens': 45, 'totalTokens': 74},
'metrics': {'latencyMs': 1208}}In [20]:
def generate_questions(topic, num_questions=3):
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-5-sonnet-20241022-v2:0"
messages = [{
"role": "user",
"content": [{"text": f"Generate {num_questions} questions about {topic} as a numbered list."}]
}]
inference_config={"maxTokens":100, "stopSequences": [f"{num_questions+1}."]}
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
system=[{"text": f"You are an expert on {topic}. Generate thought-provoking questions about this topic."}],
messages=messages,
inferenceConfig=inference_config,
)
return responseIn [21]:
generate_questions(topic="free will", num_questions=3)Out [21]:
{'ResponseMetadata': {'RequestId': '53bf9fbb-e07c-4de2-a079-db9bfb145760',
'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Thu, 14 Nov 2024 19:23:30 GMT',
'content-type': 'application/json',
'content-length': '694',
'connection': 'keep-alive',
'x-amzn-requestid': '53bf9fbb-e07c-4de2-a079-db9bfb145760'},
'RetryAttempts': 0},
'output': {'message': {'role': 'assistant',
'content': [{'text': '1. If our decisions are influenced by our past experiences, genetics, and environmental factors that we didn\'t choose, to what extent can we claim that our choices are truly "free"?\n\n2. In a world where we can increasingly predict human behavior through data and neuroscience, does this scientific determinism eliminate the possibility of free will, or does it simply describe the mechanisms through which we exercise it?\n\n3. How does the concept of free will align with various religious beliefs about'}]}},
'stopReason': 'max_tokens',
'usage': {'inputTokens': 39, 'outputTokens': 100, 'totalTokens': 139},
'metrics': {'latencyMs': 2817}}
