Skip to content

Commit

Permalink
add more sample
Browse files Browse the repository at this point in the history
  • Loading branch information
nalbam committed Jun 5, 2024
1 parent 6dca332 commit 5d4fc89
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
11 changes: 11 additions & 0 deletions bedrock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ $ brew install [email protected]
$ python -m pip install --upgrade -r requirements.txt
```

## Test

```bash
python invoke_claude_3.py -p "구름이가 누구?"

python invoke_claude_3_image.py

python invoke_stable_diffusion.py -p "Create an image of a cat walking on a fully frozen river surface on a cold winter day."
```

## References

* <https://docs.aws.amazon.com/ko_kr/code-library/latest/ug/python_3_bedrock-runtime_code_examples.html>
* <https://docs.aws.amazon.com/ko_kr/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html>
Binary file added bedrock/gurumi-bot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 12 additions & 7 deletions bedrock/invoke_claude_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def parse_args():
p = argparse.ArgumentParser(description="invoke_claude_3")
p.add_argument("-p", "--prompt", default="Hello", help="prompt", required=True)
p.add_argument("-p", "--prompt", default="안녕", help="prompt")
p.add_argument("-d", "--debug", default="False", help="debug")
return p.parse_args()

Expand All @@ -37,14 +37,21 @@ def invoke_claude_3(prompt):
"messages": [
{
"role": "user",
"content": prompt,
"content": [
{
"type": "text",
"text": prompt,
},
],
},
],
}

if SYSTEM_MESSAGE:
body["system"] = SYSTEM_MESSAGE

# print("request: {}".format(body))

response = bedrock.invoke_model(
modelId=model_id,
body=json.dumps(body),
Expand All @@ -53,15 +60,13 @@ def invoke_claude_3(prompt):
# Process and print the response
result = json.loads(response.get("body").read())

output_list = result.get("content", [])
# print("response: {}".format(result))

print(f"- The model returned {len(output_list)} response(s):")
content = result.get("content", [])

for output in output_list:
for output in content:
print(output["text"])

return result

except Exception as e:
print("Error: {}".format(e))

Expand Down
96 changes: 96 additions & 0 deletions bedrock/invoke_claude_3_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import json
import boto3
import base64

SYSTEM_MESSAGE = "너는 구름이(Gurumi) 야. 구름이는 한국어로 구름을 친숙하게 부르는 표현이야. AWSKRUG(AWS Korea User Group)의 마스코트지."


def parse_args():
p = argparse.ArgumentParser(description="invoke_claude_3")
p.add_argument("-p", "--prompt", default="사진 설명 해줘", help="prompt")
p.add_argument("-d", "--debug", default="False", help="debug")
return p.parse_args()


def invoke_claude_3(prompt):
"""
Invokes Anthropic Claude 3 Sonnet to run an inference using the input
provided in the request body.
:param prompt: The prompt that you want Claude 3 to complete.
:return: Inference response from the model.
"""

# Initialize the Amazon Bedrock runtime client
bedrock = boto3.client(service_name="bedrock-runtime", region_name="us-east-1")

# Invoke Claude 3 with the text prompt
model_id = "anthropic.claude-3-sonnet-20240229-v1:0"

image = "./gurumi-bot.png"

# Read reference image from file and encode as base64 strings.
with open(image, "rb") as file:
encoded_image = base64.b64encode(file.read()).decode("utf8")

try:
body = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt,
},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": encoded_image,
},
},
],
},
],
}

if SYSTEM_MESSAGE:
body["system"] = SYSTEM_MESSAGE

# print("request: {}".format(body))

response = bedrock.invoke_model(
modelId=model_id,
body=json.dumps(body),
)

# Process and print the response
result = json.loads(response.get("body").read())

# print("response: {}".format(result))

content = result.get("content", [])

for output in content:
print(output["text"])

except Exception as e:
print("Error: {}".format(e))


def main():
args = parse_args()

invoke_claude_3(args.prompt)


if __name__ == "__main__":
main()

0 comments on commit 5d4fc89

Please sign in to comment.