FoundryLocalSDK-快速参考


文档摘要

Foundry Local SDK - 快速参考 安装 服务管理 基本使用模式 流式响应 Workshop工具(简化版) 环境变量 常见模型别名 别名 | 大小 | 最适用途 | 4B | 通用,摘要 | 3.5B | 代码,重构 | 0.5B | 快速分类 | 0.5B | 代码生成 | 2B | 创意写作 错误处理 故障排除 连接错误 模型未找到 导入错误 高级:多模型 性能优化建议 缓存客户端:重复使用 实例 批量请求:顺序处理多个提示 调整maxtokens:值越低,响应越快 预加载模型:在生产使用前下载 监控使用情况:使用 跟踪令牌 资源 GitHub: https://github.

Foundry Local SDK - 快速参考

安装

# Install SDK pip install foundry-local-sdk openai # Install Foundry Local service # Windows winget install Microsoft.FoundryLocal # macOS brew tap microsoft/foundrylocal brew install foundrylocal

服务管理

# Start service foundry service start # Check status foundry service status # Stop service foundry service stop # List models foundry model ls # Download model foundry model download phi-4-mini # Get model info foundry model info phi-4-mini

基本使用模式

from foundry_local import FoundryLocalManager from openai import OpenAI # Initialize manager (starts service if needed) alias = "phi-4-mini" manager = FoundryLocalManager(alias) # Create OpenAI-compatible client client = OpenAI( base_url=manager.endpoint, api_key=manager.api_key ) # Get model ID model_id = manager.get_model_info(alias).id # Chat completion response = client.chat.completions.create( model=model_id, messages=[{"role": "user", "content": "Hello!"}] ) print(response.choices[0].message.content)

流式响应

stream = client.chat.completions.create( model=model_id, messages=[{"role": "user", "content": "Tell me a story"}], stream=True ) for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True)

Workshop工具(简化版)

from workshop_utils import chat_once # Single call with caching and retry text, usage = chat_once( 'phi-4-mini', messages=[{"role": "user", "content": "What is AI?"}], max_tokens=100, temperature=0.7 ) print(text) print(f"Tokens used: {usage.total_tokens}")

环境变量

import os # Show token usage os.environ['SHOW_USAGE'] = '1' # Enable retries os.environ['RETRY_ON_FAIL'] = '1' # Set retry delay os.environ['RETRY_BACKOFF'] = '2.0' # Custom endpoint os.environ['FOUNDRY_LOCAL_ENDPOINT'] = 'http://localhost:8000'

常见模型别名

别名 大小 最适用途
phi-4-mini ~4B 通用,摘要
phi-3.5-mini ~3.5B 代码,重构
qwen2.5-0.5b ~0.5B 快速分类
qwen2.5-coder-0.5b ~0.5B 代码生成
gemma-2b ~2B 创意写作

错误处理

from openai import OpenAIError try: text, usage = chat_once('phi-4-mini', messages=[...]) except RuntimeError as e: print(f"Manager initialization failed: {e}") print("Check: foundry service status") except OpenAIError as e: print(f"API call failed: {e}") print("Check: foundry model ls") except Exception as e: print(f"Unexpected error: {e}")

故障排除

连接错误

# Check service foundry service status # Restart foundry service stop foundry service start # Test endpoint curl http://localhost:55769/health

模型未找到

# List available foundry model ls # Download if needed foundry model download phi-4-mini

导入错误

# Reinstall SDK pip uninstall foundry-local-sdk pip install foundry-local-sdk

高级:多模型

from workshop_utils import get_client # Initialize multiple models models = ['phi-4-mini', 'qwen2.5-0.5b', 'phi-3.5-mini'] clients = {} for alias in models: manager, client, model_id = get_client(alias) clients[alias] = (client, model_id) # Use different models for alias, (client, model_id) in clients.items(): response = client.chat.completions.create( model=model_id, messages=[{"role": "user", "content": "Hello"}], max_tokens=50 ) print(f"{alias}: {response.choices[0].message.content}")

性能优化建议

  1. 缓存客户端:重复使用FoundryLocalManager实例
  2. 批量请求:顺序处理多个提示
  3. 调整max_tokens:值越低,响应越快
  4. 预加载模型:在生产使用前下载
  5. 监控使用情况:使用SHOW_USAGE=1跟踪令牌

资源

快速开始:

# Install everything winget install Microsoft.FoundryLocal pip install foundry-local-sdk openai # Start service foundry service start # Test in Python python -c "from foundry_local import FoundryLocalManager; from openai import OpenAI; m = FoundryLocalManager('phi-4-mini'); c = OpenAI(base_url=m.endpoint, api_key=m.api_key); r = c.chat.completions.create(model=m.get_model_info('phi-4-mini').id, messages=[{'role':'user','content':'Hi'}]); print(r.choices[0].message.content)"

免责声明
本文档使用AI翻译服务 Co-op Translator 进行翻译。尽管我们努力确保翻译的准确性,但请注意,自动翻译可能包含错误或不准确之处。原始语言的文档应被视为权威来源。对于关键信息,建议使用专业人工翻译。我们不对因使用此翻译而产生的任何误解或误读承担责任。


作者与出处
原作者: microsoft
来源:microsoft
许可证:MIT
整理: 灏天文库整理
由灏天文库结构化整理,提供目录导航、全文检索与在线阅读,便于系统化学习
发布者: 作者: microsoft 转发
评论区 (0)
U