neuml/txtai · 上手攻略

  • 仓库:neuml/txtai
  • 链接:https://github.com/neuml/txtai
  • 分类:ai(主分类:rag)
  • 作者:Tom
  • 更新:2026-07-08

是什么

txtai 是由 NeuML 维护的一个一体化 AI 框架,核心理念是把语义搜索、LLM 编排和工作流三大能力整合在同一个 Python 包里。

其核心组件是 Embeddings 数据库——本质上是一个融合了稀疏向量索引、密集向量索引、图网络和关系数据库的统一检索层。这使得 txtai 既能做传统向量检索,也能做 SQL 查询、主题建模、图分析。

架构图:

Embeddings Database
├── 向量索引(稀疏 + 密集)
├── 图网络
└── 关系数据库

txtai 底层基于 Hugging Face Transformers、Sentence Transformers 和 FastAPI,Python 3.10+ 可用,Apache-2.0 开源。


解决什么问题

如果你需要同时做以下事情,txtai 可以一体化解决:

  • 语义搜索:不只是关键词匹配,而是理解语义(比如搜"正面反馈"能找到"Correct")
  • RAG(检索增强生成):构建知识库 + LLM 回答的完整 pipeline
  • LLM 工作流编排:将多个 pipeline 串联、聚合业务逻辑
  • 多模态:文本、文档、图片、音视频的 embedding 和检索
  • Agent:让 Embeddings、Pipeline、Workflow 和 Agent 相互连接,自主解决复杂问题
  • 知识图谱构建:从文档中提取实体,构建语义图谱

快速安装

# 基础安装
pip install txtai

# 安装所有可选依赖(包含 pipeline、workflow、api 等)
pip install txtai[all]

# CPU-only 环境(节省体积,不安装 GPU 版 PyTorch)
pip install txtai torch==2.4.1+cpu \
  -f https://download.pytorch.org/whl/torch

# 从源码安装最新版本
pip install git+https://github.com/neuml/txtai

# 轻量最小化安装
pip install txtai_minimal

常用组合

# API 服务(含 uvicorn)
pip install txtai[api]

# LLM pipeline
pip install txtai[pipeline]

# 图分析与主题建模
pip install txtai[graph]

# 组合多个 extra
pip install txtai[pipeline,workflow,api]

⚠️ 注意:完整安装 txtai[all] 依赖较多,建议用虚拟环境。Linux 上使用音频/麦克风 pipeline 需要 PortAudio 系统库(apt install portaudio19-dev);Windows 上可选依赖需要 C++ Build Tools。


核心用法

1. 最简语义搜索(5 行代码)

import txtai

# 创建 embeddings 实例
embeddings = txtai.Embeddings()

# 构建索引
embeddings.index(["Correct", "Not what we hoped", "Great result"])

# 语义搜索——找最接近"positive"的
results = embeddings.search("positive", limit=1)
print(results)
# [(0, 0.298)] → "Correct"(正向语义)

2. 指定模型 + 配置文件

# app.yml
# embeddings:
#   path: sentence-transformers/all-MiniLM-L6-v2

# 用自定义 embedding 模型
embeddings = txtai.Embeddings(
    path="sentence-transformers/all-MiniLM-L6-v2"
)
embeddings.index([
    "猫在沙发上睡觉",
    "狗在院子里奔跑",
    "我喜欢吃苹果"
])
print(embeddings.search("宠物动物", limit=2))

3. 启动 API 服务

# app.yml
embeddings:
  path: sentence-transformers/all-MiniLM-L6-v2

# 启动服务
CONFIG=app.yml uvicorn "txtai.api:app" --host 0.0.0.0 --port 8000

# curl 测试
curl -X GET "http://localhost:8000/search?query=positive"

4. Pipeline — LLM 提示与问答

import txtai

# LLM pipeline:运行 prompt
llm = txtai.LLM("openai/gpt-4")

result = llm("What is retrieval augmented generation?")
print(result)

# 问答 pipeline
qa = txtai.Pipeline("tzw783/question-answering")
context = "The Great Wall is in China. It is over 2000 years old."
print(qa({"question": "Where is the Great Wall?", "context": context}))

5. Workflow — 串联多个 Pipeline

import txtai

# 定义 workflow 配置
workflow = txtai.Workflow([
    {"task": "text", "action": "summary", "model": "facebook/bart-large-cnn"},
    {"task": "text", "action": "translate", "model": "facebook/nmt-en-de"},
])

# 运行
results = workflow.run(["The quick brown fox jumps over the lazy dog."])
for result in results:
    print(result)

6. Agent — 自主解决问题

import txtai

# 创建 Agent,连接 embeddings 知识库 + LLM
agent = txtai.Agent()

# 添加外部工具或知识源
agent.add("knowledge", txtai.Embeddings(path="sentence-transformers/all-MiniLM-L6-v2"))

# 自主回答
answer = agent("Find information about PowerInfer in the knowledge base")
print(answer)

典型适用场景

场景 适用原因
构建 RAG 系统 Embeddings + LLM Pipeline 一体化,无需拼凑多个库
企业知识库问答 支持多模态文档,私有化部署,数据不出本机
语义搜索应用 3 行代码搭建,比单独搭 Elasticsearch + FAISS 更简单
LLM 工作流编排 Workflow 支持串联多个 Pipeline,适合复杂业务逻辑
知识图谱构建 LLM 实体抽取 + Graph 分析,一条链路搞定
Agent 构建 Embeddings × Pipeline × Workflow × Agent 自由组合

坑与注意

  1. Python 版本要求:仅支持 Python 3.10+,低版本请使用 Docker 镜像(neuml/txtai-cpu)。
  2. 完整安装体积大txtai[all] 会拉取 PyTorch GPU 版 + 大量 Transformers 模型,首次安装可能需要 5–10 GB 磁盘空间。
  3. 模型下载:首次运行会自动下载 Sentence Transformers 模型(如 all-MiniLM-L6-v2),需要网络连接或提前缓存。
  4. 向量索引不可追加:调用 index() 后不支持增量更新,需重新全量构建。生产环境建议持久化到数据库 backend。
  5. API 并发:默认 uvicorn 单 worker,生产部署建议用 uvicorn --workers 4 + Nginx 代理。
  6. Embedding 模型选择:默认模型较小,高召回场景可换成 BAAI/bge-large-zh-v1.5(中文)或 thenlper/gte-large
  7. Agent 处于早期阶段:功能相对基础,复杂 Agent 场景建议评估 LangChain/LangGraph。

与同类对比

特性 txtai LangChain LlamaIndex ChromaDB
向量检索
语义搜索 需额外组件 需额外组件
LLM Pipeline
工作流编排 部分
Agent 基础
多模态 部分 部分
轻量/上手 ⭐⭐⭐ ⭐⭐ ⭐⭐⭐ ⭐⭐⭐⭐
生产成熟度 ⭐⭐⭐ ⭐⭐⭐⭐ ⭐⭐⭐⭐ ⭐⭐⭐

一句话总结:txtai 是"小而全"的 AI 框架,适合不想拼凑 5 个库、想要一个包搞定搜索+RAG+工作流的场景。如果需要更自由的 Agent 编排或更大生态,LangChain/LlamaIndex 更成熟;如果只需要轻量向量数据库,ChromaDB 更简单。


一句话推荐结论

如果你想一个 Python 包解决语义搜索、RAG pipeline 和 LLM 工作流,不想左手 LangChain 右手 FAISS 右手 LlamaIndex,txtai 是目前最省心的选择——开箱即用,代码行数从 50 行压缩到 5 行。