Skip to content

ingest sitemaps#19

Open
srkirkland wants to merge 6 commits intomainfrom
srk/web-extractors
Open

ingest sitemaps#19
srkirkland wants to merge 6 commits intomainfrom
srk/web-extractors

Conversation

@srkirkland
Copy link
Copy Markdown
Member

@srkirkland srkirkland commented Jul 22, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced document ingestion process to handle both PDF and text files more efficiently.
    • Introduced a new function for extracting text from policy files, including support for HTML content and metadata extraction.
    • Added structured handling for different source types, improving policy retrieval functionality.
    • Implemented functionality to fetch and parse sitemaps, extracting URLs and metadata into policy details.
  • Bug Fixes

    • Improved stability by adding checks for valid URLs in sitemap processing.
  • Documentation

    • Updated comments for clarity in several functions, enhancing maintainability.
  • Refactor

    • Renamed functions for consistency and improved clarity regarding their purpose.
    • Introduced new enumeration for source types, enhancing data integrity.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jul 22, 2024

Walkthrough

The recent changes enhance the codebase's functionality and clarity by introducing a structured approach to managing various source types and streamlining the document ingestion process. New functions for policy extraction and sitemap parsing have been added, while existing methods have been updated for improved flexibility. The integration of enums and modifications to parameter types contribute to better data integrity and extensibility, setting the stage for future enhancements.

Changes

Files Change Summary
background/crawl.py Modified get_source_policy_list to accept a Source object; added get_custom_policies for unique processing of custom sources.
background/db.py Introduced SourceType enum with four types and added a type field to the Source class for improved structure and data integrity.
background/extract.py Added extract_text_from_policy_file to handle PDF and text extraction; updated existing methods to utilize a policy object for enhanced metadata handling.
background/ingest.py Renamed functions for clarity (download_pdf to download_policy, ingest_documents to ingest_policies); improved file type handling during ingestion.
background/update.py Updated ingestion process to use ingest_policies, refined comments for clarity, and enhanced the Source object with a type attribute.
background/sources/sitemap.py Introduced functionality to fetch and parse sitemaps, extracting URLs and metadata into PolicyDetails objects.
requirements.txt Added dependency lxml==5.2.2 for enhanced XML processing capabilities.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Ingest
    participant Extract
    participant Crawl

    User->>Ingest: Initiate policy ingestion
    Ingest->>Crawl: Retrieve policies via get_source_policy_list
    Crawl->>Ingest: Return list of PolicyDetails
    Ingest->>Extract: Extract text from policies
    Extract->>Ingest: Return extracted text
    Ingest->>User: Provide extracted policy data
Loading

🐇 "In the code we hop and play,
Enhancing functions day by day.
With enums and structures bright,
Our policies now take flight!
From sitemaps to texts we gleefully scurry,
In this code, there's no need to hurry!" 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Outside diff range, codebase verification and nitpick comments (10)
background/crawl.py (1)

22-25: Add TODO comments for unimplemented source types.

The function currently returns empty lists for SITEMAP and RECURSIVE types. Adding TODO comments will help track future implementation.

    if source.type == SourceType.SITEMAP:
        return []
    elif source.type == SourceType.RECURSIVE:
        return []  # TODO: implement recursive crawling
background/extract.py (3)

108-112: Ensure Metadata Consistency

Ensure that the metadata (title and keywords) are consistently updated in the policy object even if they are empty.

-    if title:
-        policy.title = title
-    if keywords:
-        policy.keywords = keywords
+    policy.title = title if title else policy.title
+    policy.keywords = keywords if keywords else policy.keywords

Line range hint 118-140: Ensure Proper Handling of Empty Text Extraction

The function should handle cases where text extraction from the PDF fails, even after attempting OCR.

-            return text
+            if not text:
+                logger.error(f"Failed to extract text from {policy.url} even after OCR")
+            return text

Line range hint 22-22: Remove Unused Import

The import extract_text_from_pdf is not used in this file and should be removed.

- from background.extract import extract_text_from_pdf, extract_text_from_policy_file
+ from background.extract import extract_text_from_policy_file
background/update.py (3)

49-53: Remove Outdated TODO Comment

The TODO comment about removing policies from the database is outdated and should be removed or updated to reflect the current state of the implementation.

-    ## TODO: eventually, we could add a check to see if the policy has been removed from the source, and if so, remove it from the db

Line range hint 56-87: Improve Error Handling for Unrecognized Sources

The function should handle cases where get_source_policy_list returns None more gracefully by providing a more informative error message and possibly retrying the operation.

