-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm_util.py
72 lines (58 loc) · 2.33 KB
/
llm_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import boto3, botocore
import json
def generate_conversation(bedrock_client,
model_id,
system_prompts,
inference_config,
input_image):
print(f"Generating message with model {model_id}")
# 1) Read the image bytes
with open(input_image, "rb") as f:
image_bytes = f.read()
img_extension = input_image.split(".")[-1]
if img_extension == "jpg":
img_extension = "jpeg"
# 2) Create a message(s) with text or image
message = {
"role": "user",
"content": [
{
"image": {
"format": img_extension,
"source": {
"bytes": image_bytes
}
}
}
]
}
# 3) Send the message using Converse API (same exact API for different model vendors + naitive tools support)
response = bedrock_client.converse(
modelId = model_id,
messages = [message,],
system = system_prompts,
inferenceConfig = inference_config,
#additionalModelRequestFields = additional_model_fields
)
return response
def get_ocr(test):
model_id : str = test['model_id']
input_image : str = test['input_image']
instructions_filename : str = test['instructions_filename']
temperature : float = test['temperature']
inference_config = {"temperature": temperature}
instructions = open(f"./prompts/{instructions_filename}", "r").read()
system_prompts = [{"text": instructions}]
bedrock_client = boto3.client('bedrock-runtime',region_name='us-east-1', config = botocore.config.Config(retries=dict(max_attempts=10)))
response = generate_conversation(
bedrock_client, model_id, system_prompts, inference_config, input_image)
output_message = response['output']['message']
print(f"Role: {output_message['role']}")
for content in output_message['content']:
print(f"Text: {content['text']}")
token_usage = response['usage']
print(f"Input tokens: {token_usage['inputTokens']}")
print(f"Output tokens: {token_usage['outputTokens']}")
print(f"Total tokens: {token_usage['totalTokens']}")
print(f"Stop reason: {response['stopReason']}")
return output_message['content'][0]['text']