Skip to content

Commit

Permalink
Update OpenAI endpoint in main.tf and ImageAnalyzerController.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
cmendible committed Apr 11, 2024
1 parent 77944d1 commit 736aeac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 64 deletions.
2 changes: 1 addition & 1 deletion infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
122 changes: 59 additions & 63 deletions src/AIHub/Controllers/ImageAnalyzerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,76 +46,72 @@ public async Task<IActionResult> 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<dynamic>(await response.Content.ReadAsStringAsync());

if (response.IsSuccessStatusCode)
{
var responseData = JsonConvert.DeserializeObject<dynamic>(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
Expand Down

0 comments on commit 736aeac

Please sign in to comment.