Smolagents with Azure AI Foundry OpenAI model and DefaultAzureCredential or ManagedIdentity

Hi there,

currently I use smolagents with AzureOpenAIServerModel() and an API key. Now I have to switch to Active Directory authentication with DefaultAzureCredential or ManagedIdentityCredential, but with smolagent’s AzureOpenAIServerModel or OpenAIServerModel it is not working. Any idea on that? I would like to keep smolagents as framework for my agents.

model = AzureOpenAIServerModel(
    model_id = AZURE_OPENAI_MODEL,
    azure_endpoint = AZURE_OPENAI_ENDPOINT,
    api_key = AZURE_OPENAI_API_KEY,
    api_version = OPENAI_API_VERSION    
)

Thanks and BR,
Ingo

1 Like

There seem to be multiple possible causes.

Hi, I found out, how it works: forward the needed parameter as client_kwargs:

from azure.identity import DefaultAzureCredential, get_bearer_token_provider
...

client_kwargs = {}
if auth_mode == "aad":                
  scope = os.getenv("AZURE_OPENAI_SCOPE", "https://cognitiveservices.azure.com/.default")
  credential = DefaultAzureCredential()
  client_kwargs["azure_ad_token_provider"] = get_bearer_token_provider(credential, scope)
else: 
  # default back to API key authentication
  api_key = os.getenv("AZURE_OPENAI_API_KEY")

Best regards,

Ingo

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.