### Install RAPTOR and Dependencies Source: https://github.com/parthsarthi03/raptor/blob/master/README.md Clones the RAPTOR repository and installs necessary Python packages using pip. Requires Git and Python 3.8+. ```bash git clone https://github.com/parthsarthi03/raptor.git cd raptor pip install -r requirements.txt ``` -------------------------------- ### Perform and Print a Specific Question Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb This snippet shows a concrete example of querying the RAPTOR tree with a specific question about Cinderella's story and printing the obtained answer. It utilizes the `answer_question` method. ```python question = "How did Cinderella reach her happy ending ?" answer = RA.answer_question(question=question) print("Answer: ", answer) ``` -------------------------------- ### Implement Custom Summarization Model with Claude Source: https://context7.com/parthsarthi03/raptor/llms.txt Provides an example of creating a custom summarization model by extending the `BaseSummarizationModel` class. This specific example integrates with Anthropic's Claude API to perform summarization, demonstrating flexibility in choosing summarization backends. ```python from raptor import BaseSummarizationModel, RetrievalAugmentation, RetrievalAugmentationConfig import anthropic class ClaudeSummarizationModel(BaseSummarizationModel): def __init__(self, model="claude-3-sonnet-20240229"): self.model = model self.client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"]) def summarize(self, context, max_tokens=150): try: message = self.client.messages.create( model=self.model, max_tokens=max_tokens, messages=[{ "role": "user", "content": f"Summarize the following text, including key details: {context}" }] ) return message.content[0].text except Exception as e: print(f"Error: {e}") return "" # Use custom model in configuration custom_summarizer = ClaudeSummarizationModel() config = RetrievalAugmentationConfig(summarization_model=custom_summarizer) RA = RetrievalAugmentation(config=config) RA.add_documents(text) ``` -------------------------------- ### Python: Configure Cluster Tree Builder and Retriever in Raptor Source: https://context7.com/parthsarthi03/raptor/llms.txt This example shows how to fine-tune the clustering algorithm and tree construction parameters for Raptor. It involves configuring `ClusterTreeConfig` for building the tree and `TreeRetrieverConfig` for retrieving information from it. Parameters like `max_tokens`, `num_layers`, `threshold`, and `top_k` can be adjusted. ```python from raptor import ClusterTreeBuilder, ClusterTreeConfig, TreeRetriever, TreeRetrieverConfig # Advanced clustering configuration cluster_config = ClusterTreeConfig( max_tokens=100, # Chunk size num_layers=5, # Tree depth threshold=0.5, # Node similarity threshold top_k=5, # Nodes per cluster selection_mode="top_k", # "top_k" or "threshold" summarization_length=100, # Summary token length reduction_dimension=10, # UMAP dimension for clustering clustering_algorithm=None, # Use default RAPTOR clustering clustering_params={} ) # Build tree manually tree_builder = ClusterTreeBuilder(cluster_config) tree = tree_builder.build_from_text(text=text, use_multithreading=True) # Configure retriever separately retriever_config = TreeRetrieverConfig( threshold=0.5, top_k=10, selection_mode="top_k", context_embedding_model="OpenAI", num_layers=None, # Traverse all layers start_layer=None # Start from root ) retriever = TreeRetriever(retriever_config, tree) context = retriever.retrieve( query="What are the main conclusions?", start_layer=tree.num_layers, # Start from root layer num_layers=tree.num_layers + 1, # Traverse to leaves top_k=15, max_tokens=4000, collapse_tree=False # Hierarchical traversal ) ``` -------------------------------- ### Batch Question Answering with Raptor Source: https://context7.com/parthsarthi03/raptor/llms.txt Shows how to efficiently process multiple questions using a single tree structure. It includes adding documents, defining a list of questions, iterating through them to get answers and layer information, and analyzing retrieval patterns. ```python from raptor import RetrievalAugmentation RA = RetrievalAugmentation() with open('research_paper.txt', 'r') as f: RA.add_documents(f.read()) # Batch questions questions = [ "What is the research methodology?", "What are the main findings?", "What are the limitations?", "What future work is suggested?" ] results = [] for question in questions: answer, layer_info = RA.answer_question( question=question, top_k=10, max_tokens=3500, return_layer_information=True ) results.append({ "question": question, "answer": answer, "layers_used": [info["layer_number"] for info in layer_info], "nodes_used": [info["node_index"] for info in layer_info] }) # Analyze retrieval patterns for result in results: print(f"\nQ: {result['question']}") print(f"A: {result['answer']}") print(f"Layers accessed: {set(result['layers_used'])}") ``` -------------------------------- ### Python: Use Custom Multilingual Embedding Model with Retrieval Augmentation Source: https://context7.com/parthsarthi03/raptor/llms.txt This example shows how to integrate a custom multilingual embedding model with Raptor's RetrievalAugmentation. It utilizes the 'sentence-transformers' library for embeddings. The MultilingualEmbeddingModel class encodes text into embeddings. This setup allows for processing documents in various languages. ```python from raptor import BaseEmbeddingModel, RetrievalAugmentation, RetrievalAugmentationConfig from sentence_transformers import SentenceTransformer import numpy as np class MultilingualEmbeddingModel(BaseEmbeddingModel): def __init__(self, model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"): self.model = SentenceTransformer(model_name) def create_embedding(self, text): # Returns numpy array or list of floats embedding = self.model.encode(text) return embedding.tolist() if isinstance(embedding, np.ndarray) else embedding # Configure with custom embedding model multilingual_emb = MultilingualEmbeddingModel() config = RetrievalAugmentationConfig(embedding_model=multilingual_emb) RA = RetrievalAugmentation(config=config) # Now works with multilingual documents RA.add_documents("Ceci est un document en français...") answer = RA.answer_question(question="Quel est le sujet principal?") ``` -------------------------------- ### Python: Access Tree Structure with Raptor Source: https://context7.com/parthsarthi03/raptor/llms.txt This snippet shows the basic setup for accessing and potentially manipulating the tree structure within Raptor. It initializes RetrievalAugmentation and adds documents, which is a prerequisite for interacting with the underlying tree. ```python from raptor import RetrievalAugmentation import pickle RA = RetrievalAugmentation() RA.add_documents(text) ``` -------------------------------- ### Initialize RAPTOR and Set API Key Source: https://github.com/parthsarthi03/raptor/blob/master/README.md Sets the OpenAI API key as an environment variable and initializes the RetrievalAugmentation class with default configurations. Requires the 'openai' library. ```python import os os.environ["OPENAI_API_KEY"] = "your-openai-api-key" from raptor import RetrievalAugmentation # Initialize with default configuration. For advanced configurations, check the documentation. [WIP] RA = RetrievalAugmentation() ``` -------------------------------- ### Initialize and Use RetrievalAugmentation Pipeline Source: https://context7.com/parthsarthi03/raptor/llms.txt Demonstrates the basic usage of the RetrievalAugmentation class to add documents, answer questions, and save/load the generated tree structure. It requires setting the OPENAI_API_KEY environment variable. ```python import os os.environ["OPENAI_API_KEY"] = "sk-your-key-here" from raptor import RetrievalAugmentation # Initialize with default configuration (OpenAI embeddings and GPT-3.5-turbo) RA = RetrievalAugmentation() # Add documents to build the tree structure with open('sample.txt', 'r') as file: text = file.read() RA.add_documents(text) # Answer questions using the tree question = "What are the main themes of the document?" answer = RA.answer_question(question=question) print(f"Answer: {answer}") # Save the tree for later use RA.save("path/to/saved_tree.pkl") # Load a previously saved tree RA_loaded = RetrievalAugmentation(tree="path/to/saved_tree.pkl") answer = RA_loaded.answer_question(question=question) ``` -------------------------------- ### Configure Raptor with Local Models Source: https://context7.com/parthsarthi03/raptor/llms.txt Shows how to configure the RetrievalAugmentation model using local embedding and QA models, avoiding the need for API keys for these components. Note that OpenAI might still be needed for summarization if not using a local alternative. ```python from raptor import ( SBertEmbeddingModel, UnifiedQAModel, GPT3TurboSummarizationModel, RetrievalAugmentationConfig, RetrievalAugmentation ) # Option 2: Local models (no API key required for embedding/QA) local_config = RetrievalAugmentationConfig( embedding_model=SBertEmbeddingModel(model_name="sentence-transformers/multi-qa-mpnet-base-cos-v1"), qa_model=UnifiedQAModel(model_name="allenai/unifiedqa-v2-t5-3b-1363200"), summarization_model=GPT3TurboSummarizationModel() # Still needs OpenAI for summarization if not using a local summarizer ) # Use the configuration RA = RetrievalAugmentation(config=local_config) # RA.add_documents(text) # Assuming 'text' is defined elsewhere ``` -------------------------------- ### Core API: RetrievalAugmentation Source: https://context7.com/parthsarthi03/raptor/llms.txt Initialize and manage the complete RAPTOR pipeline with document indexing and question answering. This includes adding documents, answering questions, and saving/loading the constructed tree. ```APIDOC ## Core API: RetrievalAugmentation ### Description Initialize and manage the complete RAPTOR pipeline with document indexing and question answering. ### Method Not Applicable (Class Initialization and Methods) ### Endpoint Not Applicable (Library Usage) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import os os.environ["OPENAI_API_KEY"] = "sk-your-key-here" from raptor import RetrievalAugmentation # Initialize with default configuration (OpenAI embeddings and GPT-3.5-turbo) RA = RetrievalAugmentation() # Add documents to build the tree structure with open('sample.txt', 'r') as file: text = file.read() RA.add_documents(text) # Answer questions using the tree question = "What are the main themes of the document?" answer = RA.answer_question(question=question) print(f"Answer: {answer}") # Save the tree for later use RA.save("path/to/saved_tree.pkl") # Load a previously saved tree RA_loaded = RetrievalAugmentation(tree="path/to/saved_tree.pkl") answer = RA_loaded.answer_question(question=question) ``` ### Response #### Success Response (200) - **answer** (str) - The answer to the question. - **layer_info** (dict) - Information about the layers used in retrieval (optional). #### Response Example ```json { "answer": "The main themes of the document are X, Y, and Z.", "layer_info": { ... } } ``` ``` -------------------------------- ### Configure Raptor with OpenAI Models Source: https://context7.com/parthsarthi03/raptor/llms.txt Demonstrates how to configure the RetrievalAugmentation model using OpenAI's embedding and QA models. Requires the OPENAI_API_KEY environment variable to be set. ```python from raptor import ( OpenAIEmbeddingModel, GPT4QAModel, GPT3TurboSummarizationModel, RetrievalAugmentationConfig, RetrievalAugmentation ) # Option 1: OpenAI models (requires OPENAI_API_KEY) openai_config = RetrievalAugmentationConfig( embedding_model=OpenAIEmbeddingModel(model="text-embedding-ada-002"), qa_model=GPT4QAModel(model="gpt-4"), summarization_model=GPT3TurboSummarizationModel(model="gpt-3.5-turbo") ) # Use the configuration RA = RetrievalAugmentation(config=openai_config) # RA.add_documents(text) # Assuming 'text' is defined elsewhere ``` -------------------------------- ### Load and Display Text from File Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb This code reads the content of 'sample.txt' into a string variable and prints the first 100 characters. It demonstrates basic file handling for text input. ```python with open('demo/sample.txt', 'r') as file: text = file.read() print(text[:100]) ``` -------------------------------- ### Save and Load RAPTOR Tree Source: https://github.com/parthsarthi03/raptor/blob/master/README.md Demonstrates saving the constructed RAPTOR tree to disk and then loading it back for subsequent use. Allows for persistence of the indexed data. ```python SAVE_PATH = "demo/cinderella" RA.save(SAVE_PATH) RA = RetrievalAugmentation(tree=SAVE_PATH) answer = RA.answer_question(question=question) ``` -------------------------------- ### Initialize RAPTOR and Add Documents Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Initializes the RetrievalAugmentation object from the raptor library and adds the provided text content to construct the summarization tree. This is the core step for building the RAPTOR index. ```python from raptor import RetrievalAugmentation RA = RetrievalAugmentation() # construct the tree RA.add_documents(text) ``` -------------------------------- ### Authenticate with HuggingFace for Gemma Model Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb This snippet demonstrates how to authenticate with HuggingFace using `login()`. This step is necessary if you intend to use models like Gemma that require authentication and are not already downloaded. ```python # if you want to use the Gemma, you will need to authenticate with HuggingFace, Skip this step, if you have the model already downloaded from huggingface_hub import login login() ``` -------------------------------- ### Python Retrieval Augmentation Configuration and Initialization Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Demonstrates the configuration and initialization of the RetrievalAugmentation class. It instantiates summarization, QA, and embedding models and passes them to the RetrievalAugmentationConfig, which is then used to create a RetrievalAugmentation instance. ```python RAC = RetrievalAugmentationConfig(summarization_model=GEMMASummarizationModel(), qa_model=GEMMAQAModel(), embedding_model=SBertEmbeddingModel()) RA = RetrievalAugmentation(config=RAC) ``` -------------------------------- ### Import Transformers for Custom Models Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Imports `AutoTokenizer` and `pipeline` from the transformers library along with torch. These are foundational components for loading and using pre-trained models for various NLP tasks. ```python from transformers import AutoTokenizer, pipeline import torch ``` -------------------------------- ### Load RAPTOR Tree from Disk Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Loads a previously saved RAPTOR tree from a specified path by passing the path to the `RetrievalAugmentation` constructor. This enables querying from a persisted tree. ```python # load back the tree by passing it into RetrievalAugmentation RA = RetrievalAugmentation(tree=SAVE_PATH) answer = RA.answer_question(question=question) print("Answer: ", answer) ``` -------------------------------- ### Initialize OpenAI API Key for RAPTOR Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb This snippet sets the OpenAI API key in the environment variables. It's a prerequisite for initializing the RAPTOR application, even if OpenAI models are not actively used. A placeholder string can be used if OpenAI is not in use. ```python import os os.environ["OPENAI_API_KEY"] = "your-openai-key" ``` -------------------------------- ### Python: Integrate Custom Models with RAPTOR Source: https://github.com/parthsarthi03/raptor/blob/master/README.md This snippet demonstrates how to initialize RAPTOR with custom summarization, question answering, and embedding models. It requires the 'raptor' library and assumes the existence of custom model classes. ```python from raptor import RetrievalAugmentation, RetrievalAugmentationConfig # Initialize your custom models custom_summarizer = CustomSummarizationModel() custom_qa = CustomQAModel() custom_embedding = CustomEmbeddingModel() # Create a config with your custom models custom_config = RetrievalAugmentationConfig( summarization_model=custom_summarizer, qa_model=custom_qa, embedding_model=custom_embedding ) # Initialize RAPTOR with your custom config RA = RetrievalAugmentation(config=custom_config) ``` -------------------------------- ### Advanced Configuration Source: https://context7.com/parthsarthi03/raptor/llms.txt Customize models, clustering parameters, and retrieval behavior for the RetrievalAugmentation pipeline. ```APIDOC ## Advanced Configuration ### Description Customize models, clustering parameters, and retrieval behavior. ### Method Not Applicable (Class Initialization with Configuration) ### Endpoint Not Applicable (Library Usage) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from raptor import ( RetrievalAugmentation, RetrievalAugmentationConfig, ClusterTreeConfig, TreeRetrieverConfig, SBertEmbeddingModel, GPT4QAModel, GPT3TurboSummarizationModel ) # Configure custom embedding model custom_embedding = SBertEmbeddingModel(model_name="sentence-transformers/multi-qa-mpnet-base-cos-v1") # Configure custom QA and summarization models custom_qa = GPT4QAModel(model="gpt-4") custom_summarizer = GPT3TurboSummarizationModel(model="gpt-3.5-turbo") # Create configuration with custom parameters config = RetrievalAugmentationConfig( # Embedding and model settings embedding_model=custom_embedding, qa_model=custom_qa, summarization_model=custom_summarizer, # Tree builder settings tb_max_tokens=100, # Maximum tokens per chunk tb_num_layers=5, # Number of tree layers tb_summarization_length=100, # Summary length in tokens # Retrieval settings tr_top_k=10, # Top-k nodes to retrieve tr_threshold=0.5, # Similarity threshold tr_selection_mode="top_k" # Selection mode: "top_k" or "threshold" ) RA = RetrievalAugmentation(config=config) RA.add_documents(text) # Retrieve with custom parameters answer, layer_info = RA.answer_question( question="What is the main argument?", top_k=15, max_tokens=4000, collapse_tree=True, # Search across all layers return_layer_information=True # Get layer metadata ) print(f"Answer: {answer}") print(f"Layers used: {layer_info}") ``` ### Response #### Success Response (200) - **answer** (str) - The answer to the question. - **layer_info** (dict) - Information about the layers used in retrieval. #### Response Example ```json { "answer": "The main argument is...", "layer_info": { ... } } ``` ``` -------------------------------- ### Query RAPTOR Tree with a Question Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Demonstrates how to ask a question to the RAPTOR model after the tree has been constructed. The `answer_question` method takes the question string as input and returns the answer. ```python question = # any question RA.answer_question(question) ``` -------------------------------- ### Raptor Error Handling and Validation Source: https://context7.com/parthsarthi03/raptor/llms.txt Illustrates robust error handling for production deployments. It includes initializing with validation parameters, checking if the tree is built before querying, validating question format, and catching potential exceptions like ValueError, FileNotFoundError, and general Exceptions. ```python from raptor import RetrievalAugmentation, RetrievalAugmentationConfig import logging logging.basicConfig(level=logging.INFO) try: # Initialize with validation config = RetrievalAugmentationConfig( tb_max_tokens=100, tb_num_layers=5, tr_top_k=10 ) RA = RetrievalAugmentation(config=config) # Check if tree is built before querying if RA.tree is None: print("Building tree...") with open('document.txt', 'r') as f: RA.add_documents(f.read()) # Validate question format question = "What is the main topic?" if not isinstance(question, str) or len(question.strip()) == 0: raise ValueError("Question must be a non-empty string") answer = RA.answer_question( question=question, top_k=10, max_tokens=3500 ) print(f"Answer: {answer}") except ValueError as ve: print(f"Configuration error: {ve}") except FileNotFoundError as fe: print(f"File error: {fe}") except Exception as e: print(f"Unexpected error: {e}") logging.exception("Full traceback:") ``` -------------------------------- ### Customize RetrievalAugmentation with Advanced Configuration Source: https://context7.com/parthsarthi03/raptor/llms.txt Shows how to customize the RAPTOR pipeline by configuring embedding models, QA models, summarization models, and various tree building and retrieval parameters. This allows for fine-tuning the system's behavior and performance. ```python from raptor import ( RetrievalAugmentation, RetrievalAugmentationConfig, ClusterTreeConfig, TreeRetrieverConfig, SBertEmbeddingModel, GPT4QAModel, GPT3TurboSummarizationModel ) # Configure custom embedding model custom_embedding = SBertEmbeddingModel(model_name="sentence-transformers/multi-qa-mpnet-base-cos-v1") # Configure custom QA and summarization models custom_qa = GPT4QAModel(model="gpt-4") custom_summarizer = GPT3TurboSummarizationModel(model="gpt-3.5-turbo") # Create configuration with custom parameters config = RetrievalAugmentationConfig( # Embedding and model settings embedding_model=custom_embedding, qa_model=custom_qa, summarization_model=custom_summarizer, # Tree builder settings tb_max_tokens=100, tb_num_layers=5, tb_summarization_length=100, # Retrieval settings tr_top_k=10, tr_threshold=0.5, tr_selection_mode="top_k" ) RA = RetrievalAugmentation(config=config) RA.add_documents(text) # Retrieve with custom parameters answer, layer_info = RA.answer_question( question="What is the main argument?", top_k=15, max_tokens=4000, collapse_tree=True, return_layer_information=True ) print(f"Answer: {answer}") print(f"Layers used: {layer_info}") ``` -------------------------------- ### Access and Inspect Raptor Tree Structure Source: https://context7.com/parthsarthi03/raptor/llms.txt Demonstrates how to access the tree structure, print statistics like total nodes, leaf nodes, root nodes, and number of layers. It also shows how to inspect nodes by layer and save/load the tree structure using pickle. ```python import pickle # Assume RA is an instance of RetrievalAugmentation tree = RA.tree print(f"Total nodes: {len(tree.all_nodes)}") print(f"Leaf nodes: {len(tree.leaf_nodes)}") print(f"Root nodes: {len(tree.root_nodes)}") print(f"Number of layers: {tree.num_layers}") # Inspect nodes by layer for layer_num, nodes in tree.layer_to_nodes.items(): print(f"Layer {layer_num}: {len(nodes)} nodes") for node in nodes[:2]: # Show first 2 nodes per layer print(f" Node {node.index}: {node.text[:100]}...") print(f" Children: {node.children}") print(f" Embedding keys: {list(node.embeddings.keys())}") # Save tree structure separately with open("tree_structure.pkl", "wb") as f: pickle.dump(tree, f) # Load and inspect later with open("tree_structure.pkl", "rb") as f: loaded_tree = pickle.load(f) ``` -------------------------------- ### Add Documents and Answer Questions with RAPTOR Source: https://github.com/parthsarthi03/raptor/blob/master/README.md Reads text from a file, adds it to the RAPTOR index, and then uses the index to answer a specific question. Outputs the answer to the console. Requires the 'raptor' library. ```python with open('sample.txt', 'r') as file: text = file.read() RA.add_documents(text) question = "How did Cinderella reach her happy ending?" answer = RA.answer_question(question=question) print("Answer: ", answer) ``` -------------------------------- ### Import Custom Model Components for RAPTOR Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Imports necessary classes from the raptor library and the transformers library to enable the use of custom open-source models for summarization, QA, and embeddings within RAPTOR. ```python import torch from raptor import BaseSummarizationModel, BaseQAModel, BaseEmbeddingModel, RetrievalAugmentationConfig from transformers import AutoTokenizer, pipeline ``` -------------------------------- ### Custom QA Model Implementation Source: https://github.com/parthsarthi03/raptor/blob/master/README.md Illustrates how to implement a custom question-answering model by inheriting from BaseQAModel and overriding the answer_question method. This enables the use of alternative QA systems. ```python from raptor import BaseQAModel class CustomQAModel(BaseQAModel): def __init__(self): # Initialize your model here pass def answer_question(self, context, question): # Implement your QA logic here # Return the answer as a string answer = "Your answer here" return answer ``` -------------------------------- ### Python GEMMA Question Answering Model Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Provides a question answering model by inheriting from a base class, using the GEMMA model. It sets up the tokenizer and QA pipeline, then answers questions based on provided context. The generation uses specific parameters for temperature, top_k, and top_p, with CUDA utilization preferred. ```python from transformers import AutoTokenizer, pipeline import torch class GEMMAQAModel(BaseQAModel): def __init__(self, model_name= "google/gemma-2b-it"): # Initialize the tokenizer and the pipeline for the model self.tokenizer = AutoTokenizer.from_pretrained(model_name) self.qa_pipeline = pipeline( "text-generation", model=model_name, model_kwargs={"torch_dtype": torch.bfloat16}, device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'), ) def answer_question(self, context, question): # Apply the chat template for the context and question messages=[ {"role": "user", "content": f"Given Context: {context} Give the best full answer amongst the option to question {question}"} ] prompt = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) # Generate the answer using the pipeline outputs = self.qa_pipeline( prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95 ) # Extracting and returning the generated answer answer = outputs[0]["generated_text"].split("\n")[1].strip() # Adjusted to extract answer after prompt return answer ``` -------------------------------- ### Python GEMMA Summarization Model Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Implements a summarization model by extending a base class, utilizing the GEMMA model from Hugging Face. It initializes the tokenizer and pipeline, then generates summaries from provided context with adjustable token limits. CUDA is used if available, otherwise CPU. ```python from transformers import AutoTokenizer, pipeline import torch class GEMMASummarizationModel(BaseSummarizationModel): def __init__(self, model_name="google/gemma-2b-it"): # Initialize the tokenizer and the pipeline for the GEMMA model self.tokenizer = AutoTokenizer.from_pretrained(model_name) self.summarization_pipeline = pipeline( "text-generation", model=model_name, model_kwargs={"torch_dtype": torch.bfloat16}, device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'), # Use "cpu" if CUDA is not available ) def summarize(self, context, max_tokens=150): # Format the prompt for summarization messages=[ {"role": "user", "content": f"Write a summary of the following, including as many key details as possible: {context}:"} ] prompt = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) # Generate the summary using the pipeline outputs = self.summarization_pipeline( prompt, max_new_tokens=max_tokens, do_sample=True, temperature=0.7, top_k=50, top_p=0.95 ) # Extracting and returning the generated summary summary = outputs[0]["generated_text"].strip() return summary ``` -------------------------------- ### Python: Create Custom QA Model with Retrieval Augmentation Source: https://context7.com/parthsarthi03/raptor/llms.txt This snippet demonstrates how to create a custom QA model by subclassing BaseQAModel and integrating it with RetrievalAugmentation. It requires the 'raptor' and 'requests' libraries. The custom model defines an 'answer_question' method that sends a POST request to a specified endpoint. ```python from raptor import BaseQAModel, RetrievalAugmentation, RetrievalAugmentationConfig class CustomQAModel(BaseQAModel): def __init__(self, model_endpoint="http://localhost:8000/qa"): self.endpoint = model_endpoint def answer_question(self, context, question): import requests try: response = requests.post( self.endpoint, json={"context": context, "question": question}, timeout=30 ) return response.json()["answer"] except Exception as e: print(f"QA Error: {e}") return "Unable to generate answer" # Integrate custom QA model custom_qa = CustomQAModel() config = RetrievalAugmentationConfig(qa_model=custom_qa) RA = RetrievalAugmentation(config=config) RA.add_documents(text) answer = RA.answer_question(question="What are the key findings?") ``` -------------------------------- ### Custom Summarization Model Implementation Source: https://github.com/parthsarthi03/raptor/blob/master/README.md Provides a template for creating a custom summarization model by extending the BaseSummarizationModel class and implementing the summarize method. This allows integration of custom summarization logic. ```python from raptor import BaseSummarizationModel class CustomSummarizationModel(BaseSummarizationModel): def __init__(self): # Initialize your model here pass def summarize(self, context, max_tokens=150): # Implement your summarization logic here # Return the summary as a string summary = "Your summary here" return summary ``` -------------------------------- ### Python Document Addition and Question Answering with Retrieval Augmentation Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Shows how to add documents to the RetrievalAugmentation instance and perform a question-answering query. It reads text from a file, adds it to the RA object, and then asks a question to retrieve an answer, printing the result. ```python with open('demo/sample.txt', 'r') as file: text = file.read() RA.add_documents(text) question = "How did Cinderella reach her happy ending?" answer = RA.answer_question(question=question) print("Answer: ", answer) ``` -------------------------------- ### Save RAPTOR Tree to Disk Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Saves the constructed RAPTOR tree to a specified file path using the `save` method. This allows the tree to be loaded later without rebuilding it. ```python # Save the tree by calling RA.save("path/to/save") SAVE_PATH = "demo/cinderella" RA.save(SAVE_PATH) ``` -------------------------------- ### Custom Summarization Model Source: https://context7.com/parthsarthi03/raptor/llms.txt Implement your own summarization logic by extending the BaseSummarizationModel class and integrating it into the RAPTOR pipeline. ```APIDOC ## Custom Summarization Model ### Description Implement your own summarization logic by extending `BaseSummarizationModel`. ### Method Not Applicable (Class Implementation) ### Endpoint Not Applicable (Library Usage) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from raptor import BaseSummarizationModel, RetrievalAugmentation, RetrievalAugmentationConfig import anthropic import os class ClaudeSummarizationModel(BaseSummarizationModel): def __init__(self, model="claude-3-sonnet-20240229"): self.model = model self.client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"]) def summarize(self, context, max_tokens=150): try: message = self.client.messages.create( model=self.model, max_tokens=max_tokens, messages=[ { "role": "user", "content": f"Summarize the following text, including key details: {context}" } ] ) return message.content[0].text except Exception as e: print(f"Error: {e}") return "" # Use custom model in configuration custom_summarizer = ClaudeSummarizationModel() config = RetrievalAugmentationConfig(summarization_model=custom_summarizer) RA = RetrievalAugmentation(config=config) RA.add_documents(text) ``` ### Response #### Success Response (200) - **answer** (str) - The answer to the question. #### Response Example ```json { "answer": "The summary of the provided context..." } ``` ``` -------------------------------- ### Python: Retrieval-Only Mode with Raptor Source: https://context7.com/parthsarthi03/raptor/llms.txt This snippet demonstrates how to use Raptor in retrieval-only mode, focusing on extracting relevant context without generating answers. It uses the RetrievalAugmentation class. Key parameters include `top_k`, `max_tokens`, and `collapse_tree`. The retrieved context can then be used with external LLMs like OpenAI's GPT-4. ```python from raptor import RetrievalAugmentation RA = RetrievalAugmentation() RA.add_documents(text) # Retrieve context without answering context, layer_info = RA.retrieve( question="What are the experimental results?", start_layer=None, # Start from top layer (default) num_layers=None, # Traverse all layers (default) top_k=10, # Retrieve top 10 nodes max_tokens=3500, # Maximum context tokens collapse_tree=True, # Search all nodes regardless of layer return_layer_information=True ) print(f"Retrieved context length: {len(context)} characters") print(f"Nodes from layers: {[info['layer_number'] for info in layer_info]}") # Use context with your own LLM import openai response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "Answer based on the provided context."}, {"role": "user", "content": f"Context: {context}\n\nQuestion: What are the experimental results?"} ] ) print(response.choices[0].message.content) ``` -------------------------------- ### Custom Embedding Model Implementation Source: https://github.com/parthsarthi03/raptor/blob/master/README.md Shows how to integrate a custom embedding model by extending the BaseEmbeddingModel class and implementing the create_embedding method. This allows for the use of different text embedding techniques. ```python from raptor import BaseEmbeddingModel class CustomEmbeddingModel(BaseEmbeddingModel): def __init__(self): # Initialize your model here pass def create_embedding(self, text): # Implement your embedding logic here # Return the embedding as a numpy array or a list of floats embedding = [0.0] * embedding_dim # Replace with actual embedding logic return embedding ``` -------------------------------- ### Python Sentence-BERT Embedding Model Source: https://github.com/parthsarthi03/raptor/blob/master/demo.ipynb Defines an embedding model using the sentence-transformers library. It initializes with a specified pre-trained model and provides a method to encode input text into numerical embeddings. This model is suitable for tasks requiring semantic understanding of text. ```python from sentence_transformers import SentenceTransformer class SBertEmbeddingModel(BaseEmbeddingModel): def __init__(self, model_name="sentence-transformers/multi-qa-mpnet-base-cos-v1"): self.model = SentenceTransformer(model_name) def create_embedding(self, text): return self.model.encode(text) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.