-        if policy_details is None:
-            logger.error(f"Source {source.name} not recognized")
+        if policy_details is None:
+            logger.error(f"Source {source.name} not recognized or no policies found")

83-84: Clarify Comment on JSON Storage

The comment about moving the JSON file to remote storage should be clarified to indicate the future plan more explicitly.

-            ## TODO: move to remote storage of JSON file
+            ## TODO: Plan to move JSON file to remote storage for better scalability and access
background/ingest.py (3)

Line range hint 92-123: Handle Unsupported Content Types

The function should handle unsupported content types more gracefully by providing a more informative error message and possibly retrying the operation.

-    if "Content-Type" in response.headers:
-        content_type = response.headers["Content-Type"]
-        if "application/pdf" in content_type:
-            file_type = "pdf"
+    content_type = response.headers.get("Content-Type", "")
+    if "application/pdf" in content_type:
+        file_type = "pdf"
+    elif "text/plain" not in content_type:
+        logger.error(f"Unsupported content type: {content_type}")
+        return None

Line range hint 138-185: Ensure Proper Handling of Missing Text Extraction

The function should handle cases where text extraction fails more gracefully by providing a more informative error message and possibly retrying the operation.

-            logger.warning(f"No text extracted from {local_policy_path}")
+            logger.warning(f"No text extracted from {local_policy_path}. Skipping policy.")
Tools
Ruff

157-157: f-string without any placeholders

Remove extraneous f prefix

(F541)


Line range hint 138-185: Fix Variable Scope Issue

The variables num_docs_indexed and num_new_docs should be updated within the loop to reflect the correct counts.

-        logger.info(f"Indexed {num_docs_indexed} documents from source {source.name}")
+        logger.info(f"Indexed {num_docs_indexed} documents from source {source.name}")

+        num_docs_indexed += 1
+        if not document:
+            num_new_docs += 1
Tools
Ruff

157-157: f-string without any placeholders

Remove extraneous f prefix

(F541)

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1cfa115 and ff0b9f8.

Files selected for processing (6)
  • background/crawl.py (2 hunks)
  • background/db.py (1 hunks)
  • background/extract.py (3 hunks)
  • background/ingest.py (5 hunks)
  • background/sources/sitemap.py (1 hunks)
  • background/update.py (6 hunks)
Additional context used
Ruff
background/ingest.py

22-22: background.extract.extract_text_from_pdf imported but unused

Remove unused import: background.extract.extract_text_from_pdf

(F401)

Additional comments not posted (6)
background/db.py (2)

42-46: LGTM! Well-defined SourceType enum.

The enum clearly categorizes different source types, improving the structure and clarity of the code.


52-52: LGTM! Addition of type field in Source class.

The type field enforces the use of the SourceType enum, enhancing data integrity and clarity.

background/sources/sitemap.py (1)

73-76: LGTM! _ensure_absolute_url function is well-implemented.

The function correctly ensures that a URL is absolute, handling relative URLs appropriately.

background/crawl.py (1)

Line range hint 33-47:
LGTM! get_custom_policies function is well-implemented.

The function correctly handles specific sources that require unique processing and delegates to the appropriate functions.

background/update.py (2)

216-216: Ensure Consistency in Source Object Initialization

Ensure that the type attribute is consistently initialized across all instances of the Source object.


197-197: Ensure Consistency in Source Object Initialization

Ensure that the type attribute is consistently initialized across all instances of the Source object.

Comment on lines +28 to +32
response.raise_for_status()

if response is None:
logger.error(f"Failed to fetch sitemap {sitemap_url}")
return []
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle potential None response before calling raise_for_status.

The response.raise_for_status() call should be placed after checking if the response is None to avoid potential errors.

-    response.raise_for_status()
-
     if response is None:
         logger.error(f"Failed to fetch sitemap {sitemap_url}")
         return []

+    response.raise_for_status()
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
response.raise_for_status()
if response is None:
logger.error(f"Failed to fetch sitemap {sitemap_url}")
return []
if response is None:
logger.error(f"Failed to fetch sitemap {sitemap_url}")
return []
response.raise_for_status()

Comment on lines +61 to +115
def extract_text_from_text_file(input_path: str, policy: PolicyDetails) -> str:
"""
Extract text from a text file. Sounds simple.
But we do want to check if it looks like HTML, and if so we should attempt to extract metadata too (like title)
"""

try:
with open(input_path, "r") as file:
file_contents = file.read()
except Exception as e:
logger.error(f"Error extracting text from {policy.url}: {e}")
return ""

