Skip to content

Commit

Permalink
Merge pull request #175 from Azure-Samples/gk/fix-nextjs-docker-issue
Browse files Browse the repository at this point in the history
  • Loading branch information
thegovind committed Nov 7, 2023
2 parents 8fe0e27 + b8c4ae4 commit fda1eba
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task<IActionResult> GetRecommendations([FromBody] MiyagiContext miy
sw.Start();
// ========= Import semantic functions as plugins =========
log.LogDebug("Path: {S}", Directory.GetCurrentDirectory());
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Plugins");
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "plugins");
var advisorPlugin = _kernel.ImportSemanticFunctionsFromDirectory(pluginsDirectory, "AdvisorPlugin");

// ========= Import native function =========
Expand Down Expand Up @@ -95,7 +95,7 @@ public async Task<IActionResult> GetRecommendations([FromBody] MiyagiContext miy
public async Task<IActionResult> GetRecommendationsRunAsync([FromBody] MiyagiContext miyagiContext)
{
// ========= Import semantic functions as plugins =========
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Plugins");
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "plugins");
var advisorPlugin = _kernel.ImportSemanticFunctionsFromDirectory(pluginsDirectory, "AdvisorPlugin");

// ========= Import native function =========
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task<IActionResult> GetRecommendationsWithPlanner([FromBody] Miyagi
log.BeginScope("InvestmentController.GetRecommendationsAsync");
// ========= Import Advisor skill from local filesystem =========
log.LogDebug("Path: {P}", Directory.GetCurrentDirectory());
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Plugins");
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "plugins");
_kernel.ImportSemanticFunctionsFromDirectory(pluginsDirectory, "AdvisorPlugin");
_kernel.ImportFunctions(new UserProfilePlugin(), "UserProfilePlugin");
var memoryCollection = _kernelSettings.CollectionName;
Expand Down Expand Up @@ -105,7 +105,7 @@ public async Task<IActionResult> GetRecommendations([FromBody] MiyagiContext miy
log.BeginScope("InvestmentController.GetRecommendationsAsync");
// ========= Import Advisor skill from local filesystem =========
log.LogDebug("Path: {P}", Directory.GetCurrentDirectory());
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Plugins");
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "plugins");
var advisorPlugin = _kernel.ImportSemanticFunctionsFromDirectory(pluginsDirectory, "AdvisorPlugin");
var userProfilePlugin = _kernel.ImportFunctions(new UserProfilePlugin(), "UserProfilePlugin");
var memoryCollection = _kernelSettings.CollectionName;
Expand Down
16 changes: 15 additions & 1 deletion services/recommendation-service/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,29 @@
// Add Semantic Kernel services
builder.Services.AddSkServices();

// Configure CORS to allow specific origins from KernelSettings
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "MiyagiAllowSpecificOrigins",
policy =>
{
policy.WithOrigins(kernelSettings.CorsAllowedOrigins)
.AllowAnyHeader()
.AllowAnyMethod();
});
});

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();

app.UseCors();
app.UseCors("MiyagiAllowSpecificOrigins");
// app.UseHttpsRedirection(); // Issue with Next.js to use https redirection

app.UseRouting();

app.Map("/", () => Results.Redirect("/swagger"));

