你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Foundry Local SDK 参考

重要

  • Foundry Local 以预览版提供。 公共预览版提供对活动部署中的功能的早期访问。
  • 正式发布 (GA) 之前,功能、方法和流程可能会发生更改或具有受限的功能。

Foundry Local SDK 通过提供独立于数据平面推理代码的控制平面作,简化了本地环境中的 AI 模型管理。 此参考文档介绍了适用于 Python、JavaScript 和 C# 的 SDK 实现。

Python SDK 参考

安装

安装 Python 包:

pip install foundry-local-sdk

FoundryLocalManager 类

FoundryLocalManager 类提供用于管理模型、缓存和 Foundry Local 服务的方法。

初始化

from foundry_local import FoundryLocalManager

# Initialize and optionally bootstrap with a model
manager = FoundryLocalManager(alias_or_model_id=None, bootstrap=True)
  • alias_or_model_id:(可选)在启动时下载和加载的别名或模型 ID。
  • bootstrap:(默认值 True) 如果为 True,则启动服务(如果未运行),并在提供时加载模型。

有关别名的注释

此引用中概述的许多方法在签名中都有一个 alias_or_model_id 参数。 可以将 别名模型 ID 作为值传入方法。 使用别名将:

  • 选择适合可用硬件的最佳模型。 例如,如果 Nvidia CUDA GPU 可用,Foundry Local 将选择 CUDA 模型。 如果支持的 NPU 可用,Foundry Local 将选择 NPU 模型。
  • 允许使用较短的名称,而无需记住模型 ID。

小窍门

建议将alias_or_model_id别名传入参数,因为在部署应用程序时,Foundry Local 会在运行时为最终用户的计算机选择最佳模型。

服务管理

方法 签名 DESCRIPTION
is_service_running() () -> bool 检查 Foundry Local 服务是否正在运行。
start_service() () -> None 启动 Foundry Local 服务。
service_uri @property -> str 返回服务 URI。
endpoint @property -> str 返回服务终结点。
api_key @property -> str 返回 API 密钥(从 env 或默认值)。

目录管理

方法 签名 DESCRIPTION
list_catalog_models() () -> list[FoundryModelInfo] 列出目录中的所有可用模型。
refresh_catalog() () -> None 刷新模型目录。
get_model_info() (alias_or_model_id: str, raise_on_not_found=False) -> FoundryModelInfo or None 按别名或 ID 获取模型信息。

缓存管理

方法 签名 DESCRIPTION
get_cache_location() () -> str 返回模型缓存目录路径。
list_cached_models() () -> list[FoundryModelInfo] 列出下载到本地缓存的模型。

模型管理

方法 签名 DESCRIPTION
download_model() (alias_or_model_id: str, token: str = None, force: bool = False) -> FoundryModelInfo 将模型下载到本地缓存。
load_model() (alias_or_model_id: str, ttl: int = 600) -> FoundryModelInfo 将模型加载到推理服务器。
unload_model() (alias_or_model_id: str, force: bool = False) -> None 从推理服务器中卸载模型。
list_loaded_models() () -> list[FoundryModelInfo] 列出服务中当前加载的所有模型。

示例用法

以下代码演示如何使用 FoundryManager 类管理模型并与 Foundry Local 服务交互。

from foundry_local import FoundryLocalManager

# By using an alias, the most suitable model will be selected 
# to your end-user's device.
alias = "phi-3.5-mini"

# Create a FoundryLocalManager instance. This will start the Foundry.
manager = FoundryLocalManager()

# List available models in the catalog
catalog = manager.list_catalog_models()
print(f"Available models in the catalog: {catalog}")

# Download and load a model
model_info = manager.download_model(alias)
model_info = manager.load_model(alias)
print(f"Model info: {model_info}")

# List models in cache
local_models = manager.list_cached_models()
print(f"Models in cache: {local_models}")

# List loaded models
loaded = manager.list_loaded_models()
print(f"Models running in the service: {loaded}")

