forked from Azure-Samples/azure-openai-entity-extraction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_pdf_receipt.py
71 lines (60 loc) · 2.15 KB
/
extract_pdf_receipt.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
import logging
import os
import azure.identity
import openai
import pymupdf4llm
from dotenv import load_dotenv
from pydantic import BaseModel
from rich import print
logging.basicConfig(level=logging.WARNING)
load_dotenv()
if os.getenv("OPENAI_HOST", "azure") == "azure":
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(
credential, "https://cognitiveservices.azure.com/.default"
)
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
else:
if not os.getenv("GITHUB_TOKEN"):
logging.warning("GITHUB_TOKEN env variable is empty. See README.")
exit(1)
client = openai.OpenAI(
base_url="https://models.inference.ai.azure.com",
api_key=os.environ["GITHUB_TOKEN"],
# Specify the API version to use the Structured Outputs feature
default_query={"api-version": "2024-08-01-preview"},
)
model_name = "gpt-4o"
# Define models for Structured Outputs
class Item(BaseModel):
product: str
price: float
quantity: int
class Receipt(BaseModel):
total: float
shipping: float
payment_method: str
items: list[Item]
order_number: int
# Prepare PDF as markdown text
md_text = pymupdf4llm.to_markdown("example_receipt.pdf")
# Send request to GPT model to extract using Structured Outputs
completion = client.beta.chat.completions.parse(
model=model_name,
messages=[
{"role": "system", "content": "Extract the information the receipt"},
{"role": "user", "content": md_text},
],
response_format=Receipt,
)
output = completion.choices[0].message.parsed
receipt = Receipt.model_validate(output)
print(receipt)