使用说明
使用MCP工具的本地LLM-完成本地设置
A. 100%本地 具有热插拔LLM模型、MCP工具调用和本地聊天界面的AI系统。无需外部服务!
🎯 这是什么
- ✅ 本地LLM模型 (米斯特拉尔,《骆驼3》,Phi-3)
- ✅ 热插拔 (切换型号而不重新启动)
- ✅ MCP工具 (LLM可以调用的6个工具)
- ✅ 本地聊天界面 (CLI+Web)
- ✅ 无外部服务 (没有克劳德、OpenAI等)
🚀 快速开始
第一步:安装Olama
Ollama是运行模型的本地LLM运行时。
macOS
brew install ollama
Linux
curl -fsSL https://ollama.com/install.sh | sh
视窗
从下载并安装 ollama.com/下载
第二步:启动Ollama服务
macOS/Linux:
ollama serve
窗户: Ollama在安装后自动作为服务运行。如果没有,运行:
ollama serve
保持此终端/窗口运行! 这是LLM发动机。
步骤3:安装Python依赖项
macOS/Linux:
cd /Users/oseekero/robotics_stuff/local_mcp
pip install -r requirements.txt
窗户:
cd C:\path\to\local_mcp
pip install -r requirements.txt
步骤4:运行聊天界面
打开A 新终端/命令提示符 (让奥利玛在第一场比赛中继续奔跑)。
选项A:简单聊天(推荐)⭐
macOS/Linux:
python simple_chat.py
窗户:
python simple_chat.py
特征:
- 工具的直接斜线命令
/models-列出可用型号/switch <model>-改变模型(例如。,/switch llama3.1:8b)/info-获取当前型号信息/clear-清晰的对话- 只需正常输入即可与LLM聊天
选项B:自动工具调用聊天
macOS/Linux:
python chat.py
窗户:
python chat.py
特征:
- LLM在需要时自动调用工具
- 与Llama 3.1 8B或Mistral 7B配合使用效果最佳
- 较小的模型(phi3:mini)可能无法很好地遵循JSON格式
选项C:Web界面
macOS/Linux:
python web_chat.py
窗户:
python web_chat.py
然后打开浏览器: http://localhost:8000
特征:
- 漂亮的渐变UI
- 实时WebSocket聊天
- 打字指示器
- 浏览器中的模型切换
💬 使用聊天
简单聊天命令
You: /models
Assistant: Available Models:
• mistral:7b (active)
• llama3.1:8b
• phi3:mini
...
You: /switch llama3.1:8b
Assistant: Switched to llama3.1:8b
You: /info
Assistant: Model Information:
Name: llama3.1:8b
Size: 4.7GB
Context: 8192 tokens
...
You: Hello! How are you?
Assistant: I'm doing well! How can I help you today?
自动工具调用聊天
LLM会自动检测何时使用工具:
You: What models are available?
🔧 Calling tool: list_models
✓ Tool result: Available Models: mistral:7b, llama3.1:8b...
Assistant: Here are the available models: Mistral 7B (currently active), Llama 3.1 8B, Phi-3 Mini...
You: Switch to llama
🔧 Calling tool: switch_model
✓ Tool result: Successfully switched to llama3.1:8b
Assistant: I've switched to Llama 3.1 8B for you!
🤖 预配置型号
| 型号 | 尺寸 | 最适合 | 工具调用 |
|---|---|---|---|
| 米斯特拉尔:7b ⭐ | 4.1GB | 平衡,良好的工具调用 | ✅ 太好了 |
| 美洲驼3.1:8b ⭐ | 4.7GB | 质量最好,工具调用很棒 | ✅ 太好了 |
| llama3.2:3b | 2.0GB | 快速、轻便 | ⚠️ 有限 |
| phi3:mini | 2.3GB | 非常快,高效 | ⚠️ 有限 |
| phi3:中等 | 7.9GB | 功能最强 | ✅ 很好 |
推荐: 使用 米斯特拉尔:7b 或 美洲驼3.1:8b 以获得最佳的工具调用性能。
🔧 可用工具
LLM可以使用这些工具:
| 工具 | 说明 | 示例 |
|---|---|---|
chat |
使用对话历史记录聊天 | 自动 |
complete |
单文本完成 | 自动 |
switch_model |
热交换到不同型号 | “切换到mistral” |
list_models |
显示所有可用型号 | “有哪些型号可用?” |
get_model_info |
获取型号详细信息 | “告诉我当前型号的情况” |
clear_conversation |
重置对话历史记录 | “清除对话” |
🔄 热交换模型
无需重新启动即可立即更改型号:
方法1:聊天
/switch mistral:7b
方法2:编辑config.yaml
active_model: "llama3.1:8b"
保存文件-系统会自动重新加载!
方法3:程序化
from config_manager import ConfigManager
config = ConfigManager()
config.set_active_model("phi3:mini")
📁 项目结构
local_mcp/
├── simple_chat.py # ⭐ CLI chat with slash commands (RECOMMENDED)
├── chat.py # CLI chat with auto tool-calling
├── web_chat.py # Web UI chat interface
├── local_mcp_client.py # MCP client for tool calling
├── model_manager.py # Model operations (download, switch, generate)
├── llm_tools.py # 6 MCP tools
├── config_manager.py # Configuration management
├── config.yaml # Settings (active model, parameters)
├── main.py # MCP server (for external clients like Claude)
├── requirements.txt # Python dependencies
├── test_server.py # Test suite
├── debug_chat.py # Debug tool calling
└── README.md # This file
🛠️ 平台特定说明
macOS
1号航站楼:
ollama serve
2号航站楼:
cd /Users/oseekero/robotics_stuff/local_mcp
python simple_chat.py
第一轮: 下载活动模型(约4GB用于mistral:7b)
Linux
1号航站楼:
ollama serve
2号航站楼:
cd /path/to/local_mcp
python3 simple_chat.py
注: 使用 python3 而不是 python 在大多数Linux发行版上。
视窗
命令提示符1:
ollama serve
(或者Ollama可能已经作为服务运行)
命令提示符2:
cd C:\path\to\local_mcp
python simple_chat.py
PowerShell替代方案:
cd C:\path\to\local_mcp
python simple_chat.py
🧪 测试
快速测试
python debug_chat.py
准确显示工具调用的情况。
完整测试套件
python test_server.py
测试所有组件。
简单的例子
python quick_test.py
快速功能测试。
⚙️ 配置
编辑 config.yaml 自定义:
# Active model (hot-swappable)
active_model: "mistral:7b"
# Model parameters
model_params:
temperature: 0.7 # Randomness (0.0-2.0)
top_p: 0.9 # Nucleus sampling
num_ctx: 4096 # Context window
num_predict: 512 # Max tokens to generate
# Ollama settings
ollama:
host: "http://localhost:11434"
auto_pull: true # Auto-download models
🐛 故障排除
“找不到olama”
解决方案: 安装Ollama(见上述步骤1)
“连接被拒绝”
解决方案: 确保 ollama serve 正在运行
模型下载缓慢
解决方案: 首次下载需要时间(每个型号2-5GB)。耐心点!
工具未被调用
解决方案:
- 使用
simple_chat.py使用斜线命令(始终有效) - 或切换到
mistral:7b或llama3.1:8b(更擅长工具调用) - 跑
debug_chat.py看看发生了什么
端口8000已在使用中(网络聊天)
解决方案: 编辑 web_chat.py 并更改端口:
uvicorn.run(app, host="0.0.0.0", port=8001) # Change to 8001
Windows:“找不到python”
解决方案: 使用 py 而不是 python:
py simple_chat.py
🔌 用作MCP服务器(高级)
要与外部MCP客户端(如Claude Desktop)一起使用:
启动MCP服务器:
python main.py
配置Claude桌面 (~/Library/Application Support/Claude/claude_desktop_config.json 在macOS上):
{
"mcpServers": {
"local-llm": {
"command": "python",
"args": ["/Users/oseekero/robotics_stuff/local_mcp/main.py"]
}
}
}
重新启动Claude Desktop以进行连接。
🎉 总结
你现在有一个 完全本地的人工智能系统:
✅ 不需要API密钥
✅ 无外部服务
✅ 完全隐私
✅ 离线工作 (模型下载后)
✅ 热插拔型号
✅ 工具调用功能
✅ 多个接口 (CLI+Web)
推荐工作流程:
ollama serve(1号航站楼)python simple_chat.py(2号航站楼)- 使用
/models,/switch,/info命令 - 与LLM正常聊天
享受你的本地AI! 🚀
