第三节:开源模型的发现与管理 概述 本节课重点介绍如何使用 Foundry Local 进行实际的模型发现与管理。您将学习如何列出可用模型、测试不同选项,并了解基本性能特性。课程强调通过 Foundry CLI 的动手探索,帮助您为自己的使用场景选择合适的模型。 学习目标 掌握使用 Foundry CLI 进行模型发现与管理的命令 理解模型缓存和本地存储模式 学习快速测试和比较不同模型的方法 建立模型选择和基准测试的实用工作流程 探索通过 Foundry Local 可用的不断增长的模型生态系统 前置条件 完成第一节课:Foundry Local 入门 已安装并可访问 Foundry Local CLI 有足够的存储空间下载模型(模型大小从 1GB 到 20GB+ 不等)
本节课重点介绍如何使用 Foundry Local 进行实际的模型发现与管理。您将学习如何列出可用模型、测试不同选项,并了解基本性能特性。课程强调通过 Foundry CLI 的动手探索,帮助您为自己的使用场景选择合适的模型。
本节课探讨如何将开源模型引入 Foundry Local,选择社区模型、集成 Hugging Face 内容,以及采用“自带模型”(BYOM)策略。您还将了解 Model Mondays 系列,以持续学习和发现模型。
基于示例 03 创建自己的模型评估脚本:
REM create_model_test.cmd @echo off echo Model Discovery and Testing Script echo ===================================== echo. echo Step 1: List available models foundry model list echo. echo Step 2: Check what's cached foundry cache list echo. echo Step 3: Start phi-4-mini for testing foundry model run phi-4-mini --verbose echo. echo Step 4: Test with a simple prompt curl -X POST http://localhost:8000/v1/chat/completions ^ -H "Content-Type: application/json" ^ -d "{\"model\":\"phi-4-mini\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello, please introduce yourself.\"}],\"max_tokens\":100}" echo. echo Model test complete!
samples\03\list_and_bench.cmdModel Comparison Results: ======================== phi-4-mini: Fast (~2s), good for general chat qwen2.5-7b: Slower (~5s), better reasoning deepseek-r1: Medium (~3s), excellent for code Recommendation: Start with phi-4-mini for development, switch to qwen2.5-7b for production reasoning tasks.
模型无法启动:
REM Check service status foundry service status REM Restart service if needed foundry service stop foundry service start REM Try with verbose output foundry model run phi-4-mini --verbose
内存不足:
phi-4-mini)性能较慢:
phi-4-mini 验证设置✅ 模型发现:使用 foundry model list 探索可用模型
✅ 快速测试:使用 list_and_bench.cmd 模式进行快速评估
✅ 性能监控:基本资源使用和响应时间测量
✅ 模型选择:根据使用场景选择模型的实用指南
✅ 缓存管理:理解存储和清理流程
您现在掌握了使用 Foundry Local 的简单 CLI 方法发现、测试和选择适合您 AI 应用的模型的实用技能。
参考资料:
Foundry CLI 提供了简单的命令用于模型发现与管理:
REM List all available models in the catalog foundry model list REM List cached (downloaded) models foundry cache list REM Check cache directory location foundry cache ls
从流行且经过测试的模型开始,了解其性能特性:
REM Run Phi-4-Mini (lightweight, fast) foundry model run phi-4-mini --verbose REM Run Qwen 2.5 7B (larger, more capable) foundry model run qwen2.5-7b --verbose REM Run DeepSeek (specialized for coding) foundry model run deepseek-r1-7b --verbose
注意:--verbose 标志提供详细的启动信息,包括:
小型语言模型 (SLMs):
phi-4-mini:快速、高效,适合一般聊天phi-4:更强大的版本,推理能力更佳中型模型:
qwen2.5-7b:优秀的推理能力和更长的上下文deepseek-r1-7b:针对代码生成优化大型模型:
llama-3.2:Meta 最新的开源模型qwen2.5-14b:企业级推理能力基于示例 03 模式,以下是最小化工作流程:
@echo off REM Sample 03 - List and bench pattern echo Listing available models... foundry model list echo. echo Checking cached models... foundry cache list echo. echo Starting phi-4-mini with verbose output... foundry model run phi-4-mini --verbose
模型运行后,使用一致的提示进行测试:
REM Test via curl (Windows Command Prompt) curl -X POST http://localhost:8000/v1/chat/completions ^ -H "Content-Type: application/json" ^ -d "{\"model\":\"phi-4-mini\",\"messages\":[{\"role\":\"user\",\"content\":\"Explain edge AI in one sentence.\"}],\"max_tokens\":50}"
# PowerShell approach for testing $body = @{ model = "phi-4-mini" messages = @( @{ role = "user" content = "Explain edge AI in one sentence." } ) max_tokens = 50 } | ConvertTo-Json -Depth 3 Invoke-RestMethod -Uri "http://localhost:8000/v1/chat/completions" -Method Post -Body $body -ContentType "application/json"
Foundry Local 自动管理模型下载和缓存:
REM Check cache directory and contents foundry cache ls REM View cache location foundry cache cd REM Clean up unused models (if needed) foundry cache clean
典型模型大小:
phi-4-mini:约 2.5 GBqwen2.5-7b:约 4.1 GBdeepseek-r1-7b:约 4.3 GBllama-3.2:约 4.9 GBqwen2.5-14b:约 8.2 GB存储最佳实践:
foundry cache clean模型运行时,监控系统资源:
Windows 任务管理器:
命令行监控:
REM Check memory usage (PowerShell) Get-Process | Where-Object {$_.ProcessName -like "*foundry*"} | Select-Object ProcessName, WorkingSet64 REM Monitor running models foundry service ps
用于一般聊天和问答:
phi-4-mini(快速、高效)phi-4(推理能力更强)qwen2.5-7b(更长的上下文)用于代码生成:
deepseek-r1-7bqwen2.5-7b(代码生成也不错)用于复杂推理:
qwen2.5-7b 或 qwen2.5-14bphi-4最低系统要求:
phi-4-mini: 8GB RAM, entry-level CPU phi-4: 12GB RAM, mid-range CPU qwen2.5-7b: 16GB RAM, mid-range CPU deepseek-r1: 16GB RAM, mid-range CPU qwen2.5-14b: 24GB RAM, high-end CPU
推荐以获得最佳性能:
REM Stop current model (if needed) foundry service stop REM Start different model foundry model run qwen2.5-7b REM Verify model is running foundry service status
以下是比较模型性能的简单方法:
# simple_bench.py - Based on Sample 03 patterns import time import requests import json def test_model_response(model_name, prompt="Explain edge AI in one sentence."): """Test a single model with a prompt and measure response time.""" start_time = time.time() try: response = requests.post( "http://localhost:8000/v1/chat/completions", headers={"Content-Type": "application/json"}, json={ "model": model_name, "messages": [{"role": "user", "content": prompt}], "max_tokens": 64 }, timeout=30 ) elapsed = time.time() - start_time if response.status_code == 200: result = response.json() return { "model": model_name, "latency_sec": round(elapsed, 3), "response": result["choices"][0]["message"]["content"], "status": "success" } else: return { "model": model_name, "status": "error", "error": f"HTTP {response.status_code}" } except Exception as e: return { "model": model_name, "status": "error", "error": str(e) } # Test the currently running model if __name__ == "__main__": # Test with different models (start each model first) test_models = ["phi-4-mini", "qwen2.5-7b", "deepseek-r1-7b"] print("Model Performance Test") print("=" * 50) for model in test_models: print(f"\nTesting {model}...") print("Note: Make sure this model is running first with 'foundry model run {model}'") result = test_model_response(model) if result["status"] == "success": print(f"✅ {model}: {result['latency_sec']}s") print(f" Response: {result['response'][:100]}...") else: print(f"❌ {model}: {result['error']}")
对于每个模型,使用一致的提示进行测试并手动评估:
测试提示:
评估标准:
REM Monitor while testing different models REM Start model foundry model run phi-4-mini REM In another terminal, monitor resources foundry service status foundry service ps REM Check system resources (PowerShell) Get-Process | Where-Object ProcessName -Like "*foundry*" | Format-Table ProcessName, WorkingSet64, CPU
models.json免责声明:
本文档使用AI翻译服务 Co-op Translator 进行翻译。尽管我们努力确保翻译的准确性,但请注意,自动翻译可能包含错误或不准确之处。原始语言的文档应被视为权威来源。对于关键信息,建议使用专业人工翻译。我们对因使用此翻译而产生的任何误解或误读不承担责任。