> ## Documentation Index
> Fetch the complete documentation index at: https://azure-foundry.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Get started with Azure AI Foundry

> This article provides instructions on how to start using the Azure AI Foundry portal and the Azure AI Foundry SDK.

<Tabs>
  <Tab title="Foundry project">
    In this quickstart, you use [Azure AI Foundry](https://ai.azure.com/?cid=learnDocs) to:

    * Create a project
    * Deploy a model
    * Run a chat completion
    * Create and run an agent
    * Upload files to the agent
      The Azure AI Foundry SDK is available in multiple languages, including Python, Java, JavaScript, and C#. This quickstart provides instructions for each of these languages.

    <Tip>
      The rest of this article shows how to use a **Foundry project**. Select **hub based project** at the top of this article if you want to use a hub based project instead. [Which type of project do I need?](https://learn.microsoft.com/en-us/azure/ai-foundry/what-is-azure-ai-foundry#which-type-of-project-do-i-need)
    </Tip>

    ## Prerequisites

    * An [Azure subscription](https://azure.microsoft.com/free/). If you don't have an Azure subscription, create a free account before you begin.
    * You must be **Owner** of the subscription to receive the appropriate access control needed to use your project.

    <Note>
      Items marked (preview) in this article are currently in public 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](https://azure.microsoft.com/support/legal/preview-supplemental-terms/).
    </Note>

    ## Start with a project and model

    1. Sign in to the [Azure AI Foundry portal](https://ai.azure.com/?cid=learnDocs).
    2. On the home page, search and then select the **gpt-4o** model.

    <Frame>
      <img src="https://mintlify.s3.us-west-1.amazonaws.com/azure-foundry/images/ai-foundry/media/quickstarts/start-building.png" alt="" />
    </Frame>

    3. On the model details page, select **Use this model**.
    4. Fill in a name to use for your project and select **Create**.
    5. Once your resources are created, you are in the chat playground.

    ## Set up your environment

    <Tabs>
      <Tab title="Azure AI Foundry project">
        No installation is necessary to use the Azure AI Foundry portal.
      </Tab>

      <Tab title="Python">
        1. [Install Python and Azure CLI](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/develop/install-cli-sdk?pivots=programming-language-python)
        2. Install these packages:

           ```bash theme={"system"}
           pip install openai azure-ai-projects azure-identity
           ```
        3. Copy the **Azure AI Foundry project endpoint** in the **Overview** section of your project. You'll use it in a moment.

        <Frame>
          <img src="https://mintlify.s3.us-west-1.amazonaws.com/azure-foundry/images/ai-foundry/media/how-to/projects/fdp-project-overview.png" alt="" />
        </Frame>

        <Tip>
          If you don't see the **Azure AI Foundry project endpoint**, you're using a hub based project. (See [Types of projects](https://learn.microsoft.com/en-us/azure/ai-foundry/what-is-azure-ai-foundry#project-types)). Switch to a Foundry project, or use the preceding steps to create one.
        </Tip>

        4. Make sure to sign in using the CLI `az login` (or `az login --use-device-code`) command to authenticate before running your Python scripts.

        <Note>
          All the code in this article is at [GitHub Quickstart](https://github.com/azure-ai-foundry/foundry-samples/tree/main/samples/microsoft/python/mslearn-resources/quickstart).
        </Note>
      </Tab>

      <Tab title="Java (preview)">
        1. [Install Java and Azure CLI](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/develop/install-cli-sdk?pivots=programming-language-java).
        2. Copy the **Azure AI Foundry project endpoint** in the **Overview** section of your project. You'll use it in a moment.

        <Frame>
          <img src="https://mintlify.s3.us-west-1.amazonaws.com/azure-foundry/images/ai-foundry/media/how-to/projects/fdp-project-overview.png" alt="" />
        </Frame>

        <Tip>
          If you don't see the **Azure AI Foundry project endpoint**, you're using a hub based project. (See [Types of projects](https://learn.microsoft.com/en-us/azure/ai-foundry/what-is-azure-ai-foundry#project-types)). Switch to a Foundry project, or use the preceding steps to create one.
        </Tip>

        3.

        ```bash theme={"system"}
        MODEL_DEPLOYMENT_NAME=gpt-4o
        PROJECT_ENDPOINT=https://<your-foundry-resource-name>.services.ai.azure.com/api/projects/<your-foundry-project-name>
        ```

        4. Make sure to sign in using the CLI `az login` (or `az login --use-device-code`) command to authenticate before running your Java scripts.
        5. Download [POM.XML](https://github.com/azure-ai-foundry/foundry-samples/blob/main/samples/microsoft/java/mslearn-resources/quickstart/pom.xml) to your Java IDE.

        <Note>
          All the code in this article is at [GitHub Quickstart](https://github.com/azure-ai-foundry/foundry-samples/blob/main/samples/microsoft/java/mslearn-resources/quickstart).
        </Note>
      </Tab>
    </Tabs>

    ## Run a chat completion

    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.

    <Tabs>
      <Tab title="Azure AI Foundry Portal">
        1. In the chat playground, fill in the prompt and select the **Send** button.
        2. The model returns a response in the **Response** pane.
      </Tab>

      <Tab title="Python">
        Substitute your endpoint for the `endpoint` in this code:

        ```python theme={"system"}
        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.inference.get_azure_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)
        ```
      </Tab>

      <Tab title="Java (preview)">
        ```java expandable theme={"system"}
                package com.azure.ai.foundry.samples;

                import com.azure.ai.inference.ChatCompletionsClient;
                import com.azure.ai.inference.ChatCompletionsClientBuilder;
                import com.azure.ai.inference.models.ChatCompletions;
                import com.azure.core.credential.AzureKeyCredential;
                import com.azure.core.credential.TokenCredential;
                import com.azure.core.exception.HttpResponseException;
                import com.azure.core.util.logging.ClientLogger;
                import com.azure.identity.DefaultAzureCredentialBuilder;

                /**
                * Sample demonstrating non-streaming chat completion functionality
                * using the Azure AI Inference SDK, wired to your AOAI project endpoint.
                *
                * Environment variables:
                * - PROJECT_ENDPOINT:          Required. Your Azure AI project endpoint.
                * - AZURE_AI_API_KEY:          Optional. Your API key (falls back to DefaultAzureCredential).
                * - AZURE_MODEL_DEPLOYMENT_NAME: Optional. Model deployment name (default: "phi-4").
                * - AZURE_MODEL_API_PATH:      Optional. API path segment (default: "deployments").
                * - CHAT_PROMPT:               Optional. The prompt to send (uses a default if not provided).
                * 
                * SDK Features Demonstrated:
                * - Using the Azure AI Inference SDK (com.azure:azure-ai-inference:1.0.0-beta.5)
                * - Creating a ChatCompletionsClient with Azure or API key authentication
                * - Configuring endpoint paths for different model deployments
                * - Using the simplified complete() method for quick completions
                * - Accessing response content through strongly-typed objects
                * - Implementing proper error handling for service requests
                * - Choosing between DefaultAzureCredential and AzureKeyCredential
                * 
                */

                public class ChatCompletionSample {
                    private static final ClientLogger logger = new ClientLogger(ChatCompletionSample.class);

                    public static void main(String[] args) {
                        // 1) Read and validate the project endpoint
                        String projectEndpoint = System.getenv("PROJECT_ENDPOINT");
                        if (projectEndpoint == null || projectEndpoint.isBlank()) {
                            logger.error("PROJECT_ENDPOINT is required but not set");
                            return;
                        }

                        // 2) Optional auth + model settings
                        String apiKey          = System.getenv("AZURE_AI_API_KEY");
                        String deploymentName  = System.getenv("AZURE_MODEL_DEPLOYMENT_NAME");
                        String apiPath         = System.getenv("AZURE_MODEL_API_PATH");
                        String prompt          = System.getenv("CHAT_PROMPT");

                        if (deploymentName == null || deploymentName.isBlank()) {
                            deploymentName = "phi-4";
                            logger.info("No AZURE_MODEL_DEPLOYMENT_NAME provided, using default: {}", deploymentName);
                        }
                        if (apiPath == null || apiPath.isBlank()) {
                            apiPath = "deployments";
                            logger.info("No AZURE_MODEL_API_PATH provided, using default: {}", apiPath);
                        }
                        if (prompt == null || prompt.isBlank()) {
                            prompt = "What best practices should I follow when asking an AI model to review Java code?";
                            logger.info("No CHAT_PROMPT provided, using default prompt: {}", prompt);
                        }

                        try {
                            // 3) Build the full inference endpoint URL
                            String fullEndpoint = projectEndpoint.endsWith("/") 
                                ? projectEndpoint 
                                : projectEndpoint + "/";
                            fullEndpoint += apiPath + "/" + deploymentName;
                            logger.info("Using inference endpoint: {}", fullEndpoint);

                            // 4) Create the client with key or token credential :contentReference[oaicite:0]{index=0}
                            ChatCompletionsClient client;
                            if (apiKey != null && !apiKey.isBlank()) {
                                logger.info("Authenticating using API key");
                                client = new ChatCompletionsClientBuilder()
                                    .credential(new AzureKeyCredential(apiKey))
                                    .endpoint(fullEndpoint)
                                    .buildClient();
                            } else {
                                logger.info("Authenticating using DefaultAzureCredential");
                                TokenCredential credential = new DefaultAzureCredentialBuilder().build();
                                client = new ChatCompletionsClientBuilder()
                                    .credential(credential)
                                    .endpoint(fullEndpoint)
                                    .buildClient();
                            }

                            // 5) Send a simple chat completion request
                            logger.info("Sending chat completion request with prompt: {}", prompt);
                            ChatCompletions completions = client.complete(prompt);

                            // 6) Process the response
                            String content = completions.getChoice().getMessage().getContent();
                            logger.info("Received response from model");
                            System.out.println("\nResponse from AI assistant:\n" + content);
                        } catch (HttpResponseException e) {
                            // Handle API errors
                            int status = e.getResponse().getStatusCode();
                            logger.error("Service error {}: {}", status, e.getMessage());
                            if (status == 401 || status == 403) {
                                logger.error("Authentication failed. Check API key or Azure credentials.");
                            } else if (status == 404) {
                                logger.error("Deployment not found. Verify deployment name and endpoint.");
                            } else if (status == 429) {
                                logger.error("Rate limit exceeded. Please retry later.");
                            }
                        } catch (Exception e) {
                            // Handle all other exceptions
                            logger.error("Error in chat completion: {}", e.getMessage(), e);
                        }
                        }
                        }
        ```
      </Tab>
    </Tabs>
  </Tab>

  <Tab title="Hub based project">
    In this quickstart, we walk you through setting up your local development environment with the [Azure AI Foundry](https://ai.azure.com/?cid=learnDocs) SDK. We write a prompt, run it as part of your app code, trace the LLM calls being made, and run a basic evaluation on the outputs of the LLM.

    <Tip>
      The rest of this article shows how to use a \*\* \*\*.  Select **Foundry Project** at the top of this article if you want to use a Foundry Project instead.  [Which type of project do I need?](../what-is-azure-ai-foundry.md#which-type-of-project-do-i-need)
    </Tip>

    ## Prerequisites

    * An [Azure subscription](https://azure.microsoft.com/free/). If you don't have an Azure subscription, create a free account before you begin.
    * A hub based project . If you're new to Azure AI Foundry and don't have a hub based project , select **Foundry Project** at the top of this article to use a Foundry project instead.

    ## Set up your development environment

    1. [Set up your development environment](../how-to/develop/install-cli-sdk)

    2. Install these packages.

       ```bash theme={"system"}
       pip install azure-ai-inference azure-identity azure-ai-projects==1.0.0b10
       ```

           <Note>
             Different project types require different versions of the `azure-ai-projects` package. To avoid conflicts, create separate Python environments: use version `1.0.0b10` for hub based project s and the latest version for Foundry projects.
           </Note>

    ## Deploy a model

    <Tip>
      Because you can [customize the left pane](https://learn.microsoft.com/en-us/azure/ai-foundry/what-is-azure-ai-foundry#left-pane) in the Azure AI Foundry portal, you might see different items than shown in these steps. If you don't see what you're looking for, select **... More** at the bottom of the left pane.
    </Tip>

    1. Sign in to [Azure AI Foundry](https://ai.azure.com/?cid=learnDocs).

    2. Select a hub based project. If you don't have a hub based project , select **Foundry Project** at the top of this article to use a Foundry project instead.

    3. Select **Model catalog** from the left pane.

    4. Select the **gpt-4o-mini** model from the list of models. You can use the search bar to find it.

    5. On the model details page, select **Deploy**.

           <Frame>
             <img src="https://mintlify.s3.us-west-1.amazonaws.com/azure-foundry/images/ai-foundry/media/tutorials/chat/deploy-model.png" alt="" />
           </Frame>

    6. Leave the default **Deployment name**. Select **Deploy**.

    7. Once the model is deployed, select **Open in playground** to test your model.

    ## Build your chat app

    Create a file named **chat.py**.  Copy and paste the following code into it.

    ```python theme={"system"}
    from azure.ai.projects import AIProjectClient
    from azure.identity import DefaultAzureCredential

    project_connection_string = "<your-connection-string-goes-here>"

    project = AIProjectClient.from_connection_string(
        conn_str=project_connection_string, credential=DefaultAzureCredential()
    )

    chat = project.inference.get_chat_completions_client()
    response = chat.complete(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "system",
                "content": "You are an AI assistant that speaks like a techno punk rocker from 2350. Be cool but not too cool. Ya dig?",
            },
            {"role": "user", "content": "Hey, can you help me with my taxes? I'm a freelancer."},
        ],
    )

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

    ## Insert your connection string

    Your project connection string is required to call the Azure OpenAI in Azure AI Foundry Models from your code.

    Find your connection string in the Azure AI Foundry project you created in the [Azure AI Foundry playground quickstart](../quickstarts/get-started-playground).  Open the project, then find the connection string on the **Overview** page.

    <Frame>
      <img src="https://mintlify.s3.us-west-1.amazonaws.com/azure-foundry/images/ai-foundry/media/quickstarts/azure-ai-sdk/connection-string.png" alt="" />
    </Frame>

    Copy the connection string and replace `<your-connection-string-goes-here>` in the **chat.py** file.

    ## Run your chat script

    Run the script to see the response from the model.

    ```bash theme={"system"}
    python chat.py
    ```

    ## Generate prompt from user input and a prompt template

    The script uses hardcoded input and output messages. In a real app you'd take input from a client application, generate a system message with internal instructions to the model, and then call the LLM with all of the messages.

    Let's change the script to take input from a client application and generate a system message using a prompt template.

    1. Remove the last line of the script that prints a response.

    2. Now define a `get_chat_response` function that takes messages and context, generates a system message using a prompt template, and calls a model.  Add this code to your  existing **chat.py** file:

       ```python theme={"system"}
       def get_chat_response(messages, context):
           system_message = "You are a helpful assistant."
           messages.append({"role": "system", "content": system_message})
           response = openai.chat.completions.create(
               model="gpt-4o-mini",
               messages=messages)
           return response.choices[0].message.content
       ```

           <Note>
             The prompt template uses mustache format.
           </Note>

       The `get_chat_response` function could be easily added as a route to a FastAPI or Flask app to enable calling this function from a front-end web application.

    3. Now simulate passing information from a frontend application to this function.  Add the following code to the end of your **chat.py** file.  Feel free to play with the message and add your own name.

       ```python theme={"system"}
       messages = [
           {"role": "user", "content": "What is the weather in Tokyo?"}
       ]
       response = get_chat_response(messages, context)
       print(response)
       ```

    Run the revised script to see the response from the model with this new input.

    ```bash theme={"system"}
    python chat.py
    ```
  </Tab>
</Tabs>
