全部產品
Search
文件中心

E-MapReduce:AI 中心

更新時間:Jun 30, 2026

StarRocks AI Function 將大語言模型 (LLM) 的能力直接嵌入到 OLAP 分析流程中,您可以在一條 SQL 內實現從資料處理、分析到 AI 推理的全過程,極大地提升了 BI 和資料分析的效率。

核心優勢

  • 資料不出湖,分析推理一體化:未經處理資料始終留存於 StarRocks 內,AI 函數僅按需加密傳輸待處理欄位至外部模型服務,杜絕資料複製與跨系統流轉,兼顧安全合規與分析效率。

  • 原生 SQL 互動,簡單易用:通過內建的 AI Function,您可以使用標準 SQL 將 LLM 的強大能力無縫整合到 OLAP 分析中,無需學習新的程式設計語言或工具。

  • 結果即時可用,高效分析:AI 函數返回的結果可立即參與後續的 JOIN、彙總、過濾等計算,無需二次匯入,實現了 AI 分析與資料查詢的即時聯動。

  • 高並發效率:僅需 4 線程即可驅動數百並發的 LLM 調用;相比之下,用戶端方案需要等量進程支撐,作業系統的調度往往會先於業務出現瓶頸。

  • 零營運限流:內建三層防護機制併兼容百鍊非標介面,您無需理解 RPM/TPM 等限流機制即可平穩運行。

  • Token 成本最佳化:通過謂詞下推減少調用量、緩衝消除重複請求、精確歸賬,同樣任務的費用可降低 30% 以上。

  • 工業級可靠性:提供行級容錯、智能重試與 Profile 可觀測能力,百萬行批處理可實現零人工幹預。

用量限制

單使用者享有 100 萬 Token 的免費試用額度。超出免費試用額度後,超出部分將根據實際Token消耗量進行收費,計費詳見AI Function計費

前提條件

核心版本要求

  • 3.3.20-2.1.1及以上

  • 3.5.16-2.1.1及以上

配置網路訪問

目的:StarRocks 的 BE 節點需要通過公網訪問外部的 AI 模型服務端點,因此您必須為其開通公網訪問能力。

配置方法

為 StarRocks 叢集所在的VPC配置 NAT Gateway,並設定 SNAT 規則,允許 BE 節點主動訪問公網。

  1. 建立 NAT Gateway並為其綁定彈性公網 IP。

  2. 配置 SNAT 規則,將 BE 節點所在網段的流量通過 NAT Gateway路由至公網。

  3. 確保相關的 VPC 路由表和安全性群組策略已允許存取出向的網路流量。

詳細配置步驟,請參見公網 NAT Gateway

數一覽

下表匯總了 StarRocks 內建的所有 AI Function,涵蓋文本理解、產生、轉換與安全處理四大能力,全部支援原生 SQL 調用、結果直接參与後續計算(JOIN/AGG/FILTER),無需 ETL 中轉。

函數

功能簡述

適用情境

ai_sentiment(text)

情感分析,返回 positive/negative/neutral/mixed/unknown

營運分析與商業智慧(BI),例如使用者評論情感分析看板。

ai_summarize(text)

文本摘要

資料治理,例如評論和工單自動摘要。

ai_fix_grammar(text)

文法與拼字自動校正

資料治理和質檢,例如使用者 UGC 內容質檢。

ai_redact(text, categories)

PII 脫敏

安全合規情境,例如開發/測試環境敏感性資料脫敏、審計日誌脫敏輸出。

ai_classify(text, labels)

按自訂標籤分類

內容與知識管理,例如商品評論打標。

ai_extract(text, entity_labels)

抽取實體並以 JSON 返回

內容與知識管理,例如構建銷售線索知識圖譜。

ai_translate(text, source_lang, target_lang)

機器翻譯

全球化,例如國際化報表自動本地化。

ai_similarity(text1, text2)

語義相似性

精準檢索和RAG,例如FAQ 匹配度排序。

ai_complete(model, prompt)

通用 AI 補全

通用產生與問答,例如營銷活動話術批量產生、通用問答。

ai_complete(model, prompt, params)

帶參數的 AI 補全

通過params控制模型行為(如 temperature、max_tokens 等)。

ai_custom_query(resource, prompt)

自訂 Resource 查詢

支援私人模型整合,例如合規情境專用小模型部署。

ai_filter(text, condition)

AI 條件過濾

資料篩選,例如基於語義條件過濾資料行。

函數詳情

ai_sentiment(AI情感分析‌)

對輸入文本進行情感分析。

  • 文法

    ai_sentiment(text)
  • 參數

    text(VARCHAR): 需要分析的常值內容。

  • 傳回值

    返回一個字串(VARCHAR),其值可能是 'positive'(正面)、'negative'(負面)、'neutral'(中性)、'mixed'(混合)或 'unknown'(未知)。

  • 樣本

    SELECT ai_sentiment('I am happy');  
    -- 返回: 'positive'

ai_classify(AI分類)

根據您提供的一組標籤,對輸入文本進行分類。

  • 文法

    ai_classify(text, labels)
  • 參數

    • text(VARCHAR):需要分類的常值內容。

    • labels(ARRAY<VARCHAR>):一個包含候選標籤的數組。此數組必須包含至少 2 個、至多 20 個元素。

  • 傳回值

    返回一個 JSON 對象,包含分類結果。如果無法分類,則返回 NULL

  • 樣本

    SELECT ai_classify("My password is leaked.", ["urgent", "not urgent"]);
    -- 返回: {"labels": ["urgent"]}

ai_extract(實體擷取)

從文本中抽取您指定的實體資訊。

  • 文法

    ai_extract(text, entity_labels)
  • 參數

    • text(VARCHAR): 需要從中抽取資訊的文本。

    • entity_labels(ARRAY<VARCHAR>): 一個包含待抽取實體類型的數組,例如 ['person', 'location']

  • 傳回值

    返回一個 JSON 對象。對象的鍵(key)是您在 entity_labels 中指定的實體類型,值(value)是從文本中抽取的對應內容。

  • 樣本

    SELECT ai_extract('John Doe lives in New York and works for Acme Corp.', ['person', 'location', 'organization']); 
    -- 返回: {"person":"John Doe","location":"New York","organization":"Acme Corp"}

ai_fix_grammar(文法錯誤修正)

對輸入文本進行文法和拼字校正。

  • 文法

    ai_fix_grammar(text)
  • 參數

    text(VARCHAR):需要校正的文本。

  • 傳回值

    返回一個經過文法和拼字校正的字串(VARCHAR)。

  • 樣本

    SELECT ai_fix_grammar('This sentence have some mistake'); 
    -- 返回: "This sentence has some mistake"

ai_complete(AI補全)

通用和帶參數的 AI 補全函數,支援指定模型進行內容產生和問答。

  • 文法

    ai_complete(model, prompt)
    ai_complete(model, prompt, params)
  • 參數

    • model(VARCHAR):模型名稱,例如 '__system__' 表示系統模型。

    • prompt(VARCHAR):指導模型產生內容的提示詞。

    • params(MAP,可選):控制模型行為的參數,支援 temperature、max_tokens、top_p 等。

  • 傳回值

    返回一個根據提示詞產生的字串(VARCHAR)。

  • 樣本

    -- 簡單樣本
    SELECT ai_complete('__system__', '為一場夏季單車促銷活動寫一個迷人的郵件標題,折扣為八折');
    -- 返回: "夏日騎行狂歡,八折優惠限時開啟!"
    
    -- 帶參數樣本
    SELECT ai_complete('__system__', 'What is the capital of France?', map{'temperature': '0.1', 'max_tokens': '50'});
    -- 返回: "Paris"

ai_filter(AI條件過濾)

基於自然語言條件對文本進行過濾判斷。

  • 文法

    ai_filter(text, condition)
  • 參數

    • text(VARCHAR):需要判斷的常值內容。

    • condition(VARCHAR):自然語言表述的過濾條件。

  • 傳回值

    返回 BOOLEAN,表示文本是否滿足條件。

  • 樣本

    SELECT ai_filter('這個產品品質很差,不推薦購買', '負面評價');
    -- 返回: true

ai_redact(PII脫敏)

對文本中的指定實體進行 PII 脫敏處理。

  • 文法

    ai_redact(text, categories)
  • 參數

    • text(VARCHAR):需要脫敏的文本。

    • categories(ARRAY<VARCHAR>):一個包含待脫敏實體類型的數組,例如 ['person', 'email', 'phone']

  • 傳回值

    返回一個將指定實體資訊脫敏後的字串(VARCHAR)。

  • 樣本

    -- 簡單樣本
    SELECT ai_redact('John Doe lives in New York. His email is john.doe@example.com.', ['person', 'email']); 
    -- 返回: "[REDACTED] lives in New York. His email is [REDACTED]."

ai_translate(機器翻譯)

將文本翻譯成指定的目標語言。

  • 文法

    ai_translate(text, source_lang, target_lang)
  • 參數

    • text (VARCHAR): 需要翻譯的文本。

    • source_lang (VARCHAR): 源語言代碼,例如 '中文'

    • target_lang (VARCHAR): 目標語言代碼,例如 'en' (英語), 'zh' (中文), 'es' (西班牙語)。建議遵循 ISO 639-1 語言代碼標準。

  • 傳回值

    返回翻譯後的字串(VARCHAR)。

  • 樣本

    SELECT ai_translate('Hello, how are you?', 'en', 'es'); 
    -- 返回: "Hola, ¿cómo estás?"

