最近在玩OpenAi的Gpts,我用公众号里几百篇文章作为知识库,让Gpts学习知识库里的文章,仿写公众号文章。
但是Gpts上传知识库的文件有如下限制:
1.每个 GPT 限制:10 个文件
2. 每个文件限制:512MB(图像文件为 20MB),2M 令牌
3. 每个用户限制:10 GB。每个组织限制:100 GB。
我在实践的时候也产生了好多疑问
1、为了确保 GPT 从源头获取所需的所有信息,哪种格式最好?Word、PDF、JSON 等
2、获得最大性能的最佳方法是什么?上传到知识库?上传到网络服务器并使用 API?
3、为了实现最佳的彻底搜索,最好将所有内容按文件(例如章节)分开,还是可以将所有 PDF 分组为 1 个大 PDF,从而失去收集所需信息的能力?
4、为了充分利用源文件,内容管理最佳实践建议是什么?
原来想把所有文件打成压缩包,提示我文件太大,并且不能检索。
调研了一圈,发现将所有文件转换为文本文件,将内容分成较小的文件以提高搜索效率。
最后通过把word文件转为txt,然后每10-20个txt文件合并成一个文件。再把合并后的文件上传到知识库。
文件的转换和合并都通过chatgpt编程实现。以下是提示词和效果。
复制ChatGpt生成的代码
import os
import glob
from docx import Document
def extract_text_from_docx(docx_path):
""" 从.docx文件中提取文本。 """
try:
doc = Document(docx_path)
return "\n".join([para.text for para in doc.paragraphs])
except Exception as e:
print(f"Error reading {docx_path}: {e}")
return ""
def write_text_to_file(text, file_path):
""" 将文本写入文件。 """
with open(file_path, 'w', encoding='utf-8') as file:
file.write(text)
def main(folder_path):
# 遍历文件夹中的所有.docx文件
docx_files = glob.glob(os.path.join(folder_path, "*.docx"))
txt_files = []
for docx_file in docx_files:
text = extract_text_from_docx(docx_file)
txt_file = f"{os.path.splitext(docx_file)[0]}.txt"
write_text_to_file(text, txt_file)
txt_files.append(txt_file)
# 每10个txt文件合并成一个新的txt文件
for i in range(0, len(txt_files), 10):
combined_txt_file = f"combined_{i//10}.txt"
with open(combined_txt_file, 'w', encoding='utf-8') as outfile:
for txt_file in txt_files[i:i+10]:
with open(txt_file, 'r', encoding='utf-8') as infile:
outfile.write(infile.read() + "\n")
if __name__ == "__main__":
folder_path = "path_to_your_folder" # 替换为你的文件夹路径
main(folder_path)
执行上面代码后,完美上传了所有文件。