SurrealDB
SurrealDB 是一款为现代应用程序(包括 Web、移动、无服务器、Jamstack、后端和传统应用程序)设计的端到端云原生数据库。使用 SurrealDB,您可以简化数据库和 API 基础设施,缩短开发时间,并快速经济地构建安全、高效的应用程序。
SurrealDB 的主要功能包括
- 缩短开发时间:SurrealDB 通过消除对大多数服务器端组件的需求,简化了数据库和 API 堆栈,使您能够更快、更便宜地构建安全、高效的应用程序。
- 实时协作 API 后端服务:SurrealDB 同时充当数据库和 API 后端服务,支持实时协作。
- 支持多种查询语言:SurrealDB 支持来自客户端设备的 SQL 查询、GraphQL、ACID 事务、WebSocket 连接、结构化和非结构化数据、图查询、全文索引和地理空间查询。
- 细粒度访问控制:SurrealDB 提供基于行的权限访问控制,使您可以精确地管理数据访问。
此笔记本展示了如何使用与 SurrealDBStore
相关的功能。
设置
取消注释以下单元格以安装 surrealdb。
# %pip install --upgrade --quiet surrealdb langchain langchain-community
使用 SurrealDBStore
# add this import for running in jupyter notebook
import nest_asyncio
nest_asyncio.apply()
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import SurrealDBStore
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_text_splitters import CharacterTextSplitter
documents = TextLoader("../../how_to/state_of_the_union.txt").load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
model_name = "sentence-transformers/all-mpnet-base-v2"
embeddings = HuggingFaceEmbeddings(model_name=model_name)
创建 SurrealDBStore 对象
db = SurrealDBStore(
dburl="ws://localhost:8000/rpc", # url for the hosted SurrealDB database
embedding_function=embeddings,
db_user="root", # SurrealDB credentials if needed: db username
db_pass="root", # SurrealDB credentials if needed: db password
# ns="langchain", # namespace to use for vectorstore
# db="database", # database to use for vectorstore
# collection="documents", #collection to use for vectorstore
)
# this is needed to initialize the underlying async library for SurrealDB
await db.initialize()
# delete all existing documents from the vectorstore collection
await db.adelete()
# add documents to the vectorstore
ids = await db.aadd_documents(docs)
# document ids of the added documents
ids[:5]
['documents:38hz49bv1p58f5lrvrdc',
'documents:niayw63vzwm2vcbh6w2s',
'documents:it1fa3ktplbuye43n0ch',
'documents:il8f7vgbbp9tywmsn98c',
'documents:vza4c6cqje0avqd58gal']
(或者)创建 SurrealDBStore 对象并添加文档
await db.adelete()
db = await SurrealDBStore.afrom_documents(
dburl="ws://localhost:8000/rpc", # url for the hosted SurrealDB database
embedding=embeddings,
documents=docs,
db_user="root", # SurrealDB credentials if needed: db username
db_pass="root", # SurrealDB credentials if needed: db password
# ns="langchain", # namespace to use for vectorstore
# db="database", # database to use for vectorstore
# collection="documents", #collection to use for vectorstore
)
相似性搜索
query = "What did the president say about Ketanji Brown Jackson"
docs = await db.asimilarity_search(query)
print(docs[0].page_content)
Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections.
Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.
One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.
And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.
带分数的相似性搜索
返回的距离分数是余弦距离。因此,分数越低越好。
docs = await db.asimilarity_search_with_score(query)
docs[0]
(Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'id': 'documents:slgdlhjkfknhqo15xz0w', 'source': '../../how_to/state_of_the_union.txt'}),
0.39839531721941895)