ai_similarity(語義相似性)

計算兩個文本之間的語義相似性。

  • 文法

    ai_similarity(text1, text2)
  • 參數

    • text1(VARCHAR): 第一個文本。

    • text2(VARCHAR): 第二個文本。

  • 傳回值

    返回一個浮點數 (FLOAT),範圍在 0 到 1 之間,表示兩個文本的語義相似性。分數越高,表示語義越接近。1.0 表示文本完全相同。該分數主要用於排序。

  • 樣本

    SELECT
      ai_similarity (
        'I enjoy hiking in the mountains.',
        'I love walking through mountain trails.'
      );
    -- 返回: 0.82

ai_summarize(AI總結概覽)

對輸入文本產生摘要。

  • 文法

    ai_summarize(text)
  • 參數

    • text (VARCHAR): 需要產生摘要的文本。

  • 傳回值

    返回一個文本摘要字串(VARCHAR)。

  • 樣本

    SELECT ai_summarize('Apache Spark is a unified analytics engine for large-scale data processing.It provides high-level APIs in Java, Scala, Python and R, and an optimized engine that supports general execution graphs. It also supports a rich set of higher-level tools including Spark SQL for SQL and structured data processing, pandas API on Spark for pandas workloads, MLlib for machine learning, GraphX for graph processing, and Structured Streaming for incremental computation and stream processing.'); 
    -- 返回: “Spark: unified engine for large-scale data processing with APIs and tools.”

ai_custom_query(自訂Resource查詢)

使用使用者自訂的 Resource 進行查詢。

  • 文法

    ai_custom_query(resource, prompt)
  • 參數

    • resource (VARCHAR):使用者自訂的 Resource 名稱。

    • prompt (VARCHAR):查詢提示詞。

  • 傳回值

    返回一個字串(VARCHAR)。

  • 樣本

    SELECT ai_custom_query('my_knowledge_base', '什麼是 StarRocks?');
    -- 返回: "StarRocks 是一個高效能分析型資料倉儲..."

附錄:叢集配置參數

您可以通過 ADMIN SET CONFIG 命令修改以下 BE 動態參數。

參數名

參數詳情

ai_function_classify_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 10

  • 是否重啟: 否

  • 資料類型: INT

  • 描述:分類功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_classify_prompt

  • 參數類型: BE 動態參數

  • 預設值: "Classify each of the following texts into one of the following JSON encoded labels: $0. Return only the labels in a JSON array string (not a JSON object) in the same order as the input. Output only the label. Texts: $1"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 分類功能的提示詞模板。

  • 備忘: $0為標籤列表(JSON編碼),$1為待分類文本列表。

ai_function_custom_query_prompt

  • 參數類型: BE 動態參數

  • 預設值: "$0. For each of the following inputs, provide a response according to the instruction above. Return only the responses in a JSON array string (not a JSON object) in the same order as the input. Output only the response content without any additional text. Inputs: $1"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 自訂查詢功能的提示詞模板。

  • 備忘: $0為查詢指令,$1為輸入文本列表。

ai_function_extract_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 1

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: 提取功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_extract_prompt

  • 參數類型: BE 動態參數

  • 預設值: "Extract a value for each of the JSON encoded labels from the texts below. For each label, only extract a single value. Labels: $0. Output the extracted values as a JSON object array in the same order as the input texts. Output only the JSON. Do not output a code block for the JSON. Texts: $1"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 提取功能的提示詞模板。

  • 備忘: $0為標籤列表(JSON編碼),$1為待提取文本列表。

ai_function_fix_grammar_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 3

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: 文法修正功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_fix_grammar_prompt

  • 參數類型: BE 動態參數

  • 預設值: "Fix the grammar in each of the following texts. Return only the corrected texts in a JSON array string (not a JSON object) in the same order as the input. Output only the corrected text. Texts: $0"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 文法修正功能的提示詞模板。

  • 備忘: $0為待修本文本列表。

ai_function_gen_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 1

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: 文本產生功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_gen_prompt

  • 參數類型: BE 動態參數

  • 預設值: "$0. Return only the generated content in a JSON array string (not a JSON object). Output only the generated content."

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 文本產生功能的提示詞模板。

  • 備忘: $0為產生指令。

ai_function_http_connect_timeout_ms

  • 參數類型: BE 動態參數

  • 預設值: 10000

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: AI模型HTTP連線逾時時間(毫秒)。

  • 備忘: 預設10秒。

ai_function_http_timeout_ms

  • 參數類型: BE 動態參數

  • 預設值: 600000

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: AI模型HTTP請求逾時時間(毫秒)。

  • 備忘: 預設10分鐘。

