借助 LiteRT-LM 命令行界面 (CLI),您可以使用终端运行模型并与之互动。
安装
使用 uv(推荐)
按照 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**.
这里发生了什么?
当您提出需要外部信息(例如当前时间)的问题时,模型会识别出需要调用工具。
- 模型发出
tool_call:模型输出 JSON 请求以调用get_current_time函数。 - CLI 执行工具:LiteRT-LM CLI 会拦截此调用,并执行
preset.py中定义的相应 Python 函数。 - CLI 发送
tool_response:CLI 将结果发送回模型。 - 模型生成最终答案:模型使用工具响应来计算并生成用户的最终答案。
此“函数调用”循环会在 CLI 中自动进行,让您无需编写任何复杂的编排代码,即可使用 Python 功能增强本地 LLM。
Python、C++ 和 Kotlin API 也提供相同的功能。