[OPENAIINT-14] Add documentation for Azure openai services Created: 26/Oct/23  Updated: 21/Dec/23  Resolved: 21/Dec/23

Status: Closed
Project: OpenAI Integrations
Component/s: None
Affects Version/s: None
Fix Version/s: 1.0.7

Type: Improvement Priority: Neutral
Reporter: Tobias Kerschbaum Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File SCR-20231026-mrbq.png     PNG File SCR-20231026-mslg.png    
Template:
Acceptance criteria:
Empty
Date of First Response:

 Description   

We could/should add a way to the documentation how to use our Openai module with Azure Openai services. Maybe we could even add the special filter for Azure to our code base and just mention how to change the endpoint config.

Decoration:

baseUrl: https://magnolia-azure-openai.openai.azure.com/openai/deployments/magnolia-azure-openai
components:
  authorization:
    class: info.magnolia.ai.automations.rest.RestAzureClientAuthorizationFilter
restCalls:
  completions:
    queryParameters:
      api-version: 2023-07-01-preview
  chatCompletions:
    queryParameters:
      api-version: 2023-07-01-preview 

Custom filter to set authorization token:

/**
 * This file Copyright (c) 2023 Magnolia International
 * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
 *
 *
 * This program and the accompanying materials are made
 * available under the terms of the Magnolia Network Agreement
 * which accompanies this distribution, and is available at
 * http://www.magnolia-cms.com/mna.html
 *
 * Any modifications to this file must keep this entire header
 * intact.
 *
 */
package info.magnolia.services.rest;
​
import info.magnolia.ai.automations.OpenAIAutomationsModule;
import info.magnolia.ai.automations.OpenAICredentialsProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
​
import javax.inject.Inject;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import java.io.IOException;
​
/**
 * The filter adds the api-key header to the requests
 */
@Slf4j
public class RestAzureClientAuthorizationFilter implements ClientRequestFilter {
​
    private final OpenAIAutomationsModule openAIAutomationsModule;
    private final OpenAICredentialsProvider openAICredentialsProvider;
    private final String API_KEY = "api-key";
​
    @Inject
    public RestAzureClientAuthorizationFilter(OpenAIAutomationsModule openAIAutomationsModule, OpenAICredentialsProvider openAICredentialsProvider) {
        this.openAIAutomationsModule = openAIAutomationsModule;
        this.openAICredentialsProvider = openAICredentialsProvider;
    }
​
    @Override
    public void filter(ClientRequestContext requestContext) throws IOException {
        requestContext.getHeaders().add(API_KEY, getAzureApiKey());
    }
​
    public String getAzureApiKey() {
        String bearerToken = openAICredentialsProvider.getAuthorization(openAIAutomationsModule.getApiKey());
        if (bearerToken != null && bearerToken.startsWith("Bearer")) {
            bearerToken = StringUtils.replace(bearerToken, "Bearer ", "");
        }
​
        return bearerToken;
    }
​
}


 Comments   
Comment by Minh Nguyen [ 26/Oct/23 ]

Ref doc from Azure:
https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line&pivots=rest-api

Once your application is approved, you can try out here: https://oai.azure.com/

curl "https://magnolia-azure-openai.openai.azure.com/openai/deployments/magnolia-azure-openai/chat/completions?api-version=2023-07-01-preview" \
  -H "Content-Type: application/json" \
  -H "api-key: YOUR_API_KEY" \
  -d "{
  \"messages\": [{\"role\":\"system\",\"content\":\"You are an AI assistant that helps people find information.\"}],
  \"max_tokens\": 800,
  \"temperature\": 0.7,
  \"frequency_penalty\": 0,
  \"presence_penalty\": 0,
  \"top_p\": 0.95,
  \"stop\": null
}"

Thank you.

Generated at Mon Feb 12 10:23:54 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.