Skip to main content
SurrealDB 是一个为现代应用程序设计的端到端云原生数据库,支持 Web、移动端、无服务器、Jamstack、后端及传统应用。使用 SurrealDB,您可以简化数据库和 API 基础设施,缩短开发时间,快速、经济地构建安全高性能的应用程序。 SurrealDB 的主要特性包括:
  • 缩短开发时间: SurrealDB 通过消除大多数服务器端组件的需求来简化数据库和 API 层,让您能更快、更省钱地构建安全高性能的应用程序。
  • 实时协作 API 后端服务: SurrealDB 同时充当数据库和 API 后端服务,支持实时协作。
  • 多查询语言支持: SurrealDB 支持客户端设备上的 SQL 查询、GraphQL、ACID 事务、WebSocket 连接、结构化和非结构化数据、图查询、全文索引和地理空间查询。
  • 细粒度访问控制: SurrealDB 提供基于行级权限的访问控制,让您能够精确管理数据访问。
查看功能介绍、最新发布版本文档
本笔记本演示如何使用 SurrealDBLoader 相关功能。

概述

SurrealDB 文档加载器从 SurrealDB 数据库中返回 LangChain Document 列表。 文档加载器接受以下可选参数:
  • dburl:WebSocket 端点的连接字符串,默认:ws://localhost:8000/rpc
  • ns:命名空间名称,默认:langchain
  • db:数据库名称,默认:database
  • table:表名称,默认:documents
  • db_user:SurrealDB 凭证(如需要):数据库用户名。
  • db_pass:SurrealDB 凭证(如需要):数据库密码。
  • filter_criteria:用于构造过滤结果的 WHERE 子句的字典。
输出的 Document 具有以下结构:
Document(
    page_content=<包含结果文档的 JSON 编码字符串>,
    metadata={
        'id': <文档 id>,
        'ns': <命名空间名称>,
        'db': <数据库名称>,
        'table': <表名称>,
        ... <文档 metadata 属性中的其他字段>
    }
)

安装

取消注释下方单元格以安装 surrealdb 和 langchain。
# pip install -qU  surrealdb langchain langchain-community
# 在 jupyter notebook 中运行时添加此导入
import nest_asyncio

nest_asyncio.apply()
import json

from langchain_community.document_loaders.surrealdb import SurrealDBLoader
loader = SurrealDBLoader(
    dburl="ws://localhost:8000/rpc",
    ns="langchain",
    db="database",
    table="documents",
    db_user="root",
    db_pass="root",
    filter_criteria={},
)
docs = loader.load()
len(docs)
42
doc = docs[-1]
doc.metadata
{'id': 'documents:zzz434sa584xl3b4ohvk',
 'source': '../../how_to/state_of_the_union.txt',
 'ns': 'langchain',
 'db': 'database',
 'table': 'documents'}
len(doc.page_content)
18078
page_content = json.loads(doc.page_content)
page_content["text"]
'When we use taxpayer dollars to rebuild America – we are going to Buy American: buy American products to support American jobs. \n\nThe federal government spends about $600 Billion a year to keep the country safe and secure. \n\nThere\'s been a law on the books for almost a century \nto make sure taxpayers\' dollars support American jobs and businesses. \n\nEvery Administration says they\'ll do it, but we are actually doing it. \n\nWe will buy American to make sure everything from the deck of an aircraft carrier to the steel on highway guardrails are made in America. \n\nBut to compete for the best jobs of the future, we also need to level the playing field with China and other competitors. \n\nThat\'s why it is so important to pass the Bipartisan Innovation Act sitting in Congress that will make record investments in emerging technologies and American manufacturing. \n\nLet me give you one example of why it\'s so important to pass it.'