深度学习系列85:sqlite-ai系列

1. 介绍

目前用到三个插件:

sqlite-ai:使用sql来操作llm

sqlite-vec:类似faiss的向量检索插件

sqlite-rag:基于sqlite的混合rag插件

2. sqlite-ai

使用pip进行安装,下面是使用示例:

复制代码
import importlib.resources
import sqlite3
conn = sqlite3.connect("example.db")
ext_path = importlib.resources.files("sqliteai.binaries.cpu") / "ai"
conn.enable_load_extension(True)
conn.load_extension(str(ext_path))
conn.enable_load_extension(False)
conn.execute("SELECT llm_model_load('../MiniCPM4-0.5B-bf16.gguf', 'n_predict=4096,n_gpu_layers=99');")
conn.execute("SELECT llm_context_create('n_ctx=512,n_threads=6,n_batch=128');")
conn.execute("SELECT llm_chat_respond('你是谁?replay within 10 words');").fetchone()[0]

3. sqlite-vec

使用pip进行安装,下面是使用示例:

复制代码
import sqlite3
import sqlite_vec
import sqlite_lembed
from typing import List
db = sqlite3.connect(":memory:")
db.enable_load_extension(True)
sqlite_vec.load(db)
sqlite_lembed.load(db)
db.enable_load_extension(False)
db.executef"""INSERT INTO temp.lembed_models(name, model) select 'default', lembed_model_from_file('m3e-base.f16.gguf')""")
db.execute("""create table articles (headline text);""")
db.execute("""create virtual table vec_articles using vec0(headline_embeddings float[768]);""")
db.execute(f"""insert into articles VALUES ('{text[:500]}');""")
db.execute("""insert into vec_articles(rowid, headline_embeddings) select rowid, lembed(headline) from articles;""")
db.execute(f"""with matches as (SELECT rowid,distance FROM vec_articles WHERE headline_embeddings MATCH lembed('天气') and k = 2 ORDER BY distance)
select headline,100/(1+distance) from matches left join articles on articles.rowid = matches.rowid;""").fetchall()
相关推荐
Mintopia几秒前
agent-cli 哪家强?别只看“能跑”,要看“能交付”
人工智能
kishu_iOS&AI3 分钟前
PyCharm 结合 uv 进行 AI 大模型开发
人工智能·pycharm·大模型·uv
币之互联万物6 分钟前
LLM 偏好算法解析:大语言模型内容收录倾向与 NEOXGEO 技术底蕴
人工智能·算法·语言模型
Mintopia9 分钟前
衡量AI水平的六个核心指标:别再只看跑分了
人工智能
咚咚王者9 分钟前
人工智能之语言领域 自然语言处理 第十七章 多模态预训练模型
人工智能·自然语言处理
盼小辉丶13 分钟前
PyTorch实战(36)——PyTorch自动机器学习
人工智能·pytorch·深度学习·自动机器学习
郝学胜-神的一滴13 分钟前
PyTorch 张量基础:零张量/一张量/指定值张量全解析
人工智能·pytorch·python
7yewh13 分钟前
Dense / 全连接层 / Gemm — 综合全局特征理解与运用
网络·人工智能·python·深度学习·cnn
智算菩萨15 分钟前
AGI神话:人工通用智能的幻象如何扭曲与分散数字治理的注意力
论文阅读·人工智能·深度学习·ai·agi
Roy_Sashulin15 分钟前
基于AI的Java编程平台
java·开发语言·人工智能·sashulin·deepseek