Limekiller / moodle-block_openai_chat

An AI chat block for Moodle, powered by GPT
https://moodle.org/plugins/block_openai_chat
GNU General Public License v3.0
27 stars 15 forks source link

azure openai api #19

Closed grantcolin closed 4 months ago

grantcolin commented 1 year ago

Hi. I have modded the plugin to work with Azure openai api but it would be good if this was an option out of the box. https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference

See change for Azure openai support here using gpt-3.5-turbo model

    $curl->setopt(array(
        'CURLOPT_HTTPHEADER' => array(

//'Authorization: Bearer ' . $this->apikey, 'api-key: ' . $this->apikey, 'Content-Type: application/json' ), )); //$response = $curl->post("https://api.openai.com/v1/chat/completions",

// the api endpoint in azure is different for each customer. Would be good if that could be configured within settings

    $response = $curl->post("https://<servicename>.openai.azure.com/openai/deployments/mlsopenai/chat/completions?api-version=2023-03-15-preview",
bennya1 commented 11 months ago

Hi +1 on needing Azure OpenAI supported, noticed it is required by several academic institutes in my region.

Limekiller commented 11 months ago

I would definitely merge a pull request that provided this functionality. It sounds like it would need to both provide a checkbox on the settings page to "enable Azure endpoints" as well as a textbox that allows the admin to enter their service name. However, I would need to understand the Azure URL better before I could implement this myself. How does the domain change when different models are used, especially GPT vs non-GPT models? What is the significance of the api-version parameter, and how often would that need to be updated?

grantcolin commented 11 months ago

Hi. I think all you need on the settings form

is a checkbox - use Azure Openai a text field for the AZ openai api endpoint which will be different for each user (visible if AZ openai is checked) and as above in classes/completion/chat.php wrap the apikey / response lines in an if az openai=checked

the lines that are required to be changed to use AZ openai are:

'api-key: ' . $this->apikey,

and

$response = $curl->post("https://.openai.azure.com/openai/deployments/mlsopenai/chat/completions?api-version=2023-03-15-preview",

the curl->post being a variable from the settings text field for api endpoint.

Hope that helps. If I have time I will create a pull request.

Limekiller commented 5 months ago

Just to update everyone on this issue, development on this has been sponsored and I've been given access to an Azure account so that I can understand and test the API. An upcoming 2.1 release will include support for Azure endpoints by creating a new selectable API type, "azure," that adds a "resource name," "deployment ID," and "API version" setting. This should release in the next few weeks.

Limekiller commented 4 months ago

Support for Azure is included in version 2.1.2, released today.