# Create a BeautifulSoup object
soup = BeautifulSoup(file_contents, "lxml")

# If the document does not have an 'html' tag, it is not a webpage
if not soup.find("html"):
return file_contents

# Extract title (og:title or title tag)
title = None
og_title = soup.find("meta", property="og:title")
if og_title and og_title.get("content"):
title = og_title["content"]
else:
title_tag = soup.find("title")
if title_tag:
title = title_tag.string

# Extract keywords
keywords = []
meta_keywords = soup.find("meta", attrs={"name": "keywords"})
if meta_keywords and meta_keywords.get("content"):
keywords_content = meta_keywords["content"]
keywords = [keyword.strip() for keyword in keywords_content.split(",")]

# Extract content from <main> or <body>
content = ""
main_content = soup.find("main")
if main_content:
content = main_content.get_text(separator="\n").strip()
else:
body_content = soup.find("body")
if body_content:
content = body_content.get_text(separator="\n").strip()

# modify the policy if we have a title or keywords
if title:
policy.title = title
if keywords:
policy.keywords = keywords

# return the content
return content
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve Error Handling for File Reading

The function should handle specific exceptions related to file reading, such as FileNotFoundError and IOError, to provide more precise error messages.

-    except Exception as e:
+    except (FileNotFoundError, IOError) as e:
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def extract_text_from_text_file(input_path: str, policy: PolicyDetails) -> str:
"""
Extract text from a text file. Sounds simple.
But we do want to check if it looks like HTML, and if so we should attempt to extract metadata too (like title)
"""
try:
with open(input_path, "r") as file:
file_contents = file.read()
except Exception as e:
logger.error(f"Error extracting text from {policy.url}: {e}")
return ""
# Create a BeautifulSoup object
soup = BeautifulSoup(file_contents, "lxml")
# If the document does not have an 'html' tag, it is not a webpage
if not soup.find("html"):
return file_contents
# Extract title (og:title or title tag)
title = None
og_title = soup.find("meta", property="og:title")
if og_title and og_title.get("content"):
title = og_title["content"]
else:
title_tag = soup.find("title")
if title_tag:
title = title_tag.string
# Extract keywords
keywords = []
meta_keywords = soup.find("meta", attrs={"name": "keywords"})
if meta_keywords and meta_keywords.get("content"):
keywords_content = meta_keywords["content"]
keywords = [keyword.strip() for keyword in keywords_content.split(",")]
# Extract content from <main> or <body>
content = ""
main_content = soup.find("main")
if main_content:
content = main_content.get_text(separator="\n").strip()
else:
body_content = soup.find("body")
if body_content:
content = body_content.get_text(separator="\n").strip()
# modify the policy if we have a title or keywords
if title:
policy.title = title
if keywords:
policy.keywords = keywords
# return the content
return content
try:
with open(input_path, "r") as file:
file_contents = file.read()
except (FileNotFoundError, IOError) as e:
logger.error(f"Error extracting text from {policy.url}: {e}")
return ""

Comment on lines +74 to +79
# Create a BeautifulSoup object
soup = BeautifulSoup(file_contents, "lxml")

# If the document does not have an 'html' tag, it is not a webpage
if not soup.find("html"):
return file_contents
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize HTML Check

The current check for HTML documents can be optimized by directly checking the presence of the html tag in the file contents before creating a BeautifulSoup object.

-    # Create a BeautifulSoup object
-    soup = BeautifulSoup(file_contents, "lxml")
-    # If the document does not have an 'html' tag, it is not a webpage
-    if not soup.find("html"):
+    # If the document does not contain an 'html' tag, it is not a webpage
+    if "<html" not in file_contents.lower():
+        return file_contents
+    
+    soup = BeautifulSoup(file_contents, "lxml")
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Create a BeautifulSoup object
soup = BeautifulSoup(file_contents, "lxml")
# If the document does not have an 'html' tag, it is not a webpage
if not soup.find("html"):
return file_contents
# If the document does not contain an 'html' tag, it is not a webpage
if "<html" not in file_contents.lower():
return file_contents
soup = BeautifulSoup(file_contents, "lxml")

Comment on lines +49 to +59
def extract_text_from_policy_file(input_path: str, policy: PolicyDetails) -> str:
"""
Determine which extractor to use based on the file extension.
"""
ext = os.path.splitext(input_path)[1].lower()

if ext == ".pdf":
return extract_text_from_pdf(input_path, policy)
else:
return extract_text_from_text_file(input_path, policy)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure Proper Handling of Unsupported File Types

