Skip to main content

SQL-style semantic query language and retrieval middleware for Agents & RAG

Project description

NLQL

PyPI version Python License: MIT Documentation

NLQL 让你用类似 SQL 的语句做语义检索。把"从文本里找相关内容"这件事,变得像查数据库一样直接——相关度计算、过滤、排序写在一条查询里,不再需要拼凑 embedding 调用和后处理代码。

适合 Agent 与 RAG 应用:查询本身就是结构化数据,可以直接作为大模型的工具调用载体。

它长什么样

import nlql

engine = nlql.Engine(nlql.FakeEmbedder())  # 或 OpenAIEmbedder,以及任意 Embedder 实现
engine.add_text("AI agents plan tasks and call tools.", metadata={"status": "published"})
engine.add_text("Banana bread needs flour and sugar.", metadata={"status": "draft"})

for unit in engine.search('''
    SELECT SENTENCE
    LET rel = SIMILARITY(content, "autonomous agents")
    WHERE rel >= 0.2 AND meta.status == "published"
    ORDER BY rel DESC
    LIMIT 5
'''):
    print(f"{unit.scores['rel']:+.3f}  {unit.content}")

语句和 SQL 几乎一样:SELECT 指定返回粒度,LET 算相关度,WHERE 过滤,ORDER BY / LIMIT 排序限量。

特性

  • 一条语句表达完整意图 —— 相关度、过滤、排序集中在一处,不再散在业务代码里
  • 三种写法,结果一致 —— SQL 语句、Python 链式构造、JSON IR,都编译到同一份内部表示
  • 后端可插拔 —— 内置存储开箱即用;切换 Qdrant / Faiss / Chroma / HnswLib / pgvector 只需改一行
  • 召回 + 重排两段式 —— 向量召回后挂重排器,提升结果准确性
  • 多模态 —— 文本与图像在同一向量空间,用文字检索图像
  • 可解释 —— engine.explain() 输出查询的执行计划

安装

pip install python-nlql

可选依赖:

命令 用途
pip install "python-nlql[faiss]" Faiss 后端
pip install "python-nlql[hnsw]" HnswLib 后端(适合大数据量)
pip install "python-nlql[qdrant]" Qdrant 后端
pip install "python-nlql[chroma]" Chroma 后端
pip install "python-nlql[pgvector]" Postgres + pgvector 后端
pip install "python-nlql[local]" 本地 sentence-transformers / CLIP / cross-encoder
pip install "python-nlql[loaders]" 加载 DOCX / PDF 文件

切换后端

切换存储后端只需一行,写入与查询代码完全不变:

from nlql.store.qdrant_store import QdrantStore
engine = nlql.Engine(embedder, store=QdrantStore(location=":memory:"))

文档

完整文档、教程与 API 参考:https://natural-language-query-language.github.io/python-nlql/

更多示例见 examples/ 目录。

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_nlql-0.2.0.tar.gz (194.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_nlql-0.2.0-py3-none-any.whl (105.9 kB view details)

Uploaded Python 3

File details

Details for the file python_nlql-0.2.0.tar.gz.

File metadata

  • Download URL: python_nlql-0.2.0.tar.gz
  • Upload date:
  • Size: 194.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for python_nlql-0.2.0.tar.gz
Algorithm Hash digest
SHA256 208e8b9ccbbe33984dcf27797f222703524a61b42e6e9e77dc61ffe431ecb265
MD5 ac5781d21d123613a9d6f49eaa7bcf79
BLAKE2b-256 1c9cfdb8a721e8f6b55503e909dbbe186a966dac0154eb77b1a1108c631f93f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_nlql-0.2.0.tar.gz:

Publisher: publish.yml on natural-language-query-language/python-nlql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_nlql-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: python_nlql-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 105.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for python_nlql-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54ea06fbae4aaa01437e563aa98ccb70445c205cf5d96f62de86c312e6cf11bb
MD5 7f7aa7ae44c49df92190e78c83a8217b
BLAKE2b-256 c65193a163042b717403bb168c576be8e8899cb71fc07f428ff1f3d76e126577

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_nlql-0.2.0-py3-none-any.whl:

Publisher: publish.yml on natural-language-query-language/python-nlql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page