diff --git a/infra/main.tf b/infra/main.tf index 205a873..c39e4f1 100644 --- a/infra/main.tf +++ b/infra/main.tf @@ -213,7 +213,7 @@ module "ca_aihub" { storage_container_name = module.st.storage_container_name search_service_name = module.search.search_service_name search_index_name = module.search.search_index_name - openai_endpoint = var.enable_apim ? module.apim[0].gateway_url : module.openai.openai_endpoint + openai_endpoint = var.enable_apim ? "${module.apim[0].gateway_url}/" : module.openai.openai_endpoint chat_fqdn = module.ca_chat.fqdn pbi_report_link = var.pbi_report_link content_safety_endpoint = module.cog.content_safety_endpoint diff --git a/src/AIHub/Controllers/ImageAnalyzerController.cs b/src/AIHub/Controllers/ImageAnalyzerController.cs index f2780b8..2f789a7 100644 --- a/src/AIHub/Controllers/ImageAnalyzerController.cs +++ b/src/AIHub/Controllers/ImageAnalyzerController.cs @@ -46,76 +46,72 @@ public async Task DenseCaptionImage(string image_url, string prom string GPT4V_ENDPOINT = $"{AOAIendpoint}openai/deployments/{AOAIDeploymentName}/extensions/chat/completions?api-version=2023-07-01-preview"; image_url = image_url + sasUri.Query; - using (httpClient = new HttpClient()) + if (string.IsNullOrEmpty(AOAIsubscriptionKey)) { - if (string.IsNullOrEmpty(AOAIsubscriptionKey)) - { - var credential = new DefaultAzureCredential(); - httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", credential.GetToken(new TokenRequestContext(["https://cognitiveservices.azure.com/.default"])).Token); - } - else + var credential = new DefaultAzureCredential(); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", credential.GetToken(new TokenRequestContext(["https://cognitiveservices.azure.com/.default"])).Token); + } + else + { + httpClient.DefaultRequestHeaders.Add("api-key", AOAIsubscriptionKey); + } + var payload = new + { + enhancements = new { - httpClient.DefaultRequestHeaders.Add("api-key", AOAIsubscriptionKey); - } - var payload = new + ocr = new { enabled = true }, + grounding = new { enabled = true } + }, + messages = new object[] { - enhancements = new - { - ocr = new { enabled = true }, - grounding = new { enabled = true } - }, - messages = new object[] - { - new { - role = "system", - content = new object[] { - new { - type = "text", - text = "You are an AI assistant that helps people find information." - } - } - }, - new { - role = "user", - content = new object[] { - new { - type = "image_url", - image_url = new { - url = image_url - } - }, - new { - type = "text", - text = prompt - } - } - } + new { + role = "system", + content = new object[] { + new { + type = "text", + text = "You are an AI assistant that helps people find information." + } + } }, - temperature = 0.7, - top_p = 0.95, - max_tokens = 800, - stream = false - }; - var response = await httpClient.PostAsync(GPT4V_ENDPOINT, new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")); - + new { + role = "user", + content = new object[] { + new { + type = "image_url", + image_url = new { + url = image_url + } + }, + new { + type = "text", + text = prompt + } + } + } + }, + temperature = 0.7, + top_p = 0.95, + max_tokens = 800, + stream = false + }; + var response = await httpClient.PostAsync(GPT4V_ENDPOINT, new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")); + + if (response.IsSuccessStatusCode) + { + var responseData = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); - if (response.IsSuccessStatusCode) - { - var responseData = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); - - // Get the web pages from the response - var response_final = responseData!.choices[0]; - string final = response_final.message.content; - model.Message = final; - model.Image = image_url; - } - else - { - Console.WriteLine($"Error after GPT4V: {response.StatusCode}, {response.ReasonPhrase}"); - } + // Get the web pages from the response + var response_final = responseData!.choices[0]; + string final = response_final.message.content; + model.Message = final; + model.Image = image_url; + } + else + { + Console.WriteLine($"Error after GPT4V: {response.StatusCode}, {response.ReasonPhrase}"); } - return View("ImageAnalyzer"); + return View("ImageAnalyzer", model); } // Upload a file to my azure storage account