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

# How to generate embeddings with Microsoft Foundry Models service (classic)

> Learn how to generate embeddings with Microsoft Foundry Models (classic)

export const ZonePivot = ({group, options = [], defaultValue, label = "Choose an experience"}) => {
  const values = options.map(option => option.id);
  const optionKey = options.map(option => `${option.id}:${option.title}`).join("|");
  const [activePivot, setActivePivot] = useState(defaultValue || values[0]);
  const slugify = value => value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
  const resolvePivot = () => {
    if (typeof window === "undefined") return defaultValue || values[0];
    const params = new URLSearchParams(window.location.search);
    const requested = params.get("pivots");
    if (requested) {
      const requestedIds = requested.split(",").map(value => value.trim()).filter(Boolean);
      const match = requestedIds.find(id => values.includes(id));
      if (match) return match;
    }
    const hash = window.location.hash.replace(/^#/, "");
    if (hash) {
      const match = options.find(option => option.id === hash || slugify(option.title) === hash);
      if (match) return match.id;
    }
    try {
      const stored = window.localStorage.getItem(`foundry-zone-pivot:${group}`);
      if (values.includes(stored)) return stored;
    } catch {
      return defaultValue || values[0];
    }
    return defaultValue || values[0];
  };
  const publishPivotChange = value => {
    if (typeof window === "undefined") return;
    window.dispatchEvent(new CustomEvent("foundry-zone-pivot-change", {
      detail: {
        group,
        value
      }
    }));
  };
  const syncTableOfContents = () => {
    if (typeof window === "undefined") return;
    window.requestAnimationFrame(() => {
      const toc = document.getElementById("table-of-contents-content");
      if (!toc) return;
      const links = Array.from(toc.querySelectorAll('a[href^="#"]'));
      for (const link of links) {
        const item = link.closest("li");
        const rawId = link.getAttribute("href")?.slice(1);
        if (!item || !rawId) continue;
        let id = rawId;
        try {
          id = decodeURIComponent(rawId);
        } catch {}
        item.style.display = document.getElementById(id) ? "" : "none";
      }
    });
  };
  useEffect(() => {
    const resolvedPivot = resolvePivot();
    setActivePivot(resolvedPivot);
    publishPivotChange(resolvedPivot);
    window.setTimeout(syncTableOfContents, 0);
  }, [group, defaultValue, values.join("|"), optionKey]);
  const selectPivot = value => {
    setActivePivot(value);
    if (typeof window !== "undefined") {
      try {
        window.localStorage.setItem(`foundry-zone-pivot:${group}`, value);
      } catch {}
      const url = new URL(window.location.href);
      const current = url.searchParams.get("pivots");
      const preserved = current ? current.split(",").map(id => id.trim()).filter(id => id && !values.includes(id)) : [];
      url.searchParams.set("pivots", [...preserved, value].join(","));
      window.history.replaceState(null, "", `${url.pathname}${url.search}${url.hash}`);
    }
    publishPivotChange(value);
    window.setTimeout(syncTableOfContents, 0);
  };
  if (options.length < 2) return null;
  return <div className="not-prose my-6 border-b border-slate-200 pb-3 dark:border-slate-800">
      <div className="mb-2 text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400">
        {label}
      </div>
      <div className="flex flex-wrap gap-2" role="tablist" aria-label={label}>
        {options.map(option => {
    const selected = option.id === activePivot;
    return <button key={option.id} type="button" role="tab" aria-selected={selected} onClick={() => selectPivot(option.id)} className={`rounded-md border px-3 py-1.5 text-sm font-medium transition ${selected ? "border-slate-900 bg-slate-900 text-white shadow-sm dark:border-slate-100 dark:bg-slate-100 dark:text-slate-950" : "border-slate-200 bg-white text-slate-700 hover:border-slate-400 hover:text-slate-950 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-200 dark:hover:border-slate-500"}`}>
              {option.title}
            </button>;
  })}
      </div>
    </div>;
};

export const ZoneContent = ({group, value, options = [], values = [], defaultValue, children}) => {
  const optionKey = options.map(option => `${option.id}:${option.title}`).join("|");
  const [activePivot, setActivePivot] = useState(defaultValue || values[0]);
  const slugify = value => value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
  const resolvePivot = () => {
    if (typeof window === "undefined") return defaultValue || values[0];
    const params = new URLSearchParams(window.location.search);
    const requested = params.get("pivots");
    if (requested) {
      const requestedIds = requested.split(",").map(value => value.trim()).filter(Boolean);
      const match = requestedIds.find(id => values.includes(id));
      if (match) return match;
    }
    const hash = window.location.hash.replace(/^#/, "");
    if (hash) {
      const match = options.find(option => option.id === hash || slugify(option.title) === hash);
      if (match) return match.id;
    }
    try {
      const stored = window.localStorage.getItem(`foundry-zone-pivot:${group}`);
      if (values.includes(stored)) return stored;
    } catch {
      return defaultValue || values[0];
    }
    return defaultValue || values[0];
  };
  useEffect(() => {
    setActivePivot(resolvePivot());
  }, [group, defaultValue, values.join("|"), optionKey]);
  useEffect(() => {
    const onPivotChange = event => {
      if (event.detail?.group === group && values.includes(event.detail.value)) {
        setActivePivot(event.detail.value);
      }
    };
    window.addEventListener("foundry-zone-pivot-change", onPivotChange);
    return () => window.removeEventListener("foundry-zone-pivot-change", onPivotChange);
  }, [group, values.join("|")]);
  if (activePivot !== value) return null;
  return <>{children}</>;
};

**Applies only to:** <img src="https://mintcdn.com/hobbyist-e43fa225/irqB4qz-UwcRETil/images/yes-icon.svg?fit=max&auto=format&n=irqB4qz-UwcRETil&q=85&s=fc11d20d284fa4fc8529a352d007d262" width="27" height="16" data-path="images/yes-icon.svg" /> **Foundry (classic) portal**. This article isn't available for the new Foundry portal. [Learn more about the new portal](https://learn.microsoft.com/en-us/azure/foundry/what-is-foundry).

<Note>
  Links in this article might open content in the new Microsoft Foundry documentation instead of the Foundry (classic) documentation you're viewing now.
</Note>

<Info>
  Azure AI Inference beta SDK is deprecated and will be retired on August 26, 2026. Switch to the generally available [OpenAI/v1 API](https://aka.ms/openai/v1) with a stable OpenAI SDK. Follow the [migration guide](../how-to/model-inference-to-openai-migration) to switch to OpenAI/v1, using the SDK for your preferred programming language.
</Info>

<ZonePivot group="programming-language-csharp__programming-language-java__programming-language-javascript__programming-language-python__programming-language-rest" options={[{"id": "programming-language-python", "title": "Python"}, {"id": "programming-language-javascript", "title": "JavaScript"}, {"id": "programming-language-java", "title": "Java"}, {"id": "programming-language-csharp", "title": "C#"}, {"id": "programming-language-rest", "title": "REST"}]} defaultValue="programming-language-python" />

<ZoneContent group="programming-language-csharp__programming-language-java__programming-language-javascript__programming-language-python__programming-language-rest" value="programming-language-python" options={[{"id": "programming-language-python", "title": "Python"}, {"id": "programming-language-javascript", "title": "JavaScript"}, {"id": "programming-language-java", "title": "Java"}, {"id": "programming-language-csharp", "title": "C#"}, {"id": "programming-language-rest", "title": "REST"}]} values={["programming-language-python", "programming-language-javascript", "programming-language-java", "programming-language-csharp", "programming-language-rest"]} defaultValue="programming-language-python">
  This article explains how to use embeddings API with models deployed in Microsoft Foundry Models.

  ## Prerequisites

  To use embedding models in your application, you need:

  * An Azure subscription. If you're using GitHub Models, you can upgrade your experience and create an Azure subscription in the process. Read [Upgrade from GitHub Models to Microsoft Foundry Models](../how-to/quickstart-github-models) if that's your case.

  * A Foundry project. This kind of project is managed under a Foundry resource. If you don't have a Foundry project, see [Create a project for Foundry (Foundry projects)](../../how-to/create-projects).

  * The endpoint's URL.

  * The endpoint's key (if you choose to use API key for authentication).

  - Install the [Azure AI inference package for Python](https://aka.ms/azsdk/azure-ai-inference/python/reference) with the following command:

    ```bash theme={null}
    pip install -U azure-ai-inference
    ```

  - An embeddings model deployment. If you don't have one read [Add and configure Foundry Models](../../how-to/create-model-deployments) to add an embeddings model to your resource.

  ## Use embeddings

  First, create the client to consume the model. The following code uses an endpoint URL and key that are stored in environment variables.

  ```python theme={null}
  import os
  from azure.ai.inference import EmbeddingsClient
  from azure.core.credentials import AzureKeyCredential

  model = EmbeddingsClient(
      endpoint="https://<resource>.services.ai.azure.com/models",
      credential=AzureKeyCredential(os.environ["AZURE_INFERENCE_CREDENTIAL"]),
      model="text-embedding-3-small"
  )
  ```

  If you have configured the resource to with **Microsoft Entra ID** support, you can use the following code snippet to create a client.

  ```python theme={null}
  import os
  from azure.ai.inference import EmbeddingsClient
  from azure.identity import DefaultAzureCredential

  model = EmbeddingsClient(
      endpoint="https://<resource>.services.ai.azure.com/models",
      credential=DefaultAzureCredential(),
      model="text-embedding-3-small"
  )
  ```

  ### Create embeddings

  Create an embedding request to see the output of the model.

  ```python theme={null}
  response = model.embed(
      input=["The ultimate answer to the question of life"],
  )
  ```

  <Tip>
    When creating a request, take into account the token's input limit for the model. If you need to embed larger portions of text, you would need a chunking strategy.
  </Tip>

  The response is as follows, where you can see the model's usage statistics:

  ```python theme={null}
  import numpy as np

  for embed in response.data:
      print("Embedding of size:", np.asarray(embed.embedding).shape)

  print("Model:", response.model)
  print("Usage:", response.usage)
  ```

  It can be useful to compute embeddings in input batches. The parameter `inputs` can be a list of strings, where each string is a different input. In turn the response is a list of embeddings, where each embedding corresponds to the input in the same position.

  ```python theme={null}
  response = model.embed(
      input=[
          "The ultimate answer to the question of life", 
          "The largest planet in our solar system is Jupiter",
      ],
  )
  ```

  The response is as follows, where you can see the model's usage statistics:

  ```python theme={null}
  import numpy as np

  for embed in response.data:
      print("Embedding of size:", np.asarray(embed.embedding).shape)

  print("Model:", response.model)
  print("Usage:", response.usage)
  ```

  <Tip>
    When creating batches of request, take into account the batch limit for each of the models. Most models have a 1024 batch limit.
  </Tip>

  #### Specify embeddings dimensions

  You can specify the number of dimensions for the embeddings. The following example code shows how to create embeddings with 1024 dimensions. Notice that not all the embedding models support indicating the number of dimensions in the request and on those cases a 422 error is returned.

  ```python theme={null}
  response = model.embed(
      input=["The ultimate answer to the question of life"],
      dimensions=1024,
  )
  ```

  #### Create different types of embeddings

  Some models can generate multiple embeddings for the same input depending on how you plan to use them. This capability allows you to retrieve more accurate embeddings for RAG patterns.

  The following example shows how to create embeddings that are used to create an embedding for a document that will be stored in a vector database:

  ```python theme={null}
  from azure.ai.inference.models import EmbeddingInputType

  response = model.embed(
      input=["The answer to the ultimate question of life, the universe, and everything is 42"],
      input_type=EmbeddingInputType.DOCUMENT,
  )
  ```

  When you work on a query to retrieve such a document, you can use the following code snippet to create the embeddings for the query and maximize the retrieval performance.

  ```python theme={null}
  from azure.ai.inference.models import EmbeddingInputType

  response = model.embed(
      input=["What's the ultimate meaning of life?"],
      input_type=EmbeddingInputType.QUERY,
  )
  ```

  Notice that not all the embedding models support indicating the input type in the request and on those cases a 422 error is returned. By default, embeddings of type `Text` are returned.
</ZoneContent>

<ZoneContent group="programming-language-csharp__programming-language-java__programming-language-javascript__programming-language-python__programming-language-rest" value="programming-language-javascript" options={[{"id": "programming-language-python", "title": "Python"}, {"id": "programming-language-javascript", "title": "JavaScript"}, {"id": "programming-language-java", "title": "Java"}, {"id": "programming-language-csharp", "title": "C#"}, {"id": "programming-language-rest", "title": "REST"}]} values={["programming-language-python", "programming-language-javascript", "programming-language-java", "programming-language-csharp", "programming-language-rest"]} defaultValue="programming-language-python">
  This article explains how to use embeddings API with models deployed in Microsoft Foundry Models.

  ## Prerequisites

  To use embedding models in your application, you need:

  * An Azure subscription. If you're using GitHub Models, you can upgrade your experience and create an Azure subscription in the process. Read [Upgrade from GitHub Models to Microsoft Foundry Models](../how-to/quickstart-github-models) if that's your case.

  * A Foundry project. This kind of project is managed under a Foundry resource. If you don't have a Foundry project, see [Create a project for Foundry (Foundry projects)](../../how-to/create-projects).

  * The endpoint's URL.

  * The endpoint's key (if you choose to use API key for authentication).

  - Install the [Azure Inference library for JavaScript](https://aka.ms/azsdk/azure-ai-inference/javascript/reference) with the following command:

    ```bash theme={null}
    npm install @azure-rest/ai-inference
    npm install @azure/core-auth
    npm install @azure/identity
    ```

    If you are using Node.js, you can configure the dependencies in **package.json**:

    **package.json**

    ```json theme={null}
    {
      "name": "main_app",
      "version": "1.0.0",
      "description": "",
      "main": "app.js",
      "type": "module",
      "dependencies": {
        "@azure-rest/ai-inference": "1.0.0-beta.6",
        "@azure/core-auth": "1.9.0",
        "@azure/core-sse": "2.2.0",
        "@azure/identity": "4.8.0"
      }
    }
    ```

  - Import the following:

    ```javascript theme={null}
    import ModelClient from "@azure-rest/ai-inference";
    import { isUnexpected } from "@azure-rest/ai-inference";
    import { createSseStream } from "@azure/core-sse";
    import { AzureKeyCredential } from "@azure/core-auth";
    import { DefaultAzureCredential } from "@azure/identity";
    ```

  - An embeddings model deployment. If you don't have one read [Add and configure Foundry Models](../../how-to/create-model-deployments) to add an embeddings model to your resource.

  ## Use embeddings

  First, create the client to consume the model. The following code uses an endpoint URL and key that are stored in environment variables.

  ```javascript theme={null}
  const client = ModelClient(
      "https://<resource>.services.ai.azure.com/models", 
      new AzureKeyCredential(process.env.AZURE_INFERENCE_CREDENTIAL)
  );
  ```

  If you've configured the resource with **Microsoft Entra ID** support, you can use the following code snippet to create a client.

  ```javascript theme={null}
  const clientOptions = { credentials: { "https://cognitiveservices.azure.com" } };

  const client = ModelClient(
      "https://<resource>.services.ai.azure.com/models", 
      new DefaultAzureCredential()
      clientOptions,
  );
  ```

  ### Create embeddings

  Create an embedding request to see the output of the model.

  ```javascript theme={null}
  var response = await client.path("/embeddings").post({
      body: {
          model: "text-embedding-3-small",
          input: ["The ultimate answer to the question of life"],
      }
  });
  ```

  <Tip>
    When creating a request, take into account the token's input limit for the model. If you need to embed larger portions of text, you would need a chunking strategy.
  </Tip>

  The response is as follows, where you can see the model's usage statistics:

  ```javascript theme={null}
  if (isUnexpected(response)) {
      throw response.body.error;
  }

  console.log(response.embedding);
  console.log(response.body.model);
  console.log(response.body.usage);
  ```

  It can be useful to compute embeddings in input batches. The parameter `inputs` can be a list of strings, where each string is a different input. In turn the response is a list of embeddings, where each embedding corresponds to the input in the same position.

  ```javascript theme={null}
  var response = await client.path("/embeddings").post({
      body: {
          model: "text-embedding-3-small",
          input: [
              "The ultimate answer to the question of life", 
              "The largest planet in our solar system is Jupiter",
          ],
      }
  });
  ```

  The response is as follows, where you can see the model's usage statistics:

  ```javascript theme={null}
  if (isUnexpected(response)) {
      throw response.body.error;
  }

  console.log(response.embedding);
  console.log(response.body.model);
  console.log(response.body.usage);
  ```

  <Tip>
    When creating batches of request, take into account the batch limit for each of the models. Most models have a 1024 batch limit.
  </Tip>

  #### Specify embeddings dimensions

  You can specify the number of dimensions for the embeddings. The following example code shows how to create embeddings with 1024 dimensions. Notice that not all the embedding models support indicating the number of dimensions in the request and on those cases a 422 error is returned.

  ```javascript theme={null}
  var response = await client.path("/embeddings").post({
      body: {
          model: "text-embedding-3-small",
          input: ["The ultimate answer to the question of life"],
          dimensions: 1024,
      }
  });
  ```

  #### Create different types of embeddings

  Some models can generate multiple embeddings for the same input depending on how you plan to use them. This capability allows you to retrieve more accurate embeddings for RAG patterns.

  The following example shows how to create embeddings that are used to create an embedding for a document that will be stored in a vector database:

  ```javascript theme={null}
  var response = await client.path("/embeddings").post({
      body: {
          model: "text-embedding-3-small",
          input: ["The answer to the ultimate question of life, the universe, and everything is 42"],
          input_type: "document",
      }
  });
  ```

  When you work on a query to retrieve such a document, you can use the following code snippet to create the embeddings for the query and maximize the retrieval performance.

  ```javascript theme={null}
  var response = await client.path("/embeddings").post({
      body: {
          model: "text-embedding-3-small",
          input: ["What's the ultimate meaning of life?"],
          input_type: "query",
      }
  });
  ```

  Notice that not all the embedding models support indicating the input type in the request and on those cases a 422 error is returned. By default, embeddings of type `Text` are returned.
</ZoneContent>

<ZoneContent group="programming-language-csharp__programming-language-java__programming-language-javascript__programming-language-python__programming-language-rest" value="programming-language-java" options={[{"id": "programming-language-python", "title": "Python"}, {"id": "programming-language-javascript", "title": "JavaScript"}, {"id": "programming-language-java", "title": "Java"}, {"id": "programming-language-csharp", "title": "C#"}, {"id": "programming-language-rest", "title": "REST"}]} values={["programming-language-python", "programming-language-javascript", "programming-language-java", "programming-language-csharp", "programming-language-rest"]} defaultValue="programming-language-python">
  This article explains how to use embeddings API with models deployed in Microsoft Foundry Models.

  ## Prerequisites

  To use embedding models in your application, you need:

  * An Azure subscription. If you're using GitHub Models, you can upgrade your experience and create an Azure subscription in the process. Read [Upgrade from GitHub Models to Microsoft Foundry Models](../how-to/quickstart-github-models) if that's your case.

  * A Foundry project. This kind of project is managed under a Foundry resource. If you don't have a Foundry project, see [Create a project for Foundry (Foundry projects)](../../how-to/create-projects).

  * The endpoint's URL.

  * The endpoint's key (if you choose to use API key for authentication).

  - Add the [Azure AI inference package](https://aka.ms/azsdk/azure-ai-inference/java/reference) to your project:

    ```xml theme={null}
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-ai-inference</artifactId>
        <version>1.0.0-beta.4</version>
    </dependency>
    ```

  - If you are using Entra ID, you also need the following package:

    ```xml theme={null}
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-identity</artifactId>
        <version>1.15.3</version>
    </dependency>
    ```

  - Import the following namespace:

    ```java theme={null}
    package com.azure.ai.inference.usage;

    import com.azure.ai.inference.EmbeddingsClient;
    import com.azure.ai.inference.EmbeddingsClientBuilder;
    import com.azure.ai.inference.ChatCompletionsClient;
    import com.azure.ai.inference.ChatCompletionsClientBuilder;
    import com.azure.ai.inference.models.EmbeddingsResult;
    import com.azure.ai.inference.models.EmbeddingItem;
    import com.azure.ai.inference.models.ChatCompletions;
    import com.azure.core.credential.AzureKeyCredential;
    import com.azure.core.util.Configuration;

    import java.util.ArrayList;
    import java.util.List;
    ```

  - Import the following namespace:

    ```java theme={null}
    package com.azure.ai.inference.usage;

    import com.azure.ai.inference.EmbeddingsClient;
    import com.azure.ai.inference.EmbeddingsClientBuilder;
    import com.azure.ai.inference.models.EmbeddingsResult;
    import com.azure.ai.inference.models.EmbeddingItem;
    import com.azure.core.credential.AzureKeyCredential;
    import com.azure.core.util.Configuration;

    import java.util.ArrayList;
    import java.util.List;
    ```

  - An embeddings model deployment. If you don't have one read [Add and configure Foundry Models](../../how-to/create-model-deployments) to add an embeddings model to your resource.

  ## Use embeddings

  First, create the client to consume the model. The following code uses an endpoint URL and key that are stored in environment variables.

  ```java theme={null}
  EmbeddingsClient client = new EmbeddingsClient(
      URI.create(System.getProperty("AZURE_INFERENCE_ENDPOINT")),
      new AzureKeyCredential(System.getProperty("AZURE_INFERENCE_CREDENTIAL")),
      "text-embedding-3-small"
  );
  ```

  If you have configured the resource to with **Microsoft Entra ID** support, you can use the following code snippet to create a client.

  ```java theme={null}
  client = new EmbeddingsClient(
      URI.create(System.getProperty("AZURE_INFERENCE_ENDPOINT")),
      new DefaultAzureCredential(),
      "text-embedding-3-small"
  );
  ```

  ### Create embeddings

  Create an embedding request to see the output of the model.

  ```java theme={null}
  EmbeddingsOptions requestOptions = new EmbeddingsOptions()
      .setInput(Arrays.asList("The ultimate answer to the question of life"));

  Response<EmbeddingsResult> response = client.embed(requestOptions);
  ```

  <Tip>
    When creating a request, take into account the token's input limit for the model. If you need to embed larger portions of text, you would need a chunking strategy.
  </Tip>

  The response is as follows, where you can see the model's usage statistics:

  ```java theme={null}
  System.out.println("Embedding: " + response.getValue().getData());
  System.out.println("Model: " + response.getValue().getModel());
  System.out.println("Usage:");
  System.out.println("\tPrompt tokens: " + response.getValue().getUsage().getPromptTokens());
  System.out.println("\tTotal tokens: " + response.getValue().getUsage().getTotalTokens());
  ```

  It can be useful to compute embeddings in input batches. The parameter `inputs` can be a list of strings, where each string is a different input. In turn the response is a list of embeddings, where each embedding corresponds to the input in the same position.

  ```java theme={null}
  requestOptions = new EmbeddingsOptions()
      .setInput(Arrays.asList(
          "The ultimate answer to the question of life", 
          "The largest planet in our solar system is Jupiter"
      ));

  response = client.embed(requestOptions);
  ```

  The response is as follows, where you can see the model's usage statistics:

  <Tip>
    When creating batches of request, take into account the batch limit for each of the models. Most models have a 1024 batch limit.
  </Tip>

  #### Specify embeddings dimensions

  You can specify the number of dimensions for the embeddings. The following example code shows how to create embeddings with 1024 dimensions. Notice that not all the embedding models support indicating the number of dimensions in the request and on those cases a 422 error is returned.

  #### Create different types of embeddings

  Some models can generate multiple embeddings for the same input depending on how you plan to use them. This capability allows you to retrieve more accurate embeddings for RAG patterns.

  The following example shows how to create embeddings that are used to create an embedding for a document that will be stored in a vector database:

  ```java theme={null}
  List<String> input = Arrays.asList("The answer to the ultimate question of life, the universe, and everything is 42");
  requestOptions = new EmbeddingsOptions(input, EmbeddingInputType.DOCUMENT);

  response = client.embed(requestOptions);
  ```

  When you work on a query to retrieve such a document, you can use the following code snippet to create the embeddings for the query and maximize the retrieval performance.

  ```java theme={null}
  input = Arrays.asList("What's the ultimate meaning of life?");
  requestOptions = new EmbeddingsOptions(input, EmbeddingInputType.QUERY);

  response = client.embed(requestOptions);
  ```

  Notice that not all the embedding models support indicating the input type in the request and on those cases a 422 error is returned. By default, embeddings of type `Text` are returned.
</ZoneContent>

<ZoneContent group="programming-language-csharp__programming-language-java__programming-language-javascript__programming-language-python__programming-language-rest" value="programming-language-csharp" options={[{"id": "programming-language-python", "title": "Python"}, {"id": "programming-language-javascript", "title": "JavaScript"}, {"id": "programming-language-java", "title": "Java"}, {"id": "programming-language-csharp", "title": "C#"}, {"id": "programming-language-rest", "title": "REST"}]} values={["programming-language-python", "programming-language-javascript", "programming-language-java", "programming-language-csharp", "programming-language-rest"]} defaultValue="programming-language-python">
  This article explains how to use embeddings API with models deployed in Microsoft Foundry Models.

  ## Prerequisites

  To use embedding models in your application, you need:

  * An Azure subscription. If you're using GitHub Models, you can upgrade your experience and create an Azure subscription in the process. Read [Upgrade from GitHub Models to Microsoft Foundry Models](../how-to/quickstart-github-models) if that's your case.

  * A Foundry project. This kind of project is managed under a Foundry resource. If you don't have a Foundry project, see [Create a project for Foundry (Foundry projects)](../../how-to/create-projects).

  * The endpoint's URL.

  * The endpoint's key (if you choose to use API key for authentication).

  - Install the [Azure AI inference package](https://aka.ms/azsdk/azure-ai-inference/python/reference) with the following command:

    ```bash theme={null}
    dotnet add package Azure.AI.Inference --prerelease
    ```

  - If you are using Entra ID, you also need the following package:

    ```bash theme={null}
    dotnet add package Azure.Identity
    ```

  - An embeddings model deployment. If you don't have one read [Add and configure Foundry Models](../../how-to/create-model-deployments) to add an embeddings model to your resource.

  ## Use embeddings

  First, create the client to consume the model. The following code uses an endpoint URL and key that are stored in environment variables.

  ```csharp theme={null}
  EmbeddingsClient client = new EmbeddingsClient(
      new Uri(Environment.GetEnvironmentVariable("AZURE_INFERENCE_ENDPOINT")),
      new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_INFERENCE_CREDENTIAL"))
  );
  ```

  If you configured the resource to with **Microsoft Entra ID** support, you can use the following code snippet to create a client. Note that here `includeInteractiveCredentials` is set to `true` only for demonstration purposes so authentication can happen using the web browser. On production workloads, you should remove such parameter.

  ```csharp theme={null}
  TokenCredential credential = new DefaultAzureCredential(includeInteractiveCredentials: true);
  AzureAIInferenceClientOptions clientOptions = new AzureAIInferenceClientOptions();
  BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(credential, new string[] { "https://cognitiveservices.azure.com/.default" });

  clientOptions.AddPolicy(tokenPolicy, HttpPipelinePosition.PerRetry);

  client = new EmbeddingsClient(
      new Uri("https://<resource>.services.ai.azure.com/models"),
      credential,
      clientOptions,
  );
  ```

  ### Create embeddings

  Create an embedding request to see the output of the model.

  ```csharp theme={null}
  EmbeddingsOptions requestOptions = new EmbeddingsOptions()
  {
      Input = {
          "The ultimate answer to the question of life"
      },
      Model = "text-embedding-3-small"
  };

  Response<EmbeddingsResult> response = client.Embed(requestOptions);
  ```

  <Tip>
    When creating a request, take into account the token's input limit for the model. If you need to embed larger portions of text, you would need a chunking strategy.
  </Tip>

  The response is as follows, where you can see the model's usage statistics:

  ```csharp theme={null}
  Console.WriteLine($"Embedding: {response.Value.Data}");
  Console.WriteLine($"Model: {response.Value.Model}");
  Console.WriteLine("Usage:");
  Console.WriteLine($"\tPrompt tokens: {response.Value.Usage.PromptTokens}");
  Console.WriteLine($"\tTotal tokens: {response.Value.Usage.TotalTokens}");
  ```

  It can be useful to compute embeddings in input batches. The parameter `inputs` can be a list of strings, where each string is a different input. In turn the response is a list of embeddings, where each embedding corresponds to the input in the same position.

  ```csharp theme={null}
  EmbeddingsOptions requestOptions = new EmbeddingsOptions()
  {
      Input = {
          "The ultimate answer to the question of life", 
          "The largest planet in our solar system is Jupiter"
      },
      Model = "text-embedding-3-small"
  };

  Response<EmbeddingsResult> response = client.Embed(requestOptions);
  ```

  The response is as follows, where you can see the model's usage statistics:

  <Tip>
    When creating batches of request, take into account the batch limit for each of the models. Most models have a 1024 batch limit.
  </Tip>

  #### Specify embeddings dimensions

  You can specify the number of dimensions for the embeddings. The following example code shows how to create embeddings with 1024 dimensions. Notice that not all the embedding models support indicating the number of dimensions in the request and on those cases a 422 error is returned.

  #### Create different types of embeddings

  Some models can generate multiple embeddings for the same input depending on how you plan to use them. This capability allows you to retrieve more accurate embeddings for RAG patterns.

  The following example shows how to create embeddings that are used to create an embedding for a document that will be stored in a vector database:

  ```csharp theme={null}
  var input = new List<string> { 
      "The answer to the ultimate question of life, the universe, and everything is 42"
  };
  var requestOptions = new EmbeddingsOptions()
  {
      Input = input,
      InputType = EmbeddingInputType.DOCUMENT, 
      Model = "text-embedding-3-small"
  };

  Response<EmbeddingsResult> response = client.Embed(requestOptions);
  ```

  When you work on a query to retrieve such a document, you can use the following code snippet to create the embeddings for the query and maximize the retrieval performance.

  ```csharp theme={null}
  var input = new List<string> { 
      "What's the ultimate meaning of life?"
  };
  var requestOptions = new EmbeddingsOptions()
  {
      Input = input,
      InputType = EmbeddingInputType.QUERY,
      Model = "text-embedding-3-small"
  };

  Response<EmbeddingsResult> response = client.Embed(requestOptions);
  ```

  Notice that not all the embedding models support indicating the input type in the request and on those cases a 422 error is returned. By default, embeddings of type `Text` are returned.
</ZoneContent>

<ZoneContent group="programming-language-csharp__programming-language-java__programming-language-javascript__programming-language-python__programming-language-rest" value="programming-language-rest" options={[{"id": "programming-language-python", "title": "Python"}, {"id": "programming-language-javascript", "title": "JavaScript"}, {"id": "programming-language-java", "title": "Java"}, {"id": "programming-language-csharp", "title": "C#"}, {"id": "programming-language-rest", "title": "REST"}]} values={["programming-language-python", "programming-language-javascript", "programming-language-java", "programming-language-csharp", "programming-language-rest"]} defaultValue="programming-language-python">
  This article explains how to use embeddings API with models deployed in Microsoft Foundry Models.

  ## Prerequisites

  To use embedding models in your application, you need:

  * An Azure subscription. If you're using GitHub Models, you can upgrade your experience and create an Azure subscription in the process. Read [Upgrade from GitHub Models to Microsoft Foundry Models](../how-to/quickstart-github-models) if that's your case.

  * A Foundry project. This kind of project is managed under a Foundry resource. If you don't have a Foundry project, see [Create a project for Foundry (Foundry projects)](../../how-to/create-projects).

  * The endpoint's URL.

  * The endpoint's key (if you choose to use API key for authentication).

  - An embeddings model deployment. If you don't have one read [Add and configure Foundry Models](../../how-to/create-model-deployments) to add an embeddings model to your resource.

  ## Use embeddings

  To use the text embeddings, use the route `/embeddings` appended to the base URL along with your credential indicated in `api-key`. `Authorization` header is also supported with the format `Bearer <key>`.

  ```http theme={null}
  POST https://<resource>.services.ai.azure.com/models/embeddings?api-version=2024-05-01-preview
  Content-Type: application/json
  api-key: <key>
  ```

  If you have configured the resource with **Microsoft Entra ID** support, pass you token in the `Authorization` header with the format `Bearer <token>`. Use scope `https://ai.azure.com/.default`.

  ```http theme={null}
  POST https://<resource>.services.ai.azure.com/models/embeddings?api-version=2024-05-01-preview
  Content-Type: application/json
  Authorization: Bearer <token>
  ```

  Using Microsoft Entra ID may require additional configuration in your resource to grant access. Learn how to [configure key-less authentication with Microsoft Entra ID](../../how-to/configure-entra-id).

  ### Create embeddings

  Create an embedding request to see the output of the model.

  ```json theme={null}
  {
      "model": "text-embedding-3-small",
      "input": [
          "The ultimate answer to the question of life"
      ]
  }
  ```

  <Tip>
    When creating a request, take into account the token's input limit for the model. If you need to embed larger portions of text, you would need a chunking strategy.
  </Tip>

  The response is as follows, where you can see the model's usage statistics:

  ```json theme={null}
  {
      "id": "0ab1234c-d5e6-7fgh-i890-j1234k123456",
      "object": "list",
      "data": [
          {
              "index": 0,
              "object": "embedding",
              "embedding": [
                  0.017196655,
                  // ...
                  -0.000687122,
                  -0.025054932,
                  -0.015777588
              ]
          }
      ],
      "model": "text-embedding-3-small",
      "usage": {
          "prompt_tokens": 9,
          "completion_tokens": 0,
          "total_tokens": 9
      }
  }
  ```

  It can be useful to compute embeddings in input batches. The parameter `inputs` can be a list of strings, where each string is a different input. In turn the response is a list of embeddings, where each embedding corresponds to the input in the same position.

  ```json theme={null}
  {
      "model": "text-embedding-3-small",
      "input": [
          "The ultimate answer to the question of life", 
          "The largest planet in our solar system is Jupiter"
      ]
  }
  ```

  The response is as follows, where you can see the model's usage statistics:

  ```json theme={null}
  {
      "id": "0ab1234c-d5e6-7fgh-i890-j1234k123456",
      "object": "list",
      "data": [
          {
              "index": 0,
              "object": "embedding",
              "embedding": [
                  0.017196655,
                  // ...
                  -0.000687122,
                  -0.025054932,
                  -0.015777588
              ]
          },
          {
              "index": 1,
              "object": "embedding",
              "embedding": [
                  0.017196655,
                  // ...
                  -0.000687122,
                  -0.025054932,
                  -0.015777588
              ]
          }
      ],
      "model": "text-embedding-3-small",
      "usage": {
          "prompt_tokens": 19,
          "completion_tokens": 0,
          "total_tokens": 19
      }
  }
  ```

  <Tip>
    When creating batches of request, take into account the batch limit for each of the models. Most models have a 1024 batch limit.
  </Tip>

  #### Specify embeddings dimensions

  You can specify the number of dimensions for the embeddings. The following example code shows how to create embeddings with 1024 dimensions. Notice that not all the embedding models support indicating the number of dimensions in the request and on those cases a 422 error is returned.

  ```json theme={null}
  {
      "model": "text-embedding-3-small",
      "input": [
          "The ultimate answer to the question of life"
      ],
      "dimensions": 1024
  }
  ```

  #### Create different types of embeddings

  Some models can generate multiple embeddings for the same input depending on how you plan to use them. This capability allows you to retrieve more accurate embeddings for RAG patterns.

  The following example shows how to create embeddings that are used to create an embedding for a document that will be stored in a vector database. Since `text-embedding-3-small` doesn't support this capability, we are using an embedding model from Cohere in the following example:

  ```json theme={null}
  {
      "model": "cohere-embed-v3-english",
      "input": [
          "The answer to the ultimate question of life, the universe, and everything is 42"
      ],
      "input_type": "document"
  }
  ```

  When you work on a query to retrieve such a document, you can use the following code snippet to create the embeddings for the query and maximize the retrieval performance. Since `text-embedding-3-small` doesn't support this capability, we are using an embedding model from Cohere in the following example:

  ```json theme={null}
  {
      "model": "cohere-embed-v3-english",
      "input": [
          "What's the ultimate meaning of life?"
      ],
      "input_type": "query"
  }
  ```

  Notice that not all the embedding models support indicating the input type in the request and on those cases a 422 error is returned. By default, embeddings of type `Text` are returned.
</ZoneContent>

## Related content

* [Use image embeddings models](./use-image-embeddings)
* [Azure AI Model Inference API](https://learn.microsoft.com/rest/api/microsoft-foundry/modelinference)
