Bagikan melalui


Panduan Cepat Microsoft Foundry

Dalam panduan cepat ini, Anda akan memulai penggunaan model dan agen di Foundry.

Anda akan:

  • Membuat respons dari model
  • Membuat agen dengan perintah yang ditentukan
  • Mengadakan percakapan bergiliran dengan agen

Prasyarat

Mengatur variabel lingkungan dan mendapatkan kode

Simpan titik akhir proyek Anda sebagai variabel lingkungan. Atur juga nilai-nilai ini untuk digunakan dalam skrip Anda.

PROJECT_ENDPOINT=<endpoint copied from welcome screen>
AGENT_NAME="MyAgent"

Ikuti di bawah ini atau dapatkan kode:

Masuk menggunakan perintah CLI az login untuk mengautentikasi sebelum menjalankan skrip Python Anda.

Menginstal dan mengautentikasi

Pastikan Anda menginstal versi paket yang benar seperti yang ditunjukkan di sini.

  1. Instal versi saat ini dari azure-ai-projects. Versi ini menggunakan API proyek Foundry (baru).

    pip install azure-ai-projects>=2.0.0
    
  2. Masuk menggunakan perintah CLI az login untuk mengautentikasi sebelum menjalankan skrip Python Anda.

Petunjuk / Saran

Kode menggunakan Azure Proyek AI 2.x dan tidak kompatibel dengan Proyek AI Azure 1.x. Lihat dokumentasi Foundry (klasik) untuk versi Azure AI Projects 1.x.

Mengobrol dengan model

Berinteraksi dengan model adalah blok penyusun dasar aplikasi AI. Kirim input dan terima respons dari model:

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

# Format: "https://resource_name.ai.azure.com/api/projects/project_name"
PROJECT_ENDPOINT = "your_project_endpoint"

# Create project and openai clients to call Foundry API
project = AIProjectClient(
    endpoint=PROJECT_ENDPOINT,
    credential=DefaultAzureCredential(),
)
openai = project.get_openai_client()

# Run a responses API call
response = openai.responses.create(
    model="gpt-5-mini",  # supports all Foundry direct models
    input="What is the size of France in square miles?",
)
print(f"Response output: {response.output_text}")

Setelah menjalankan kode, Anda akan melihat respons yang dihasilkan model di konsol (misalnya, puisi singkat atau jawaban atas perintah Anda). Ini mengonfirmasi titik akhir proyek, autentikasi, dan penyebaran model Anda berfungsi dengan benar.

Petunjuk / Saran

Kode menggunakan Azure Proyek AI 2.x dan tidak kompatibel dengan Proyek AI Azure 1.x. Lihat dokumentasi Foundry (klasik) untuk versi Azure AI Projects 1.x.

Membuat agen

Buat agen menggunakan model yang Anda sebarkan.

Agen mendefinisikan perilaku inti. Setelah dibuat, ini memastikan respons yang konsisten dalam interaksi pengguna tanpa mengulangi instruksi setiap kali. Anda dapat memperbarui atau menghapus agen kapan saja.

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

# Format: "https://resource_name.ai.azure.com/api/projects/project_name"
PROJECT_ENDPOINT = "your_project_endpoint"
AGENT_NAME = "your_agent_name"

# Create project client to call Foundry API
project = AIProjectClient(
    endpoint=PROJECT_ENDPOINT,
    credential=DefaultAzureCredential(),
)

# Create an agent with a model and instructions
agent = project.agents.create_version(
    agent_name=AGENT_NAME,
    definition=PromptAgentDefinition(
        model="gpt-5-mini",  # supports all Foundry direct models"
        instructions="You are a helpful assistant that answers general questions",
    ),
)
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")

Output mengonfirmasi bahwa agen telah dibuat. Untuk tab SDK, Anda akan melihat nama agen dan ID yang dicetak ke konsol.

Petunjuk / Saran

Kode menggunakan Azure Proyek AI 2.x dan tidak kompatibel dengan Proyek AI Azure 1.x. Lihat dokumentasi Foundry (klasik) untuk versi Azure AI Projects 1.x.

Mengobrol dengan agen

Gunakan agen yang dibuat sebelumnya bernama "MyAgent" untuk berinteraksi dengan mengajukan pertanyaan dan tindak lanjut terkait. Percakapan menyimpan riwayat dari semua interaksi ini.

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

# Format: "https://resource_name.ai.azure.com/api/projects/project_name"
PROJECT_ENDPOINT = "your_project_endpoint"
AGENT_NAME = "your_agent_name"

# Create project and openai clients to call Foundry API
project = AIProjectClient(
    endpoint=PROJECT_ENDPOINT,
    credential=DefaultAzureCredential(),
)
openai = project.get_openai_client()

# Create a conversation for multi-turn chat
conversation = openai.conversations.create()

# Chat with the agent to answer questions
response = openai.responses.create(
    conversation=conversation.id,
    extra_body={"agent_reference": {"name": AGENT_NAME, "type": "agent_reference"}},
    input="What is the size of France in square miles?",
)
print(response.output_text)

# Ask a follow-up question in the same conversation
response = openai.responses.create(
    conversation=conversation.id,
    extra_body={"agent_reference": {"name": AGENT_NAME, "type": "agent_reference"}},
    input="And what is the capital city?",
)
print(response.output_text)

Anda melihat respons agen terhadap kedua perintah. Respons tindak lanjut menunjukkan bahwa agen mempertahankan riwayat percakapan secara bergiliran.

Petunjuk / Saran

Kode menggunakan Azure Proyek AI 2.x dan tidak kompatibel dengan Proyek AI Azure 1.x. Lihat dokumentasi Foundry (klasik) untuk versi Azure AI Projects 1.x.

Membersihkan sumber daya

Jika Anda tidak lagi memerlukan sumber daya yang Anda buat, hapus grup sumber daya yang terkait dengan proyek Anda.

  • Di portal Azure, pilih grup sumber daya, lalu pilih Delete. Konfirmasikan bahwa Anda ingin menghapus grup sumber daya.

Langkah selanjutnya