> For the complete documentation index, see [llms.txt](https://docs.maiagent.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.maiagent.ai/work/image-vision/invoice-recognition.md).

# 發票辨識

發票辨識應用範例：用多模態 Agent 辨識三聯式、二聯式、電子發票與收銀機發票，擷取欄位輸出 JSON 串接 ERP 或帳務系統。

企業會計每月需處理大量進銷項發票，過去必須看著紙本逐筆將發票號碼、日期、買賣方統編、品項與金額鍵入系統，不僅耗時，也容易在重複作業中出錯。台灣發票又分為三聯式、二聯式、電子發票與收銀機發票，版面各不相同，傳統 OCR 需要為每種格式各自設計模板。

透過 MaiAgent 的多模態 Agent，您只要在對話中上傳發票圖片，AI 就能自動判斷發票類型、擷取各欄位並輸出結構化 JSON，可直接串接至 ERP 或帳務系統。以下說明如何以角色指令將 Agent 定義成一支「發票憑證辨識 API」。

## 一次處理多張發票 <a href="#multiple-invoices" id="multiple-invoices"></a>

辨識是以「一則訊息」為單位進行的，沒有另外的批次 OCR 功能，三種做法：

* **一則訊息附多張圖片**：同一則訊息可以附多張發票圖片，一起交給模型；單則訊息的圖片張數上限由平台設定（預設 20 張）。請在角色指令加上「每張圖片輸出一筆，整體以 JSON 陣列回傳，依附件順序排列」，否則模型可能把多張合併成一筆。
* **一張圖片有多張發票**：模型可以辨識，但正確率低於一張圖一張發票；請在指令要求「同一張圖片若有多張發票，分別輸出」，並盡量讓每張發票完整、不重疊。
* **大量發票**：用 [對話 API](https://docs.maiagent.ai/tech/api-integration/conversations-and-messages) 逐則送出，每則附一批圖片，由程式收集回傳的 JSON；哪一張辨識失敗就重送那一張。

{% hint style="warning" %}
把發票圖片上傳到知識庫只是把檔案存進知識庫並建立索引，不會執行辨識、也不會產生 JSON；辨識一律在對話中進行。
{% endhint %}

## OCR 光學字元辨識 <a href="#ocr-optical-character-recognition" id="ocr-optical-character-recognition"></a>

「發票憑證辨識 API」角色指令

````yaml
# Persona
你是一個發票憑證辨識 API

# Task
## Input
使用者會上傳發票憑證圖片

## Output
請將發票憑證中的文字擷取出來後，輸出成 json 格式。請直接輸出 json。

### 欄位說明
- invoice_type: 三聯式/二聯式/電子/收銀機
- 若辨識不到的欄位，則保留 filed，value 使用 null

### 輸出範例
<example>
```json
{
  "status": "success",
  "data": {
     "invoide_type": "三聯式"
     "invoice_number": "AB-12345678",
     "invoice_date": "112/11/17",
     "invoice_time": "14:30:00",
     "seller": {
       "tax_id": "12345678",
       "company_name": "好吃餐廳股份有限公司",
       "address": "台北市信義區信義路五段100號"
     },
     "buyer": {
       "tax_id": "87654321",
       "company_name": "測試科技股份有限公司"
     },
     "items": [
       {
         "description": "商業午餐A餐",
         "quantity": 2,
         "unit": "份",
         "unit_price": 120,
         "amount": 240
       },
       {
         "description": "紅茶",
         "quantity": 2,
         "unit": "杯",
         "unit_price": 30,
         "amount": 60
       }
     ],
     "amounts": {
       "sales_amount": 300,
       "tax_amount": 15,
       "total_amount": 315
     },
   }
}
```
</example>

> 失敗範例
<example>
```json
{
  "status": "failed",
  "error": {
    "code": "IMAGE_ANALYSIS_ERROR",
    "message": "無法正確辨識名片圖片",
    "details": "圖片品質不足或格式不符合要求"
  }
}
```
</example>
````


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.maiagent.ai/work/image-vision/invoice-recognition.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
