Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
heblasco committed Nov 13, 2023
1 parent e9d7dc3 commit e2d4de7
Show file tree
Hide file tree
Showing 101 changed files with 76,941 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/AIHub/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ignore the appsettings.json file
appsettings.json
#ignore the publish folder
publish/
# ignore the packages folder
!**/packages/*
# ignore the .vscode folder
!.vscode/
# ignore Debug and subfolders
**/Debug/*
# ignore Release and subfolders
**/Release/*
# ignore obj folders
obj/*
# ignore .sln file
*.sln
17 changes: 17 additions & 0 deletions src/AIHub/AIHub.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.ContentSafety" Version="1.0.0-beta.1" />
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.8" />
<PackageReference Include="Azure.Identity" Version="1.10.3" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.18.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
152 changes: 152 additions & 0 deletions src/AIHub/Controllers/BrandAnalyzerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using MVCWeb.Models;
using Azure.AI.ContentSafety;
using Azure;
using ContentSafetySampleCode;
using System;
using Azure.Storage.Blobs;
using Azure.Identity;
using Azure.AI.OpenAI;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

namespace MVCWeb.Controllers;

public class BrandAnalyzerController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration _config;
private BrandAnalyzerModel model;
private string Bingendpoint;
private string BingsubscriptionKey;
private string AOAIendpoint;
private string AOAIsubscriptionKey;
private string storageconnstring;


public BrandAnalyzerController(IConfiguration config)
{
_config = config;
Bingendpoint= _config.GetValue<string>("BrandAnalyzer:BingEndpoint");
BingsubscriptionKey= _config.GetValue<string>("BrandAnalyzer:BingKey");
AOAIendpoint= _config.GetValue<string>("BrandAnalyzer:OpenAIEndpoint");
AOAIsubscriptionKey= _config.GetValue<string>("BrandAnalyzer:OpenAISubscriptionKey");
model = new BrandAnalyzerModel();

}

public IActionResult BrandAnalyzer()
{
return View();
}

[HttpPost]
public async Task<IActionResult> AnalyzeCompany()
{

model.CompanyName = HttpContext.Request.Form["companyName"];
model.Prompt = HttpContext.Request.Form["prompt"];
string input_context="";

if (CheckNullValues(model.CompanyName))
{
ViewBag.Message = "You must enter a value for Company name";
return View("BrandAnalyzer");
}

string query_bing = model.CompanyName+" opiniones de usuarios";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Bingendpoint);

// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", BingsubscriptionKey);
string uri=Bingendpoint+"?q="+query_bing+"&mkt=es-ES&count=100";
// List data response.
HttpResponseMessage response = client.GetAsync(uri).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs.
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);

response.EnsureSuccessStatusCode();

// Parse the response as JSON
try
{
var responsejson = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());

// Get the web pages from the response
var news = responsejson.webPages.value;
// Iterate over the news items and print them
foreach (var i in news)
{
input_context=input_context+i.name+"\n"+i.snippet+"\n"+i.url+"\n"+"-------";
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

client.Dispose();

try
{

OpenAIClient client_oai = new OpenAIClient(
new Uri(AOAIendpoint),
new AzureKeyCredential(AOAIsubscriptionKey));

// ### If streaming is not selected
Response<ChatCompletions> responseWithoutStream = await client_oai.GetChatCompletionsAsync(
"DemoBuild",
new ChatCompletionsOptions()
{
Messages =
{
new ChatMessage(ChatRole.System, @"I will provide a list results from opinons on the internet about "+model.CompanyName+" Bing search. If "+model.CompanyName+" is not a company what the user is asking for, answer to provide a new Company name. The user will ask you what they want to get from the compay opinions. \n Results: "+ input_context),
new ChatMessage(ChatRole.User, model.Prompt),
},
Temperature = (float)0.7,
MaxTokens = 1000,
NucleusSamplingFactor = (float)0.95,
FrequencyPenalty = 0,
PresencePenalty = 0,
});

ChatCompletions completions = responseWithoutStream.Value;
ChatChoice results_analisis= completions.Choices[0];
ViewBag.Message =
//"Hate severity: " + (response.Value.HateResult?.Severity ?? 0);
results_analisis.Message.Content
;
}
catch (RequestFailedException ex)
{
throw;
}

return View("BrandAnalyzer", model);
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

private bool CheckNullValues(string companyName)
{
if (string.IsNullOrEmpty(companyName))
{
return true;
}
return false;
}
}
111 changes: 111 additions & 0 deletions src/AIHub/Controllers/CallCenterController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using MVCWeb.Models;
using Azure.AI.ContentSafety;
using Azure;
using ContentSafetySampleCode;
using System;
using Azure.Storage.Blobs;
using Azure.Identity;
using Azure.AI.OpenAI;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

namespace MVCWeb.Controllers;

public class CallCenterController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration _config;
private CallCenterModel model;
private string endpoint;
private string subscriptionKey;
private string storageconnstring;


public CallCenterController(IConfiguration config)
{
_config = config;
endpoint= _config.GetValue<string>("CallCenter:OpenAIEndpoint");
subscriptionKey= _config.GetValue<string>("CallCenter:OpenAISubscriptionKey");
model = new CallCenterModel();

}

public IActionResult CallCenter()
{
return View();
}

[HttpPost]
public async Task<IActionResult> AnalyzeCall()
{
model.Text = HttpContext.Request.Form["text"];
model.Prompt = HttpContext.Request.Form["prompt"];

if (CheckNullValues(model.Text, model.Prompt))
{
ViewBag.Message = "You must enter both a transcript and a prompt";
return View("CallCenter", model);
}
try
{

OpenAIClient client_oai = new OpenAIClient(
new Uri(endpoint),
new AzureKeyCredential(subscriptionKey));

// ### If streaming is not selected
Response<ChatCompletions> responseWithoutStream = await client_oai.GetChatCompletionsAsync(
"DemoBuild",
new ChatCompletionsOptions()
{
Messages =
{
new ChatMessage(ChatRole.System, model.Prompt),
new ChatMessage(ChatRole.User, @"Call transcript: "+model.Text),
},
Temperature = (float)0.1,
MaxTokens = 1000,
NucleusSamplingFactor = (float)0.95,
FrequencyPenalty = 0,
PresencePenalty = 0,
});

ChatCompletions completions = responseWithoutStream.Value;
ChatChoice results_analisis= completions.Choices[0];
System.Console.WriteLine(results_analisis);
ViewBag.Message =
results_analisis.Message.Content
;
}
catch (RequestFailedException ex)
{
throw;
}

return View("CallCenter", model);
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

private bool CheckNullValues(string companyName, string prompt)
{
if (string.IsNullOrEmpty(companyName))
{
return true;
}
if (string.IsNullOrEmpty(prompt))
{
return true;
}
return false;
}
}
Loading

0 comments on commit e2d4de7

Please sign in to comment.