Skip to content

Instantly share code, notes, and snippets.

@nicolasblanco
Created November 11, 2025 17:32
Show Gist options
  • Select an option

  • Save nicolasblanco/fe40e6f289705b998a0f11f60090fb3f to your computer and use it in GitHub Desktop.

Select an option

Save nicolasblanco/fe40e6f289705b998a0f11f60090fb3f to your computer and use it in GitHub Desktop.
defmodule ExtractorInvoice do
@schema %{
type: "object",
properties: %{
invoice_number: %{
type: "string",
description: "The unique identifier of the invoice"
},
invoice_date: %{
type: "string",
format: "date",
description: "The date the invoice was issued (YYYY-MM-DD)"
},
total_with_vat: %{
type: "number",
description: "Total amount including VAT"
},
total_without_vat: %{
type: "number",
description: "Total amount excluding VAT"
},
items: %{
type: "array",
description: "List of line items in the invoice",
items: %{
type: "object",
properties: %{
label: %{
type: "string",
description: "Description of item or service"
},
quantity: %{
type: "number",
description: "Quantity of the item"
},
price: %{
type: "number",
description: "Unit price of the item (without VAT)"
}
},
required: ["label", "quantity", "price"],
additionalProperties: false
}
}
},
required: [
"invoice_number",
"invoice_date",
"total_with_vat",
"total_without_vat",
"items"
],
additionalProperties: false
}
def extract(content) do
{:ok,
%{
choices: [
%{
"message" => %{
"content" => raw
}
}
| _
]
}} =
OpenAI.chat_completion(
[
model: "gpt-5",
messages: [
%{
role: "system",
content: ~s"""
You are the best accountant assistant.
The user is providing the text of the invoice he wants to analyse. Your goal is to extract only the useful data and get the data of the invoice. Concentrate only on extracting the data from the user input.
"""
},
%{
role: "user",
content: content
}
],
response_format: %{
type: "json_schema",
json_schema: %{name: "extracted_data", strict: true, schema: @schema}
},
stream: false
],
%OpenAI.Config{
http_options: [recv_timeout: :timer.seconds(240)]
}
)
Jason.decode(raw)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment