跳到主要內容
Open In ColabOpen on GitHub

Cube 語義層

本筆記本示範了檢索 Cube 的資料模型元資料的過程,該元資料的格式適用於作為嵌入傳遞給 LLM,從而增強上下文資訊。

關於 Cube

Cube 是用於構建數據應用程式的語義層。它幫助數據工程師和應用程式開發人員從現代資料儲存庫中存取資料,將其組織成一致的定義,並將其交付給每個應用程式。

Cube 的資料模型提供了結構和定義,這些結構和定義被用作 LLM 理解資料和生成正確查詢的上下文。LLM 不需要導航複雜的聯結和指標計算,因為 Cube 抽象化了這些,並提供了一個簡單的介面,該介面在業務級術語上運作,而不是 SQL 表格和欄位名稱。這種簡化有助於 LLM 減少錯誤並避免幻覺。

範例

輸入參數(必填)

Cube Semantic Loader 需要 2 個參數

  • cube_api_url:您的 Cube 部署 REST API 的 URL。有關配置基本路徑的更多資訊,請參閱 Cube 文件

  • cube_api_token:根據您的 Cube API 密鑰生成的身份驗證令牌。有關生成 JSON Web Tokens (JWT) 的說明,請參閱 Cube 文件

輸入參數(選填)

  • 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_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",
}

此頁面是否有幫助?