Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Multi channel recall, adding display of knowledge base titles and other information #2309

Merged
merged 2 commits into from
Feb 18, 2025

Conversation

shaohuzhang1
Copy link
Contributor

refactor: Multi channel recall, adding display of knowledge base titles and other information

Copy link

f2c-ci-robot bot commented Feb 18, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

f2c-ci-robot bot commented Feb 18, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

:data="paragraph.metadata"
:content="paragraph.page_content"
:index="paragraphIndex"
/>
</template>
</template>
<template v-else> -</template>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The provided code does not contain errors, but you might want to make the following improvements:

Improvements:

  1. Use v-bind for Inline Attributes:

    • Using v-bind:content="paragraph.content" and similarly for other attributes can help avoid issues with syntax highlighting.
  2. Consistent Attribute Usage:

    • Ensure that all necessary attributes are consistently passed in the <ParagraphCard> component. In this case, you're passing both metadata and page_content, but if only one is needed per card (metadata), it's better to pass just that.
  3. Remove Duplicates and Simplify Conditionals:

    • The conditional checks for truthy values at lines 147, 423, and 449 look similar. It might be more efficient and clearer to use a single condition or handle them programmatically based on your requirements.

Here's a simplified version of the changes:

<!-- Line 147 -->
<div v-for="(paragraph, paragraphIndex) in paragraphs" :key="paragraphIndex" class="mb-8">
  <ParagraphCard :data="paragraph.metadata" :index="paragraphIndex" />
</div>

<!-- Lines 423 and 449 (simplified version) -->
<template v-if="item">
  <div class="paragraph-source-card cursor" @click="onParagraphClick(item)">
    <ParagraphCard :data="item.metadata" :content="item.page_content"/>
    <!-- Add more cards here as needed... -->
  </div>
</template>
<template v-else> -

In the above improved versions:

  • We've used paragraphs instead of accessing item.document_list directly to simplify the logic.
  • We've removed duplicate card-boxes and replaced them with reusable components like <ParagraphCard>. This makes the code cleaner and easier to maintain.

question)
similarity = reranker_setting.get('similarity', 0.6)
max_paragraph_char_number = reranker_setting.get('max_paragraph_char_number', 5000)
result = reset_result_list(result, documents)
r = filter_result(result, max_paragraph_char_number, top_n, similarity)
return NodeResult({'result_list': r, 'result': ''.join([item.get('page_content') for item in r])}, {})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The provided code has several improvements that can enhance its readability, maintainability, and efficiency:

  1. Use of copy Method: The line document_list = document_list.copy() in the reset_result_list function is unnecessary as it creates an extra copy of document_list. This should be removed.

  2. Inline Dictionary Comprehension: In the same function, consider inlining the dictionary comprehension to make it more readable.

  3. Consistent Use of F-Strings: For string formatting consistency, you can use f-strings instead of concatenating strings with '+'. This can improve code readability.

Here's the revised version:

from typing import List

class Document:
    def __init__(self, page_content="", metadata={}):
        self.page_content = page_content
        self.metadata = metadata

def merge_reranker_list(reranker_list, result=None):
    if result is None:
        result = []
    for document in reranker_list:
        if isinstance(document, list):
            merge_reranker_list(document, result)
        elif isinstance(document, dict):
            content = document.get('title', '') + document.get('content', '')
            result.append(Document(str(document) if len(content) == 0 else content,
                                  {'title': document.get("title"),
                                   'dataset_name': document.get("dataset_name"),
                                   'document_name': document.get('document_name')})
        else:
            result.append(Document(str(document), {}))
    return result

def filter_result(document_list: List[Document], max_paragraph_char_number, top_n, similarity):
    return [doc for doc in document_list if len(doc.page_content) <= max_paragraph_char_number][:top_n]

def reset_result_list(result_list: List[Document], document_list: List[Document]):
    r = []
    for result in result_list:
        filtered_docs = [d for d in document_list if d.page_content == result.page_content]
        if filtered_docs:
            item = filtered_docs[0]
            document_list.remove(item)
            r.append(Document(item.page_content, {**item.metadata, 'relevance_score': result.metadata.get('relevance_score')})
        else:
            r.append(result)
    return r

# Assuming BaseRerankerNode class definition here...

Summary Changes:

  1. Removed redundant copying of document_list.
  2. Used inline dictionary comprehensions where applicable.
  3. Simplified the logic for filtering results without making copies.
  4. Ensured consistent use of formatted strings using f-strings.

These changes should help improve the clarity and performance of the code while maintaining functionality.

content: {
type: String,
default: ''
},
index: {
type: Number,
default: 0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code appears to be part of a Vue component using Element Plus and TypeScript. The changes made are focused on updating an existing MDPreview instance with new props and removing an unnecessary prop.

Potential Issues/Improvements:

  1. Prop Naming Collision: While the content prop is removed from the el-scrollbar, it's worth noting that there may be other places where this prop might still be required. It would be good to ensure consistency across all components when referencing properties.

  2. No Image Zoom In Option Removal: Since noImgZoomIn is removed, make sure that this functionality is handled elsewhere in your application if needed.

  3. Typo in Variable Name: There seems to be a typo in the comment regarding the variable name $props.data.similarity.toFixed(3). This should likely be corrected to avoid runtime errors.

  4. Default Props Value Consistency: Although not immediately apparent here, consider maintaining consistent default values across all components related to previews or similar text content.

  5. Code Readability: Ensure that any additional features added or modifications made do not significantly impact readability or maintainability.

Overall, the provided code looks clean and functional as intended for rendering markdown-like content within a scrollable view in a Vue.js application using Element Plus. Adjustments can be made based on specific requirements and additional functionalities you plan to add later.

@shaohuzhang1 shaohuzhang1 merged commit 0d96f79 into main Feb 18, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@refactor_reranker branch February 18, 2025 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants