Breyta

Deila með


Microsoft Foundry Quickstart

Note

This document refers to the Microsoft Foundry (classic) portal.

🔄 Switch to the Microsoft Foundry (new) documentation if you're using the new portal.

Note

This document refers to the Microsoft Foundry (new) portal.

In this quickstart, you use Microsoft Foundry to:

  • Create a project
  • Deploy a model
  • Run a chat completion
  • Create and run an agent
  • Upload files to the agent

In this quickstart, you use Microsoft Foundry to:

  • Create a project
  • Deploy a model
  • Get ready to code - install necessary packages and authenticate
  • Chat with a model
  • Create an agent
  • Chat with an agent

The Microsoft Foundry SDK is available in multiple languages, including Python, Java, TypeScript, and C#. This quickstart provides instructions for each of these languages.

Tip

The rest of this article shows how to create and use a Foundry project. See Quickstart: Get started with Microsoft Foundry (Hub projects) if you want to use a hub-based project instead. Which type of project do I need?

Prerequisites

Important

Before starting, make sure your development environment is ready.
This Quickstart focuses on scenario-specific steps like SDK installation, authentication, and running sample code.

Create resources

In the portal, you can explore a rich catalog of cutting-edge models from many different providers. For this tutorial, search and then select the gpt-4o model.

  1. Sign in to Microsoft Foundry. Make sure the New Foundry toggle is off. These steps refer to Foundry (classic).

  2. If you're in a project, select Microsoft Foundry in the upper-left breadcrumb to leave the project. You'll create a new one in a moment.

  3. From the landing page or Model catalog, select gpt-4o (or gpt-4o-mini).

    Screenshot shows how to start with a model in Foundry portal.

  4. Select Use this model. When prompted, enter a new project name and select Create.

  5. Review the deployment name and select Create.

  6. Then select Connect and deploy after selecting a deployment type.

  7. Select Open in playground from the deployment page after it's deployed.

  8. You land in the Chat playground with the model pre-deployed and ready to use.

If you're building an agent, you can instead start with Create an agent. The steps are similar, but in a different order. Once the project is created, you arrive at the Agent playground instead of the Chat playground.

Now that you have an agent, you can interact with it either in code or in the portal.

You'll start in Microsoft Foundry portal to create a project and deploy a model. This quickstart uses the gpt-4-1-mini model, but you can use any supported model from several providers.

  1. Sign in to Microsoft Foundry. Make sure the New Foundry toggle is off. These steps refer to Foundry (classic).
    Sign in to Microsoft Foundry. Make sure the New Foundry toggle is on. These steps refer to Foundry (new).
  2. Projects help organize your work. The project you're working on appears in the upper-left corner.
  3. To create a new project, select the project name, then Create new project.
  4. Give your project a name and select Create project.
  5. Now deploy a model into the project:
    1. Select Discover in the upper-right navigation.
    2. Select Models.
    3. Search for the gpt-4.1-mini model.
    4. Select Deploy > Default settings to add it to your project.

Foundry Models allows customers to consume the most powerful models from flagship model providers using a single endpoint and credentials. This means that you can switch between models and consume them from your application without changing a single line of code.

You're now ready to move on to interacting with your model and creating an agent.

Get ready to code

Tip

Code uses Foundry projects (classic) API and is incompatible with Foundry projects (new) API (preview). Switch to Foundry (new) documentation for the Foundry projects (new) API (preview) version.

  1. Install these packages:

    pip install openai azure-identity azure-ai-projects==1.0.0
    
  2. Microsoft Foundry Models allows customers to consume the most powerful models from flagship model providers using a single endpoint and credentials. This means that you can switch between models and consume them from your application without changing a single line of code.

    Copy the Foundry project endpoint in the Overview section of your project. You'll use it in a moment.

    Screenshot shows the project overview for a Foundry project.

    Tip

    If you don't see the Foundry project endpoint, you're using a hub-based project. (See Types of projects). Switch to a Foundry project, or use the preceding steps to create one.

    1. Select Home from the upper-right navigation.
    2. Select Keys and copy the Endpoint. You'll use it in a moment.
  3. Make sure to sign in using the CLI az login (or az login --use-device-code) command to authenticate before running your Python scripts.

Follow along below or get the code:

Important

Code in this article uses packages that are currently in preview. This preview is provided without a service-level agreement, and we don't recommend it for production workloads. Certain features might not be supported or might have constrained capabilities. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.

Set environment variables

Store the endpoint as an environment variable. Also set these values for use in your scripts.

  1. Copy your endpoint from the welcome screen. You'll use it in the next step.

    Screenshot of Microsoft Foundry Models welcome screen showing the endpoint URL and copy button.

  2. Set these environment variables to use in your scripts:

    AZURE_AI_FOUNDRY_PROJECT_ENDPOINT=<endpoint copied from welcome screen>
    AZURE_AI_FOUNDRY_AGENT_NAME="MyAgent"
    AZURE_AI_FOUNDRY_MODEL_DEPLOYMENT_NAME="gpt-4.1-mini"
    

Install and authenticate

Tip

