indexify开源程序包、适用于数据密集型生成式 AI 应用的实时服务引擎、提取和索引 PDF 文档、汇总网站、转录和汇总音频文件、对象检测和描述、知识图谱 RAG 和问答

一、软件介绍

文末提供下载

       Indexify 简化了构建和提供持久的多阶段数据密集型工作流的过程,并将其作为 HTTP API 或 Python 远程 API 公开。Indexify 是开源核心计算引擎,为 Tensorlake 的无服务器工作流引擎提供支持,用于处理非结构化数据。Indexify 是一个多功能的数据处理框架,适用于各种使用案例,包括:提取和索引 PDF 文档、汇总网站、转录和汇总音频文件、对象检测和描述、知识图谱 RAG 和问答。

二、主要特点

  • 多云/数据中心/区域:在来自其他云的工作流中利用计算,只需很少的麻烦和配置。
  • 分布式处理:跨计算机并行运行函数,用于横向扩展用例。
  • 资源分配:跨 GPU 和 CPU 实例跨工作流,以便将功能分配给其最佳硬件。
  • 动态路由:根据条件分支逻辑将数据路由到分布在集群上的不同专用计算函数。

三、安装

安装用于构建工作流的 Tensorlake SDK 和 Indexify CLI。

pip install indexify tensorlake
 

四、一个最小的例子

       修饰的 @tensorlake_function() 函数是工作流 API 中的计算单位。这些函数可以与其他函数具有数据依赖关系。

        Tensorlake 函数是持久的,即,如果函数崩溃或运行函数的节点丢失,它将在具有相同输入的另一个正在运行的实例上自动重试。

        您可以在集群上运行任意数量的函数实例,当其他应用程序并行调用工作流时,输入将自动在它们之间进行负载均衡。

       下面的示例是一个工作流 API,它接受一些文本,嵌入文件,并将其写入本地向量数据库。每个函数都可以放置在不同类别的机器上(用于分块和写入数据库的纯 CPU 机器,用于嵌入的 NVIDIA GPU)

from pydantic import BaseModel
from tensorlake import tensorlake_function, Graph, Image, TensorlakeCompute
from typing import List, Union

# Define Input and Outputs of various functions in your workflow
class Text(BaseModel):
    text: str


class TextChunk(BaseModel):
    chunk: str
    page_number: int


class ChunkEmbedding(BaseModel):
    text: str
    embedding: List[float]

# Define an image capable of running the functions. Each image
# can have their own image
embedding_image = (
    Image()
    .name("text_embedding_image")
    .run("pip install langchain")
    .run("pip install sentence_transformer")
    .run("pip install langchain-text-splitters")
    .run("pip install chromadb")
    .run("pip install uuid")
)


# Chunk the text for embedding and retrieval
@tensorlake_function(input_encoder="json", image=embedding_image)
def chunk_text(input: dict) -> List[TextChunk]:
    text = Text.model_validate(input)
    from langchain_text_splitters import RecursiveCharacterTextSplitter

    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=1000,
        chunk_overlap=20,
        length_function=len,
        is_separator_regex=False,
    )
    texts = text_splitter.create_documents([text.text])
    return [
        TextChunk(chunk=chunk.page_content, page_number=i)
        for i, chunk in enumerate(texts)
    ]


# Embed a single chunk.
# Note: (Automatic Map) Indexify automatically parallelize functions when they consume an
# element from functions that produces a List. In this case each text chunk is processed
# in parallel by an Embedder function
class Embedder(TensorlakeCompute):
    name = "embedder"
    image = embedding_image

    # TensorlakeCompute function allows initializing resources in the constructors
    # and they are not unloaded again until the compute object is destroyed.  
    def __init__(self):
        from sentence_transformers import SentenceTransformer
        self._model = SentenceTransformer("all-MiniLM-L6-v2")

    def run(self, chunk: TextChunk) -> ChunkEmbedding:
        embeddings = self._model.encode(chunk.chunk)
        return ChunkEmbedding(text=chunk.chunk, embedding=embeddings)


class EmbeddingWriter(TensorlakeCompute):
    name = "embedding_writer"
    image = embedding_image

    def __init__(self):
        import chromadb

        self._chroma = chromadb.PersistentClient("./chromadb_tensorlake")
        self._collection = collection = self._chroma.create_collection(
            name="my_collection", get_or_create=True
        )

    def run(self, embedding: ChunkEmbedding) -> None:
        import uuid

        self._collection.upsert(
            ids=[str(uuid.uuid4())],
            embeddings=[embedding.embedding],
            documents=[embedding.text],
        )


# Constructs a compute graph connecting the three functions defined above into a workflow that generates
# runs them as a pipeline
graph = Graph(
    name="text_embedder",
    start_node=chunk_text,
    description="Splits, embeds and indexes text",
)
graph.add_edge(chunk_text, Embedder)
graph.add_edge(Embedder, EmbeddingWriter)


 本地测试


您可以在仅安装 tensorlake 包的情况下在本地测试工作流程。