# Unload a model
manager.unload_model(alias)

与 OpenAI SDK 集成

安装 OpenAI 包:

pip install openai

以下代码演示如何使用 OpenAI SDK 集成 FoundryLocalManager 以与本地模型交互。

import openai
from foundry_local import FoundryLocalManager

# By using an alias, the most suitable model will be downloaded 
# to your end-user's device.
alias = "phi-3.5-mini"

# Create a FoundryLocalManager instance. This will start the Foundry 
# Local service if it is not already running and load the specified model.
manager = FoundryLocalManager(alias)

# The remaining code us es the OpenAI Python SDK to interact with the local model.

# Configure the client to use the local Foundry service
client = openai.OpenAI(
    base_url=manager.endpoint,
    api_key=manager.api_key  # API key is not required for local usage
)

# Set the model to use and generate a streaming response
stream = client.chat.completions.create(
    model=manager.get_model_info(alias).id,
    messages=[{"role": "user", "content": "Why is the sky blue?"}],
    stream=True
)

# Print the streaming response
for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="", flush=True)

JavaScript SDK 参考文档

安装

请从 npm 安装该软件包:

npm install foundry-local-sdk

FoundryLocalManager 类

FoundryLocalManager 类允许你在浏览器和 Node.js 环境中管理模型、控制缓存以及与 Foundry Local 服务交互。

初始化

import { FoundryLocalManager } from "foundry-local-sdk";

const foundryLocalManager = new FoundryLocalManager()

可用选项:

  • serviceUrl:Foundry Local 服务的基 URL
  • fetch:(可选) 适用于 Node.js 等环境的自定义提取实现

有关别名的注释

此引用中概述的许多方法在签名中都有一个 aliasOrModelId 参数。 可以将 别名模型 ID 作为值传入方法。 使用别名将:

  • 选择适合可用硬件的最佳模型。 例如,如果 Nvidia CUDA GPU 可用,Foundry Local 将选择 CUDA 模型。 如果支持的 NPU 可用,Foundry Local 将选择 NPU 模型。
  • 允许使用较短的名称,而无需记住模型 ID。

小窍门

建议将aliasOrModelId别名传入参数,因为在部署应用程序时,Foundry Local 会在运行时为最终用户的计算机选择最佳模型。

服务管理

方法 签名 DESCRIPTION
init() (aliasOrModelId?: string) => Promise<void> 初始化 SDK 并选择性地加载模型。
isServiceRunning() () => Promise<boolean> 检查 Foundry Local 服务是否正在运行。
startService() () => Promise<void> 启动 Foundry Local 服务。
serviceUrl string Foundry Local 服务的基 URL。
endpoint string API 终结点(serviceUrl + /v1)。
apiKey string API 密钥(无)。

目录管理

方法 签名 DESCRIPTION
listCatalogModels() () => Promise<FoundryModelInfo[]> 列出目录中的所有可用模型。
refreshCatalog() () => Promise<void> 刷新模型目录。
getModelInfo() (aliasOrModelId: string, throwOnNotFound = false) => Promise<FoundryModelInfo \| null> 按别名或 ID 获取模型信息。

缓存管理

方法 签名 DESCRIPTION
getCacheLocation() () => Promise<string> 返回模型缓存目录路径。
listCachedModels() () => Promise<FoundryModelInfo[]> 列出下载到本地缓存的模型。

模型管理

方法 签名 DESCRIPTION
downloadModel() (aliasOrModelId: string, token?: string, force = false, onProgress?) => Promise<FoundryModelInfo> 将模型下载到本地缓存。
loadModel() (aliasOrModelId: string, ttl = 600) => Promise<FoundryModelInfo> 将模型加载到推理服务器。
unloadModel() (aliasOrModelId: string, force = false) => Promise<void> 从推理服务器中卸载模型。
listLoadedModels() () => Promise<FoundryModelInfo[]> 列出服务中当前加载的所有模型。

示例用法

