ホームページ > テクノロジー周辺機器 > AI > 人類のコンテキスト検索:実装のガイド

人類のコンテキスト検索:実装のガイド

William Shakespeare
リリース: 2025-03-02 09:34:12
オリジナル
449 人が閲覧しました

検索された生成(RAG)は、外部知識を統合することによりAIモデルを強化します。ただし、従来のぼろきれはドキュメントを断片化し、重要なコンテキストを失い、精度に影響を与えることがよくあります。

Anthropicのコンテキスト検索は、埋め込む前に各ドキュメントチャンクに簡潔でコンテキストが豊富な説明を追加することにより、これに対処します。 これにより、検索エラーが大幅に減少し、ダウンストリームタスクのパフォーマンスが向上します。 この記事では、コンテキストの検索とその実装について詳しく説明しています

langchain

でrag

LangchainとRagを活用して、外部データをLLMSと統合します。

コンテキスト検索で説明されています

従来のRAGメソッドは、ドキュメントをより小さなチャンクに分割して、簡単に検索しますが、これにより本質的なコンテキストが排除されます。たとえば、チャンクは、「385万人以上の住民が欧州連合で最も人口の多い都市になっている」と述べているかもしれません。 このコンテキストの欠如は、正確性を妨げます。

コンテキスト検索は、埋め込む前に各チャンクにコンテキスト固有の短い要約を準備することにより、これを解決します。 前の例は次のとおりです

多様なデータセット(コードベース、科学論文、フィクション)にわたる人類の内部テストは、コンテキストの検索が、コンテキスト埋め込みモデルとコンテキストBM25とペアになった場合、検索エラーを最大49%削減することを示しています。
<code>contextualized_chunk = """Berlin is the capital and largest city of Germany, known for being the EU's most populous city within its limits.
Its more than 3.85 million inhabitants make it the European Union's most populous city, as measured by population within city limits.
"""</code>
ログイン後にコピー

Anthropic's Contextual Retrieval: A Guide With Implementation コンテキスト検索の実装

このセクションでは、サンプルドキュメントを使用してステップバイステップの実装の概要を説明します。

Anthropic's Contextual Retrieval: A Guide With Implementation ステップ1:Chunk Creation ドキュメントをより小さく独立したチャンク(ここ、文)に分割します:

ステップ2:プロンプトテンプレートの定義
<code># Input text for the knowledge base
input_text = """Berlin is the capital and largest city of Germany, both by area and by population.
Its more than 3.85 million inhabitants make it the European Union's most populous city, as measured by population within city limits.
The city is also one of the states of Germany and is the third smallest state in the country in terms of area.
Paris is the capital and most populous city of France.
It is situated along the Seine River in the north-central part of the country.
The city has a population of over 2.1 million residents within its administrative limits, making it one of Europe's major population centers."""</code>
ログイン後にコピー

コンテキスト生成のプロンプトを定義します(Anthropicのテンプレートが使用されています):

ステップ3:LLM初期化# Splitting the input text into smaller chunks test_chunks = [ 'Berlin is the capital and largest city of Germany, both by area and by population.', "\n\nIts more than 3.85 million inhabitants make it the European Union's most populous city, as measured by population within city limits.", '\n\nThe city is also one of the states of Germany and is the third smallest state in the country in terms of area.', '\n\n# Paris is the capital and most populous city of France.', '\n\n# It is situated along the Seine River in the north-central part of the country.', "\n\n# The city has a population of over 2.1 million residents within its administrative limits, making it one of Europe's major population centers." ]LLMを選択します(OpenaiのGPT-4Oはここで使用されています):

ステップ4:チェーン作成

<code>from langchain.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate

# Define the prompt for generating contextual information
anthropic_contextual_retrieval_system_prompt = """<document>
{WHOLE_DOCUMENT}
</document>
Here is the chunk we want to situate within the whole document
<chunk>
{CHUNK_CONTENT}
</chunk>
Please give a short succinct context to situate this chunk within the overall document for the purposes of improving search retrieval of the chunk. Answer only with the succinct context and nothing else."""

# ... (rest of the prompt template code remains the same)</code>
ログイン後にコピー
プロンプトとLLMを接続します:

ステップ5:チャンク処理

各チャンクのコンテキストを生成します:
<code>import os
from langchain_openai import ChatOpenAI

# Load environment variables
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

# Initialize the model instance
llm_model_instance = ChatOpenAI(
    model="gpt-4o",
)</code>
ログイン後にコピー

(出力は元の例に示されています) 精度を向上させるための

再ランキング

再ランキングは、最も関連性の高いチャンクに優先順位を付けることにより、検索をさらに改善します。 これにより、精度が向上し、コストが削減されます。人類のテストでは、再ランキングが5.7%から1.9%に減少し、67%の改善。
<code>from langchain.output_parsers import StrOutputParser

# Chain the prompt with the model instance
contextual_chunk_creation = anthropic_contextual_retrieval_final_prompt | llm_model_instance | StrOutputParser()</code>
ログイン後にコピー

追加の考慮事項

より小さな知識ベース(&lt; 200,000トークン)の場合、プロンプト内の知識ベース全体を直接含めることは、検索システムを使用するよりも効率的かもしれません。 また、迅速なキャッシュ(クロードで利用可能)を使用すると、コストを大幅に削減し、応答時間を改善できます。

結論

Anthropicのコンテキスト検索は、RAGシステムを改善するための簡単で強力な方法を提供します。 コンテキスト埋め込み、BM25、および再ランキングの組み合わせにより、精度が大幅に向上します。 他の検索技術のさらなる調査が推奨されます

以上が人類のコンテキスト検索:実装のガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート