当我使用 Python 和 langchain 将文档分割成块时,为什么会出现 TypeError:字符串索引必须是整数,而不是“str”?

问题描述 投票:0回答:1

以下是我正在使用 Python 和 langchain 将 PDF 文档提取到矢量存储中的一些代码,以用作我正在开展的人工智能计划的参考手册。

从本质上讲,人工智能将成为在一组特定文档(主要是编程参考手册)中查找答案的参考工具。

这是我用 Python 编写的代码,它可以将 PDF 转换为可以通过打印到屏幕输出的文本,但是当它将它分成几块时,我得到以下结果:

TypeError: string indices must be integers, not 'str'

代码如下:

# Description: This script is used to extract text from a PDF file and store it in a database.
import tika
from tika import parser
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma

# Parse the PDF
text = parser.from_file('..\mql5_new_compressed.pdf')["content"]

# Initialize the text splitter
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)

# print(text["content"])

# Split the text into chunks
chunks = text_splitter.create_documents([text["content"]])

我需要对这段文本进行分块,因为我要将其放入矢量存储中作为我的搜索数据库。

python py-langchain pdf-conversion vectorstore
1个回答
0
投票

我已经声明了两次相同的变量名,“内容”,在这里:

# Parse the PDF
text = parser.from_file('..\mql5_new_compressed.pdf')["content"]

然后这里:

chunks = text_splitter.create_documents([text["content"]])

我需要做的就是从文本声明中删除第一个[“内容”],它就起作用了。

© www.soinside.com 2019 - 2024. All rights reserved.