跳到主要内容

retrieve_utils

非结构化格式

如果安装了'unstructured'库,这些格式将被解析。

split_text_to_chunks

def split_text_to_chunks(text: str,
max_tokens: int = 4000,
chunk_mode: str = "multi_lines",
must_break_at_empty_line: bool = True,
overlap: int = 0)

将长文本分割成最大长度的块。

从PDF中提取文本

def extract_text_from_pdf(file: str) -> str

从PDF文件中提取文本

split_files_to_chunks

def split_files_to_chunks(
files: list,
max_tokens: int = 4000,
chunk_mode: str = "multi_lines",
must_break_at_empty_line: bool = True,
custom_text_split_function: Callable = None
) -> Tuple[List[str], List[dict]]

将一个文件列表分割成最大令牌数的块。

get_files_from_dir

def get_files_from_dir(dir_path: Union[str, List[str]],
types: list = TEXT_FORMATS,
recursive: bool = True)

返回给定目录、URL、文件路径或它们的列表中的所有文件的列表。

parse_html_to_markdown

def parse_html_to_markdown(html: str, url: str = None) -> str

将HTML解析为Markdown。

get_file_from_url

def get_file_from_url(url: str, save_path: str = None) -> Tuple[str, str]

从URL下载文件。

is_url

def is_url(string: str)

如果字符串是有效的URL,则返回True。

从目录创建向量数据库

def create_vector_db_from_dir(dir_path: Union[str, List[str]],
max_tokens: int = 4000,
client: API = None,
db_path: str = "tmp/chromadb.db",
collection_name: str = "all-my-documents",
get_or_create: bool = False,
chunk_mode: str = "multi_lines",
must_break_at_empty_line: bool = True,
embedding_model: str = "all-MiniLM-L6-v2",
embedding_function: Callable = None,
custom_text_split_function: Callable = None,
custom_text_types: List[str] = TEXT_FORMATS,
recursive: bool = True,
extra_docs: bool = False) -> API

从给定目录中的所有文件创建向量数据库,该目录也可以是单个文件或单个文件的URL。我们支持使用chromadb兼容的API来创建向量数据库,如果您已经准备了自己的向量数据库,则不需要使用此函数。

参数:

  • dir_path Union[str, List[str]] - 目录、文件、URL的路径或它们的列表。
  • max_tokens 可选, int - 每个块的最大标记数。默认值为4000。
  • client 可选, API - chromadb 客户端。默认为 None。
  • db_path 可选, str - 指向chromadb的路径。默认是"tmp/chromadb.db"。对于版本 <=0.2.24,默认是 /tmp/chromadb.db
  • collection_name 可选, str - 集合的名称。默认为 "all-my-documents"。
  • get_or_create 可选, bool - 是否获取或创建集合。默认为 False。如果为 True,且集合已存在,则返回该集合。如果集合已存在且 get_or_create 为 False,则抛出 ValueError。
  • chunk_mode 可选, str - 块模式。默认是 "multi_lines"。
  • must_break_at_empty_line 可选, 布尔值 - 是否在空行处换行。默认值为 True。
  • embedding_model 可选参数, str - 使用的嵌入模型。默认为 "all-MiniLM-L6-v2"。如果 embedding_function 不为 None,此参数将被忽略。
  • embedding_function 可选, Callable - 使用的嵌入函数。默认是 None,将使用带有给定embedding_model的 SentenceTransformer。如果你想使用 OpenAI、Cohere、HuggingFace 或其他嵌入函数,可以在这里传递,参考 https://docs.trychroma.com/guides/embeddings 中的示例。
  • custom_text_split_function 可选,可调用 - 自定义函数,用于将字符串拆分为字符串列表。 默认为 None,将使用 autogen.retrieve_utils.split_text_to_chunks 中的默认函数。
  • custom_text_types 可选, List[str] - 要处理的文件类型列表。默认为TEXT_FORMATS。
  • recursive 可选, bool - 是否在dir_path中递归搜索文档。默认为True。
  • extra_docs 可选,布尔型 - 是否在集合中添加更多的文档。默认值为 False

返回:

chromadb 客户端。

query_vector_db

def query_vector_db(query_texts: List[str],
n_results: int = 10,
client: API = None,
db_path: str = "tmp/chromadb.db",
collection_name: str = "all-my-documents",
search_string: str = "",
embedding_model: str = "all-MiniLM-L6-v2",
embedding_function: Callable = None) -> QueryResult

查询一个向量数据库。我们支持与chromadb兼容的API,如果您已经准备了自己的向量数据库和查询函数,这不是必需的。

参数:

  • query_texts List[str] - 用于查询向量数据库的字符串列表。
  • n_results 可选, int - 返回的结果数量。默认值为10。
  • client 可选,API - 兼容chromadb的客户端。默认为None,将使用chromadb客户端。
  • db_path 可选, str - 向量数据库的路径。默认值为"tmp/chromadb.db"。在版本 <=0.2.24 中,默认值为 /tmp/chromadb.db
  • collection_name 可选, str - 集合的名称。默认为 "all-my-documents"。
  • search_string 可选, str - 搜索字符串。只有包含该字符串完全匹配的文档才会被检索。默认值为 ""。
  • embedding_model 可选参数, str - 使用的嵌入模型。默认为 "all-MiniLM-L6-v2"。如果 embedding_function 不为 None,此参数将被忽略。
  • embedding_function 可选, Callable - 使用的嵌入函数。默认是 None,将使用带有给定embedding_model的 SentenceTransformer。如果你想使用 OpenAI、Cohere、HuggingFace 或其他嵌入函数,可以在这里传递,参考 https://docs.trychroma.com/guides/embeddings 中的示例。

返回:

查询结果。格式为:

class QueryResult(TypedDict):
ids: List[IDs]
embeddings: Optional[List[List[Embedding]]]
documents: Optional[List[List[Document]]]
metadatas: Optional[List[List[Metadata]]]
distances: Optional[List[List[float]]]