LiteRT-LM CLI

借助命令行界面 (CLI),您可以立即测试模型,无需编写任何代码

支持的平台

  • Linux
  • macOS
  • Windows(通过 WSL)
  • Raspberry Pi

安装

litert-lm 安装为系统范围的二进制文件。需要 uv

uv tool install litert-lm

方法 2:pip

在虚拟环境中进行标准安装。

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

聊天

从 HuggingFace 下载并运行模型:

litert-lm run  \
  --from-huggingface-repo=litert-community/gemma-4-E2B-it-litert-lm \
  gemma-4-E2B-it.litertlm \
  --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=litert-community/gemma-4-E2B-it-litert-lm \
  gemma-4-E2B-it.litertlm \
  --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 也提供相同的功能。