次の方法で共有


Microsoft Foundry クイック スタート (クラシック)

このドキュメントでは、 Microsoft Foundry (クラシック) ポータルを参照します。

🔍 新しいポータルの詳細については、Microsoft Foundry (新しい) ドキュメント を参照してください。

このクイック スタートでは、 Microsoft Foundry を使用して次の操作を行います。

  • プロジェクトを作成する
  • モデルをデプロイする
  • チャット補完を実行する
  • エージェントを作成して実行する
  • エージェントにファイルをアップロードする

Microsoft Foundry SDK は、Python、Java、TypeScript、C# などの複数の言語で使用できます。 このクイック スタートでは、これらの各言語の手順について説明します。

ヒント

この記事の残りの部分では、 Foundry プロジェクトを作成して使用する方法について説明します。 代わりにハブベースのプロジェクトを使用する場合は、「 クイック スタート: Microsoft Foundry (ハブ プロジェクト) の使用を開始 する」を参照してください。 必要なプロジェクトの種類

[前提条件]

Important

開始する前に、開発環境の準備ができていることを確認します。
このクイック スタートでは、SDK のインストール、認証、サンプル コードの実行などの シナリオ固有の手順 について説明します。

ポータルでは、さまざまなプロバイダーの最先端モデルの豊富なカタログを調べることができます。 このチュートリアルでは、 gpt-4o モデルを検索して選択します。

  1. Microsoft Foundry にサインインします。 「New Foundry」トグルがオフになっていることを確認します。 これらの手順は Foundry (クラシック) を参照します。

  2. プロジェクトを使用している場合は、左上の階層リンクで Microsoft Foundry を選択してプロジェクトを終了します。 すぐに新しいものを作成します。

  3. ランディング ページまたは モデル カタログから、 gpt-4o (または gpt-4o-mini) を選択します。

    Foundry ポータルでモデルを開始する方法を示すスクリーンショット。

  4. [ このモデルを使用] を選択します。 メッセージが表示されたら、新しいプロジェクト名を入力し、[ 作成] を選択します。

  5. デプロイ名を確認し、[作成] を選びます。

  6. 次に、デプロイの種類を選んだ後、[接続とデプロイ] を選びます。

  7. デプロイされた後、デプロイ ページから [プレイグラウンドで開く] を選びます。

  8. モデルが事前にデプロイされて使用できる状態で、チャット プレイグラウンドに移動します。

エージェントを構築する場合は、代わりにエージェントの 作成から始めることができます。 手順は似ていますが、順序は異なります。 プロジェクトが作成されると、チャットプレイグラウンドではなくエージェントプレイグラウンドに到着します。

コーディングの準備をする

ヒント

コードは Azure AI Projects 1.x SDK を 使用し、Azure AI Projects 2.x と互換性がありません。 Azure AI Projects 2.x バージョンの Foundry (新しい) ドキュメントを参照してください

  1. 次のパッケージをインストールします。

    pip install openai azure-identity azure-ai-projects==1.0.0
    
  2. プロジェクトのウェルカム画面でプロジェクト エンドポイントを見つけます。

    エンドポイントの URL とコピー ボタンを示す Microsoft Foundry Models のようこそ画面のスクリーンショット。

  3. Python スクリプトを実行する前に、CLI az login (または az login --use-device-code) コマンドを使用して認証を行ってサインインしてください。

以下に従うか、コードを取得します。

モデルとのチャット

チャットの完了は、AI アプリケーションの基本的な構成要素です。 チャットの完了を使用すると、メッセージの一覧を送信し、モデルから応答を取得できます。

ヒント

コードは Azure AI Projects 1.x SDK を 使用し、Azure AI Projects 2.x と互換性がありません。 Azure AI Projects 2.x バージョンの Foundry (新しい) ドキュメントを参照してください

エンドポイントを次のコードの endpoint に置き換える:

from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential

project = AIProjectClient(
    endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name",
    credential=DefaultAzureCredential(),
)

models = project.get_openai_client(api_version="2024-10-21")
response = models.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful writing assistant"},
        {"role": "user", "content": "Write me a poem about flowers"},
    ],
)

print(response.choices[0].message.content)

エージェントとのチャット

エージェントを作成してチャットします。

ヒント

コードは Azure AI Projects 1.x SDK を 使用し、Azure AI Projects 2.x と互換性がありません。 Azure AI Projects 2.x バージョンの Foundry (新しい) ドキュメントを参照してください

エンドポイントを次のコードの endpoint に置き換える:

from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.agents.models import ListSortOrder, FilePurpose

project = AIProjectClient(
    endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name",
    credential=DefaultAzureCredential(),
)

agent = project.agents.create_agent(
    model="gpt-4o",
    name="my-agent",
    instructions="You are a helpful writing assistant")

thread = project.agents.threads.create()
message = project.agents.messages.create(
    thread_id=thread.id, 
    role="user", 
    content="Write me a poem about flowers")

run = project.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
if run.status == "failed":
    # Check if you got "Rate limit is exceeded.", then you want to get more quota
    print(f"Run failed: {run.last_error}")

# Get messages from the thread
messages = project.agents.messages.list(thread_id=thread.id)

# Get the last message from the sender
messages = project.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
for message in messages:
    if message.run_id == run.id and message.text_messages:
        print(f"{message.role}: {message.text_messages[-1].text.value}")

# Delete the agent once done
project.agents.delete_agent(agent.id)
print("Deleted agent")

エージェントにファイルを追加する

エージェントには、ツールを使用して強力な機能があります。 知識の取得を可能にするファイル検索ツールを追加しましょう。

ヒント

コードは Azure AI Projects 1.x SDK を 使用し、Azure AI Projects 2.x と互換性がありません。 Azure AI Projects 2.x バージョンの Foundry (新しい) ドキュメントを参照してください

エンドポイントを次のコードの endpoint に置き換える:

from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.agents.models import ListSortOrder, FileSearchTool

project = AIProjectClient(
    endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name",
    credential=DefaultAzureCredential(),
)

# Upload file and create vector store
file = project.agents.files.upload(file_path="./product_info_1.md", purpose=FilePurpose.AGENTS)
vector_store = project.agents.vector_stores.create_and_poll(file_ids=[file.id], name="my_vectorstore")

# Create file search tool and agent
file_search = FileSearchTool(vector_store_ids=[vector_store.id])
agent = project.agents.create_agent(
    model="gpt-4o",
    name="my-assistant",
    instructions="You are a helpful assistant and can search information from uploaded files",
    tools=file_search.definitions,
    tool_resources=file_search.resources,
)

# Create thread and process user message
thread = project.agents.threads.create()
project.agents.messages.create(thread_id=thread.id, role="user", content="Hello, what Contoso products do you know?")
run = project.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)

# Handle run status
if run.status == "failed":
    print(f"Run failed: {run.last_error}")

# Print thread messages
messages = project.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
for message in messages:
    if message.run_id == run.id and message.text_messages:
        print(f"{message.role}: {message.text_messages[-1].text.value}")

# Cleanup resources
project.agents.vector_stores.delete(vector_store.id)
project.agents.files.delete(file_id=file.id)
project.agents.delete_agent(agent.id)

リソースをクリーンアップする

作成したリソースが不要になった場合は、プロジェクトに関連付けられているリソース グループを削除します。

  • Azure portal でリソース グループを選択し、[削除] を選択します。 リソース グループを削除することを確認します。