-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |