You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from typing import List
import faiss
from langchain_community.docstore import InMemoryDocstore
from langchain_community.vectorstores import FAISS
from langchain_core.documents import Document
from langchain_huggingface import HuggingFaceEmbeddings
# 准备好向量化的对象
model_name = "BAAI/bge-small-zh-v1.5" # 模型名
model_kwargs = {'device': 'cpu'} # 没有显卡就用cpu有英伟达显卡写cuda
encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity
# 第一次运行会自动下载模型去huggingface上下载下载到hf默认的缓存目录。
hf_embedding = HuggingFaceEmbeddings(
model_name=model_name,
model_kwargs=model_kwargs,
encode_kwargs=encode_kwargs
)
# 1、
# 把数据库写入磁盘
vector_store = FAISS.load_local('./faiss_db', embeddings=hf_embedding, allow_dangerous_deserialization=True)
vector_store.delete(ids=['id8'])
# results = vector_store.similarity_search('今天的金融投资新闻', k=2)
results = vector_store.similarity_search_with_score('有美食的内容吗', k=4, filter={"source": 'tweet'}) # 带分数
for res, score in results:
print(type(res))
print(res.id)
print(f"* [Score={score:3f}] {res.page_content} [{res.metadata}]")