invocation_id = graph.run(input={"text": "This is a test text"})
print(f"Invocation ID: {invocation_id}")

# You can get output from each function of the graph
embedding = graph.output(invocation_id, "embedder")
print(embedding)


将工作流部署为 API


       从大局来看,您将在计算机上部署 Indexify Server,并分别为工作流程中的每个函数运行容器。

        但首先,我们将展示如何在单台机器上本地执行此作。

 启动 Server


        从此处下载 Server 版本。打开终端并启动服务器。

./indexify-server -dev


 启动 Executor


       Executor 是负责运行函数的组件。在安装了所有依赖项的终端上,以 mode 启动 development 执行程序。

indexify-cli executor --dev


设置环境变量 -

export INDEXIFY_URL=http://localhost:8900
将工作流程中的代码更改为以下内容 -

from tensorlake import RemoteGraph
RemoteGraph.deploy(graph)
此时,您现在在 Indexify Server 上有一个 Graph 终端节点,可以作为 API 从任何应用程序调用。

 调用 Graph


如果第一个函数配置为接受 JSON 有效负载,则可以将 Graph 作为 REST API 调用。

curl -X 'POST' http://localhost:8900/namespaces/default/compute_graphs/text_embedder/invoke_object -H 'Content-Type: application/json' -d '{"input": {"text": "hello world"}}'
这将返回一个调用 ID - {"id":"55df51b4a84ffc69"} 。Invocation Id (调用 ID) 可用于获取工作流在处理该输入时的状态,以及从图表中获取任何输出。

获取 Embedding 函数的输出 -

curl -X GET http://localhost:8900/namespaces/default/compute_graphs/text_embedder/invocations/55df51b4a84ffc69/outputs
这将返回函数的所有输出 -

{"status":"finalized","outputs":[{"compute_fn":"chunk_text","id":"89de2063abadf5d3","created_at":1738110077424},{"compute_fn":"embedder","id":"4908f00d711c4cd1","created_at":1738110081015}],"cursor":null}
您现在可以检索其中一个输出 -

curl -X GET http://localhost:8900/namespaces/default/compute_graphs/text_embedder/invocations/55df51b4a84ffc69/fn/embedder/output/4908f00d711c4cd1 
您也可以从 Python 调用 Graph

from tensorlake import RemoteGraph
remote_graph = RemoteGraph.by_name("text_embedder")


部署到生产集群


将工作流部署到生产环境的过程分为两个步骤 -

 1. 运行服务器
docker run -it -p 8900:8900 tensorlake/indexify-server 
2. 构建和部署 Function Containers
首先构建和部署包含代码的所有 python 和系统依赖项的容器映像。它们可以使用标准 Docker 构建系统构建。在此示例中,我们有一个可以运行所有函数的映像。您可以将它们分开以减小图像的大小,以用于更复杂的项目。
indexify-cli build-image workflow.py
这将构建以下图像,如上面的工作流代码中所定义 - text_embedding_image

下一步:部署容器
docker run --it text_embedding_image indexify-cli executor --function default:text_embedder:chunk_document
docker run --it text_embedding_image indexify-cli executor --function default:text_embedder:embed_chunk
docker run --it text_embedding_image indexify-cli executor --function default:text_embedder:write_to_db
容器被视为临时的,在单个容器上只调度一种类型的函数。我们启动了两个容器,用于在每个容器中放置一个函数。

 缩放到零


如果您在晚上关闭容器,Indexify 不会抱怨。即使找不到运行函数的机器,它仍将接受来自外部系统的新 API 调用。它只会将它们排队,然后等待函数出现。它发出待处理任务的遥测数据,等待放置在可用作 Autoscaler 输入的函数上。

 就是这样!

生产就绪型分布式和始终在线的数据工作流


您已经构建了一个工作流 API,该 API 是持久的,能够在多种硬件上分发,并且可以处理无限的规模。
您可以使用阳光下的任何 Python 库、任何系统包,并可以使用您最喜欢的工具将它们打包到容器映像中。
部署代码就像将代码上传到服务器一样简单,它们会自动分发和更新。
 ️ 路线图
 ⏳ 调度
函数批处理:在单个批次中处理多个函数以提高效率。
数据本地化执行:通过在已经存在中间输出的机器上优先执行来提高性能。
Reducer 优化:通过批处理减少的函数调用的串行执行来优化性能。
并行调度:通过跨多台计算机并行执行来减少延迟。
Cyclic Graph 支持:通过利用图形中的循环实现更灵活的代理行为。
临时图:执行多阶段推理和检索,而无需保留中间输出。
Data Loader Functions:使用 yield 关键字将值随时间推移流式传输到图形中。


 ️ 开发工具包


TypeScript SDK:构建用于在 Typescript 中编写工作流的 SDK。

五、软件下载

夸克网盘分享

本文信息来源于GitHub作者地址:GitHub - tensorlakeai/indexify: A realtime serving engine for Data-Intensive Generative AI Applications

你可能感兴趣的:(pdf,数据结构,算法,深度优先,逻辑回归,宽度优先,开源)