Skip to content

Commit

Permalink
Merge pull request #44 from Azure-Samples/gk/41-personalize-on-frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
thegovind committed May 4, 2023
2 parents 9ebf696 + cb98b6f commit 196a8ff
Show file tree
Hide file tree
Showing 18 changed files with 348 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function PersonalizeDrawer() {
const { isPersonalizeOpen, closePersonalize } = usePersonalizeDrawer();
const personalizeMutation = usePersonalize();
const [, setFetchedData] = useAtom(investmentsDataAtom);
const [, setLoading] = useState(false);
const [loading, setLoading] = useState(false);

const [userInfo] = useAtom(userInfoAtom);
const [investmentsInfo, setInvestmentsDataAtom] = useAtom(investmentsDataAtom);
Expand Down Expand Up @@ -152,10 +152,36 @@ export default function PersonalizeDrawer() {
size="large"
shape="rounded"
fullWidth={true}
className="mx-auto mt-8 text-lg bg-indigo-500"
className={`mx-auto mt-8 text-lg bg-indigo-500 ${loading ? 'opacity-50' : ''}`}
onClick={handlePersonalize}
disabled={loading}
>
Personalize
{loading ? (
<>
<svg
className="animate-spin h-5 w-5 mr-3 inline-block"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
></circle>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l1-1.647z"
></path>
</svg>
Semantic Kernel processing w/ AZ OpenAI...
</>
) : (
'Personalize'
)}
</Button>
<div className="mt-8 mx-auto px-12">
<BingNews className="mt-8"/>
Expand Down
95 changes: 46 additions & 49 deletions services/recommendation-service/Controllers/AssetsController.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,59 @@
using System.Text.Json;
using GBB.Miyagi.RecommendationService.Models;
using GBB.Miyagi.RecommendationService.Skills;
using Microsoft.AspNetCore.Mvc;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.KernelExtensions;
using Microsoft.SemanticKernel.Orchestration;
using System.Text.Json;
using GBB.Miyagi.RecommendationService.Skills;

namespace GBB.Miyagi.RecommendationService.Controllers
namespace GBB.Miyagi.RecommendationService.Controllers;

[ApiController]
[Route("recommendations")]
public class AssetsController : ControllerBase
{
[ApiController]
[Route("recommendations")]
public class AssetsController : ControllerBase
private readonly IKernel _kernel;

public AssetsController(IKernel kernel)
{
private readonly IKernel _kernel;
_kernel = kernel;
}

public AssetsController(IKernel kernel)
{
_kernel = kernel;
}
[HttpPost("/assets")]
public async Task<IActionResult> GetRecommendations([FromBody] MiyagiContext miyagiContext)
{
var skillsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Skills");
var advisorSkill = _kernel.ImportSemanticSkillFromDirectory(skillsDirectory, "AdvisorSkill");

[HttpPost("/assets")]
public async Task<IActionResult> GetRecommendations([FromBody] MiyagiContext miyagiContext)
{
var userProfileSkill = _kernel.ImportSkill(new UserProfileSkill(), "UserProfileSkill");

var context = new ContextVariables();
context.Set("userId", miyagiContext.UserInfo.UserId);
context.Set("portfolio", JsonSerializer.Serialize(miyagiContext.Portfolio));
context.Set("risk", miyagiContext.UserInfo.RiskLevel);

_kernel.Log.LogDebug("Context: {0}", context.ToString());

var result = await _kernel.RunAsync(
context,
userProfileSkill["GetUserAge"],
userProfileSkill["GetAnnualHouseholdIncome"],
advisorSkill["PortfolioAllocation"]);

var skillsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Skills");
var advisorSkill = _kernel.ImportSemanticSkillFromDirectory(skillsDirectory, "AdvisorSkill");

var userProfileSkill = _kernel.ImportSkill(new UserProfileSkill(), "UserProfileSkill");

var context = new ContextVariables();
context.Set("userId", miyagiContext.UserInfo.UserId);
context.Set("portfolio", JsonSerializer.Serialize(miyagiContext.Portfolio));
context.Set("risk", miyagiContext.UserInfo.RiskLevel);

_kernel.Log.LogDebug("Context: {0}", context.ToString());

var result = await _kernel.RunAsync(
context,
userProfileSkill["GetUserAge"],
userProfileSkill["GetAnnualHouseholdIncome"],
advisorSkill["PortfolioAllocation"]);

_kernel.Log.LogDebug("Result: {0}", result.Result);

var output = result.Result.Replace("\n", "").Replace("\r", "").Replace(" ", "");

/*// Deserialize the result JSON string into a PortfolioRecommendations object
var recommendations = JsonConvert.DeserializeObject<PortfolioRecommendations>(result.Result);
// You can now access and manipulate the recommendations object as needed
// For example, you can print the name and recommendation for each asset in the portfolio
foreach (var asset in recommendations.Portfolio)
{
Console.WriteLine($"Name: {asset.Name}, GPT Recommendation: {asset.GptRecommendation}");
}*/

return Content(output, "application/json");
}
_kernel.Log.LogDebug("Result: {0}", result.Result);

var output = result.Result.Replace("\n", "").Replace("\r", "").Replace(" ", "");

/*// Deserialize the result JSON string into a PortfolioRecommendations object
var recommendations = JsonConvert.DeserializeObject<PortfolioRecommendations>(result.Result);
// You can now access and manipulate the recommendations object as needed
// For example, you can print the name and recommendation for each asset in the portfolio
foreach (var asset in recommendations.Portfolio)
{
Console.WriteLine($"Name: {asset.Name}, GPT Recommendation: {asset.GptRecommendation}");
}*/

return Content(output, "application/json");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,66 +1,61 @@
using System.Text.Json;
using GBB.Miyagi.RecommendationService.Models;
using GBB.Miyagi.RecommendationService.Skills;
using Microsoft.AspNetCore.Mvc;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.KernelExtensions;
using Microsoft.SemanticKernel.Orchestration;
using System.Text.Json;
using GBB.Miyagi.RecommendationService.Skills;
using GBB.Miyagi.RecommendationService.Utils;
using Microsoft.SemanticKernel.Skills.Web;
using Microsoft.SemanticKernel.Skills.Web.Bing;

namespace GBB.Miyagi.RecommendationService.Controllers
namespace GBB.Miyagi.RecommendationService.Controllers;

[ApiController]
[Route("recommendations")]
public class InvestmentsController : ControllerBase
{
[ApiController]
[Route("recommendations")]
public class InvestmentsController : ControllerBase
private readonly IKernel _kernel;
private readonly WebSearchEngineSkill _webSearchEngineSkill;

public InvestmentsController(IKernel kernel, WebSearchEngineSkill webSearchEngineSkill)
{
private readonly IKernel _kernel;
private readonly WebSearchEngineSkill _webSearchEngineSkill;
_kernel = kernel;
_webSearchEngineSkill = webSearchEngineSkill;
}

[HttpPost("/investments")]
public async Task<IActionResult> GetRecommendations([FromBody] MiyagiContext miyagiContext)
{
var skillsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Skills");
var advisorSkill = _kernel.ImportSemanticSkillFromDirectory(skillsDirectory, "AdvisorSkill");

var userProfileSkill = _kernel.ImportSkill(new UserProfileSkill(), "UserProfileSkill");

var context = new ContextVariables();
context.Set("userId", miyagiContext.UserInfo.UserId);
context.Set("stocks", JsonSerializer.Serialize(miyagiContext.Stocks));
context.Set("voice", miyagiContext.UserInfo.FavoriteAdvisor);
context.Set("risk", miyagiContext.UserInfo.RiskLevel);

_kernel.Log.LogDebug("Context: {0}", context.ToString());

public InvestmentsController(IKernel kernel, WebSearchEngineSkill webSearchEngineSkill)
{
_kernel = kernel;
_webSearchEngineSkill = webSearchEngineSkill;
}
var result = await _kernel.RunAsync(
context,
userProfileSkill["GetUserAge"],
userProfileSkill["GetAnnualHouseholdIncome"],
advisorSkill["InvestmentAdvise"]);
_kernel.Log.LogDebug("Result: {0}", result.Result);

[HttpPost("/investments")]
public async Task<IActionResult> GetRecommendations([FromBody] MiyagiContext miyagiContext)
{
var output = result.Result.Replace("\n", "");

var skillsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Skills");
var advisorSkill = _kernel.ImportSemanticSkillFromDirectory(skillsDirectory, "AdvisorSkill");

var userProfileSkill = _kernel.ImportSkill(new UserProfileSkill(), "UserProfileSkill");

var context = new ContextVariables();
context.Set("userId", miyagiContext.UserInfo.UserId);
context.Set("stocks", JsonSerializer.Serialize(miyagiContext.Stocks));
context.Set("voice", miyagiContext.UserInfo.FavoriteAdvisor);
context.Set("risk", miyagiContext.UserInfo.RiskLevel);

_kernel.Log.LogDebug("Context: {0}", context.ToString());

var result = await _kernel.RunAsync(
context,
userProfileSkill["GetUserAge"],
userProfileSkill["GetAnnualHouseholdIncome"],
advisorSkill["InvestmentAdvise"]);
_kernel.Log.LogDebug("Result: {0}", result.Result);

var output = result.Result.Replace("\n", "");

// TODO: Load Bing News Search Skill.
var search = _kernel.ImportSkill(_webSearchEngineSkill, "bing");
// TODO: Load Bing News Search Skill.
var search = _kernel.ImportSkill(_webSearchEngineSkill, "bing");

var bingResult = await _kernel.RunAsync(
"MSFT news",
search["SearchAsync"]
);
_kernel.Log.LogDebug("Bing result: {0}", bingResult.Result);

return Content(output, "application/json");
}
var bingResult = await _kernel.RunAsync(
"MSFT news",
search["SearchAsync"]
);
_kernel.Log.LogDebug("Bing result: {0}", bingResult.Result);

return Content(output, "application/json");
}
}
}
Loading

0 comments on commit 196a8ff

Please sign in to comment.