.NET 的 DALL·E 整合庫
注意目前版本的庫需要 DALL·E 3 資源。如果您想使用DALL·E 2,請參考此版本。
該庫可在 NuGet 上使用。只需在程式包管理器 GUI中搜尋DallENet或在.NET CLI中執行以下命令:
dotnet add package DallENet在應用程式啟動時註冊DALL·E服務:
builder . Services . AddDallE ( options =>
{
// Azure OpenAI Service.
options . UseAzure ( resourceName : "" , apiKey : "" , authenticationType : AzureAuthenticationType . ApiKey ) ;
options . DefaultSize = DallEImageSizes . _1792x1024 ; // Default: 1024x1024
options . DefaultQuality = DallEImageQualities . HD ; // Default: Standard
options . DefaultStyle = DallEImageStyles . Natural ; // Default: Vivid
options . DefaultResponseFormat = DallEImageResponseFormats . Url ; // Default: Url
} ) ;目前, DallENet僅支援 Azure OpenAI 服務。未來版本將增加對 OpenAI 的支援。所需的配置參數如下:
DALL·E 3 能夠產生不同解析度的影像:
使用DefaultSize屬性,可以指定預設影像大小,除非您在GenerateImageAsync或GetImageStreamAsync方法中傳遞明確值。預設解析度為 1024x1024。
DALL·E 3 能夠產生標準或高清品質的影像,即影像具有更精細的細節和更高的一致性。使用DefaultQuality屬性,可以指定預設質量,除非您在GenerateImageAsync或GetImageStreamAsync方法中傳遞明確值。預設品質為Standard 。
DALL·E 3能夠產生具有生動自然風格的圖像:
使用DefaultStyle屬性,可以指定預設樣式,除非您在GenerateImageAsync或GetImageStreamAsync方法中傳遞明確值。預設樣式是Vivid 。
DALL·E 3 能夠傳回其 Base64 編碼所產生的圖像的 URL。使用DefaultResponseFormat屬性,可以指定預設質量,除非您在GenerateImageAsync或GetImageStreamAsync方法中傳遞明確值。預設質量是Url 。
可以使用appsettings.json檔案中的DallE部分自動從 IConfiguration 讀取配置:
"DallE" : {
"Provider" : "Azure" , // Optional. Currently only Azure is supported
"ApiKey" : "" , // Required
"ResourceName" : "" , // Required
"ApiVersion" : "2023-12-01-preview" , // Optional, used only by Azure OpenAI Service. Allowed values: 2023-12-01-preview (default)
"AuthenticationType" : "ApiKey" , // Optional, Allowed values: ApiKey (default) or ActiveDirectory
"DefaultModel" : "dall-e-3" , // Required
"DefaultSize" : "1792x1024" , // Optional, Allowed values: 1024x1024 (default), 1792x1024 or 1024x1792
"DefaultQuality" : "standard" , // Optional, Allowed values: standard (default) or hd
"DefaultResponseFormat" : "url" , // Optional, Allowed values: url (default) or b64_json
"DefaultStyle" : "vivid" , // Optional, Allowed values: natural (default), or vivid
"ThrowExceptionOnError" : true
//"User": "UserName" // Optional
}然後使用che AddDallE方法的相應重載:
// Adds DALL·E service using settings from IConfiguration.
builder . Services . AddDallE ( builder . Configuration ) ;AddDallE方法還有一個重載,它接受 IServiceProvider 作為參數。例如,如果我們在 Web API 中,並且需要支援每個使用者都有不同的 API 金鑰(可以透過依賴注入存取資料庫來檢索該金鑰)的場景,則可以使用它:
builder . Services . AddDallE ( ( services , options ) =>
{
var accountService = services . GetRequiredService < IAccountService > ( ) ;
// Dynamically gets the Resource name and the API Key from the service.
var resourceName = "..." ;
var apiKey = "..."
options . UseAzure ( resourceName , apiKey ) ;
} ) ;在更複雜的場景中,可以使用程式碼和 IConfiguration 來設定DallENet 。如果我們想要設定一堆通用屬性,但同時我們需要一些配置邏輯,這可能很有用。例如:
builder . Services . AddDallE ( ( services , options ) =>
{
// Configure common properties (default size, default style, ecc.) using IConfiguration.
options . UseConfiguration ( builder . Configuration ) ;
var accountService = services . GetRequiredService < IAccountService > ( ) ;
// Dynamically gets the Resource name and the API Key from the service.
var resourceName = "..." ;
var apiKey = "..."
options . UseAzure ( resourceName , apiKey ) ;
} ) ; 該程式庫可用於使用 .NET 6.0 或更高版本建置的任何 .NET 應用程式。例如,我們可以這樣建立一個Minimal API:
app . MapPost ( "/api/image" , async ( Request request , IDallEClient dallEClient ) =>
{
var response = await dallEClient . GenerateImagesAsync ( request . Prompt ) ;
return TypedResults . Ok ( response ) ;
} )
. WithOpenApi ( ) ;
public record class Request ( string Prompt ) ;特別是,回應包含生成圖像的 URL。如果我們只想檢索第一個產生的圖像的 URL,我們可以呼叫GetImageUrl方法:
var imageUrl = response . GetImageUrl ( ) ;注意生成的影像將在 24 小時後自動刪除。
檢查 Samples 資料夾以獲取有關不同實現的更多資訊。
完整的技術文件可在此處取得。
該項目正在不斷發展。歡迎貢獻。請隨意在儲存庫上提出問題和拉取請求,我們將盡力解決這些問題。
警告請記住在開發分支上工作,不要直接使用主分支。建立針對開發的拉取請求。