-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathresponses-pdfupload-base64-entra-aoai-v1.py
More file actions
42 lines (36 loc) · 1.17 KB
/
responses-pdfupload-base64-entra-aoai-v1.py
File metadata and controls
42 lines (36 loc) · 1.17 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
import base64
from openai import OpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
import os
from dotenv import load_dotenv
load_dotenv()
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = OpenAI(
base_url = os.getenv("AZURE_OPENAI_V1_API_ENDPOINT"),
api_key = token_provider
)
with open("../assets/employee_handbook.pdf", "rb") as f: # assumes PDF is in the assets directory
data = f.read()
base64_string = base64.b64encode(data).decode("utf-8")
response = client.responses.create(
model=os.environ["AZURE_OPENAI_API_MODEL"],
input=[
{
"role": "user",
"content": [
{
"type": "input_file",
"filename": "employee_handbook.pdf",
"file_data": f"data:application/pdf;base64,{base64_string}",
},
{
"type": "input_text",
"text": "What are the company values?",
},
],
},
]
)
print(response.output_text)