semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

Can I use token from azure credential instead of api key to authenticate azure openai service backend

Open LittleLittleCloud opened this issue 1 year ago • 5 comments

LittleLittleCloud avatar Apr 01 '23 06:04 LittleLittleCloud

hi @LittleLittleCloud, the Azure OpenAI service client currently supports only API Key authentication. I believe you're looking for the Azure Active Directory authentication, which currently is not supported.

/cc @evchaki

dluc avatar Apr 01 '23 07:04 dluc

@dluc I can use apikey for now, thanks for the answer

LittleLittleCloud avatar Apr 04 '23 08:04 LittleLittleCloud

@dluc - Azure AD Auth is supported (via Bearer token) in AOAI: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/managed-identity. I'd recommend reopening this one as this is a feature that should be supported. We generally recommend use of MSI over API Keys (as they are much more secure). This will be a limiting factor for adoption of SK for more security focused organizations.

Here are the REST API reference docs showing the AAD Auth support via Bearer token: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#authentication

PlagueHO avatar Apr 13 '23 20:04 PlagueHO

@LittleLittleCloud - any chance you would be able to reopen this issue?

PlagueHO avatar Apr 17 '23 22:04 PlagueHO

@PlagueHO Sure

LittleLittleCloud avatar Apr 18 '23 17:04 LittleLittleCloud

@LittleLittleCloud @PlagueHO this is now supported! See here for an example: https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet/kernel-syntax-examples/Example25_AADAuth.cs

I'll go ahead and close - please reach out if you believe there's more to do here.

Thanks for the feature request.

shawncal avatar Apr 28 '23 01:04 shawncal

Awesome! Thanks @shawncal - much appreciated.

PlagueHO avatar Apr 28 '23 01:04 PlagueHO

HI @shawncal the link referenced here is not found. Can you share an example link again please?

vamshisiram avatar Jul 06 '23 22:07 vamshisiram

This works :

import semantic_kernel as sk
from azure.identity import DefaultAzureCredential
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion

default_credential = DefaultAzureCredential()
token = default_credential.get_token(
    "https://cognitiveservices.azure.com/.default"
)

kernel = sk.Kernel()
kernel.add_text_completion_service(
    "dv",
    AzureTextCompletion(
        deployment_name=<your deployment name>,
        endpoint=<your endpoint>
        api_key=token.token,
        api_version="2023-05-15",
        ad_auth=True,
    ),
)

rm2631 avatar Jul 19 '23 19:07 rm2631

@vamshisiram I think this is the new link https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/KernelSyntaxExamples/Example26_AADAuth.cs

RohanKrishna-2000 avatar Mar 18 '24 12:03 RohanKrishna-2000