dotnet / eShop

A reference .NET application implementing an eCommerce site

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't deploy to Azure with AI support

aaronpowell opened this issue · comments

Cross-linking from dotnet/aspire#3409 for visibility.

Due to the above mentioned bug, it's not possible to deploy to Azure if you have AI support enabled. I've worked around it by changing the AI setup code like this:

bool useOpenAI = true;
if (useOpenAI)
{
    const string openAIName = "openai";
    const string textEmbeddingName = "text-embedding-ada-002";
    const string chatModelName = "gpt-35-turbo-16k";

    // to use an existing OpenAI resource, add the following to the AppHost user secrets:
    // "ConnectionStrings": {
    //   "openai": "Key=<API Key>" (to use https://api.openai.com/)
    //     -or-
    //   "openai": "Endpoint=https://<name>.openai.azure.com/" (to use Azure OpenAI)
    // }
    if (builder.Configuration.GetConnectionString(openAIName) is not null)
    {
        var openAI = builder.AddConnectionString(openAIName);

        catalogApi
            .WithReference(openAI)
            .WithEnvironment("AI__OPENAI__EMBEDDINGNAME", textEmbeddingName);

        webApp
            .WithReference(openAI)
            .WithEnvironment("AI__OPENAI__CHATMODEL", chatModelName); ;
    }
    else
    {
        // to use Azure provisioning, add the following to the AppHost user secrets:
        // "Azure": {
        //   "SubscriptionId": "<your subscription ID>"
        //   "Location": "<location>"
        // }
        var chatAIResource = builder.AddAzureOpenAI(openAIName)
            .AddDeployment(new AzureOpenAIDeployment(chatModelName, "gpt-35-turbo", "0613"));

        var embeddingAIResource = builder.AddAzureOpenAI($"{openAIName}-embedding")
            .AddDeployment(new AzureOpenAIDeployment(textEmbeddingName, "text-embedding-ada-002", "2"));

        catalogApi
            .WithReference(chatAIResource)
            .WithReference(embeddingAIResource)
            .WithEnvironment("AI__OPENAI__EMBEDDINGNAME", textEmbeddingName);

        webApp
            .WithReference(chatAIResource)
            .WithReference(embeddingAIResource)
            .WithEnvironment("AI__OPENAI__CHATMODEL", chatModelName);
    }
}

It's only a quick solution, as I don't think both services require chat + embeddings (and you need to update the services to be aware of the additional resource too).

I'd added the AI support in eShop for OpenAI. @sebastienros recently updated it to also work with Azure OpenAI as part of incorporating the aspire component for OpenAIClient.

cc: @eerhardt

I confirm this is not an issue with eShop but Aspire. I assigned it to myself. I'll test eShop to confirm the fix.

Yeah, it's not a bug in eShop, it's a limit within Azure.Provisioning, but there is a workaround we can implement (I tested the above and it works) which will unblock people.

@aaronpowell this is actually how it was done initially, a proud moment too ;) https://github.com/dotnet/aspire/pull/2529/files#diff-c73863c6064137e5eebea0eb216e18f8e89ceb98819ea5148d4fd1ba08f167da

But we recently switched the way these files are constructed and I assume this specificity was lost. I'll check if there were other resources with the same issue, though I doubt it.

I've not had any other problems with it, and I've got a version using Azure redis and postgres flexible rather than docker

@sebastienros Are we leaving this open to validate the Aspire change?

Ideally we could keep this open until we update the package version. I have no preference, I tested that the fix was working outside of eShop already.