-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnova_micro_test.py
More file actions
61 lines (49 loc) · 1.69 KB
/
nova_micro_test.py
File metadata and controls
61 lines (49 loc) · 1.69 KB
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
import boto3
import json
client = boto3.client("bedrock-runtime")
MODEL_ID = "us.amazon.nova-micro-v1:0"
system = [{"text": "You are an AI assistant that provides only JSON formatted responses. Do not include any extra text, just return the JSON object."}]
def process_document(text_content):
messages = [
{
"role": "user",
"content": [
{"text": text_content},
{"text": "Provide only the following information in JSON format: Summary, Title, and Document Type."}
],
}
]
inf_params = {
"maxTokens": 200,
"topP": 0.2,
"topK": 20,
"temperature": 0.5
}
body = json.dumps({
"schemaVersion": "messages-v1",
"messages": messages,
"system": system,
"inferenceConfig": inf_params,
})
try:
response = client.invoke_model(
modelId=MODEL_ID,
body=body
)
# Parse the response
model_response = json.loads(response["body"].read())
outputs = model_response.get("output").get("message").get("content")[0].get("text")
print("outputs:", outputs)
try:
parsed_json = json.loads(outputs)
return parsed_json
except json.JSONDecodeError:
print("Error parsing JSON response:", outputs)
return None
except Exception as e:
print("Error:", e)
return "Error retrieving summary", "Error retrieving title", "Error retrieving document type"
# Example usage with extracted text
text_content = "This is a sample document about financial reports and Q1 earnings."
result = process_document(text_content)
print(result)