Skip to main content
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Udo_S
New Contributor III

How to use an agent from Azure AI Foundry in a Fabric notebook?

Dear all,
We want to use an agent that we created in Azure AI Foundry in a Python notebook in Fabric. Unfortunately, we have no idea how that works fundamentally and how the authentication works in particular.
I am grateful for any help.
Best regards,
Udo

3 ACCEPTED SOLUTIONS
Udo_S
New Contributor III

Hello @v-nmadadi-msft and @v-nmadadi-msft ,

The permission issue has been resolved, but I still can't fully understand the root of the problem. Perhaps a bit more context from my side will help:

  • I have a Fabric workspace with a Python notebook.
  • I also have a subscription in the Azure Portal, and the Fabric workspace has been assigned the Contributor role on this subscription.
  • Within this subscription, I created a project in Azure AI Foundry, which includes a project endpoint.
  • Finally, I created an agent within this project. This agent has an Agent ID, but I can't find any additional credentialsโ€”such as a REST API key or any other form of authentication.

I tried running the following code in my notebook, hoping that authentication would happen automatically since the Fabric workspace has contributor permissions on my Azure subscription:

 

project = AIProjectClient(
    credential=DefaultAzureCredential(),
    endpoint="PROJECT_ENDPOINT")

agent = project.agents.get_agent("AGENT_ID")

 But this doesn't work.

I must admit I'm not a pro when it comes to Azure, therefore I would be very grateful if someone could give me a specific hint about what I need to do โ€” for example, where to find the credentials for the agent's REST API (if that is the correct approach). 

Many thanks in advance!

Best regards,
Udo

 

 

View solution in original post

Udo_S
New Contributor III

Hi @v-nmadadi-msft ,


Thank you for the hint.


I was unsure where the error could actually be located. As it turned out yesterday, the error is actually in our Fabric environment.


For historical reasons, our Fabric workspaces are based on a Power BI Premium SKU (P1). This has some limitations compared to the newer Fabric SKU (e.g., F64). Specifically, it lacks exactly those features that are required to use Azure AI Foundry agents from a Python notebook in Fabric.


The medium-term solution is to switch from a Power BI SKU to a Fabric SKU. Technically, this is not complicated, but in our case it is problematic for commercial reasons.

Many thanks to everyone for your support!


Best regards,
Udo

View solution in original post

Udo_S
New Contributor III

Hello everyone,

 

We have now found a solution to our problem, which I would like to briefly document here. Perhaps it will help someone else who is struggling with a similar issue as we were.

 

First, let me briefly restate the challenge (which can serve as an example for other similar use cases):

We created an Agent in Azure AI Foundry. We want to use this Agent in a Python Notebook in Fabric (to clarify: this is not about a "Fabric Agent," which is not helpful for our purposes).

 

The agent uses an LLM (in our case, gpt-4o) and a tool for web search (in our case, Bing Grounding Search).

 

The agent should perform a web search based on product data from a Lakehouse table (e.g., an EAN number) and enrich the product data in the Lakehouse with the results from the web search.

 

Our main problem was authenticating the agent in Fabric.

 

Here is the solution:

The magic word is "Role-Based Access Control" (RBAC).

 

Requirements:

  1. An Azure subscription.
  2. A project in Azure AI Foundry ( https://learn.microsoft.com/en-us/azure/ai-foundry/how-to-create-projects?tabs=ai-foundry&pivots=fdp...)
  3. An Agent in Azure AI Foundry ( https://learn.microsoft.com/en-us/azure/ai-foundry/agents/quickstart?pivots=ai-foundry-portal)
  4. An Azure Service Principal at the tenant level (setup is fairly simple, but can only be done by someone with the Cloud Application Administrator role).
  5. An Azure Key Vault.

 

Once the prerequisites are met, the following initial steps must be performed:

  1. Open the project in the Azure Portal and assign the Service Principal the role of "Azure AI User" via "Access Control (IAM)" (this is the crucial step in "Role-Based Access Control").
  2. In the Azure Portal, create a Client Secret for the Service Principal and store the secret in the Azure Key Vault.
  3. The following data should also be stored in the Azure Key Vault:
  • The Tenant ID.
  • The Tlient ID of the Service Principal.
  1. In addition, you will need the project endpoint and the ID of the Foundry Agent.

 

The Foundry Agent can now be used in a Python Notebook in Fabric. Below is a code example.

Maybe this helps someone. Feel free to reach out if you have any questions about the procedure described above.

 

Best regards,
Udo

from azure.ai.projects import AIProjectClient
from azure.identity import ClientSecretCredential
from azure.ai.agents.models import ListSortOrder

# Retrieve secrets from Azure Key Vault using mssparkutils
TENANT_ID = mssparkutils.credentials.getSecret('YOUR-KEY-VAULT-URL','TENANT_ID')
CLIENT_ID = mssparkutils.credentials.getSecret('YOUR-KEY-VAULT-URL','CLIENT_ID')
CLIENT_SECRET = mssparkutils.credentials.getSecret('YOUR-KEY-VAULT-URL','CLIENT_SECRET')
PROJECT_ENDPOINT = 'YOUR-PROJECT-ENDPOINT"

# Define credentials for authentication
credential = ClientSecretCredential(
    tenant_id=TENANT_ID,
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET
)

# Initialize the AI project client with the credentials and endpoint
project = AIProjectClient(credential=credential, endpoint=PROJECT_ENDPOINT)

# Retrieve the specific agent by its ID
agent = project.agents.get_agent('YOUR-AGENT-ID')

# Create a new thread for the conversation
thread = project.agents.threads.create()
print(f"Created thread, ID: {thread.id}")

# Send a message to the agent with the specific query
message = project.agents.messages.create(
    thread_id=thread.id,
    role="user",
    content="Hello Agent"
)
# Create and process a run for the agent to handle the message
run = project.agents.runs.create_and_process(
    thread_id=thread.id,
    agent_id=agent.id
)
# Check the status of the run and handle accordingly
if run.status == "failed":
    print(f"Run failed: {run.last_error}")
else:
    # Retrieve and print all messages in the thread in ascending order
    messages = project.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
    for message in messages:
        if message.text_messages:
            print(f"{message.role}: {message.text_messages[-1].text.value}")




View solution in original post

9 REPLIES 9
Vinodh247
Contributor II

To use an agent from azure AI foundry in a Fabric notebook you need to interact with the agentโ€™s REST API endpoint, which becomes available once the agent is deployed in azure AI studio. The process fundamentally involves sending an HTTP request from the notebook to the agentโ€™s endpoint with appropriate authentication. You can authenticate in one of two main ways: using an AAD access token or with an API key, depending on how the agent is configured in Azure AI Studio. In Fabric notebooks, authentication using AAD typically involves leveraging the Azure Identity library to generate a token for the cognitive services scope. If your agent is set up with API key authentication, you will need to include the key in the request header. It is important to ensure that your Fabric workspace has been properly configured to access external endpoints if using private networking. Once authentication is set up correctly, your notebook can send messages to the agent and receive intelligent responses as part of your workflow.

 


Please 'Kudos' and 'Accept as Solution' if this answered your query.

Please 'Kudos' and 'Accept as Solution' if this answered your query.

Regards,
Vinodh
Microsoft MVP [Fabric]
Udo_S
New Contributor III

Hi @Vinodh247 and @v-nmadadi-msft ,
Thanks for your help!

As it turns out, we are missing permissions for our Azure subscriptions. Once that is corrected, we can conduct further tests.

Best regards,
Udo

v-nmadadi-msft
Honored Contributor II

Hi @Udo_S  ,
Thanks for reaching out to the Microsoft fabric community forum.

To get started on how to use Azure AI foundry with Fabric Notebooks, You can refer these articles for more information for the points mentioned by @Vinodh247  who talks about using the REST API endpoint.
Azure AI Projects client library for Python | Microsoft Learn

Quickstart - Create a new Azure AI Foundry Agent Service project - Azure AI Foundry | Microsoft Lear...

Azure AI Projects client library for Python | Microsoft Learn

Other than that approach, to ease things instead of Using Azure AI foundry there is also a feature called Fabric data agent where you can create conversational AI experiences that answer questions about data stored in lakehouses, warehouses, Power BI semantic models, and KQL databases in Fabric itself
Please check this article to know more about it and learn how to create it:
Create a Fabric data agent (preview) - Microsoft Fabric | Microsoft Learn

You can then use Fabric data agent SDK to connect to the agent using your Fabric Notebook, the SDK is designed for code-first users, and it simplifies the creation, management, and utilization of Fabric data agents within Microsoft Fabric notebooks.
Fabric data agent Python SDK (preview) - Microsoft Fabric | Microsoft Learn

If the Azure AI Foundry approach feels too complex, consider using the Fabric Data Agent as a simpler alternative to achieve your requirements.

I hope this information helps. Please do let us know if you have any further queries.
Thank you

Udo_S
New Contributor III

Hello @v-nmadadi-msft and @v-nmadadi-msft ,

The permission issue has been resolved, but I still can't fully understand the root of the problem. Perhaps a bit more context from my side will help:

  • I have a Fabric workspace with a Python notebook.
  • I also have a subscription in the Azure Portal, and the Fabric workspace has been assigned the Contributor role on this subscription.
  • Within this subscription, I created a project in Azure AI Foundry, which includes a project endpoint.
  • Finally, I created an agent within this project. This agent has an Agent ID, but I can't find any additional credentialsโ€”such as a REST API key or any other form of authentication.

I tried running the following code in my notebook, hoping that authentication would happen automatically since the Fabric workspace has contributor permissions on my Azure subscription:

 

project = AIProjectClient(
    credential=DefaultAzureCredential(),
    endpoint="PROJECT_ENDPOINT")

agent = project.agents.get_agent("AGENT_ID")

 But this doesn't work.

I must admit I'm not a pro when it comes to Azure, therefore I would be very grateful if someone could give me a specific hint about what I need to do โ€” for example, where to find the credentials for the agent's REST API (if that is the correct approach). 

Many thanks in advance!

Best regards,
Udo

 

 

v-nmadadi-msft
Honored Contributor II

Hi @Udo_S ,
In this Forum you will find people who are good at Power BI and Fabric, as the root cause of the issue is related to Azure AI Foundry, for issues related to it, please raise a question in Azure Community  Azure Community Support | Microsoft Azure to get the best possible help.
Azure community members who are good with Azure AI Foundry will take a look and will provide suggestions.
In the meantime for any other issues related to Fabric please donโ€™t hesitate to raise a post in this forum.

Thank you

Udo_S
New Contributor III

Hi @v-nmadadi-msft ,


Thank you for the hint.


I was unsure where the error could actually be located. As it turned out yesterday, the error is actually in our Fabric environment.


For historical reasons, our Fabric workspaces are based on a Power BI Premium SKU (P1). This has some limitations compared to the newer Fabric SKU (e.g., F64). Specifically, it lacks exactly those features that are required to use Azure AI Foundry agents from a Python notebook in Fabric.


The medium-term solution is to switch from a Power BI SKU to a Fabric SKU. Technically, this is not complicated, but in our case it is problematic for commercial reasons.

Many thanks to everyone for your support!


Best regards,
Udo

Udo_S
New Contributor III

Hello everyone,

 

We have now found a solution to our problem, which I would like to briefly document here. Perhaps it will help someone else who is struggling with a similar issue as we were.

 

First, let me briefly restate the challenge (which can serve as an example for other similar use cases):

We created an Agent in Azure AI Foundry. We want to use this Agent in a Python Notebook in Fabric (to clarify: this is not about a "Fabric Agent," which is not helpful for our purposes).

 

The agent uses an LLM (in our case, gpt-4o) and a tool for web search (in our case, Bing Grounding Search).

 

The agent should perform a web search based on product data from a Lakehouse table (e.g., an EAN number) and enrich the product data in the Lakehouse with the results from the web search.

 

Our main problem was authenticating the agent in Fabric.

 

Here is the solution:

The magic word is "Role-Based Access Control" (RBAC).

 

Requirements:

  1. An Azure subscription.
  2. A project in Azure AI Foundry ( https://learn.microsoft.com/en-us/azure/ai-foundry/how-to-create-projects?tabs=ai-foundry&pivots=fdp...)
  3. An Agent in Azure AI Foundry ( https://learn.microsoft.com/en-us/azure/ai-foundry/agents/quickstart?pivots=ai-foundry-portal)
  4. An Azure Service Principal at the tenant level (setup is fairly simple, but can only be done by someone with the Cloud Application Administrator role).
  5. An Azure Key Vault.

 

Once the prerequisites are met, the following initial steps must be performed:

  1. Open the project in the Azure Portal and assign the Service Principal the role of "Azure AI User" via "Access Control (IAM)" (this is the crucial step in "Role-Based Access Control").
  2. In the Azure Portal, create a Client Secret for the Service Principal and store the secret in the Azure Key Vault.
  3. The following data should also be stored in the Azure Key Vault:
  • The Tenant ID.
  • The Tlient ID of the Service Principal.
  1. In addition, you will need the project endpoint and the ID of the Foundry Agent.

 

The Foundry Agent can now be used in a Python Notebook in Fabric. Below is a code example.

Maybe this helps someone. Feel free to reach out if you have any questions about the procedure described above.

 

Best regards,
Udo

from azure.ai.projects import AIProjectClient
from azure.identity import ClientSecretCredential
from azure.ai.agents.models import ListSortOrder

# Retrieve secrets from Azure Key Vault using mssparkutils
TENANT_ID = mssparkutils.credentials.getSecret('YOUR-KEY-VAULT-URL','TENANT_ID')
CLIENT_ID = mssparkutils.credentials.getSecret('YOUR-KEY-VAULT-URL','CLIENT_ID')
CLIENT_SECRET = mssparkutils.credentials.getSecret('YOUR-KEY-VAULT-URL','CLIENT_SECRET')
PROJECT_ENDPOINT = 'YOUR-PROJECT-ENDPOINT"

# Define credentials for authentication
credential = ClientSecretCredential(
    tenant_id=TENANT_ID,
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET
)

# Initialize the AI project client with the credentials and endpoint
project = AIProjectClient(credential=credential, endpoint=PROJECT_ENDPOINT)

# Retrieve the specific agent by its ID
agent = project.agents.get_agent('YOUR-AGENT-ID')

# Create a new thread for the conversation
thread = project.agents.threads.create()
print(f"Created thread, ID: {thread.id}")

# Send a message to the agent with the specific query
message = project.agents.messages.create(
    thread_id=thread.id,
    role="user",
    content="Hello Agent"
)
# Create and process a run for the agent to handle the message
run = project.agents.runs.create_and_process(
    thread_id=thread.id,
    agent_id=agent.id
)
# Check the status of the run and handle accordingly
if run.status == "failed":
    print(f"Run failed: {run.last_error}")
else:
    # Retrieve and print all messages in the thread in ascending order
    messages = project.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
    for message in messages:
        if message.text_messages:
            print(f"{message.role}: {message.text_messages[-1].text.value}")




Vinodh247
Contributor II

Great! Pls drop in here after your tests to see how it goes.

Please 'Kudos' and 'Accept as Solution' if this answered your query.

Regards,
Vinodh
Microsoft MVP [Fabric]
Udo_S
New Contributor III

Hi @Vinodh247 ,
Unfortunately, the tests didn't go well. I'm still totally stuck when it comes to authenticating my agent in Fabric.

I tried assigning a contributor role to my Fabric workspace in the Azure portal and I tried CLI authentication. Both didn't work. I don't see any API endpoint for my agent (jut an ID).
AI (Perplexity and ChatGPT) suggest using using environment variables as an alternative, but this only applies to apps and not to agents. 

 

Unfortunately, by now I am quite desperate. I left a reply below with some context, and I hope that someone might come up with a step-by-step guide for my specific problem.

Helpful resources

Announcements
Users online (25)