Infinispan
Infinispan 是一個開源的鍵值資料網格,它可以作為單一節點以及分散式節點工作。
自 15.x 版本起支援向量搜尋。更多資訊:Infinispan 首頁
# Ensure that all we need is installed
# You may want to skip this
%pip install sentence-transformers
%pip install langchain
%pip install langchain_core
%pip install langchain_community
設定
要執行此演示,我們需要一個沒有身份驗證且正在運行的 Infinispan 實例和一個資料檔案。在接下來的三個儲存格中,我們將
- 下載資料檔案
- 建立設定
- 在 docker 中執行 Infinispan
%%bash
#get an archive of news
wget https://raw.githubusercontent.com/rigazilla/infinispan-vector/main/bbc_news.csv.gz
%%bash
#create infinispan configuration file
echo 'infinispan:
cache-container:
name: default
transport:
cluster: cluster
stack: tcp
server:
interfaces:
interface:
name: public
inet-address:
value: 0.0.0.0
socket-bindings:
default-interface: public
port-offset: 0
socket-binding:
name: default
port: 11222
endpoints:
endpoint:
socket-binding: default
rest-connector:
' > infinispan-noauth.yaml
!docker rm --force infinispanvs-demo
!docker run -d --name infinispanvs-demo -v $(pwd):/user-config -p 11222:11222 infinispan/server:15.0 -c /user-config/infinispan-noauth.yaml
程式碼
選擇嵌入模型
在此演示中,我們使用 HuggingFace 嵌入模式。
from langchain_core.embeddings import Embeddings
from langchain_huggingface import HuggingFaceEmbeddings
model_name = "sentence-transformers/all-MiniLM-L12-v2"
hf = HuggingFaceEmbeddings(model_name=model_name)
API 參考:Embeddings | HuggingFaceEmbeddings
設定 Infinispan 快取
Infinispan 是一個非常靈活的鍵值儲存區,它可以儲存原始位元以及複雜的資料類型。使用者在資料網格設定中擁有完全的自由,但對於簡單的資料類型,python 層會自動設定一切。我們利用此功能,以便可以專注於我們的應用程式。
準備資料
在此演示中,我們依賴預設設定,因此文字、中繼資料和向量位於同一個快取中,但其他選項也是可行的:例如,內容可以儲存在其他地方,而向量儲存區可能只包含對實際內容的參考。
import csv
import gzip
import time
# Open the news file and process it as a csv
with gzip.open("bbc_news.csv.gz", "rt", newline="") as csvfile:
spamreader = csv.reader(csvfile, delimiter=",", quotechar='"')
i = 0
texts = []
metas = []
embeds = []
for row in spamreader:
# first and fifth values are joined to form the content
# to be processed
text = row[0] + "." + row[4]
texts.append(text)
# Store text and title as metadata
meta = {"text": row[4], "title": row[0]}
metas.append(meta)
i = i + 1
# Change this to change the number of news you want to load
if i >= 5000:
break
填充向量儲存區
# add texts and fill vector db
from langchain_community.vectorstores import InfinispanVS
ispnvs = InfinispanVS.from_texts(texts, hf, metas)
API 參考:InfinispanVS
一個用於列印結果文件的輔助函數
預設情況下,InfinispanVS 在 Document.page_content
中傳回 protobuf ŧext
欄位,並在 metadata
中傳回所有剩餘的 protobuf 欄位(向量除外)。此行為可在設定時透過 lambda 函數進行設定。
def print_docs(docs):
for res, i in zip(docs, range(len(docs))):
print("----" + str(i + 1) + "----")
print("TITLE: " + res.metadata["title"])
print(res.page_content)
試試看!!!
以下是一些範例查詢
docs = ispnvs.similarity_search("European nations", 5)
print_docs(docs)
print_docs(ispnvs.similarity_search("Milan fashion week begins", 2))
print_docs(ispnvs.similarity_search("Stock market is rising today", 4))
print_docs(ispnvs.similarity_search("Why cats are so viral?", 2))
print_docs(ispnvs.similarity_search("How to stay young", 5))
!docker rm --force infinispanvs-demo