Closed6

Anthropic Claude Citationsを日本語文書で試してみる

ojapiojapi

サマリ

Anthropic Claude Citations(引用)機能を日本語文書で試してみが、日本語は引用元のチャンク化が恐らく正式にサポートされていると言えなさそうな状況。(間違っていたらコメントください

詳細実験結果はこちら
https://github.com/kentaojapi/trial-claude-citations

公式の説明を読むと、以下の通り引用元は適切な情報量にチャンクされた上で提示されるように読める。

Document contents are “chunked” in order to define the minimum granularity of possible citations. For example, sentence chunking would allow Claude to cite a single sentence or chain together multiple consecutive sentences to cite a paragraph (or longer)!

https://docs.anthropic.com/en/docs/build-with-claude/citations

ojapiojapi

1. Anthropic公式のサンプル

以下、Anthropic公式のExampleで実行。もちろん、英語の文章(短文)。
https://docs.anthropic.com/en/docs/build-with-claude/citations

コード

document_text = """The grass is green. The sky is blue."""
query = "What color is the grass and sky?"

client = AnthropicClient()
client.query_with_document(document_text, query)

出力結果

Based on the documents, the grass is green[^1] and the sky is blue[^2].

[^1]: My Document: "The grass is green."
[^2]: My Document: "The sky is blue."

結果

これは grass と sky のcolor引用元の文章チャンクがそれぞれ提示されていて機能している。

ojapiojapi

2. Anthropic公式の例を日本語にして実行してみる

今度は1.で試した英語の文章を日本語にした上で、同様に正しく引用が出力されるか試してみる。

コード

from claude_client import AnthropicClient

document_text = """草は緑です。空は青いです。"""
query = "草と空の色は何ですか?"

client = AnthropicClient()
client.query_with_document(document_text, query)

出力結果

文書に基づいて回答いたします:
草は緑色で、空は青色です。[^1]

[^1]: My Document: "草は緑です。空は青いです。"

結果

1.では「the grass is green」と「 the sky is blue」にそれぞれ1文ずつ引用元が提示されていたが、今回は回答に対してざっくり1つ引用元が提示されてた形になっている。

ojapiojapi

3. 日本語の契約書で実験

2.より長文となる日本語の契約書で正しく引用が提示されるか実験してみる。

コード

from markitdown import MarkItDown

from claude_client import AnthropicClient

md = MarkItDown()
result = md.convert("./docs/contract.pdf")

document_text = result.text_content
query = "契約期間を教えて"

client = AnthropicClient()
client.query_with_document(document_text, query)

出力結果

契約期間は令和6年4月1日から令和10年3月31日までです[^1]。

[^1]: My Document: "システム運用業務

第2条(契約期間)

 委託期間は、令和6年4月1日から令和10年3月31日までとする

第3条(委託料)

 本契約に基づく乙の委託料は、5,000,000円とする。

 支払は、銀行振込にて実施するものとする。

第4条(成果物の権利帰属)

 委託業務により作成された成果物の、無体財産権及び有体物に関する一切の権利は、甲に帰 属する。

第5条(再委託の制限)

 乙は、甲の書面による事前の承諾を得た場合を除き、本件業務を第三者に再委託してはなら ない。

第6条(権利義務の譲渡)

 甲および乙は、本契約により生ずる権利の全部または一部を、第三者に譲渡または引き受け させてはならない。

第7条(秘密保持)

乙は、甲から秘密とされた本件業務に関する一切の情報を第三者に開示してはならない。
...

結果

契約書全文が引用元として提示されてしまっている。この点から恐らく引用元の日本語のチャンク化はサポートしていないと言えそう。

ojapiojapi

4. 英語の契約書で実験

英語契約書でも実験してみる。

コード

from markitdown import MarkItDown

from claude_client import AnthropicClient

md = MarkItDown()
result = md.convert("./docs/sample.pdf")

document_text = result.text_content
query = "what the condition of early termination?"

client = AnthropicClient()
client.query_with_document(document_text, query)

出力結果

Based on the document, there are three main conditions for early termination of the agreement:

1. By COMMISSION for convenience: The COMMISSION can terminate the agreement for its convenience at any time by giving the CONSULTANT a 30-day written notice. If this happens, the COMMISSION must pay CONSULTANT for allowable costs incurred up to the termination date and those costs determined to be reasonably necessary for termination. After that, CONSULTANT has no further claims against COMMISSION.[^1]
2. By COMMISSION for CONSULTANT's default: The COMMISSION can terminate if:
* A federal or state proceeding for relief of debtors is undertaken by/against CONSULTANT
* CONSULTANT makes an assignment for creditors' benefit
* CONSULTANT breaches any terms or violates provisions and doesn't cure within 10 days of written notice In this case, CONSULTANT is liable for reasonable costs incurred by COMMISSION to reprocure similar services.[^2]
3. By CONSULTANT: CONSULTANT can terminate by giving COMMISSION at least 120 days advance written notice. However, CONSULTANT will be liable for reasonable costs incurred by COMMISSION to reprocure the same or similar services.[^3]

[^1]: My Document: "COMMISSION may terminate this Agreement for its convenience any time, in whole or part, by giving CONSULTANT thirty-day (30-day) written notice thereof. Within thirty days of the COMMISSION's receipt of CONSULTANT's final billing, COMMISSION shall pay CONSULTANT its allowable costs incurred to date of termination and those allowable costs determined by COMMISSION to be reasonably necessary to effect such termination. Thereafter, CONSULTANT shall have no further claims against COMMISSION under this Agreement."
[^2]: My Document: "COMMISSION may terminate this Agreement for CONSULTANT's default if a federal or state proceeding for the relief of debtors is undertaken by or against CONSULTANT, or CONSULTANT's principal, or if CONSULTANT or CONSULTANT's principal makes an assignment for the benefit of creditors, or if CONSULTANT breaches any term(s) or violates any provision(s) of this Agreement and does not cure such breach or violation within ten (10) days after written notice thereof by COMMISSION. CONSULTANT shall be liable for any and all reasonable costs incurred by COMMISSION as a result of such default, including but not limited to reprocurement costs of the same or similar services defaulted by CONSULTANT under this Agreement."
[^3]: My Document: "C. CONSULTANT may terminate this Agreement by giving the COMMISSION at least one hundred and twenty (120) days advance written notice. CONSULTANT shall be liable for any and all reasonable costs incurred by COMMISSION as a result of such default, including but not limited to reprocurement costs of the same or similar services defaulted or not provided by CONSULTANT under this Agreement."

結果

やはり、英語では長文の文章でも引用元が過不足なく適切にチャンクされて提示される。

ojapiojapi

5. Custom Content Documents機能を用いて日本語の契約書チャンクで実験(参考)

Custom Content Documents という機能があり、クライアント側でチャンク化した文章を配列として渡して、引用元として該当のチャンクを提示する機能もCitationsの機能として存在している。

日本語文書でこれを利用してみる。

コード

from claude_client import AnthropicClient

document_chunks = [
    "第2条(契約期間) 委託期間は、令和6年4月1日から令和10年3月31日までとする",
    "第3条(委託料)本契約に基づく乙の委託料は、5,000,000円とする。",
    "第10条(不可抗力免責)天才地変、戦争・内乱・暴動、法令の改廃・制定、公権力による命令・処分、労働争議、輸送機 関・通信回線の事故、為替の大幅な変動など、当事者の責めに帰すことのできない不可抗力に よる契約の全部または一部の履行遅滞、履行不能または不完全履行については、当該当事者 は責任を負わない。",
    "第11条(管轄裁判所) 本契約に関し裁判上の紛争が生じたときには、甲の所在地を管轄する簡易裁判所または地方 裁判所を管轄裁判所とする。"
]
query = "委託料はいくらですか?"

client = AnthropicClient()
client.query_with_multiple_chunks(document_chunks, query)

出力結果

本契約に基づく委託料は5,000,000円です。[^1]

[^1]: My Document: "第3条(委託料)本契約に基づく乙の委託料は、5,000,000円とする。"

結果

日本語文書の引用でもCustom Content Documentsを利用して、引用文書のチャンクを配列で渡すことによって、引用元として適切なチャンクを提示してくれることが分かった。

ただし、やっぱりクライアント側で事前にチャンクを作成しないといけないのが面倒であり、日本語の文章も自動でチャンクされてほしい。

このスクラップは14日前にクローズされました