Code uses Foundry projects (new) API (preview) and is incompatible with Foundry projects (classic) API version. Switch to Foundry (classic) documentation for the Foundry projects (classic) API version.

  1. Install these packages, including the preview version of azure-ai-projects. This version uses the Foundry projects (new) API (preview).

    pip install azure-ai-projects --pre
    pip install openai azure-identity python-dotenv
    
  2. Make sure to sign in using the CLI az login (or az login --use-device-code) command to authenticate before running your Python scripts.

Follow along below or get the code:

Chat with a model

Chat completions are the basic building block of AI applications. Using chat completions you can send a list of messages and get a response from the model.

Tip

Code uses Foundry projects (classic) API and is incompatible with Foundry projects (new) API (preview). Switch to Foundry (new) documentation for the Foundry projects (new) API (preview) version.

Substitute your endpoint for the endpoint in this code:

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)

Interacting with a model is the basic building block of AI applications. Send an input and receive a response from the model:

Tip

Code uses Foundry projects (new) API (preview) and is incompatible with Foundry projects (classic) API version. Switch to Foundry (classic) documentation for the Foundry projects (classic) API version.

import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

load_dotenv()

print(f"Using AZURE_AI_FOUNDRY_PROJECT_ENDPOINT: {os.environ['AZURE_AI_FOUNDRY_PROJECT_ENDPOINT']}")
print(f"Using AZURE_AI_FOUNDRY_MODEL_DEPLOYMENT_NAME: {os.environ['AZURE_AI_FOUNDRY_MODEL_DEPLOYMENT_NAME']}")

project_client = AIProjectClient(
    endpoint=os.environ["AZURE_AI_FOUNDRY_PROJECT_ENDPOINT"],
    credential=DefaultAzureCredential(),
)

openai_client = project_client.get_openai_client()

response = openai_client.responses.create(
    model=os.environ["AZURE_AI_FOUNDRY_MODEL_DEPLOYMENT_NAME"],
    input="What is the size of France in square miles?",
)
print(f"Response output: {response.output_text}")

Create an agent

Create an agent using your deployed model.

An agent defines core behavior. Once created, it ensures consistent responses in user interactions without repeating instructions each time. You can update or delete agents anytime.

Tip

Code uses Foundry projects (new) API (preview) and is incompatible with Foundry projects (classic) API version. Switch to Foundry (classic) documentation for the Foundry projects (classic) API version.

import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition

load_dotenv()

project_client = AIProjectClient(
    endpoint=os.environ["AZURE_AI_FOUNDRY_PROJECT_ENDPOINT"],
    credential=DefaultAzureCredential(),
)

agent = project_client.agents.create_version(
    agent_name=os.environ["AZURE_AI_FOUNDRY_AGENT_NAME"],
    definition=PromptAgentDefinition(
        model=os.environ["AZURE_AI_FOUNDRY_MODEL_DEPLOYMENT_NAME"],
        instructions="You are a helpful assistant that answers general questions",
    ),
)
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")

Chat with an agent

Create an agent and chat with it.

Tip

Code uses Foundry projects (classic) API and is incompatible with Foundry projects (new) API (preview). Switch to Foundry (new) documentation for the Foundry projects (new) API (preview) version.

Substitute your endpoint for the endpoint in this code:

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")

Use the previously created agent named "MyAgent" to interact by asking a question and a related follow-up. The conversation maintains history across these interactions.

Tip

Code uses Foundry projects (new) API (preview) and is incompatible with Foundry projects (classic) API version. Switch to Foundry (classic) documentation for the Foundry projects (classic) API version.

import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

load_dotenv()

project_client = AIProjectClient(
    endpoint=os.environ["AZURE_AI_FOUNDRY_PROJECT_ENDPOINT"],
    credential=DefaultAzureCredential(),
)

agent_name = os.environ["AZURE_AI_FOUNDRY_AGENT_NAME"]
openai_client = project_client.get_openai_client()

# Optional Step: Create a conversation to use with the agent
conversation = openai_client.conversations.create()
print(f"Created conversation (id: {conversation.id})")

# Chat with the agent to answer questions
response = openai_client.responses.create(
    conversation=conversation.id, #Optional conversation context for multi-turn
    extra_body={"agent": {"name": agent_name, "type": "agent_reference"}},
    input="What is the size of France in square miles?",
)
print(f"Response output: {response.output_text}")

# Optional Step: Ask a follow-up question in the same conversation
response = openai_client.responses.create(
    conversation=conversation.id,
    extra_body={"agent": {"name": agent_name, "type": "agent_reference"}},
    input="And what is the capital city?",
)
print(f"Response output: {response.output_text}")

Add files to the agent

Agents have powerful capabilities through the use of tools. Let's add a file search tool that enables us to do knowledge retrieval.

Tip

Code uses Foundry projects (classic) API and is incompatible with Foundry projects (new) API (preview). Switch to Foundry (new) documentation for the Foundry projects (new) API (preview) version.

Substitute your endpoint for the endpoint in this code:

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)

Clean up resources

If you no longer need any of the resources you created, delete the resource group associated with your project.

In the Microsoft Foundry portal, select your project name in the top right corner. Then select the link for the resource group to open it in the Azure portal. Select the resource group, and then select Delete. Confirm that you want to delete the resource group.

In the Azure portal, find and select your resource group. Select Delete and confirm to delete the resource group and all its associated resources.

Next step