屏幕文字智能提取革命:告别手动输入的新时代办公利器
2025/12/18 14:02:30
创建包含向量字段的表
CREATETABLEproducts(idSERIALPRIMARYKEY,nameVARCHAR(100),descriptionTEXT,embedding VECTOR(1536)-- 假设使用1536维向量);插入向量数据
INSERTINTOproducts(name,description,embedding)VALUES('智能手机','高端旗舰手机','[0.1, 0.2, ..., 0.5]');向量相似度查询(以pgvector为例)
SELECTid,name,1-(embedding<=>'[0.3, 0.1, ..., 0.4]')ASsimilarityFROMproductsORDERBYembedding<=>'[0.3, 0.1, ..., 0.4]'LIMIT10;创建向量索引
CREATEINDEXONproductsUSINGivfflat(embedding vector_l2_ops)WITH(lists=100);-- 针对IVFFlat索引案例1:商品推荐系统
基于用户浏览历史的向量相似度推荐
# 假设已获取用户浏览记录的向量表示user_vector=[0.2,0.15,...,0.3]# 使用pgvector查询相似商品query=""" SELECT id, name, 1 - (embedding <=> %s) AS similarity FROM products WHERE category = 'electronics' ORDER BY embedding <=> %s LIMIT 5 """cursor.execute(query,(user_vector,user_vector))案例2:语义搜索实现
文本语义相似度搜索
fromsentence_transformersimportSentenceTransformer model=SentenceTransformer('all-MiniLM-L6-v2')# 将搜索查询转换为向量query_text="续航持久的蓝牙耳机"query_vector=model.encode(query_text).tolist()# 向量数据库查询similar_products=collection.query(query_embeddings=[query_vector],n_results=3,include=["metadata","distances"])使用FAISS实现(Python)
importfaissimportnumpyasnp# 创建索引dimension=768index=faiss.IndexFlatIP(dimension)# 添加向量数据vectors=np.random.rand(1000,dimension).astype('float32')index.add(vectors)# 相似度搜索query_vector=np.random.rand(1,dimension).astype('float32')D,I=index.search(query_vector,k=5)# 返回前5个最相似结果使用Milvus向量数据库
frompymilvusimportconnections,Collection# 连接数据库connections.connect("default",host="localhost",port="19530")# 获取集合collection=Collection("products")# 向量搜索search_params={"metric_type":"L2","params":{"nprobe":10}}results=collection.search(data=[query_vector],anns_field="embedding",param=search_params,limit=5,output_fields=["name"])索引参数调优(以IVF_FLAT为例)
-- 调整nlist参数平衡查询精度和速度CREATEINDEXONproductsUSINGivfflat(embedding vector_l2_ops)WITH(lists=500);混合查询(结合向量和标量过滤)
SELECTid,nameFROMproductsWHEREprice<1000ANDembedding<=>'[0.1, ..., 0.2]'<0.3ORDERBYembedding<=>'[0.1, ..., 0.2]'LIMIT10;向量量化技术应用(如PQ编码)
# 使用Faiss的PQ压缩quantizer=faiss.IndexFlatL2(dimension)index=faiss.IndexIVFPQ(quantizer,dimension,100,16,8)# 100个簇,16个子向量,8bits