LiteRT-LM CLI

您可以使用指令列介面 (CLI) 立即測試模型,無須編寫程式碼

支援的平台:

  • Linux
  • macOS
  • Windows (透過 WSL)
  • Raspberry Pi

安裝

litert-lm 安裝為全系統二進位檔。需要 uv

uv tool install litert-lm-nightly

方法 2:pip

在虛擬環境中進行標準安裝。

python3 -m venv .venv
source .venv/bin/activate
pip install litert-lm-nightly

即時通訊

從 HuggingFace 下載並執行模型:

litert-lm run  \
  --from-huggingface-repo=google/gemma-3n-E2B-it-litert-lm \
  gemma-3n-E2B-it-int4 \
  --prompt="What is the capital of France?"

函式呼叫 / 工具

你可以使用預設值執行工具。建立 preset.py

import datetime
import base64

def get_current_time() -> str:
    """Returns the current date and time."""
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

system_instruction = "You are a helpful assistant with access to tools."
tools = [get_current_time]

使用預設設定執行:

litert-lm run  \
  --from-huggingface-repo=google/gemma-3n-E2B-it-litert-lm \
  gemma-3n-E2B-it-int4 \
  --preset=preset.py

提示詞範例和互動式輸出內容:

> what will the time be in two hours?
[tool_call] {"arguments": {}, "name": "get_current_time"}
[tool_response] {"name": "get_current_time", "response": "2026-03-25 21:54:07"}
The current time is 2026-03-25 21:54:07.

In two hours, it will be **2026-03-25 23:54:07**.

這是怎麼回事?

當您提出需要外部資訊的問題 (例如目前時間) 時,模型會辨識出需要呼叫工具。

  1. 模型發出 tool_call:模型會輸出 JSON 要求,呼叫 get_current_time 函式。
  2. CLI 執行工具:LiteRT-LM CLI 會攔截這項呼叫,並執行 preset.py 中定義的對應 Python 函式。
  3. CLI 傳送 tool_response:CLI 會將結果傳回模型。
  4. 模型生成最終答案:模型會使用工具回覆計算並生成最終答案,提供給使用者。

這個「函式呼叫」迴圈會在 CLI 中自動執行,讓您使用 Python 功能擴增本機 LLM,不必編寫任何複雜的自動化調度管理程式碼。

PythonC++Kotlin API 也提供相同功能。