이 빠른 시작에서는 OpenAI 또는 Azure OpenAI DALL-E AI 모델을 사용하여 이미지를 생성하는 데 사용하는 OpenAI.Images.ImageClient .NET 콘솔 앱을 만듭니다. 이러한 모델은 텍스트 프롬프트에서 이미지를 생성합니다.
필수 조건
- .NET 8.0 SDK 이상 - .NET 8.0 SDK을 설치하십시오.
- 이 샘플을 실행할 수 있는 OpenAI의 API 키입니다.
필수 조건
- .NET 8.0 SDK 이상 - .NET 8 SDK설치하십시오.
- Azure 구독 – 무료로 만드세요.
- Azure 개발자 CLI(선택 사항) - Azure 개발자 CLI를 설치하거나 업데이트하기.
비고
의미 체계 커널 사용하여 이 문서의 작업을 수행할 수도 있습니다. 의미 체계 커널은 AI 에이전트를 빌드하고 최신 AI 모델을 .NET 앱에 통합할 수 있는 간단한 오픈 소스 SDK입니다.
앱 만들기
다음 단계를 완료하여 AI 모델에 연결할 .NET 콘솔 앱을 만듭니다.
컴퓨터의 빈 디렉터리에서
dotnet new명령을 사용하여 새 콘솔 앱을 만듭니다.dotnet new console -o ImagesAI디렉터리를 앱 폴더로 변경합니다.
cd ImagesAI필요한 패키지를 설치합니다.
dotnet add package Azure.AI.OpenAI dotnet add package Azure.Identity dotnet add package Microsoft.Extensions.Configuration dotnet add package Microsoft.Extensions.Configuration.UserSecretsdotnet add package OpenAI dotnet add package Microsoft.Extensions.Configuration dotnet add package Microsoft.Extensions.Configuration.UserSecretsVisual Studio Code 또는 선택한 편집기에서 앱을 엽니다.
code .
AI 서비스 만들기
Azure OpenAI 서비스 및 모델을 프로비전하려면 Azure OpenAI 서비스 리소스 만들기 및 배포 문서의 단계를 완료합니다.
터미널 또는 명령 프롬프트에서 프로젝트 디렉터리의 루트로 이동합니다.
다음 명령을 실행하여 샘플 앱에 대한 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>
앱 구성
터미널 또는 명령 프롬프트에서 .NET 프로젝트의 루트로 이동합니다.
다음 명령을 실행하여 OpenAI API 키를 샘플 앱의 암호로 구성합니다.
dotnet user-secrets init dotnet user-secrets set OpenAIKey <your-OpenAI-key> dotnet user-secrets set ModelName <your-OpenAI-model-name>
앱 코드 추가
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 리소스를 프로비전하지 않는 경우, Visual Studio 또는 Azure CLI에 로그인하는 데 사용한 계정에Azure AI Developer역할을 할당해야 합니다. 자세한 내용은 .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을 콘솔 출력에 인쇄합니다.
앱을 실행합니다.
dotnet run콘솔 출력의 이미지 URL로 이동하여 생성된 이미지를 봅니다. 프롬프트의 텍스트 콘텐츠를 사용자 지정하여 새 이미지를 만들거나 원본을 수정합니다.
리소스 정리
더 이상 필요하지 않은 경우 Azure OpenAI 리소스 및 GPT-4 모델 배포를 삭제합니다.
- Azure PortalAzure OpenAI 리소스로 이동합니다.
- Azure OpenAI 리소스를 선택한 다음, 삭제선택합니다.
다음 단계
- 빠른 시작 - .NET 사용하여 AI 채팅 앱 빌드
- .NET 및 Azure OpenAI Completions을 사용하여 텍스트와 대화를 생성
.NET