In this blog we will walk through a comprehensive example of indexing research papers with extracting different metadata — beyond full text chunking and embedding — and build semantic embeddings for indexing and querying. हम बहुत सराहना करेंगे अगर आप कर सकते हैं यदि आपको यह ट्यूटोरियल उपयोगी लगता है। ⭐ star GitHub पर CocoIndex GitHub पर CocoIndex मामलों का उपयोग अकादमिक खोज और खोज, साथ ही साथ अनुसंधान आधारित एआई एजेंट कागज सिफारिश प्रणाली अनुसंधान ज्ञान ग्राफ वैज्ञानिक साहित्य के सेमेंटिक विश्लेषण हम क्या हासिल करेंगे आइए इस पर एक नज़र डालें उदाहरण के रूप में। पीडीएफ यहां हम क्या हासिल करना चाहते हैं: Extract the paper metadata, including file name, title, author information, abstract, and number of pages. Build vector embeddings for the metadata, such as the title and abstract, for semantic search. यह बेहतर मेटाडेटा-आधारित सेमेंटिक खोज परिणामों को सक्षम बनाता है. उदाहरण के लिए, आप शीर्षकों और अवलोकनों के साथ टेक्स्ट पूछताछ को समायोजित कर सकते हैं। Build an index of authors and all the file names associated with each author to answer questions like "Give me all the papers by Jeff Dean." If you want to perform full PDF embedding for the paper, you can also refer to . this article आप पूरा कोड पा सकते हैं . यहां यदि यह लेख आपके लिए उपयोगी है, तो कृपया हमें एक स्टार दें ⭐ पर हमें बढ़ने में मदद करें। गीता मुख्य घटकों PDF Preprocessing Reads PDFs using and extracts: pypdf Total number of pages First page content (used as a proxy for metadata-rich information) Markdown Conversion Converts the first page to Markdown using . Marker LLM-Powered Metadata Extraction Sends the first-page Markdown to GPT-4o using CocoIndex's function. ExtractByLlm Extracted metadata includes and more. (string) title (with name, email, and affiliation) authors (string) abstract Semantic Embedding The title is embedded directly using the model by the SentenceTransformer. all-MiniLM-L6-v2 Abstracts are chunked based on semantic punctuation and token count, then each chunk is embedded individually. Relational Data Collection Authors are unrolled and collected into an relation, enabling queries like: author_papers Show all papers by X Which co-authors worked with Y? पूर्वानुमान . Install PostgreSQL CocoIndex uses PostgreSQL internally for incremental processing. . Configure your OpenAI API key वैकल्पिक रूप से, हमारे पास जुड़वां, ओलामा, LiteLLM, चेकअप के लिए मूल समर्थन है . गाइड आप अपने पसंदीदा एलएलएम प्रदाता चुन सकते हैं और पूरी तरह से स्थान पर काम कर सकते हैं। Indexing Flow का वर्णन करें यह परियोजना वास्तविक दुनिया के उपयोग मामलों के करीब मेटाडेटा समझ का एक थोड़ा अधिक व्यापक उदाहरण प्रदर्शित करती है। आप देखेंगे कि CocoIndex द्वारा इस डिजाइन को 100 पंक्तियों के भीतर प्राप्त करना कितना आसान है - . कोड आपको बेहतर मार्गदर्शन करने में मदद करने के लिए जो हम चलने जा रहे हैं, यहां एक प्रवाह चार्ट है। PDF में दस्तावेज़ों की एक सूची आयात करें। For each file: Extract the first page of the paper. Convert the first page to Markdown. Extract metadata (title, authors, abstract) from the first page. Split the abstract into chunks, and compute embeddings for each chunk. Export to the following tables in Postgres with PGVector: Metadata (title, authors, abstract) for each paper. Author-to-paper mapping, for author-based query. Embeddings for titles and abstract chunks, for semantic search. चलो कदमों में ज़ूम करते हैं। दस्तावेजों का आयात @cocoindex.flow_def(name="PaperMetadata") def paper_metadata_flow( flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.DataScope ) -> None: data_scope["documents"] = flow_builder.add_source( cocoindex.sources.LocalFile(path="papers", binary=True), refresh_interval=datetime.timedelta(seconds=10), ) निम्नलिखित क्षेत्रों के साथ एक टेबल बनाएगा ( , , flow_builder.add_source filename content हम संदर्भित कर सकते हैं और अधिक विवरण के लिए। दस्तावेज मेटाडेटा निकालना और इकट्ठा करना बुनियादी जानकारी के लिए पहला पृष्ठ निकालें पीडीएफ के पहले पृष्ठ और पृष्ठों की संख्या निकालने के लिए एक कस्टम फ़ंक्शन परिभाषित करें। @dataclasses.dataclass class PaperBasicInfo: num_pages: int first_page: bytes @cocoindex.op.function() def extract_basic_info(content: bytes) -> PaperBasicInfo: """Extract the first pages of a PDF.""" reader = PdfReader(io.BytesIO(content)) output = io.BytesIO() writer = PdfWriter() writer.add_page(reader.pages[0]) writer.write(output) return PaperBasicInfo(num_pages=len(reader.pages), first_page=output.getvalue()) अब इसे अपने प्रवाह में जोड़ें। हम प्रसंस्करण लागत को कम करने के लिए पहली पृष्ठ से मेटाडेटा निकालते हैं, क्योंकि पूरे पीडीएफ बहुत बड़ा हो सकता है। with data_scope["documents"].row() as doc: doc["basic_info"] = doc["content"].transform(extract_basic_info) इस चरण के बाद, आपको प्रत्येक कागज की मूल जानकारी होनी चाहिए। बुनियादी जानकारी हम Marker का उपयोग करके पहली पृष्ठ को Markdown में परिवर्तित करेंगे। वैकल्पिक रूप से, आप आसानी से अपने पसंदीदा पीडीएफ विश्लेषक को जोड़ सकते हैं, जैसे Docling। एक मार्कर कनवर्टर फ़ंक्शन को परिभाषित करें और इसे कैश करें, क्योंकि इसकी प्रारंभिककरण संसाधन-आधारित है। यह सुनिश्चित करता है कि एक ही कनवर्टर संस्करण विभिन्न इनपुट फ़ाइलों के लिए पुनः उपयोग किया जाता है। @cache def get_marker_converter() -> PdfConverter: config_parser = ConfigParser({}) return PdfConverter( create_model_dict(), config=config_parser.generate_config_dict() ) इसे एक कस्टम फ़ंक्शन में प्लग करें। @cocoindex.op.function(gpu=True, cache=True, behavior_version=1) def pdf_to_markdown(content: bytes) -> str: """Convert to Markdown.""" with tempfile.NamedTemporaryFile(delete=True, suffix=".pdf") as temp_file: temp_file.write(content) temp_file.flush() text, _, _ = text_from_rendered(get_marker_converter()(temp_file.name)) return text इसे अपने परिवर्तित करने के लिए भेजें with data_scope["documents"].row() as doc: doc["first_page_md"] = doc["basic_info"]["first_page"].transform( pdf_to_markdown ) इस चरण के बाद, आपको Markdown प्रारूप में प्रत्येक पेपर का पहला पृष्ठ होना चाहिए। LLM के साथ बुनियादी जानकारी निकालना एलएलएम निकासी के लिए एक योजना परिभाषित करें. CocoIndex जटिल और निहित योजनाओं के साथ एलएलएम संरचित निकासी का मूल रूप से समर्थन करता है। यदि आप निहित योजनाओं के बारे में अधिक जानने में रुचि रखते हैं, तो देखें . इस लेख @dataclasses.dataclass class PaperMetadata: """ Metadata for a paper. """ title: str authors: list[Author] abstract: str इसे प्लग करें एक डेटा क्लास परिभाषित होने के साथ, CocoIndex स्वचालित रूप से डेटा क्लास में एलएलएम प्रतिक्रिया का विश्लेषण करेगा। ExtractByLlm doc["metadata"] = doc["first_page_md"].transform( cocoindex.functions.ExtractByLlm( llm_spec=cocoindex.LlmSpec( api_type=cocoindex.LlmApiType.OPENAI, model="gpt-4o" ), output_type=PaperMetadata, instruction="Please extract the metadata from the first page of the paper.", ) ) इस चरण के बाद, आपको प्रत्येक पेपर के मेटाडेटा होना चाहिए। पेपर मेटाडेटा संग्रह paper_metadata = data_scope.add_collector() with data_scope["documents"].row() as doc: # ... process # Collect metadata paper_metadata.collect( filename=doc["filename"], title=doc["metadata"]["title"], authors=doc["metadata"]["authors"], abstract=doc["metadata"]["abstract"], num_pages=doc["basic_info"]["num_pages"], ) बस जो कुछ भी आप की जरूरत है इकट्ठा करें :) संग्रह दो जानकारी author फ़िल्में लेखक फ़िल्में यहां हम एक खोज कार्यक्षमता बनाने के लिए एक अलग तालिका में लेखक → दस्तावेज़ इकट्ठा करना चाहते हैं। लेखक द्वारा एकत्र किया जाता है। author_papers = data_scope.add_collector() with data_scope["documents"].row() as doc: with doc["metadata"]["authors"].row() as author: author_papers.collect( author_name=author["name"], filename=doc["filename"], ) गिनती और इकट्ठा करें शीर्षक doc["title_embedding"] = doc["metadata"]["title"].transform( cocoindex.functions.SentenceTransformerEmbed( model="sentence-transformers/all-MiniLM-L6-v2" ) ) अवलोकन प्रत्येक टुकड़े को टुकड़ों में विभाजित करें, प्रत्येक टुकड़े को एम्बेड करें और उनके एम्बेडमेंट इकट्ठा करें। कभी-कभी अवलोकन बहुत लंबा हो सकता है। doc["abstract_chunks"] = doc["metadata"]["abstract"].transform( cocoindex.functions.SplitRecursively( custom_languages=[ cocoindex.functions.CustomLanguageSpec( language_name="abstract", separators_regex=[r"[.?!]+\s+", r"[:;]\s+", r",\s+", r"\s+"], ) ] ), language="abstract", chunk_size=500, min_chunk_size=200, chunk_overlap=150, ) इस चरण के बाद, आपको प्रत्येक कागज के विशिष्ट टुकड़ों को होना चाहिए। प्रत्येक टुकड़े को फेंक दें और उनके एम्बेड्स को इकट्ठा करें। with doc["abstract_chunks"].row() as chunk: chunk["embedding"] = chunk["text"].transform( cocoindex.functions.SentenceTransformerEmbed( model="sentence-transformers/all-MiniLM-L6-v2" ) ) इस चरण के बाद, आपको प्रत्येक कागज के असाधारण टुकड़ों के इनपुट होना चाहिए। इमारतें इकट्ठा करें metadata_embeddings = data_scope.add_collector() with data_scope["documents"].row() as doc: # ... process # collect title embedding metadata_embeddings.collect( id=cocoindex.GeneratedField.UUID, filename=doc["filename"], location="title", text=doc["metadata"]["title"], embedding=doc["title_embedding"], ) with doc["abstract_chunks"].row() as chunk: # ... process # collect abstract chunks embeddings metadata_embeddings.collect( id=cocoindex.GeneratedField.UUID, filename=doc["filename"], location="abstract", text=chunk["text"], embedding=chunk["embedding"], ) निर्यात अंत में, हम डेटा को Postgres में निर्यात करते हैं। paper_metadata.export( "paper_metadata", cocoindex.targets.Postgres(), primary_key_fields=["filename"], ) author_papers.export( "author_papers", cocoindex.targets.Postgres(), primary_key_fields=["author_name", "filename"], ) metadata_embeddings.export( "metadata_embeddings", cocoindex.targets.Postgres(), primary_key_fields=["id"], vector_indexes=[ cocoindex.VectorIndexDef( field_name="embedding", metric=cocoindex.VectorSimilarityMetric.COSINE_SIMILARITY, ) ], ) इस उदाहरण में हम PGVector का उपयोग embedding stores/ के रूप में करते हैं। CocoIndex के साथ, आप अन्य समर्थित वेक्टर डेटाबेस जैसे Qdrant पर एक लाइन स्विच कर सकते हैं, यह देखें और अधिक विवरण के लिए। गाइड हम इंटरफ़ेस को मानकीकृत करना चाहते हैं और इसे लेगो बनाने की तरह बनाते हैं। CocoInsight में कदम से कदम देखें आप परियोजना के माध्यम से कदम-दर-चरण चल सकते हैं देखिए कॉकआउट बिल्कुल कैसे प्रत्येक क्षेत्र का निर्माण किया जाता है और मंचों के पीछे क्या होता है। चाहते हैं इंडेक्स आप इस अनुभाग को संदर्भित कर सकते हैं बारे में संस्करण Embedded इनबॉक्स के खिलाफ पूछताछ कैसे बनाएं अभी CocoIndex अतिरिक्त पूछताछ इंटरफ़ेस प्रदान नहीं करता है. हम SQL लिख सकते हैं या लक्ष्य भंडारण द्वारा पूछताछ इंजन पर भरोसा कर सकते हैं. कई डेटाबेस ने पहले से ही अपने स्वयं के सर्वोत्तम प्रथाओं के साथ पूछताछ कार्यान्वयनों को अनुकूलित किया है पूछताछ अंतरिक्ष में पूछताछ, रीरेकिंग और अन्य खोज से संबंधित कार्यक्षमता के लिए उत्कृष्ट समाधान हैं। यदि आपको पूछताछ लिखने में मदद की ज़रूरत है, तो कृपया हमसे संपर्क करने के लिए स्वतंत्र महसूस करें . मतभेद हमें समर्थन हम लगातार सुधार कर रहे हैं, और जल्द ही अधिक विशेषताएं और उदाहरण आने वाले हैं। यदि यह लेख आपके लिए उपयोगी है, तो कृपया हमें एक स्टार दें ⭐ पर हमें बढ़ने में मदद करें। गीता पढ़ने के लिए धन्यवाद!