@Generable
struct ReceiptInferenceResponse {
@Guide(description: "Merchant/store name.")
let shopName: String?
@Guide(description: "Purchase (transaction) date normalized
to ISO 8601 `YYYY-MM-DD`.")
let date: String?
@Guide(description: "Grand total amount paid in JPY (tax-
inclusive), as a non-negative integer.")
let amount: Int?
@Guide(description: "Category for this receipt.")
let category: Category?
}
Slide 82
Slide 82 text
final class ReceiptStructureInferrer {
private let session: LanguageModelSession
init() {
self.session = LanguageModelSession(
instructions: Instructions {
"""
…
"""
}
)
}
}
Slide 83
Slide 83 text
Instructions {
"""
You are an information extractor for retail receipts.
Carefully read through the input text in Japanese, scanned
from a receipt.
Line breaks correspond to each line of the receipt from top
to bottom.
Extract receipt information and return it in the provided
schema format
Each category definition for a receipt is following:
- groceries: foodstuff, groceries
- diningOut: payments related to restaurants, bars, and
other dining expenses
- householdSupplies: daily necessities, excluding food items
…
"""
}
Slide 84
Slide 84 text
final class ReceiptStructureInferrer {
…
func infer(receiptTextLines: [String]) async throws ->
ReceiptInferenceResponse {
let prompt = Prompt { … }
let response = try await session.respond(
to: prompt,
generating: ReceiptInferenceResponse.self,
options: GenerationOptions(temperature: 0.1)
)
return response.content
}
}
Slide 85
Slide 85 text
Prompt {
"""
## Task
Extract fields from the following OCR lines of a Japanese
retail receipt.
## Input
\(receiptTextLines.joined(separator: "\n"))
## Requirements
- The output format must be valid.
- Ensure that the output is complete and not truncated.
- If any information is missing or unclear, use "nil" for
that field.
"""
}
Slide 86
Slide 86 text
ಈ͔ͯ͠ΈΔ📸
Slide 87
Slide 87 text
No content
Slide 88
Slide 88 text
No content
Slide 89
Slide 89 text
No content
Slide 90
Slide 90 text
'PVOEBUJPO.PEFMTͷՄೳੑ
w ళ໊ɾͷநग़݁ߏ͏·͍͘͘
w ֹۚͷநग़ɺΧςΰϦͷਪपΓͰগ͠՝͋Γ
w ͜Ε͕ແྉ͔ͭMPDBM݁Ͱମݧͱͯ͠ఏڙͰ͖Δͷ͍͢͝
'PVOEBUJPO.PEFMTͷධՁ
Slide 91
Slide 91 text
·ͱΊ
Slide 92
Slide 92 text
·ͱΊ
w "*ϨγʔτಡΈऔΓػೳ📸
w --.ͷlߏԽzͱ͍͏ڧΈΛ׆͔ͯ҆͠Ձʹߴ͍ਫ਼Λ࣮ݱͨ͠
w (BSCBHFJO
HBSCBHFPVU🔥
w ΠϯϓοτΛ͍͔ʹ៉ྷʹ͢Δ͔͕--.ͷਪͷਫ਼ʹ݁͢Δ
w 'PVOEBUJPO.PEFMT🧠
w -PDBM--.ͷϙςϯγϟϧΛײͤͨ͡͞