深度学习系列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()
相关推荐
IT_陈寒2 小时前
SpringBoot 3.0实战:5个高频踩坑点及性能优化方案,让你的应用吞吐量提升40%
前端·人工智能·后端
yiersansiwu123d2 小时前
从生成式到智能体:AI的下一站是万物互联的智能协同
人工智能
liulanba2 小时前
机器学习评估指标详解 - 入门篇
人工智能·机器学习
wenzhangli72 小时前
2025智能家居创新大会:AI热潮下的冷思考,机遇窗口正在收窄
人工智能
Godspeed Zhao2 小时前
自动驾驶中的传感器技术80——Sensor Fusion(3)
人工智能·机器学习·自动驾驶
Niuguangshuo2 小时前
渐进式GAN (ProGAN):高分辨率图像生成的革命
人工智能·机器学习·生成对抗网络
张哈大2 小时前
免费薅国产旗舰 LLM!GLM-4.7+MiniMax-M2.1
人工智能·python
wheeldown2 小时前
AI Ping:大模型时代的“性能罗盘”——免费新上线GLM-4.7 与 MiniMax M2.1 实测
人工智能
大刘讲IT2 小时前
精准检索-数据交互-专业交付:2026企业AI落地的三维价值重构
人工智能·程序人生·重构·交互·创业创新·制造