use existed files in local folder to populate database and/or vector store. set parameter `mode` to: recreate_vs: recreate all vector store and fill info to database using existed files in local folder fill_info_only(disabled): do not create vector store, fill info to db usi
(
kb_names: List[str],
mode: Literal["recreate_vs", "update_in_db", "increment"],
vs_type: Literal["faiss", "milvus", "pg", "chromadb"] = Settings.kb_settings.DEFAULT_VS_TYPE,
embed_model: str = get_default_embedding(),
chunk_size: int = Settings.kb_settings.CHUNK_SIZE,
chunk_overlap: int = Settings.kb_settings.OVERLAP_SIZE,
zh_title_enhance: bool = Settings.kb_settings.ZH_TITLE_ENHANCE,
)
| 109 | |
| 110 | |
| 111 | def folder2db( |
| 112 | kb_names: List[str], |
| 113 | mode: Literal["recreate_vs", "update_in_db", "increment"], |
| 114 | vs_type: Literal["faiss", "milvus", "pg", "chromadb"] = Settings.kb_settings.DEFAULT_VS_TYPE, |
| 115 | embed_model: str = get_default_embedding(), |
| 116 | chunk_size: int = Settings.kb_settings.CHUNK_SIZE, |
| 117 | chunk_overlap: int = Settings.kb_settings.OVERLAP_SIZE, |
| 118 | zh_title_enhance: bool = Settings.kb_settings.ZH_TITLE_ENHANCE, |
| 119 | ): |
| 120 | """ |
| 121 | use existed files in local folder to populate database and/or vector store. |
| 122 | set parameter `mode` to: |
| 123 | recreate_vs: recreate all vector store and fill info to database using existed files in local folder |
| 124 | fill_info_only(disabled): do not create vector store, fill info to db using existed files only |
| 125 | update_in_db: update vector store and database info using local files that existed in database only |
| 126 | increment: create vector store and database info for local files that not existed in database only |
| 127 | """ |
| 128 | |
| 129 | def files2vs(kb_name: str, kb_files: List[KnowledgeFile]) -> List: |
| 130 | result = [] |
| 131 | for success, res in files2docs_in_thread( |
| 132 | kb_files, |
| 133 | chunk_size=chunk_size, |
| 134 | chunk_overlap=chunk_overlap, |
| 135 | zh_title_enhance=zh_title_enhance, |
| 136 | ): |
| 137 | if success: |
| 138 | _, filename, docs = res |
| 139 | print( |
| 140 | f"正在将 {kb_name}/{filename} 添加到向量库,共包含{len(docs)}条文档" |
| 141 | ) |
| 142 | kb_file = KnowledgeFile(filename=filename, knowledge_base_name=kb_name) |
| 143 | kb_file.splited_docs = docs |
| 144 | kb.add_doc(kb_file=kb_file, not_refresh_vs_cache=True) |
| 145 | result.append({"kb_name": kb_name, "file": filename, "docs": docs}) |
| 146 | else: |
| 147 | print(res) |
| 148 | return result |
| 149 | |
| 150 | kb_names = kb_names or list_kbs_from_folder() |
| 151 | for kb_name in kb_names: |
| 152 | start = datetime.now() |
| 153 | kb = KBServiceFactory.get_service(kb_name, vs_type, embed_model) |
| 154 | if not kb.exists(): |
| 155 | kb.create_kb() |
| 156 | |
| 157 | # 清除向量库,从本地文件重建 |
| 158 | if mode == "recreate_vs": |
| 159 | kb.clear_vs() |
| 160 | kb.create_kb() |
| 161 | kb_files = file_to_kbfile(kb_name, list_files_from_folder(kb_name)) |
| 162 | result = files2vs(kb_name, kb_files) |
| 163 | kb.save_vector_store() |
| 164 | # # 不做文件内容的向量化,仅将文件元信息存到数据库 |
| 165 | # # 由于现在数据库存了很多与文本切分相关的信息,单纯存储文件信息意义不大,该功能取消。 |
| 166 | # elif mode == "fill_info_only": |
| 167 | # files = list_files_from_folder(kb_name) |
| 168 | # kb_files = file_to_kbfile(kb_name, files) |
nothing calls this directly
no test coverage detected