-
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 #36 from Azure-Samples/gk/26-aggregate-miyagi-skil…
…ls-into-a-notebook
- Loading branch information
Showing
13 changed files
with
230 additions
and
21 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
74 changes: 56 additions & 18 deletions
74
services/recommendation-service/Controllers/RecommendationsController.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,33 +1,71 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using GBB.Miyagi.RecommendationService.Utils; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Connectors.Memory.Qdrant; | ||
using Microsoft.SemanticKernel.Skills.Web; | ||
|
||
namespace GBB.Miyagi.RecommendationService.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class RecommendationsController : ControllerBase | ||
{ | ||
private static readonly string[] Questions = new[] | ||
{ | ||
"Portfolio allocation", "Expenses reduction" | ||
}; | ||
|
||
private readonly ILogger<RecommendationsController> _logger; | ||
private readonly IKernel _kernel; | ||
private readonly QdrantMemoryStore _memoryStore; | ||
private readonly WebSearchEngineSkill _webSearchEngineSkill; | ||
|
||
public RecommendationsController(ILogger<RecommendationsController> logger) | ||
public RecommendationsController(IKernel kernel, QdrantMemoryStore memoryStore, WebSearchEngineSkill webSearchEngineSkill) | ||
{ | ||
_logger = logger; | ||
_kernel = kernel; | ||
_memoryStore = memoryStore; | ||
_webSearchEngineSkill = webSearchEngineSkill; | ||
} | ||
|
||
[HttpPost(Name = "GetRecommendations")] | ||
public IEnumerable<Response> Post() | ||
[HttpGet("GetRecommendations")] | ||
public async Task<IActionResult> GetRecommendations(string user, string portfolio) | ||
{ | ||
return Enumerable.Range(1, 5).Select(index => new Response | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
InteractionNum = Random.Shared.Next(-20, 55), | ||
Summary = "Todo: SK to LLM flow" | ||
}) | ||
.ToArray(); | ||
|
||
Console.WriteLine("== Printing Collections in DB =="); | ||
var collections = _memoryStore.GetCollectionsAsync(); | ||
await foreach (var collection in collections) | ||
{ | ||
Console.WriteLine(collection); | ||
} | ||
|
||
string memoryCollectionName = Env.Var("QDRANT_MEMORY_COLLECTION"); // Replace with your desired memory collection name | ||
string prompt = $"How should {user} allocate {portfolio}?"; | ||
|
||
// Get embeddings for the prompt | ||
|
||
// var embeddings = await _kernel.RunAsync() | ||
|
||
// Search Qdrant vector store | ||
var searchResults = _kernel.Memory.SearchAsync(memoryCollectionName, embeddings, limit: 3, minRelevanceScore: 0.8); | ||
var similarTexts = new List<string>(); | ||
|
||
await foreach (var item in searchResults) | ||
{ | ||
similarTexts.Add(item.Metadata.Text); | ||
} | ||
|
||
// Get latest finance news from Bing | ||
var newsResults = await _webSearchEngineSkill.SearchAsync($"latest finance news {portfolio}", count: 3); | ||
var newsTexts = new List<string>(); | ||
|
||
foreach (var newsItem in newsResults) | ||
{ | ||
newsTexts.Add(newsItem.Title); | ||
} | ||
|
||
// Build the new prompt with similar texts and news | ||
string newPrompt = $"{prompt}\n\nSimilar recommendations:\n{string.Join("\n", similarTexts)}\n\nLatest news:\n{string.Join("\n", newsTexts)}\n\n"; | ||
|
||
// Call text completion API | ||
var completionResult = await _kernel.RunAsync(newPrompt, _kernel.Skills.Get("text-davinci-003")); | ||
|
||
return Ok(new { Recommendation = completionResult }); | ||
} | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace GBB.Miyagi.RecommendationService.Models; | ||
|
||
public class Question | ||
{ | ||
public string input { get; set; } | ||
} |
2 changes: 1 addition & 1 deletion
2
services/recommendation-service/Response.cs → ...recommendation-service/Models/Response.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
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
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace GBB.Miyagi.RecommendationService.Utils; | ||
|
||
/// <summary> | ||
/// Basic logger printing to console | ||
/// </summary> | ||
internal static class ConsoleLogger | ||
{ | ||
internal static ILogger Log => LogFactory.CreateLogger<object>(); | ||
|
||
private static ILoggerFactory LogFactory => s_loggerFactory.Value; | ||
|
||
private static readonly Lazy<ILoggerFactory> s_loggerFactory = new(LogBuilder); | ||
|
||
private static ILoggerFactory LogBuilder() | ||
{ | ||
return LoggerFactory.Create(builder => | ||
{ | ||
builder.SetMinimumLevel(LogLevel.Warning); | ||
// builder.AddFilter("Microsoft", LogLevel.Trace); | ||
// builder.AddFilter("Microsoft", LogLevel.Debug); | ||
// builder.AddFilter("Microsoft", LogLevel.Information); | ||
// builder.AddFilter("Microsoft", LogLevel.Warning); | ||
// builder.AddFilter("Microsoft", LogLevel.Error); | ||
builder.AddFilter("Microsoft", LogLevel.Warning); | ||
builder.AddFilter("System", LogLevel.Warning); | ||
builder.AddConsole(); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace GBB.Miyagi.RecommendationService.Utils; | ||
|
||
#pragma warning disable CA1812 // instantiated by AddUserSecrets | ||
internal sealed class Env | ||
#pragma warning restore CA1812 | ||
{ | ||
/// <summary> | ||
/// Simple helper used to load env vars and secrets like credentials, | ||
/// to avoid hard coding them in the sample code | ||
/// </summary> | ||
/// <param name="name">Secret name / Env var name</param> | ||
/// <returns>Value found in Secret Manager or Environment Variable</returns> | ||
internal static string Var(string name) | ||
{ | ||
var configuration = new ConfigurationBuilder() | ||
.AddUserSecrets<Env>() | ||
.Build(); | ||
|
||
var value = configuration[name]; | ||
if (!string.IsNullOrEmpty(value)) | ||
{ | ||
return value; | ||
} | ||
|
||
value = Environment.GetEnvironmentVariable(name); | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
throw new MiyagiException($"Secret / Env var not set: {name}"); | ||
} | ||
|
||
return value; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace GBB.Miyagi.RecommendationService.Utils; | ||
|
||
public class MiyagiException : Exception | ||
{ | ||
public MiyagiException() : base() | ||
{ | ||
} | ||
|
||
public MiyagiException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public MiyagiException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
} |
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
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