How to read Azure openai service OPENAI_API_KEY from an Azure HTTP trigger function

JDR 90 Reputation points
2023-08-26T07:55:46.1033333+00:00

Hi,

Azure Openai service comes with a key (KEY1 and KEY2 under Keys and Endpoints) that are read from python chat code using os.getenv("OPEN_API_KEY").

I would like to call the openai service (openai.ChatCompletion.create) from an Azure function (HTTP trigger). Is there a way to access my Openai service key (KEY1 or KEY2) from such a function?

If not, how can I define an OPEN_API_KEY environment variable in the Azure function and set it to the value of the key of the Azure Openai service.

Thanks in advance for any help

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,331 questions
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,537 questions
{count} votes

Accepted answer
  1. MayankBargali-MSFT 70,796 Reputation points
    2023-08-28T04:53:41.6633333+00:00

    @JDR Thanks for reaching out.

    To answer your first query. Yes, your understanding is correct.

    In Azure Functions, Environment Variables are set for the Function App, and can be accessed by all the functions within that app. They are set within the ‘Configuration’ tab within Settings on the left pane. They are referred to as ‘Application Settings’.

    To answer your second question if you don't want to store credentials in your applications that run in the cloud then you can leverage the managed identity to authenticate your azure openai from function app. For more details from open ai end you can refer to this document. From the function app you can refer to this document.

    In case if you don't want to use the managed identity (which should be used in the first case if you don't want to deal with storing the keys) and still want to use the function app configuration to store the keys and you don't have to manual copy paste it then you need to have your custom code/logic to automate it. You can use the Azure REST API to get the keys from open AI value and update it at the function end.

    Let me know if you have any queries or concerns.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.