立方体语义层
本笔记本演示了如何以适合传递给大型语言模型作为嵌入的格式检索 Cube 的数据模型元数据,从而增强上下文信息。
关于 Cube
Cube 是用于构建数据应用的语义层。它帮助数据工程师和应用开发者从现代数据存储中访问数据,将其组织为一致的定义,并交付给每一个应用程序。
Cube 的数据模型提供了结构和定义,作为上下文帮助大语言模型理解数据并生成正确的查询。大语言模型无需处理复杂的连接和指标计算,因为 Cube 对这些进行了抽象,并提供了一个基于业务术语的简单接口,而非直接使用 SQL 表名和列名。这种简化有助于降低大语言模型的出错概率,避免产生幻觉。
示例
输入参数(必填)
Cube Semantic Loader 需要 2 个参数:
-
cube_api_url: 您的 Cube 部署 REST API 的 URL。有关配置基础路径的更多信息,请参阅 Cube 文档。 -
cube_api_token:根据您的 Cube API 密钥生成的身份验证令牌。请参阅Cube 文档以获取生成 JSON Web 令牌 (JWT) 的说明。
输入参数(可选)
-
load_dimension_values:是否为每个字符串维度加载维度值。 -
dimension_values_limit: 要加载的维度值的最大数量。 -
dimension_values_max_retries: 加载维度值的最大重试次数。 -
dimension_values_retry_delay: 重试加载维度值之间的延迟。
import jwt
from langchain_community.document_loaders import CubeSemanticLoader
api_url = "https://api-example.gcp-us-central1.cubecloudapp.dev/cubejs-api/v1/meta"
cubejs_api_secret = "api-secret-here"
security_context = {}
# Read more about security context here: https://cube.dev/docs/security
api_token = jwt.encode(security_context, cubejs_api_secret, algorithm="HS256")
loader = CubeSemanticLoader(api_url, api_token)
documents = loader.load()
API 参考:CubeSemanticLoader
返回一个包含以下属性的文档列表:
page_contentmetadatatable_namecolumn_namecolumn_data_typecolumn_titlecolumn_descriptioncolumn_valuescube_data_obj_type
# Given string containing page content
page_content = "Users View City, None"
# Given dictionary containing metadata
metadata = {
"table_name": "users_view",
"column_name": "users_view.city",
"column_data_type": "string",
"column_title": "Users View City",
"column_description": "None",
"column_member_type": "dimension",
"column_values": [
"Austin",
"Chicago",
"Los Angeles",
"Mountain View",
"New York",
"Palo Alto",
"San Francisco",
"Seattle",
],
"cube_data_obj_type": "view",
}