你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
重要
- Foundry Local 以预览版提供。 公共预览版提供对活动部署中的功能的早期访问。
- 正式发布 (GA) 之前,功能、方法和流程可能会发生更改或具有受限的功能。
本教程介绍如何使用 Foundry Local SDK 和 LangChain 创建应用程序。 在本教程中,你将生成一个翻译应用程序,该应用程序将文本从一种语言翻译成另一种语言,以使用本地模型。
先决条件
在开始本教程之前,需要:
- 你的计算机上已安装Foundry Local。 安装说明请参阅 Foundry Local 入门指南 。
- 计算机上安装了 Python 3.10 或更高版本。 可以从 官方网站下载 Python。
安装 Python 包
需要安装以下 Python 包:
pip install langchain[openai]
pip install foundry-local-sdk
小窍门
建议使用虚拟环境来避免包冲突。 可以使用 venv
或 conda
创建虚拟环境。
创建翻译应用程序
在偏好的 IDE 中创建一 translation_app.py
个名为的新 Python 文件,并添加以下代码:
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from foundry_local import FoundryLocalManager
# 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: `foundry model list`.
alias = "phi-3-mini-4k"
# 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)
# Configure ChatOpenAI to use your locally-running model
llm = ChatOpenAI(
model=manager.get_model_info(alias).id,
base_url=manager.endpoint,
api_key=manager.api_key,
temperature=0.6,
streaming=False
)
# Create a translation prompt template
prompt = ChatPromptTemplate.from_messages([
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}."
),
("human", "{input}")
])
# Build a simple chain by connecting the prompt to the language model
chain = prompt | llm
input = "I love to code."
print(f"Translating '{input}' to French...")
# Run the chain with your inputs
ai_msg = chain.invoke({
"input_language": "English",
"output_language": "French",
"input": input
})
# print the result content
print(f"Response: {ai_msg.content}")
注释
Foundry Local 的主要优点之一是,它 会自动 为用户的硬件选择最合适的模型 变体 。 例如,如果用户具有 GPU,则会下载模型的 GPU 版本。 如果用户具有 NPU (神经处理单元),则会下载 NPU 版本。 如果用户没有 GPU 或 NPU,则会下载模型的 CPU 版本。
运行应用程序
若要运行应用程序,请打开终端并导航到保存 translation_app.py
文件的目录。 然后,运行以下命令:
python translation_app.py
先决条件
在开始本教程之前,需要:
- 你的计算机上已安装Foundry Local。 安装说明请参阅 Foundry Local 入门指南 。
- 计算机上安装了 Node.js 18 或更高版本。 可以从 官方网站下载 Node.js。
安装 Node.js 包
需要安装以下 Node.js 包:
npm install @langchain/openai @langchain/core
npm install foundry-local-sdk
创建翻译应用程序
在喜欢的 IDE 中创建一 translation_app.js
个新的 JavaScript 文件,并添加以下代码:
import { FoundryLocalManager } from "foundry-local-sdk";
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
// 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-mini-4k";
// 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)
// Configure ChatOpenAI to use your locally-running model
const llm = new ChatOpenAI({
model: modelInfo.id,
configuration: {
baseURL: foundryLocalManager.endpoint,
apiKey: foundryLocalManager.apiKey
},
temperature: 0.6,
streaming: false
});
// Create a translation prompt template
const prompt = ChatPromptTemplate.fromMessages([
{
role: "system",
content: "You are a helpful assistant that translates {input_language} to {output_language}."
},
{
role: "user",
content: "{input}"
}
]);
// Build a simple chain by connecting the prompt to the language model
const chain = prompt.pipe(llm);
const input = "I love to code.";
console.log(`Translating '${input}' to French...`);
// Run the chain with your inputs
chain.invoke({
input_language: "English",
output_language: "French",
input: input
}).then(aiMsg => {
// Print the result content
console.log(`Response: ${aiMsg.content}`);
}).catch(err => {
console.error("Error:", err);
});
注释
Foundry Local 的主要优点之一是,它 会自动 为用户的硬件选择最合适的模型 变体 。 例如,如果用户具有 GPU,则会下载模型的 GPU 版本。 如果用户具有 NPU (神经处理单元),则会下载 NPU 版本。 如果用户没有 GPU 或 NPU,则会下载模型的 CPU 版本。
运行应用程序
若要运行应用程序,请打开终端并导航到保存 translation_app.js
文件的目录。 然后,运行以下命令:
node translation_app.js
后续步骤
- 浏览 LangChain 文档 ,了解更高级的特性和功能。
- 编译Hugging Face模型以在Foundry Local上运行