ai_function_mask_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 3

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: 掩碼功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_mask_prompt

  • 參數類型: BE 動態參數

  • 預設值: "Mask the values for each of the JSON encoded labels in the texts below. Labels: $0. Replace the values with the text [MASKED]. Output only the masked texts in a JSON array string (not a JSON object) in the same order as the input. Do not output anything else. Texts: $1"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 掩碼功能的提示詞模板。

  • 備忘: $0為標籤列表(JSON編碼),$1為待掩碼文本列表。

ai_function_max_inflight_requests

  • 參數類型: BE 動態參數

  • 預設值: 100

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: AI模型最大並發HTTP請求數。

  • 備忘: 控制同時進行的AI請求數量,避免過載。

ai_function_model_api_key

  • 參數類型:BE 動態參數

  • 預設值: "sk-89c1***********95d0"

  • 是否重啟: 否

  • 資料類型:String

  • 描述:用來訪問百鍊平台介面的密鑰。

  • 備忘:詳情請參見擷取API Key

ai_function_model_endpoint

  • 參數類型: BE 動態參數

  • 預設值: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 模型服務連接埠,或其他相容OpenAI API的Chat/Completions模型服務連接埠。

  • 備忘: 其他相容OpenAI API的Chat/Completions模型服務,可根據相應的API文檔填寫連接埠值。

ai_function_model_type

  • 參數類型: BE 動態參數

  • 預設值: "qwen-plus"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 調用的服務端的具體模型。

  • 備忘: 支援文本產生類別的模型。

ai_function_query_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 1

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: 查詢功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_query_prompt

  • 參數類型: BE 動態參數

  • 預設值: "For each of the following texts, $0. Return only the answers in a JSON array string (not a JSON object) in the same order as the input. Texts: $1"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 查詢功能的提示詞模板。

  • 備忘: $0為查詢指令,$1為待查詢文本列表。

ai_function_request_template

  • 參數類型: BE 動態參數

  • 預設值: "{"model": "$0", "messages": [{"role": "system", "content": "$1"}, {"role": "user", "content": "$2"}], "response_format": {"type": "json_object"}$3}"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: AI模型請求模板(OpenAI相容格式)。

  • 備忘: $0為模型名稱,$1為系統提示詞,$2為使用者提示詞,$3為額外參數(可選,以逗號開頭)。

ai_function_sentiment_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 10

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: 情感分析功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_sentiment_prompt

  • 參數類型: BE 動態參數

  • 預設值: "Classify each of the following texts into one of the following labels: [positive, negative, neutral, mixed]. Return only the labels in a JSON array string (not a JSON object) in the same order as the input. Output only the label. Text: $0"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 情感分析功能的提示詞模板。

  • 備忘: $0為待分析文本列表。

ai_function_similarity_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 10

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: 相似性計算功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_similarity_prompt

  • 參數類型: BE 動態參數

  • 預設值: "Calculate the similarity between each pair of texts. Return only the similarity scores in a JSON array string (not a JSON object) in the same order as the input. Each score should be a number between 0 and 1, rounded to 2 decimal places. Do not output anything else. Text pairs: $0"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 相似性計算功能的提示詞模板。

  • 備忘: $0為文本對列表。

ai_function_summarize_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 1

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: 摘要功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_summarize_prompt

  • 參數類型: BE 動態參數

  • 預設值: "Summarize each of the following texts in $0 words. Return only the summaries in a JSON array string (not a JSON object) in the same order as the input. Output only the summary. Texts: $1"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 摘要功能的提示詞模板。

  • 備忘: $0為目標字數,$1為待摘要文本列表。

ai_function_system_prompt

  • 參數類型: BE 動態參數

  • 預設值: "You are a helpful assistant."

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 所有AI功能的系統提示詞。

  • 備忘: 用於設定AI助手的基礎角色和行為。

ai_function_translate_batch_size

  • 參數類型: BE 動態參數

  • 預設值: 3

  • 是否重啟: 否

  • 資料類型: INT

  • 描述: 翻譯功能一次AI模型請求批處理大小。

  • 備忘: 批處理可減少模型請求次數,縮短回應時間。

ai_function_translate_prompt

  • 參數類型: BE 動態參數

  • 預設值: "Translate each of the following texts into $0. Return only the translated texts in a JSON array string (not a JSON object) in the same order as the input. Output only the translated text. Texts: $1"

  • 是否重啟: 否

  • 資料類型: String

  • 描述: 翻譯功能的提示詞模板。

  • 備忘: $0為目標語言,$1為待翻譯文本列表。

查詢AI Function相關參數項。

select * from information_schema.be_configs where NAME like "ai_%";