llama-index - 我们如何将文档列表添加到 llama-index 中的现有索引中?

标签 llama-index

我有一个使用 GPTVectorStoreIndex 创建的现有索引。但是,当我尝试使用 insert 方法将新文档添加到现有索引时,出现以下错误:

AttributeError:“list”对象没有属性“get_text”

我更新索引的代码如下:

max_input_size = 4096
num_outputs = 5000
max_chunk_overlap = 256
chunk_size_limit = 3900
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
    
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)

directory_path = "./trial_docs"
file_metadata = lambda x : {"filename": x}
reader = SimpleDirectoryReader(directory_path, file_metadata=file_metadata)
    
documents = reader.load_data()
print(type(documents))
index.insert(document = documents, service_context = service_context)

最佳答案

我做对了,我犯的错误是将文档作为一个整体传递,这是一个 List 对象。正确的更新方式如下

max_input_size = 4096
num_outputs = 5000
max_chunk_overlap = 256
chunk_size_limit = 3900
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
    
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)

directory_path = "./trial_docs"
file_metadata = lambda x : {"filename": x}
reader = SimpleDirectoryReader(directory_path, file_metadata=file_metadata)
    
documents = reader.load_data()
print(type(documents))
for d in documents:
    index.insert(document = d, service_context = service_context)

关于llama-index - 我们如何将文档列表添加到 llama-index 中的现有索引中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76304374/

相关文章:

python - LlamaIndex 与 ChatGPT 检索答案的时间过长

python - Llamaindex 无法将索引持久保存到 Chroma DB 并稍后加载

python - ChatGPT API 定制训练的 AI 聊天机器人对 Python 查询应答 "None"

openai-api - 如何在 LlamaIndex 中支持 OpenAI 的 Chat Completions API 格式?

python-3.x - 使用 llama 索引为商业管道创建嵌入

python - 有没有办法从我从 llama-index 获得的响应中流式传输 Fastapi 中的输出

openai-api - OpenAI 微调 API : Why would I use LlamaIndex or LangChain instead of fine-tuning a model?