// app.UseAuthorization();
Expand Down
4 changes: 2 additions & 2 deletions services/recommendation-service/dotnet/Utils/RepoUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace GBB.Miyagi.RecommendationService.Utils;
public static class RepoUtils
{
/// <summary>
/// Scan the local folders from the repo, looking for "Plugins" folder.
/// Scan the local folders from the repo, looking for "plugins" folder.
/// </summary>
/// <returns>The full path to samples/skills</returns>
public static string GetSamplePluginsPath()
{
const string Parent = "";
const string Folder = "Plugins";
const string Folder = "plugins";

bool SearchPath(string pathToFind, out string result, int maxAttempts = 10)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ internal class KernelSettings
[JsonPropertyName("cosmosDbConnectionString")]
public string CosmosDbConnectionString { get; set; } = string.Empty;

[JsonPropertyName("corsAllowedOrigins")]
public string[] CorsAllowedOrigins { get; set; } = Array.Empty<string>();


/// <summary>
/// Load the kernel settings from settings.json if the file exists and if not attempt to use user secrets.
Expand Down
2 changes: 1 addition & 1 deletion ui/typescript/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-DC9QMEZBZL
RECCOMMENDATION_SERVICE_URL=http://localhost:5224
NEXT_PUBLIC_RECCOMMENDATION_SERVICE_URL=http://localhost:5224
NEXT_PUBLIC_COPILOT_CHAT_BASE_URL=https://copilotdemo.app
2 changes: 1 addition & 1 deletion ui/typescript/.env.local.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NEXT_PUBLIC_GA_MEASUREMENT_ID=
RECCOMMENDATION_SERVICE_URL=
NEXT_PUBLIC_RECCOMMENDATION_SERVICE_URL=
NEXT_PUBLIC_CHAT_ID=
COPILOT_CHAT_BASE_URL=
NEXT_PUBLIC_COPILOT_CHAT_BASE_URL=
Expand Down
4 changes: 2 additions & 2 deletions ui/typescript/app/chat-session/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { NextResponse } from 'next/server';
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const userId = searchParams.get('userId');
const RECCOMMENDATION_SERVICE_URL = `${process.env.NEXT_PUBLIC_COPILOT_CHAT_BASE_URL}/chats`
const chatsUrl = `${process.env.NEXT_PUBLIC_COPILOT_CHAT_BASE_URL}/chats`
console.log("Request body: ");
console.dir(userId)
const res = await fetch(RECCOMMENDATION_SERVICE_URL, {
const res = await fetch(chatsUrl, {
method: 'POST',
headers: {
'Content-type': `application/json`
Expand Down
4 changes: 2 additions & 2 deletions ui/typescript/app/chatSession/getAllChats/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { NextResponse } from 'next/server';
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const userId = searchParams.get('userId');
const RECCOMMENDATION_SERVICE_URL = `${process.env.NEXT_PUBLIC_COPILOT_CHAT_BASE_URL}/chats`
const chatsUrl = `${process.env.NEXT_PUBLIC_COPILOT_CHAT_BASE_URL}/chats`
console.log("Request body: ");
console.dir(userId)
const res = await fetch(RECCOMMENDATION_SERVICE_URL, {
const res = await fetch(chatsUrl, {
method: 'POST',
headers: {
'Content-type': `application/json`
Expand Down
4 changes: 2 additions & 2 deletions ui/typescript/app/personalize/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { NextResponse } from 'next/server';

export async function POST(request: Request) {
console.dir(request);
const RECCOMMENDATION_SERVICE_URL = `${process.env.RECCOMMENDATION_SERVICE_URL}/personalize`
const personalizeUrl = `${process.env.NEXT_PUBLIC_RECCOMMENDATION_SERVICE_URL}/personalize`
const { body } = request
console.log("Request body: ");
console.dir(body)
const res = await fetch(RECCOMMENDATION_SERVICE_URL, {
const res = await fetch(personalizeUrl, {
method: 'POST',
headers: { 'Content-type': `application/json` },
duplex: 'half',
Expand Down
24 changes: 20 additions & 4 deletions ui/typescript/src/components/personalize/personalize-drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,33 @@ export default function PersonalizeDrawer() {
stocks,
};

const response = await personalizeMutation.mutateAsync(requestData);
console.log('Successfully got personalization');
const response = await fetch(`${process.env.NEXT_PUBLIC_RECCOMMENDATION_SERVICE_URL}/personalize`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestData),
});

console.dir(response);

if (!response.ok) {
console.error('HTTP error: ', response);
toast.error('Failed to fetch personalization. Try again later.');
}

const responseData = await response.json();
console.log('Successfully got personalization');
console.dir(responseData);
toast.success('Personalization successful');

// Extract the relevant data from the response
const updatedAssetData = response.assets.portfolio.map((item, index) => ({
const updatedAssetData = responseData.assets.portfolio.map((item, index) => ({
...assetsInfo[index],
gptRecommendation: item.gptRecommendation,
}));

const updatedInvestmentData = response.investments.portfolio.map((item, index) => ({
const updatedInvestmentData = responseData.investments.portfolio.map((item, index) => ({
...investmentsInfo[index],
gptRecommendation: item.gptRecommendation,
}));
Expand All @@ -107,6 +122,7 @@ export default function PersonalizeDrawer() {
}
};


return (
<Transition appear show={isPersonalizeOpen} as={Fragment}>
<Dialog
Expand Down

0 comments on commit fda1eba

Please sign in to comment.