深度学习系列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()
相关推荐
xixixi777773 分钟前
英伟达Agent专用全模态模型出击,仿冒AI智能体泛滥成灾,《AI伦理安全指引》即将落地——AI治理迎来“技术-风险-规范”三重奏
人工智能·5g·安全·ai·大模型·英伟达·智能体
直奔標竿5 分钟前
Java开发者AI转型第二十六课!Spring AI 个人知识库实战(五)——联网搜索增强实战
java·开发语言·人工智能·spring boot·后端·spring
数据皮皮侠AI9 分钟前
中国城市可再生能源数据集(2005-2021)|顶刊 Sci Data 11 种能源面板
大数据·人工智能·笔记·能源·1024程序员节
G311354227313 分钟前
如何用 QClaw 龙虾做一个规律作息健康助理 Agent
大数据·人工智能·ai·云计算
幂律智能14 分钟前
零售行业合同管理数智化转型解决方案
大数据·人工智能·零售
旺财矿工16 分钟前
零基础搭建 OpenClaw 2.6.6 Win11 本地化运行环境
人工智能·openclaw·小龙虾·龙虾·openclaw安装包
九成宫17 分钟前
动手学深度学习PyTorch版初步安装过程
人工智能·pytorch·深度学习
Traving Yu17 分钟前
Prompt提示词工程
人工智能·prompt
NOCSAH18 分钟前
统好AI CRM功能解析:智能录入与跟进
人工智能
He少年20 分钟前
【AI 辅助编程做设备数据采集:一个真实项目的迭代复盘(OpenSpec 驱动)】
人工智能