共用方式為


使用 OpenAI.Images.ImageClient 產生影像

在本快速入門中,您會建立 .NET 主控台應用程式,用來 OpenAI.Images.ImageClient 使用 OpenAI 或 Azure OpenAI DALL-E AI 模型產生映像。 這些模型從文字提示產生圖像。

先決條件

  • .NET 8.0 SDK 或更高版本 - 安裝 .NET 8.0 SDK
  • OpenAI API 金鑰,因此您可以執行此範例。

先決條件

備註

您也可以使用 語意核心 來完成本文中的工作。 語意核心是輕量型開放原始碼 SDK,可讓您建置 AI 代理程式,並將最新的 AI 模型整合到 .NET 應用程式中。

建立應用程式

完成下列步驟以建立 .NET 控制台應用程式以連線到 AI 模型。

  1. 在您的電腦上的空白目錄中,使用 dotnet new 命令來建立新的控制台應用程式:

    dotnet new console -o ImagesAI
    
  2. 將目錄變更為應用程式資料夾:

    cd ImagesAI
    
  3. 安裝必要的套件:

    dotnet add package Azure.AI.OpenAI
    dotnet add package Azure.Identity
    dotnet add package Microsoft.Extensions.Configuration
    dotnet add package Microsoft.Extensions.Configuration.UserSecrets
    
    dotnet add package OpenAI
    dotnet add package Microsoft.Extensions.Configuration
    dotnet add package Microsoft.Extensions.Configuration.UserSecrets
    
  4. 在 Visual Studio Code 或您選擇的編輯器中開啟應用程式。

    code .
    

建立 AI 服務

  1. 若要布建 Azure OpenAI 服務和模型,請完成 建立及部署 Azure OpenAI 服務資源 一文中的步驟。

  2. 在終端機或命令列中,導航到專案目錄的根目錄。

  3. 執行下列命令來設定範例應用程式的 Azure OpenAI 端點和模型名稱:

    dotnet user-secrets init
    dotnet user-secrets set AZURE_OPENAI_ENDPOINT <your-Azure-OpenAI-endpoint>
    dotnet user-secrets set AZURE_OPENAI_GPT_NAME <your-Azure-OpenAI-model-name>
    dotnet user-secrets set AZURE_OPENAI_API_KEY <your-Azure-OpenAI-key>
    

設定應用程式

  1. 請從終端機或命令提示字元導航至您的 .NET 專案根目錄。

  2. 執行下列命令,將 OpenAI API 金鑰設定為範例應用程式的秘密:

    dotnet user-secrets init
    dotnet user-secrets set OpenAIKey <your-OpenAI-key>
    dotnet user-secrets set ModelName <your-OpenAI-model-name>
    

新增應用程式程序代碼

  1. 在檔案中 Program.cs ,新增下列程序代碼,以聯機並驗證 AI 模型。

    using Microsoft.Extensions.Configuration;
    using OpenAI.Images;
    using System.ClientModel;
    using Azure.AI.OpenAI;
    using Azure.Identity;
    
    // Retrieve the local secrets saved during the Azure deployment. If you skipped the deployment
    // because you already have an Azure OpenAI available, edit the following lines to use your information,
    // e.g. string openAIEndpoint = "https://cog-demo123.openai.azure.com/";
    var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
    string endpoint = config["AZURE_OPENAI_ENDPOINT"];
    string deployment = config["AZURE_OPENAI_DALLE_NAME"];
    
    // Create the Azure OpenAI ImageClient
    ImageClient client =
        new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
            .GetImageClient(deployment);
    
    // Generate the image
    GeneratedImage generatedImage = await client.GenerateImageAsync("""
        A postal card with an happy hiker waving and a beautiful mountain in the background.
        There is a trail visible in the foreground.
        The postal card has text in red saying: 'You are invited for a hike!'
        """, new ImageGenerationOptions { Size = GeneratedImageSize.W1024xH1024 });
    
    Console.WriteLine($"The generated image is ready at:\n{generatedImage.ImageUri}");
    

    備註

    DefaultAzureCredential 從本地工具中搜尋身份驗證憑證。 如果您未使用 azd 範本來佈建 Azure OpenAI 資源,您必須將角色指派 Azure AI Developer 給您用來登入 Visual Studio 或 Azure CLI 的帳戶。 如需詳細資訊,請參閱使用 .NET 向 Azure AI 服務驗證

    // Licensed to the .NET Foundation under one or more agreements.
    // The .NET Foundation licenses this file to you under the MIT license.
    // See the LICENSE file in the project root for more information.
    using Microsoft.Extensions.Configuration;
    using OpenAI.Images;
    // Retrieve the local secrets that were set from the command line, using:
    // dotnet user-secrets init
    // dotnet user-secrets set OpenAIKey <your-openai-key>
    var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
    string key = config["OpenAIKey"];
    string modelName = config["ModelName"];
    
    // Create the OpenAI ImageClient
    ImageClient client = new(modelName, key);
    
    // Generate the image
    GeneratedImage generatedImage = await client.GenerateImageAsync("""
        A postal card with a happy hiker waving and a beautiful mountain in the background.
        There is a trail visible in the foreground.
        The postal card has text in red saying: 'You are invited for a hike!'
        """,
        new ImageGenerationOptions 
        {
            Size = GeneratedImageSize.W1024xH1024 
        });
    
    Console.WriteLine($"The generated image is ready at:\n{generatedImage.ImageUri}");
    

    上述 程式碼:

    • 從專案使用者秘密讀取重要的組態值,以連接至 AI 模型。
    • 建立一個 OpenAI.Images.ImageClient 以連接 AI 模型。
    • 將提示傳送至描述所需影像的模型。
    • 將產生的影像 URL 列印至控制台輸出。
  2. 執行應用程式:

    dotnet run
    

    流覽至主控台輸出中的影像 URL,以檢視產生的影像。 自定義提示的文字內容,以建立新的影像或修改原始影像。

清理資源

如果您不再需要它們,請刪除 Azure OpenAI 資源和 GPT-4 模型部署。

  1. Azure 入口網站中,流覽至 Azure OpenAI 資源。
  2. 選取 Azure OpenAI 資源,然後選取 [刪除

後續步驟