LangChain 速率控制向量化工具,支持批处理、自动降速、向量缓存
Project description
ratelimited-embedder
LangChain 速率控制向量化工具,支持批处理、自动降速、向量缓存、硬件建议。
安装
pip install ratelimited-embedder
开发模式:
git clone <repo>
cd ratelimited-embedder
pip install -e ".[dev]"
快速上手
from langchain_ollama import OllamaEmbeddings
from ratelimited_embedder import RateControlledEmbedder, wrap_embeddings
embeddings = OllamaEmbeddings(model="qwen3-embedding:0.6b")
wrapped = wrap_embeddings(embeddings, cache_path="vector_cache.db")
embedder = RateControlledEmbedder(
embeddings=wrapped,
batch_size=16,
delay=0.5,
slow_threshold=2.0,
)
from langchain_core.documents import Document
chunks = [Document(page_content=f"文档片段 {i}") for i in range(100)]
vectorstore = embedder.build_vectorstore(chunks, save_path="faiss_index")
功能
- 速率控制 — 分批向量化,可配置 batch_size / delay
- 自动降速 — 单批耗时超过阈值时自动减半 batch_size、增大 delay
- 向量缓存 — SQLite + MD5,避免重复计算
- 硬件建议 — 根据内存/CPU 自动推荐参数
- 进度条 — tqdm 实时显示进度
- 流式预览 — 每批完成后输出统计
API
RateControlledEmbedder
RateControlledEmbedder(embeddings, batch_size=16, delay=0.5, slow_threshold=2.0)
build_vectorstore(chunks, save_path, progress_callback)→ FAISSget_stats()→ dictset_rate(batch_size, delay)get_rate_suggestion()→ dict (静态方法)
wrap_embeddings
wrap_embeddings(embeddings, cache_path="vector_cache.db")
包装 LangChain Embeddings,自动启用 SQLite 向量缓存。
VectorCache
VectorCache(db_path="vector_cache.db")
get(text)→ list[float] | Noneput(text, vector, metadata)stats()→ dict
License
Copyright (c) 2025 oi-star
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
A copy of the license is also available in the LICENSE file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ratelimited_embedder-0.1.0.tar.gz.
File metadata
- Download URL: ratelimited_embedder-0.1.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67f5e1a12ab2bc05822a9cc1c3e13443706d4af5bc830548b25ce2b8d3a759d1
|
|
| MD5 |
c0feeaa3b8dc4a235ad89f1d29a6b215
|
|
| BLAKE2b-256 |
50b8cc69a8bf0ef955985291388e941f350be7836f60a8ff0690efac1569ca0c
|
File details
Details for the file ratelimited_embedder-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ratelimited_embedder-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97108db65be6c629fe59770ff6aaf6b2db6478bb9453a016565c60d8bef9b465
|
|
| MD5 |
9d2590fb0e75d8656128000da5f26b04
|
|
| BLAKE2b-256 |
182fe8e6a3da335b2a340d8e598824a4b2703da87a07f71e00c7da09bd880ac2
|