Edit

Share via


Microsoft Foundry quickstart (classic)

Note

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

๐Ÿ” View the Microsoft Foundry (new) documentation to learn about the 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

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.

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.

Get ready to code

Tip

Code uses Azure AI Projects 1.x SDK and is incompatible with Azure AI Projects 2.x. See the Foundry (new) documentation for the Azure AI Projects 2.x version.

  1. Install these packages:

    pip install openai azure-identity azure-ai-projects==1.0.0
    
  2. Find your project endpoint on the welcome screen of the project.

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

  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:

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 Azure AI Projects 1.x SDK and is incompatible with Azure AI Projects 2.x. See the Foundry (new) documentation for the Azure AI Projects 2.x 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)

Chat with an agent

Create an agent and chat with it.

Tip

Code uses Azure AI Projects 1.x SDK and is incompatible with Azure AI Projects 2.x. See the Foundry (new) documentation for the Azure AI Projects 2.x 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")

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 Azure AI Projects 1.x SDK and is incompatible with Azure AI Projects 2.x. See the Foundry (new) documentation for the Azure AI Projects 2.x 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 Azure portal, select the resource group, and then select Delete. Confirm that you want to delete the resource group.