-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.py
More file actions
104 lines (92 loc) · 3.82 KB
/
request.py
File metadata and controls
104 lines (92 loc) · 3.82 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import boto3
import json
import base64
# Initialiser le client Bedrock d'AWS
client = boto3.client('bedrock', region_name='us-east-1') # Remplacez par votre région AWS
# Fonction pour charger un fichier texte
def load_text_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
# Fonction pour charger une image et la convertir en base64
def load_image_file(file_path):
with open(file_path, 'rb') as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Fonction pour appeler l'API Claude Opus avec un fichier joint
def analyze_with_file(file_path, file_type="text"):
# Définir le corps de la requête
if file_type == "text":
# Charger le fichier texte
file_content = load_text_file(file_path)
# Construire la requête pour un fichier texte
request_body = {
"modelId": "anthropic.claude-3-opus-20240229-v1:0",
"contentType": "application/json",
"accept": "application/json",
"body": {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": file_content
}
]
}
]
}
}
elif file_type == "image":
# Charger l'image et la convertir en base64
file_content = load_image_file(file_path)
# Construire la requête pour un fichier image
request_body = {
"modelId": "anthropic.claude-3-opus-20240229-v1:0",
"contentType": "application/json",
"accept": "application/json",
"body": {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg", # Remplacez par le bon type MIME si nécessaire
"data": file_content
}
},
{
"type": "text",
"text": "What is in this image?"
}
]
}
]
}
}
# Appel à l'API Bedrock pour invoquer le modèle
response = client.invoke_model(
modelId=request_body["modelId"],
body=json.dumps(request_body["body"]),
contentType=request_body["contentType"],
accept=request_body["accept"]
)
# Récupérer la réponse de l'API
response_payload = json.loads(response['body'].read().decode('utf-8'))
# Afficher la réponse structurée (pour les informations importantes)
print("Réponse de l'API :")
print(response_payload)
# Exemple de traitement pour lister les informations importantes (extraction basique)
if 'completion' in response_payload:
print("\nInformations extraites du fichier :")
print(response_payload['completion'])
# Exemple d'utilisation
file_path = "bail.txt" # Remplacez par le chemin de votre fichier texte ou image
file_type = "text" # Changez en "image" si le fichier est une image
analyze_with_file(file_path, file_type)