使用llama-index与Qdrant DB无法读取目录文件的问题排查
问题排查与修复方案
核心错误点及修复:
- QdrantClient初始化错误:代码中
QdrantClient("\":memory:\"")的参数包含多余转义引号,导致客户端初始化异常。若使用内存模式,直接传入:memory:即可;若需使用本地磁盘存储的Qdrant实例,需指定path参数指向Qdrant专属数据目录(注意与存放demo.txt的目录区分,若要共用也可指定对应路径)。 - 集合名称参数错误:
collection_name="\"customer_help\""中的转义引号完全多余,会导致集合名称识别异常,应改为collection_name="customer_help"。 - 查询语句格式错误:
query_engine.query("\"What is her name?\"")中的转义引号属于无效格式,直接传入查询字符串"What is her name?"即可。 - 重复索引操作:
GPTVectorStoreIndex.from_documents已完成文档的索引构建,后续循环调用index.update(document)属于冗余操作,需删除。 - 返回值处理错误:直接返回
JsonResponse(response)会报错,因为response是llama-index的Response对象,需提取其文本内容(如response.response)后再返回。
修正后的代码:
openai.api_key = settings.OPENAI_API_KEY @require_GET def query_documents(request): try: # 若使用本地磁盘存储的Qdrant,替换为实际数据目录,示例: # client = QdrantClient(path="/home/komica/git/playbookhub/api/qdrant_data") # 若使用内存模式,直接传":memory:" client = QdrantClient(":memory:") data_path = '/home/komica/git/playbookhub/api/data' documents = SimpleDirectoryReader(data_path).load_data() vector_store = QdrantVectorStore(client=client, collection_name="customer_help") storage_context = StorageContext.from_defaults(vector_store=vector_store) # 一次性完成索引构建,无需重复更新 index = GPTVectorStoreIndex.from_documents(documents, storage_context=storage_context) query_engine = index.as_query_engine() response = query_engine.query("What is her name?") # 提取响应文本返回 return JsonResponse({'answer': response.response}) except Exception as e: error_message = str(e) response = { 'error': error_message } return JsonResponse(response, status=500)
内容的提问来源于stack exchange,提问作者Komica Verma
相关产品推荐
相关产品推荐