以下代码演示如何使用 FoundryLocalManager 类管理模型并与 Foundry Local 服务交互。

import { FoundryLocalManager } from "foundry-local-sdk";

// By using an alias, the most suitable model will be downloaded 
// to your end-user's device.
// TIP: You can find a list of available models by running the 
// following command in your terminal: `foundry model list`.
const alias = "phi-3.5-mini";

const manager = new FoundryLocalManager()

// Initialize the SDK and optionally load a model
const modelInfo = await manager.init(alias)
console.log("Model Info:", modelInfo)

// Check if the service is running
const isRunning = await manager.isServiceRunning()
console.log(`Service running: ${isRunning}`)

// List available models in the catalog
const catalog = await manager.listCatalogModels()

// Download and load a model
await manager.downloadModel(alias)
await manager.loadModel(alias)

// List models in cache
const localModels = await manager.listCachedModels()

// List loaded models
const loaded = await manager.listLoadedModels()

// Unload a model
await manager.unloadModel(alias)

与 OpenAI 客户端集成

安装 OpenAI 包:

npm install openai

以下代码演示如何将 FoundryLocalManager 与 OpenAI 客户端集成,以便与本地模型进行交互。

import { OpenAI } from "openai";
import { FoundryLocalManager } from "foundry-local-sdk";

// By using an alias, the most suitable model will be downloaded 
// to your end-user's device.
// TIP: You can find a list of available models by running the 
// following command in your terminal: `foundry model list`.
const alias = "phi-3.5-mini";

// Create a FoundryLocalManager instance. This will start the Foundry 
// Local service if it is not already running.
const foundryLocalManager = new FoundryLocalManager()

// Initialize the manager with a model. This will download the model 
// if it is not already present on the user's device.
const modelInfo = await foundryLocalManager.init(alias)
console.log("Model Info:", modelInfo)

const openai = new OpenAI({
  baseURL: foundryLocalManager.endpoint,
  apiKey: foundryLocalManager.apiKey,
});

async function streamCompletion() {
    const stream = await openai.chat.completions.create({
      model: modelInfo.id,
      messages: [{ role: "user", content: "What is the golden ratio?" }],
      stream: true,
    });
  
    for await (const chunk of stream) {
      if (chunk.choices[0]?.delta?.content) {
        process.stdout.write(chunk.choices[0].delta.content);
      }
    }
}
  
streamCompletion();

浏览器使用情况

SDK 包含与浏览器兼容的版本,必须在其中手动指定服务 URL:

import { FoundryLocalManager } from "foundry-local-sdk/browser"

// Specify the service URL
// Run the Foundry Local service using the CLI: `foundry service start`
// and use the URL from the CLI output
const endpoint = "ENDPOINT"

const manager = new FoundryLocalManager({serviceUrl: endpoint})

// Note: The `init`, `isServiceRunning`, and `startService` methods 
// are not available in the browser version

注释

浏览器版本不支持initisServiceRunningstartService方法。 在浏览器环境中使用 SDK 之前,必须确保 Foundry Local 服务正在运行。 可以使用 Foundry Local CLI 启动服务: foundry service start。 可以从 CLI 输出中收集服务 URL。

示例用法

import { FoundryLocalManager } from "foundry-local-sdk/browser"

// Specify the service URL
// Run the Foundry Local service using the CLI: `foundry service start`
// and use the URL from the CLI output
const endpoint = "ENDPOINT"

const manager = new FoundryLocalManager({serviceUrl: endpoint})

const alias = 'phi-3.5-mini'

// Get all available models
const catalog = await manager.listCatalogModels()
console.log("Available models in catalog:", catalog)

// Download and load a specific model
await manager.downloadModel(alias)
await manager.loadModel(alias)

// View models in your local cache
const localModels = await manager.listLocalModels()
console.log("Cached models:", catalog)

// Check which models are currently loaded
const loaded = await manager.listLoadedModels()
console.log("Loaded models in inference service:", loaded)

// Unload a model when finished
await manager.unloadModel(alias)