guidance icon indicating copy to clipboard operation
guidance copied to clipboard

Cannot initialize AzureOpenAI llm

Open kblissett opened this issue 1 year ago • 5 comments

A minor bug is causing the AzureOpenAI llm __init__ to fail.

Steps to reproduce:

import guidance

guidance.llms.AzureOpenAI("gpt-3.5-turbo")

Error message:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[17], line 3
      1 import guidance
----> 3 guidance.llms.AzureOpenAI("gpt-3.5-turbo")

File [~/medical-writing/.venv/lib/python3.10/site-packages/guidance/llms/_azure_openai.py:34](https://file+.vscode-resource.vscode-cdn.net/Users/kevinblissett/medical-writing/~/medical-writing/.venv/lib/python3.10/site-packages/guidance/llms/_azure_openai.py:34), in AzureOpenAI.__init__(self, model, client_id, authority, caching, max_retries, max_calls_per_min, token, endpoint, scopes, temperature, chat_mode)
     31 if os.path.exists(self._token_cache_path):
     32     self._token_cache.deserialize(open(self._token_cache_path, 'r').read())
---> 34 self._rest_headers["X-ModelType"] = self.model_name

AttributeError: 'AzureOpenAI' object has no attribute '_rest_headers'

kblissett avatar May 16 '23 19:05 kblissett

met the same issue here.

pcliupc avatar May 17 '23 02:05 pcliupc

+1

nasirus avatar May 17 '23 20:05 nasirus

+1 Also struggling with how to specify connection to azure. It is prompting me for a client_id...Received same message with and without the token param which is probably used incorrectly. Any idea how to connect this thing?

Getting response when using the program function: AADSTS900144: The request body must contain the following parameter: 'client_id'

guidance.llm = guidance.llms.AzureOpenAI(model='text-davinci-003',
                                    endpoint='https://<my instance>.openai.azure.com',
                                    token=os.environ["<MY KEY FROM AZURE PORTAL>"])

mattberns avatar May 22 '23 02:05 mattberns

I was able to solve this problem using device code flow. https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-desktop-acquire-token-device-code-flow?tabs=python https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-daemon-app-registration

guidance.llm = guidance.llms.AzureOpenAI(model="text-davinci-003",
                endpoint="https://<endpoint url>", client_id="<client id(application id)>", 
                authority="https://login.microsoftonline.com/<Azure AD tenant id>",
                scopes=["https://cognitiveservices.azure.com/.default"])

wwwcojp avatar May 22 '23 05:05 wwwcojp

@kblissett Normally, Enterprise Application, which contains client_id and client secret, is needed according to guidance/llms/_azure_openai.py If you wanna use Azure OpenAI simply with deployment name like official openai python api, try my solution (#87 )

somisawa avatar May 22 '23 08:05 somisawa

any updates regarding support for AZURE_OPEN_AI_API_KEY?

williambrach avatar May 29 '23 13:05 williambrach

any updates on fixes for token and end_point?

javier-alvarez avatar Jun 02 '23 10:06 javier-alvarez

Sorry this took a while. This should work now on 0.0.61:

import guidance

llm = guidance.llms.OpenAI(
    'text-davinci-003',
    api_type='azure',
    api_key='sk-...',
    api_base='https://example-endpoint.openai.azure.com',
    api_version='2023-05-15',
    deployment_id='...',
    caching=False
)

program = guidance("""My favorite flavor is{{gen 'flavor' max_tokens=10 stop="."}}""", llm=llm)
program()

slundberg avatar Jun 02 '23 18:06 slundberg