深度学习系列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()
相关推荐
半个落月4 小时前
从数据集划分理解大模型的数据工程
人工智能
用户8299792943935 小时前
一文带你彻底搞懂claude code中的上下文压缩
人工智能
IT_陈寒5 小时前
Vue的这个响应式陷阱让我熬到凌晨三点
前端·人工智能·后端
冬奇Lab15 小时前
Workflow 系列(01):基础理论——三种执行模型与 Anthropic 5 种模式
人工智能·agent·工作流引擎
冬奇Lab15 小时前
每日一个开源项目(第143篇):page-agent - 纯 JS 的网页 GUI Agent,无需截图、无需插件、无需后端
前端·人工智能·agent
程序员cxuan17 小时前
虽迟但到!GPT-5.6 终于来了!
人工智能·后端·程序员
ZhengEnCi19 小时前
Q03-UI设计进阶技巧-让界面更高级的7个核心原则
人工智能
IT_陈寒19 小时前
React的这个渲染问题连官方文档都没说清楚
前端·人工智能·后端
不加辣椒20 小时前
第12章 工具调用与 Agent 提示工程
人工智能
用户16931761726620 小时前
前端给AI消息做日期分组与时间线
人工智能