Using Azure OpenAI with Aspose.Words.IA

I want to use Aspose.words.IA in .Net to Check grammar using my Azure OpenAI instance. but I receive an error message : Ressource not found. Th url and Apikey are correct because I already use them in my project to reach my Azure OpenAI instance.
Here is my code in C# :

Document doc = new Document(inputFile);
string azureApiKey = "<MyApiKey>";
string azureEndpoint = "https://<My Azure OpenAI instance>.cognitiveservices.azure.com/openai/deployments/<My eployment name>/chat/completions?api-version=2025-01-01-preview";
OpenAiModel model = (OpenAiModel)AiModel.Create(AiModelType.Gpt4O)
     .WithApiKey(azureApiKey);
model.Url = azureEndpoint;

CheckGrammarOptions grammarOptions = new CheckGrammarOptions
{
    ImproveStylistics = true
};
Document documentCorrige = model.CheckGrammar(doc, grammarOptions);
documentCorrige.Save(outputFile);

@youafiey

• Is the Azure OpenAI endpoint URL correctly formatted with the deployment name and API version?
• Are you using the correct API key format and does it have proper permissions for the deployment?
• What specific error message or HTTP status code are you receiving when the resource is not found?
• Have you verified the deployment name matches exactly what’s configured in your Azure OpenAI instance?
• Is the Azure region and endpoint URL consistent with your OpenAI instance configuration?
• Are there any firewall or network restrictions preventing access to the Azure endpoint from your environment?

@youafiey Please try modifying your code like this:

Document doc = new Document(inputFile);
string azureApiKey = "<MyApiKey>";
string azureEndpoint = "https://<My Azure OpenAI instance>";
CustomOpenAiModel  model = new CustomOpenAiModel(azureEndpoint ,  "gpt-4o");
model.WithApiKey(azureApiKey);

CheckGrammarOptions grammarOptions = new CheckGrammarOptions
{
    ImproveStylistics = true
};
Document documentCorrige = model.CheckGrammar(doc, grammarOptions);
documentCorrige.Save(outputFile);
private class CustomOpenAiModel : OpenAiModel
{
    public CustomOpenAiModel(string url, string name)
    {
        base.Url = url;
        mName = name;
    }

    protected override string Name { get { return mName; } }

    private readonly string mName;
}

Hi,
I have the same error message : Ressource not found.
for azureEndpoint, I tried 2 options :
1- "<Azure OpenAI instance Name>"
2- "<Azure OpenAI instance Name>.cognitiveservices.azure.com"
With both options I have the same error message. Do you have a code sample that uses Azure Open AI instance with gpt-4o model ?
Thanks

@youafiey Yes, the following code works fine on my side with custom instance on OpenAI:

private static AiModel CreateOpenAiModelWithCustomUrlAndName()
{
    string apiUrl = Environment.GetEnvironmentVariable("CUSTOM_OPENAI_URL", EnvironmentVariableTarget.User);
    string apiName = Environment.GetEnvironmentVariable("CUSTOM_OPENAI_NAME", EnvironmentVariableTarget.User);
    string apiKey = Environment.GetEnvironmentVariable("CUSTOM_OPENAI_KEY", EnvironmentVariableTarget.User);

    return new CustomOpenAiModel(apiUrl, apiName).WithApiKey(apiKey);
}

CUSTOM_OPENAI_URL is https://llm.myurl.com/
CUSTOM_OPENAI_NAME is gpt-4o
CUSTOM_OPENAI_KEY is my api key.

Please try using with the following url
https://<My Azure OpenAI instance>.cognitiveservices.azure.com/openai/deployments/<My eployment name>/

It does not work for me, I have the same error message.
Notes :
1- I am not using OpenAI instance directly from OpenAI, I am using Azure OpenAI instance from Microsoft Azure Cloud.
2- My deployment name is “gpt-4o-test” that uses gpt-4o model, so I tried with CUSTOM_OPENAI_NAME=gpt-4o and CUSTOM_OPENAI_NAME=gpt-4o-test but nothing works

@youafiey Can you connect to your deployment via OpenAI using code like this:

using OpenAI;
using OpenAI.Chat;

ChatClient client = new(
    model: "MODEL_NAME",
    credential: new ApiKeyCredential(Environment.GetEnvironmentVariable("OPENAI_API_KEY")),
    options: new OpenAIClientOptions()
    {
        Endpoint = new Uri("BASE_URL")
    }
);