The function currently handles only PDF and text files. Consider adding a default case to handle unsupported file types gracefully.

-    else:
+    elif ext == ".txt":
         return extract_text_from_text_file(input_path, policy)
+    else:
+        logger.error(f"Unsupported file type: {ext}")
+        return ""

Committable suggestion was skipped due to low confidence.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Outside diff range, codebase verification and nitpick comments (2)
background/ingest.py (2)

Line range hint 92-123:
Enhance docstring and improve error handling.

The docstring should be more descriptive, and error handling for the file download should be improved.

def download_policy(url: str, dir: str) -> str:
    """
    Download a policy from the given URL and save it to the specified directory.
    Determines if the file is a PDF or text file based on the Content-Type header
    and adds the appropriate extension.

    Args:
        url (str): The URL of the policy to download.
        dir (str): The directory to save the downloaded policy.

    Returns:
        str: The file path of the downloaded policy.
    """
    headers = {"User-Agent": user_agent}
    response = request_with_retry(
        url, headers=headers, allow_redirects=True, timeout=60
    )

    if not response:
        logger.error(f"Failed to download {url}")
        return None

    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError as e:
        logger.error(f"HTTP error occurred: {e}")
        return None

    file_type = "txt"  # default to text

    # check if the response is a PDF
    if "Content-Type" in response.headers:
        content_type = response.headers["Content-Type"]
        if "application/pdf" in content_type:
            file_type = "pdf"

    unique_filename = f"{uuid.uuid4()}.{file_type}"
    file_path = os.path.join(dir, unique_filename)

    with open(file_path, "wb") as file:
        file.write(response.content)

    return file_path

Line range hint 138-197:
Enhance logging and error handling.

Improve logging for better traceability and add error handling for potential issues during the ingestion process.

def ingest_policies(source: Source, policies: List[PolicyDetails]) -> IngestResult:
    start_time = datetime.now(timezone.utc)
    num_docs_indexed = 0
    num_new_docs = 0

    with tempfile.TemporaryDirectory() as temp_dir:
        for policy in policies:
            if not policy:
                logger.warning(f"Policy is None, skipping")
                continue

            logger.info(f"Processing policy {policy.url}")
            log_memory_usage(logger)

            try:
                local_policy_path = download_policy(policy.url, temp_dir)
                if not local_policy_path:
                    logger.error(f"Failed to download policy at {policy.url}. Skipping.")
                    continue

                policy_file_hash = calculate_file_hash(local_policy_path)
                document = get_document_by_url(policy.url)

                if document and document.metadata.get("hash") == policy_file_hash:
                    logger.info(f"Document {policy.url} has not changed, skipping")
                    wait_before_next_request()
                    continue

                extracted_text = extract_text_from_policy_file(local_policy_path, policy)
                if not extracted_text:
                    logger.warning(f"No text extracted from {local_policy_path}")
                    continue

                vectorized_document = policy.to_vectorized_document(extracted_text)
                vectorized_document.metadata.hash = policy_file_hash
                vectorized_document.metadata.content_length = len(extracted_text)
                vectorized_document.metadata.scope = source.name

                num_new_docs += 1 if not document else 0
                num_docs_indexed += 1

                result = vectorize_text(vectorized_document)
                update_document(source, policy, document, vectorized_document, result)

            except Exception as e:
                logger.error(f"Error processing policy {policy.url}: {e}")

        logger.info(f"Indexed {num_docs_indexed} documents from source {source.name}")

        end_time = datetime.now(timezone.utc)

        ## TODO: somewhere remove old documents that are no longer in the source

        return IngestResult(
            num_docs_indexed=num_docs_indexed,
            num_new_docs=num_new_docs,
            source_id=source._id,
            start_time=start_time,
            end_time=end_time,
            duration=(end_time - start_time).total_seconds(),
        )
Tools
Ruff

157-157: f-string without any placeholders

Remove extraneous f prefix

(F541)

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ff0b9f8 and bcffeb0.

Files selected for processing (3)
  • background/extract.py (3 hunks)
  • background/ingest.py (7 hunks)
  • background/update.py (7 hunks)
Files skipped from review as they are similar to previous changes (2)
  • background/extract.py
  • background/update.py
Additional context used
Ruff
background/ingest.py

22-22: background.extract.extract_text_from_pdf imported but unused

Remove unused import: background.extract.extract_text_from_pdf

(F401)

Additional comments not posted (1)
background/ingest.py (1)

Line range hint 198-231:
LGTM!

