diff --git a/bedrock/README.md b/bedrock/README.md index 5c46251..3fcacd1 100644 --- a/bedrock/README.md +++ b/bedrock/README.md @@ -8,6 +8,17 @@ $ brew install python@3.9 $ 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 * +* diff --git a/bedrock/gurumi-bot.png b/bedrock/gurumi-bot.png new file mode 100644 index 0000000..b736cfb Binary files /dev/null and b/bedrock/gurumi-bot.png differ diff --git a/bedrock/invoke_claude_3.py b/bedrock/invoke_claude_3.py index c1aa69d..5737106 100644 --- a/bedrock/invoke_claude_3.py +++ b/bedrock/invoke_claude_3.py @@ -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() @@ -37,7 +37,12 @@ def invoke_claude_3(prompt): "messages": [ { "role": "user", - "content": prompt, + "content": [ + { + "type": "text", + "text": prompt, + }, + ], }, ], } @@ -45,6 +50,8 @@ def invoke_claude_3(prompt): if SYSTEM_MESSAGE: body["system"] = SYSTEM_MESSAGE + # print("request: {}".format(body)) + response = bedrock.invoke_model( modelId=model_id, body=json.dumps(body), @@ -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)) diff --git a/bedrock/invoke_claude_3_image.py b/bedrock/invoke_claude_3_image.py new file mode 100644 index 0000000..f202050 --- /dev/null +++ b/bedrock/invoke_claude_3_image.py @@ -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()