- Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenerateImageSC.cs
52 lines (42 loc) · 2.12 KB
/
GenerateImageSC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
usingAzure.AI.OpenAI;
usingAzure;
usingRedis.OM;
usingRedis.OM.Vectorizers;
usingMicrosoft.AspNetCore.DataProtection.KeyManagement;
usingOpenAI.Images;
namespaceOutputCacheDallESample
{
publicstaticclassGenerateImageSC
{
privatestaticRedisConnectionProvider_provider;
publicstaticasyncTaskGenerateImageSCAsync(HttpContextcontext,string_prompt,IConfiguration_config)
{
stringimageURL;
stringendpoint=_config["AZURE_OPENAI_ENDPOINT"];
stringkey=_config["apiKey"];
_provider=newRedisConnectionProvider(_config["SemanticCacheAzureProvider"]);
varcache=_provider.AzureOpenAISemanticCache(_config["apiKey"],_config["AOAIResourceName"],_config["AOAIEmbeddingDeploymentName"],1536);
if(cache.GetSimilar(_prompt).Length>0)
{
imageURL=cache.GetSimilar(_prompt)[0];
awaitcontext.Response.WriteAsync("<!DOCTYPE html><html><body> "+
$"<img src=\"{imageURL}\" alt=\"AI Generated Picture {_prompt}\" width=\"460\" height=\"345\">"+
" </body> </html>");
}
else
{
AzureOpenAIClientclient=new(newUri(endpoint),newAzureKeyCredential(key));
ImageClientimageClient=client.GetImageClient("dall-e-3");
GeneratedImagegeneratedImage=awaitimageClient.GenerateImageAsync(_prompt,newImageGenerationOptions(){
Size=GeneratedImageSize.W1024xH1024
});
// Image Generations responses provide URLs you can use to retrieve requested images
imageURL=generatedImage.ImageUri.AbsoluteUri;
awaitcache.StoreAsync(_prompt,imageURL);
awaitcontext.Response.WriteAsync("<!DOCTYPE html><html><body> "+
$"<img src=\"{imageURL}\" alt=\"AI Generated Picture {_prompt}\" width=\"460\" height=\"345\">"+
" </body> </html>");
}
}
}
}