読み方のヒント

矢印ボタン(または ← → キー)でページを移動できます。

第1節 PDFと文書を渡す

1.1 文書のブロック

Messages APIでは、user の発言の content に、文のほか、文書のブロックを並べられます。PDFは、base64の文字にして入れます。

def pdf_block(path, title):
    with open(path, "rb") as f:
        data = base64.standard_b64encode(f.read()).decode()
    return {
        "type": "document",
        "source": {"type": "base64", "media_type": "application/pdf", "data": data},
        "title": title,
        "citations": {"enabled": True},
    }
項目意味
type: "document"文書のブロック
source中身。PDFは base64、文の文書は text、上げたファイルは file
title文書の名前。引用のときに、どの文書かを示すのに使われる
citations引用の機能を使うかどうか(2節)

PDFは、文字だけでなく、表や図の見た目も読み取られます。

2 / 20