Skip to content

Commit

Permalink
add stepwise planner to v2 of stock recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
thegovind committed Jul 24, 2023
1 parent 8cd5181 commit fc10881
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Text.Json;
using GBB.Miyagi.RecommendationService.models;
using GBB.Miyagi.RecommendationService.plugins;
Expand All @@ -6,8 +7,10 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.Tokenizers;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Planning;
using Microsoft.SemanticKernel.Skills.Core;
using Microsoft.SemanticKernel.Skills.Web;
using Microsoft.SemanticKernel.Skills.Web.Bing;

namespace GBB.Miyagi.RecommendationService.Controllers;

Expand All @@ -30,11 +33,87 @@ public InvestmentsController(IKernel kernel, WebSearchEngineSkill webSearchEngin
_webSearchEngineSkill = webSearchEngineSkill;
}


[HttpPost("/v2/investments")]
public async Task<IActionResult> GetRecommendationsWithPlanner([FromBody] MiyagiContext miyagiContext)
{
var log = ConsoleLogger.Log;
log?.BeginScope("InvestmentController.GetRecommendationsAsync");
// ========= Import Advisor skill from local filesystem =========
log?.LogDebug("Path: {P}", Directory.GetCurrentDirectory());
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "plugins");
_kernel.ImportSemanticSkillFromDirectory(pluginsDirectory, "AdvisorPlugin");
_kernel.ImportSkill(new UserProfilePlugin(), "UserProfilePlugin");

// ========= Fetch memory from vector store using recall =========
_kernel.ImportSkill(new TextMemorySkill());

// ========= Set context variables for Advisor skill =========
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);
context.Set("semanticQuery", $"Investment advise for {miyagiContext.UserInfo.RiskLevel} risk level");

context[TextMemorySkill.CollectionParam] = _memoryCollection;
//context[TextMemorySkill.KeyParam] = miyagiContext.UserInfo.FavoriteBook;
context[TextMemorySkill.RelevanceParam] = "0.8";
context[TextMemorySkill.LimitParam] = "3";
context.Set("tickers", miyagiContext.Stocks?.Select(s => s.Symbol).ToList().ToString());

// ========= Log token count, which determines cost =========
var numTokens = GPT3Tokenizer.Encode(context.ToString()).Count;
log?.LogDebug("Number of Tokens: {N}", numTokens);
log?.LogDebug("Context: {S}", context.ToString());

// ========= Import Bing web search skill to augment current info =========
_kernel.ImportSkill(_webSearchEngineSkill, "WebSearch");
_kernel.ImportSkill(new TimeSkill(), "time");

var ask = "Given stocks, return investment advise, factoring current inflation from search?";

Console.WriteLine("*****************************************************");
Stopwatch sw = new();
Console.WriteLine("Question: " + ask);

var plannerConfig = new Microsoft.SemanticKernel.Planning.Stepwise.StepwisePlannerConfig();
plannerConfig.ExcludedFunctions.Add("PortfolioAllocation");
plannerConfig.MinIterationTimeMs = 1500;
plannerConfig.MaxTokens = 3000;

StepwisePlanner planner = new(_kernel, plannerConfig);
sw.Start();
var plan = planner.CreatePlan(ask);

var result = await plan.InvokeAsync(_kernel.CreateNewContext());
Console.WriteLine("Result: " + result);
if (result.Variables.TryGetValue("stepCount", out string? stepCount))
{
Console.WriteLine("Steps Taken: " + stepCount);
}

if (result.Variables.TryGetValue("skillCount", out string? skillCount))
{
Console.WriteLine("Skills Used: " + skillCount);
}

Console.WriteLine("Time Taken: " + sw.Elapsed);
Console.WriteLine("*****************************************************");

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

var jsonDocument = JsonDocument.Parse(output);

return new JsonResult(jsonDocument);

}

[HttpPost("/investments")]
public async Task<IActionResult> GetRecommendations([FromBody] MiyagiContext miyagiContext)
{
var log = ConsoleLogger.Log;
log?.BeginScope("MemoryController.SaveDatasetAsync");
log?.BeginScope("InvestmentController.GetRecommendationsAsync");
// ========= Import Advisor skill from local filesystem =========
log?.LogDebug("Path: {P}", Directory.GetCurrentDirectory());
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "plugins");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schema": 1,
"type": "completion",
"description": "Gives financial advise on how to allocate portfolio, given a risk tolerance and a set of assets",
"description": "Gives financial advise on how to allocate portfolio, given a risk tolerance and a set of stocks",
"completion": {
"max_tokens": 512,
"temperature": 0.9,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schema": 1,
"type": "completion",
"description": "Gives financial advise on how to allocate portfolio, given a risk tolerance and a set of assets",
"description": "Gives financial advise on how to allocate assets, given a risk tolerance",
"completion": {
"max_tokens": 512,
"temperature": 0.8,
Expand Down

0 comments on commit fc10881

Please sign in to comment.