Von Bedeutung
- Foundry Local はプレビューで利用できます。 パブリック プレビュー リリースでは、アクティブなデプロイ中の機能に早期にアクセスできます。
- 一般提供 (GA) の前は、機能、アプローチ、プロセスが変更されたり、機能が制限されたりする場合があります。
このチュートリアルでは、Foundry Local SDK と LangChain を使用してアプリケーションを作成する方法について説明します。 このチュートリアルでは、ローカル モデルを使用する言語間でテキストを翻訳する翻訳アプリケーションを構築します。
[前提条件]
このチュートリアルを開始する前に、次のものが必要です。
- Foundry Local がコンピューターにインストールされています。 インストール手順については、「 Foundry Local の概要 」ガイドを参照してください。
- お使いのコンピューターにインストールされている Python 3.10 以降。 Python は公式 Web サイトからダウンロードできます。
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 の主な利点の 1 つは、ユーザーのハードウェアに最適なモデルバリアントを自動的に選択することです。 たとえば、ユーザーが 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 の主な利点の 1 つは、ユーザーのハードウェアに最適なモデルバリアントを自動的に選択することです。 たとえば、ユーザーが GPU を持っている場合、モデルの GPU バージョンがダウンロードされます。 ユーザーが NPU (ニューラル処理ユニット) を持っている場合は、NPU バージョンがダウンロードされます。 ユーザーが GPU または NPU を持っていない場合は、モデルの CPU バージョンがダウンロードされます。
アプリケーションを実行する
アプリケーションを実行するには、ターミナルを開き、 translation_app.js
ファイルを保存したディレクトリに移動します。 次に、次のコマンドを実行します。
node translation_app.js
次のステップ
- より高度な機能については、 LangChain のドキュメント を参照してください。
- Foundry Local で実行する Hugging Face モデルをコンパイルする