Skip to main content
Open In ColabOpen on GitHub

Cube 语义层

此笔记本演示了检索Cube的数据模型元数据的过程,使其以适合传递给LLMs作为嵌入的形式存在,从而增强上下文信息。

关于立方体

Cube 是构建数据应用的语义层。它帮助数据工程师和应用程序开发人员从现代数据存储中访问数据,将这些数据组织成一致的定义,并将其提供给每一个应用程序。

Cube的数据模型提供了结构和定义,这些结构和定义被用作上下文,以便LLM理解数据并生成正确的查询。由于Cube抽象了复杂的连接和度量计算,LLM无需导航这些复杂操作,而是提供了一个基于业务术语的简单接口,而不是SQL表和列名。这种简化有助于减少LLM出错的可能性,并避免幻觉。

示例

输入参数(必填)

Cube Semantic Loader 需要 2 个参数:

  • cube_api_url: 您的应用部署的 Cube 的 REST API URL。请参阅 Cube 文档 以获取更多关于基本路径配置的信息。

  • cube_api_token: 依据您的立方体API密钥生成的认证令牌。请参阅立方体文档以了解生成JSON Web Tokens (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()

返回具有以下属性的文档列表:

  • page_content
  • metadata
    • table_name
    • column_name
    • column_data_type
    • column_title
    • column_description
    • column_values
    • cube_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",
}