深度学习系列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()
相关推荐
那个村的李富贵7 小时前
光影魔术师:CANN加速实时图像风格迁移,让每张照片秒变大师画作
人工智能·aigc·cann
腾讯云开发者8 小时前
“痛点”到“通点”!一份让 AI 真正落地产生真金白银的实战指南
人工智能
CareyWYR8 小时前
每周AI论文速递(260202-260206)
人工智能
hopsky9 小时前
大模型生成PPT的技术原理
人工智能
禁默10 小时前
打通 AI 与信号处理的“任督二脉”:Ascend SIP Boost 加速库深度实战
人工智能·信号处理·cann
心疼你的一切10 小时前
昇腾CANN实战落地:从智慧城市到AIGC,解锁五大行业AI应用的算力密码
数据仓库·人工智能·深度学习·aigc·智慧城市·cann
AI绘画哇哒哒10 小时前
【干货收藏】深度解析AI Agent框架:设计原理+主流选型+项目实操,一站式学习指南
人工智能·学习·ai·程序员·大模型·产品经理·转行
数据分析能量站10 小时前
Clawdbot(现名Moltbot)-现状分析
人工智能
那个村的李富贵10 小时前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
二十雨辰10 小时前
[python]-AI大模型
开发语言·人工智能·python