The function update_document handles the update process correctly.


import requests
from background.extract import extract_text_from_pdf
from background.extract import extract_text_from_pdf, extract_text_from_policy_file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused import.

The import statement extract_text_from_pdf is not used in the code and should be removed to clean up the code.

- from background.extract import extract_text_from_pdf, extract_text_from_policy_file
+ from background.extract import extract_text_from_policy_file
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
from background.extract import extract_text_from_pdf, extract_text_from_policy_file
from background.extract import extract_text_from_policy_file
Tools
Ruff

22-22: background.extract.extract_text_from_pdf imported but unused

Remove unused import: background.extract.extract_text_from_pdf

(F401)

Comment on lines +283 to 293
# if we haven't seen this document before, increment the count
num_new_docs += 1 if not document else 0
num_docs_indexed += 1 # record the indexing either way

result = vectorize_text(vectorized_document)

update_document(
source,
num_docs_indexed,
num_new_docs,
policy,
document,
vectorized_document,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance logging and error handling.

Improve logging for better traceability and add error handling for potential issues during the ingestion process.

def ingest_kb_documents(
    source: Source, policy_details_with_text: List[Tuple[PolicyDetails, str]]
) -> IngestResult:
    # KB is a special case, we already have the content
    # eventually it'd be nice to either scrape the site or get API access instead
    start_time = datetime.now(timezone.utc)
    num_docs_indexed = 0
    num_new_docs = 0

    for policy, text in policy_details_with_text:
        if not policy:
            logger.warning(f"Policy is None, skipping")
            continue

        logger.info(f"Processing document {policy.url}")
        log_memory_usage(logger)

        try:
            hash = hashlib.sha256(text.encode()).hexdigest()
            document = get_document_by_url(policy.url)

            if document and document.metadata.get("hash") == hash:
                logger.info(f"Document {policy.url} has not changed, skipping")
                continue

            if not text:
                logger.warning(f"No text extracted from {policy.url}")
                continue

            vectorized_document = policy.to_vectorized_document(text)
            vectorized_document.metadata.hash = hash
            vectorized_document.metadata.content_length = len(text)
            vectorized_document.metadata.scope = source.name

            num_new_docs += 1 if not document else 0
            num_docs_indexed += 1

            result = vectorize_text(vectorized_document)
            update_document(source, policy, document, vectorized_document, result)

        except Exception as e:
            logger.error(f"Error processing document {policy.url}: {e}")

    logger.info(f"Indexed {num_docs_indexed} documents from source {source.name}")

    end_time = datetime.now(timezone.utc)

    ## TODO: somewhere remove old documents that are no longer in the source

    return IngestResult(
        num_docs_indexed=num_docs_indexed,
        num_new_docs=num_new_docs,
        source_id=source._id,
        start_time=start_time,
        end_time=end_time,
        duration=(end_time - start_time).total_seconds(),
    )
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# if we haven't seen this document before, increment the count
num_new_docs += 1 if not document else 0
num_docs_indexed += 1 # record the indexing either way
result = vectorize_text(vectorized_document)
update_document(
source,
num_docs_indexed,
num_new_docs,
policy,
document,
vectorized_document,
# if we haven't seen this document before, increment the count
num_new_docs += 1 if not document else 0
num_docs_indexed += 1 # record the indexing either way
result = vectorize_text(vectorized_document)
update_document(
source,
policy,
document,
vectorized_document,
result
)
except Exception as e:
logger.error(f"Error processing document {policy.url}: {e}")
logger.info(f"Indexed {num_docs_indexed} documents from source {source.name}")
end_time = datetime.now(timezone.utc)
## TODO: somewhere remove old documents that are no longer in the source
return IngestResult(
num_docs_indexed=num_docs_indexed,
num_new_docs=num_new_docs,
source_id=source._id,
start_time=start_time,
end_time=end_time,
duration=(end_time - start_time).total_seconds(),
)

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bcffeb0 and 87669da.

Files selected for processing (5)
  • background/crawl.py (2 hunks)
  • background/extract.py (3 hunks)
  • background/ingest.py (7 hunks)
  • background/sources/sitemap.py (1 hunks)
  • requirements.txt (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • background/crawl.py
  • background/extract.py
  • background/ingest.py
  • background/sources/sitemap.py
Additional comments not posted (1)
requirements.txt (1)

10-10: Acknowledged: Addition of lxml==5.2.2.

The addition of lxml is appropriate for XML processing tasks, which aligns with the PR's focus on sitemap ingestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant