|
| 1 | +using Microsoft.AspNetCore.DataProtection.KeyManagement; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Azure; |
| 4 | +using Azure.AI.OpenAI; |
| 5 | +using static System.Environment; |
| 6 | +using System.Text; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using Newtonsoft.Json.Linq; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using API_GenerateConference.Models; |
| 11 | + |
| 12 | + |
| 13 | +namespace API_GenerateConference.Controllers |
| 14 | +{ |
| 15 | + [ApiController] |
| 16 | + [Route("[controller]")] |
| 17 | + public class GenerateConference : Controller |
| 18 | + { |
| 19 | + private string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); |
| 20 | + private string endpointDalle = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT_DALLE"); |
| 21 | + private string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY"); |
| 22 | + |
| 23 | + [HttpGet] |
| 24 | + public async Task<ActionResult<string>> GenerateText(string context) |
| 25 | + { |
| 26 | + OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key)); |
| 27 | + StringBuilder generatedText = new StringBuilder(""); |
| 28 | + var prompt = "You are tasked with delivering a presentation about " |
| 29 | + + context + |
| 30 | + " you'll need a well-defined structure with 4 paragraphs. " + |
| 31 | + "first paragraph Introduce the context, " + |
| 32 | + "second paragraph : talk about its history, "+ |
| 33 | + "third paragraph : explain what can be done with the context today, " + |
| 34 | + "fourth paragraph : then draw up a conclusion that covers all the points of the conference."; |
| 35 | + |
| 36 | + try |
| 37 | + { |
| 38 | + var chatCompletionsOptions = new ChatCompletionsOptions() |
| 39 | + { |
| 40 | + DeploymentName = "gpt-35-turbo", |
| 41 | + Temperature = 0.4f, |
| 42 | + Messages = { new ChatRequestSystemMessage(prompt),} |
| 43 | + }; |
| 44 | + |
| 45 | + await foreach (StreamingChatCompletionsUpdate chatUpdate in client.GetChatCompletionsStreaming(chatCompletionsOptions)) |
| 46 | + { |
| 47 | + if (chatUpdate.Role.HasValue) |
| 48 | + { |
| 49 | + generatedText.Append($"{chatUpdate.Role.Value.ToString().ToUpperInvariant()}: "); |
| 50 | + } |
| 51 | + if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate)) |
| 52 | + { |
| 53 | + generatedText.Append(chatUpdate.ContentUpdate); |
| 54 | + } |
| 55 | + } |
| 56 | + return generatedText.ToString(); |
| 57 | + } |
| 58 | + catch |
| 59 | + { |
| 60 | + return NotFound(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + [HttpGet("GenerateUrlImage")] |
| 65 | + public async Task<ActionResult<string>> GenerateUrlImage(string context, int whichParagraph) |
| 66 | + { |
| 67 | + try |
| 68 | + { |
| 69 | + var client = new HttpClient(); |
| 70 | + var request = new HttpRequestMessage(HttpMethod.Post, endpointDalle); |
| 71 | + request.Headers.Add("api-key", key); |
| 72 | + |
| 73 | + var jsonContent = new JsonDalle |
| 74 | + { |
| 75 | + prompt = $" in the following context {context} generates a photo in relation to the {whichParagraph} paragraph ", |
| 76 | + size = "1024x1024", |
| 77 | + n = 1, |
| 78 | + quality = "hd", |
| 79 | + style = "vivid" |
| 80 | + }; |
| 81 | + |
| 82 | + var serializedContent = JsonConvert.SerializeObject(jsonContent); |
| 83 | + var content = new StringContent(serializedContent, Encoding.UTF8, "application/json"); |
| 84 | + request.Content = content; |
| 85 | + |
| 86 | + var response = await client.SendAsync(request); |
| 87 | + response.EnsureSuccessStatusCode(); |
| 88 | + |
| 89 | + var responseData = await response.Content.ReadAsStringAsync(); |
| 90 | + var jsonObject = JObject.Parse(responseData); |
| 91 | + var imageUrl = jsonObject["data"][0]["url"].ToString(); |
| 92 | + |
| 93 | + return imageUrl; |
| 94 | + } |
| 95 | + catch (Exception ex) |
| 96 | + { |
| 97 | + return BadRequest(ex.Message); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + [HttpGet("GenerateConference")] |
| 103 | + public async Task<ActionResult<string>> GenerateConferance(string prompt) |
| 104 | + { |
| 105 | + try |
| 106 | + { |
| 107 | + var conferenceText = await GenerateText(prompt); |
| 108 | + Conference newConference = new Conference(); |
| 109 | + if(conferenceText.Value != null) |
| 110 | + { |
| 111 | + try |
| 112 | + { |
| 113 | + newConference.ConferenceTalk = conferenceText.Value; |
| 114 | + for (int iParagraph = 1; iParagraph <= 4; iParagraph++) |
| 115 | + { |
| 116 | + var imageUrlResult = await GenerateUrlImage(newConference.ConferenceTalk, iParagraph); |
| 117 | + if (imageUrlResult != null && imageUrlResult.Value != null) |
| 118 | + { |
| 119 | + newConference.imagesUrl.Add(imageUrlResult.Value); |
| 120 | + } |
| 121 | + } |
| 122 | + return JsonConvert.SerializeObject(newConference); |
| 123 | + } |
| 124 | + catch(Exception ex) |
| 125 | + { |
| 126 | + return BadRequest(ex.Message); |
| 127 | + } |
| 128 | + } |
| 129 | + else { return BadRequest("GenerateText failed"); } |
| 130 | + } |
| 131 | + catch (Exception ex) |
| 132 | + { |
| 133 | + return BadRequest(ex.Message); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + } |
| 139 | +} |
0 commit comments