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:
importosimportrequestsfromdatetimeimportdatetimefromtypingimportListfromdocximportDocumentfrompypdfimportPdfReaderfrompptximportPresentationfromunstructured.partition.autoimportpartitionimportpypandocclassTools:def__init__(self):# If set to true it will prevent default RAG pipelineself.file_handler=Trueself.citation=Truepassdefextract_txt_content(self,file_path:str)->str:try:withopen(file_path,"r",encoding="utf-8")asfile:returnfile.read()exceptExceptionase:returnf"Error read: {e}"defget_files(self,__files__:List[dict]=[])->str:result=""forfile_infoin__files__:file_path=file_info["file"]["meta"]["path"]file_name=file_info["file"]["meta"]["name"]content_type=file_info["file"]["meta"]["content_type"]ifcontent_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"ifisinstance(result,list):result="\n".join(result)returnresult
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>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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:
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?