azure-search-openai-demo icon indicating copy to clipboard operation
azure-search-openai-demo copied to clipboard

AttributeError: 'AzureKeyCredential' object has no attribute 'get_token'

Open deadsoul44 opened this issue 1 year ago • 2 comments

This issue is for a: (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

Minimal steps to reproduce

I am trying to run the backend service locally by using separate AzureKeyCredential instances with the keys for each service. Because, the app will be deployed to a local server (not Azure).

        azure_openai_credential = AzureKeyCredential(AZURE_OPENAI_KEY)
        token_provider = get_bearer_token_provider(azure_openai_credential, "https://cognitiveservices.azure.com/.default")

        openai_client = AsyncAzureOpenAI(
            api_version=api_version,
            azure_endpoint=endpoint,
            azure_ad_token_provider=token_provider,
        )

Any log messages given by the failure

ERROR:root:Exception in /chat: 'AzureKeyCredential' object has no attribute 'get_token'
Traceback (most recent call last):
  File "src\backend\app.py", line 178, in chat
    result = await approach.run(
  File "src\backend\approaches\chatapproach.py", line 222, in run
    return await self.run_without_streaming(messages, overrides, auth_claims, session_state)
  File "src\backend\approaches\chatapproach.py", line 134, in run_without_streaming
    extra_info, chat_coroutine = await self.run_until_final_call(
  File "src\backend\approaches\chatreadretrieveread.py", line 149, in run_until_final_call
    chat_completion: ChatCompletion = await self.openai_client.chat.completions.create(
  File "AppData\Local\Programs\Python\Python310\lib\site-packages\openai\resources\chat\completions.py", line 1181, in create
    return await self._post(
  File "AppData\Local\Programs\Python\Python310\lib\site-packages\openai\_base_client.py", line 1790, in post
    return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
  File "AppData\Local\Programs\Python\Python310\lib\site-packages\openai\_base_client.py", line 1493, in request
    return await self._request(
  File AppData\Local\Programs\Python\Python310\lib\site-packages\openai\_base_client.py", line 1511, in _request
    await self._prepare_options(options)
  File "AppData\Local\Programs\Python\Python310\lib\site-packages\openai\lib\azure.py", line 531, in _prepare_options
    azure_ad_token = await self._get_azure_ad_token()
  File "AppData\Local\Programs\Python\Python310\lib\site-packages\openai\lib\azure.py", line 517, in _get_azure_ad_token
    token = await token
  File "AppData\Local\Programs\Python\Python310\lib\site-packages\azure\identity\aio\_bearer_token_provider.py", line 44, in wrapper
    await policy.on_request(request)
  File "AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\policies\_authentication_async.py", line 70, in on_request    
    self._token = await await_result(self._credential.get_token, *self._scopes)
AttributeError: 'AzureKeyCredential' object has no attribute 'get_token'

Expected/desired behavior

No error

OS and Version?

Windows 10.

deadsoul44 avatar Jun 12 '24 09:06 deadsoul44

If you are using a key, then you would just pass in the key directly to AsyncAzureOpenAI, like:

api_key=AZURE_OPENAI_KEY

pamelafox avatar Jun 13 '24 05:06 pamelafox

Same issue here. Is it because of the openai verion? Des anyone know how to fix it?

lesleychou avatar Aug 20 '24 20:08 lesleychou