How to Disable Embedding and Ensure Full Text Return from Files in Chat Using Tools? #1798

Closed
opened 2025-11-11 14:53:29 -06:00 by GiteaMirror · 0 comments
Owner

Originally created by @gmrx on GitHub (Aug 16, 2024).

Discussed in https://github.com/open-webui/open-webui/discussions/4655

Originally posted by gmrx August 16, 2024
I'm currently facing an issue where I need to use the Tools class to translate the entire text from a file by sending it to chat. I'm using the code below, but the text is returned as a list. How can I disable embedding to ensure that the entire text from a file is returned as a single string? Here's the code I'm working with:

import os
import requests
from datetime import datetime
from typing import List
from docx import Document
from pypdf import PdfReader
from pptx import Presentation
from unstructured.partition.auto import partition
import pypandoc

class Tools:
    def __init__(self):
        # If set to true it will prevent default RAG pipeline
        self.file_handler = True
        self.citation = True
        pass

    def extract_txt_content(self, file_path: str) -> str:
        try:
            with open(file_path, "r", encoding="utf-8") as file:
                return file.read()
        except Exception as e:
            return f"Error read: {e}"

    def get_files(self, __files__: List[dict] = []) -> str:
        result = ""
        for file_info in __files__:
            file_path = file_info["file"]["meta"]["path"]
            file_name = file_info["file"]["meta"]["name"]
            content_type = file_info["file"]["meta"]["content_type"]

            if content_type == "text/plain":
                content = self.extract_txt_content(file_path)
            else:
                content = (
                    f"Error type: {file_name} "
                )

            result += f"=== START FILE: {file_name} ===\n{content}\n=== END FILE: {file_name} ===\n\n"

        if isinstance(result, list):
            result = "\n".join(result)

        return result

How can I modify the implementation to ensure that the text is returned as a single complete string rather than a list? What adjustments should be made to prevent any unwanted processing like embedding that might be affecting the output?

Originally created by @gmrx on GitHub (Aug 16, 2024). ### Discussed in https://github.com/open-webui/open-webui/discussions/4655 <div type='discussions-op-text'> <sup>Originally posted by **gmrx** August 16, 2024</sup> I'm currently facing an issue where I need to use the Tools class to translate the entire text from a file by sending it to chat. I'm using the code below, but the text is returned as a list. How can I disable embedding to ensure that the entire text from a file is returned as a single string? Here's the code I'm working with: ```python import os import requests from datetime import datetime from typing import List from docx import Document from pypdf import PdfReader from pptx import Presentation from unstructured.partition.auto import partition import pypandoc class Tools: def __init__(self): # If set to true it will prevent default RAG pipeline self.file_handler = True self.citation = True pass def extract_txt_content(self, file_path: str) -> str: try: with open(file_path, "r", encoding="utf-8") as file: return file.read() except Exception as e: return f"Error read: {e}" def get_files(self, __files__: List[dict] = []) -> str: result = "" for file_info in __files__: file_path = file_info["file"]["meta"]["path"] file_name = file_info["file"]["meta"]["name"] content_type = file_info["file"]["meta"]["content_type"] if content_type == "text/plain": content = self.extract_txt_content(file_path) else: content = ( f"Error type: {file_name} " ) result += f"=== START FILE: {file_name} ===\n{content}\n=== END FILE: {file_name} ===\n\n" if isinstance(result, list): result = "\n".join(result) return result ``` How can I modify the implementation to ensure that the text is returned as a single complete string rather than a list? What adjustments should be made to prevent any unwanted processing like embedding that might be affecting the output?</div>
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#1798