-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from Azure-Samples/gk/41-personalize-on-frontend
- Loading branch information
Showing
18 changed files
with
348 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 46 additions & 49 deletions
95
services/recommendation-service/Controllers/AssetsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
97 changes: 46 additions & 51 deletions
97
services/recommendation-service/Controllers/InvestmentsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
Oops, something went wrong.