基于 LangChain 的终端 LLM 问答工具,支持多模型、角色记忆和 MCP 工具
Project description
ask.py
基于 LangChain 的终端 LLM 问答工具,支持多模型、角色记忆和 MCP 工具调用。
特性
- 🚀 快速终端问答,直接
ask "问题"即可 - 🔧 多模型配置,支持任意 OpenAI 兼容接口
- 🎭 角色系统,自定义 System Prompt
- 🧠 三层记忆系统(短期/中期/长期),自动压缩淘汰
- 🔌 MCP(Model Context Protocol)工具支持
快速开始
1. 安装
# 使用 pipx(推荐)
pipx install ask-py-cli
# 或使用 uv tool
uv tool install ask-py-cli
2. 添加模型
ask model add openai \
-b https://api.openai.com/v1 \
-k $OPENAI_API_KEY \
-m gpt-4 \
--set-default
3. 开始使用
# 直接提问
ask "什么是量子计算?"
# 使用工具模式(时间查询、Shell 命令等)
ask -t "现在几点了?"
ask -t "列出 /tmp 目录的文件"
ask -t "整理 ~/Downloads 下的视频文件名"
# 创建角色(带记忆)
ask role add coder -s "你是一个资深程序员" --set-default
ask "写一个快速排序"
ask "改成迭代版本" # 自动记忆上下文
命令参考
提问
ask [OPTIONS] "问题"
选项:
-m, --model TEXT 指定模型
-s, --system TEXT 临时系统提示词
-r, --role TEXT 使用指定角色
-t, --tools 启用 MCP 工具
--mcp NAME 指定 MCP 服务器(可多次使用)
模型管理
ask model add NAME -b API_BASE -k API_KEY [-m MODEL] [--set-default]
ask model list
ask model default NAME
ask model remove NAME
角色管理
ask role add NAME -s "提示词" [-m MODEL] [--set-default]
ask role list
ask role show NAME
ask role edit NAME -s "新提示词"
ask role default [NAME] # 设置/清除默认角色
ask role remove NAME
ask role memory NAME # 查看记忆
ask role clear-memory NAME --confirm
配置文件
配置存储在 ~/.config/ask/ 目录:
~/.config/ask/
├── config.yaml # 模型配置
├── roles.yaml # 角色配置
├── mcp.json # MCP 服务器配置
└── memory/ # 记忆存储
config.yaml 示例
default: openai
default_role: coder
lang: zh-cn # 语言设置: en, zh-cn, zh-tw (默认根据系统 $LANG 自动检测)
models:
openai:
api_base: https://api.openai.com/v1
api_key: sk-xxx
model: gpt-4
temperature: 0.7
多语言支持
支持语言:
en- Englishzh-cn- 简体中文zh-tw- 繁體中文ja- 日本語
语言检测优先级:
- 配置文件中的
lang设置 - 环境变量
$LANG - 默认使用英文
记忆系统
角色支持三层分层记忆,自动管理对话历史:
| 层级 | 说明 | 策略 |
|---|---|---|
| 短期 | 最近完整对话 | 保留 10 轮 |
| 中期 | 早期对话摘要 | LLM 压缩生成 |
| 长期 | 整体精炼总结 | 多摘要合并 |
MCP 工具支持
MCP(Model Context Protocol)让 LLM 能够调用外部工具。
⚠️ 注意: 工具模式需要启动外部进程,响应速度较慢,建议仅在需要时使用
-t参数。
默认配置
首次运行自动创建 ~/.config/ask/mcp.json,会自动检测系统中的 uvx 或 pipx 命令:
{
"mcpServers": {
"time": {
"command": "uvx",
"args": ["mcp-server-time"]
},
"shell": {
"command": "uvx",
"args": ["mcp-shell-server"],
"env": {
"ALLOW_COMMANDS": "ls,cat,head,tail,find,grep,wc,pwd,echo,mkdir,cp,mv,touch,date"
}
}
},
"enabled": ["time", "shell"]
}
time: 查询时间shell: 执行系统命令(通过ALLOW_COMMANDS限制可用命令)- 自动检测:优先使用
uvx,不存在则使用pipx
添加更多服务器
{
"mcpServers": {
"time": { "command": "uvx", "args": ["mcp-server-time"] },
"filesystem": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"] },
"fetch": { "command": "uvx", "args": ["mcp-server-fetch"] }
},
"enabled": ["time", "filesystem"]
}
MCP 工具通过
uvx或npx动态调用,需安装 uv 或 Node.js。
使用工具
ask mcp list # 查看服务器
ask mcp tools shell # 查看 shell 工具详情
ask -t "现在几点?" # 使用默认启用的工具(time + shell)
ask -t "列出 /tmp 目录文件" # LLM 自动调用 shell
ask -t "整理 ~/Videos 下的视频文件名" # LLM 自动规划并执行命令
角色级 MCP
# ~/.config/ask/roles.yaml
coder:
system_prompt: "你是一个程序员"
mcp: ["github"] # 额外启用的服务器
支持的模型
任何 OpenAI 兼容接口:OpenAI、Azure OpenAI、DeepSeek、智谱 GLM、Ollama、vLLM、LM Studio 等。
开发
# 克隆并安装
git clone https://github.com/tiancheng91/ask.py
cd ask.py
uv sync
# 运行
uv run ask "问题"
# 测试
uv run pytest test_ask.py -v
# 构建发布
uv build
uv publish
从源码安装
pipx install git+https://github.com/tiancheng91/ask.py
# 或
uv tool install git+https://github.com/tiancheng91/ask.py
# 或从 PyPI 安装
pipx install ask-py-cli
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ask_py_cli-0.1.2.tar.gz.
File metadata
- Download URL: ask_py_cli-0.1.2.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f47781234d1978a74a11b4127582924cd4db5b2432ae63a4a09576a26b5535a
|
|
| MD5 |
0e5fb6c8b0b8d2854634c9543af43b9d
|
|
| BLAKE2b-256 |
4544031d5a2e91ba7bd80287ee9da69ed03cdb0a499963b7a7551427271e93ab
|
Provenance
The following attestation bundles were made for ask_py_cli-0.1.2.tar.gz:
Publisher:
publish.yml on tiancheng91/ask.py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ask_py_cli-0.1.2.tar.gz -
Subject digest:
0f47781234d1978a74a11b4127582924cd4db5b2432ae63a4a09576a26b5535a - Sigstore transparency entry: 844684498
- Sigstore integration time:
-
Permalink:
tiancheng91/ask.py@411a28028315ee8beafbc9282b685439b7e039cf -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/tiancheng91
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@411a28028315ee8beafbc9282b685439b7e039cf -
Trigger Event:
release
-
Statement type:
File details
Details for the file ask_py_cli-0.1.2-py3-none-any.whl.
File metadata
- Download URL: ask_py_cli-0.1.2-py3-none-any.whl
- Upload date:
- Size: 33.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1fd0ee0836e01c49007a52bbfe36849211750387e19a44afa25e1c3a45a032f7
|
|
| MD5 |
69ecee9f9aae6cd808e31a0fe9f7ec56
|
|
| BLAKE2b-256 |
dda51ce00b9ba444c5beb865b7d7fbfe26a4df45726f715baa63835adb51ce9b
|
Provenance
The following attestation bundles were made for ask_py_cli-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on tiancheng91/ask.py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ask_py_cli-0.1.2-py3-none-any.whl -
Subject digest:
1fd0ee0836e01c49007a52bbfe36849211750387e19a44afa25e1c3a45a032f7 - Sigstore transparency entry: 844684502
- Sigstore integration time:
-
Permalink:
tiancheng91/ask.py@411a28028315ee8beafbc9282b685439b7e039cf -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/tiancheng91
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@411a28028315ee8beafbc9282b685439b7e039cf -
Trigger Event:
release
-
Statement type: