Generasi Retrieval-Augmented (RAG) meningkatkan model AI dengan mengintegrasikan pengetahuan luaran. Walau bagaimanapun, kain tradisional sering menyebarkan dokumen, kehilangan konteks penting dan memberi kesan kepada ketepatan.
Pengambilan semula kontekstual Anthropic menangani ini dengan menambah penjelasan yang kaya dengan konteks untuk setiap dokumen sebelum membenamkan. Ini mengurangkan kesilapan pengambilan semula, yang membawa kepada peningkatan prestasi tugas hiliran. Artikel ini butiran pengambilan konteks dan pelaksanaannya.
Leverage Langchain dan RAG untuk mengintegrasikan data luaran dengan LLMS.
Kaedah kain tradisional membahagikan dokumen ke dalam ketulan yang lebih kecil untuk mendapatkan semula yang lebih mudah, tetapi ini dapat menghapuskan konteks penting. Sebagai contoh, sebahagian besar mungkin menyatakan "lebih daripada 3.85 juta penduduk menjadikannya bandar yang paling ramai penduduk Eropah" tanpa menentukan bandar. Kekurangan konteks ini menghalang ketepatan.
Pengambilan kontekstual menyelesaikan ini dengan mempersiapkan ringkasan khusus konteks untuk setiap bahagian sebelum membenamkan. Contoh sebelumnya akan menjadi:
<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>
Melaksanakan pengambilan semula kontekstual
Langkah 1: Penciptaan Chunk
<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>
Bahagikan dokumen ke dalam ketulan yang lebih kecil dan bebas (di sini, ayat):
Langkah 2: Definisi templat prompt
<code># 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." ]</code>
Tentukan prompt untuk penjanaan konteks (templat antropik digunakan):
Langkah 3: LLM Inisialisasi
<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>
Pilih LLM (Openai's GPT-4O digunakan di sini):
Langkah 4: Penciptaan Rantai
<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>
Sambungkan prompt dan llm:
Langkah 5: Pemprosesan Chunk
<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>
menjana konteks untuk setiap bahagian:
(output ditunjukkan dalam contoh asal)
<code># Process each chunk and generate contextual information for test_chunk in test_chunks: res = contextual_chunk_creation.invoke({ "WHOLE_DOCUMENT": input_text, "CHUNK_CONTENT": test_chunk }) print(res) print('-----')</code>
Reranking lagi Menapis pengambilan semula dengan mengutamakan ketulan yang paling relevan. Ini meningkatkan ketepatan dan mengurangkan kos. Dalam ujian antropik, pengalihan semula menurun kesilapan pengambilan dari 5.7% hingga 1.9%, peningkatan 67%.
Pertimbangan tambahan
untuk pangkalan pengetahuan yang lebih kecil (& lt; 200,000 token), termasuk keseluruhan pengetahuan asas secara langsung dalam prompt mungkin lebih efisien daripada menggunakan sistem pengambilan semula. Juga, menggunakan caching segera (tersedia dengan Claude) dapat mengurangkan kos dan meningkatkan masa tindak balas.
Pengambilan kontekstual Anthropic menawarkan kaedah yang mudah namun berkuasa untuk memperbaiki sistem RAG. Gabungan embeddings kontekstual, BM25, dan pengalihan semula meningkatkan ketepatannya. Penjelajahan lanjut mengenai teknik pengambilan lain adalah disyorkan.
Atas ialah kandungan terperinci Pengambilan Kontekstual Antropik '. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!