LiteRT-LM CLI

透過 LiteRT-LM 指令列介面 (CLI),您可以使用終端機執行模型並與之互動。

安裝

按照 uv 安裝指南操作,安裝 uv

uv tool install litert-lm-nightly

使用pip

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

即時通訊

使用 CLI 執行模型:

litert-lm run 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 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,不必編寫任何複雜的自動化調度管理程式碼。

Python、C++ 和 Kotlin API 也提供相同功能。