-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b88805b
commit 246280d
Showing
38 changed files
with
1,269 additions
and
132 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Import necessary namespaces from the ASP.NET Core framework | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.SemanticKernel; | ||
using Newtonsoft.Json; | ||
|
||
// Declare the namespace for the AbstractNodeController | ||
namespace SK_API.Controllers{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public partial class AbstractNodeController : ControllerBase | ||
{ | ||
private readonly ILogger<AbstractNodeController> _logger; | ||
private readonly IConfiguration _configuration; | ||
private readonly Auth _auth; | ||
|
||
public AbstractNodeController(ILogger<AbstractNodeController> logger, IConfiguration configuration, Auth auth) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); | ||
_auth = auth; | ||
} | ||
|
||
// Define your Lesson POST action method here | ||
[HttpPost("abstractnode")] | ||
public async Task<IActionResult> LessonPlannerInputAsync([FromHeader(Name = "ApiKey")] string token, [FromHeader(Name = "SetupModel")] string setupModel, [FromBody] AbstractNodeRequestModel input){ | ||
string output = ""; | ||
try{ | ||
// Authentication with the token | ||
if (token == null) | ||
{ | ||
return Unauthorized("API token is required"); | ||
} | ||
else if (!_auth.Authenticate(token)){ | ||
return BadRequest("API token is required"); | ||
} | ||
else{ | ||
Console.WriteLine("Authenticated successfully"); | ||
} | ||
|
||
// Validate the setup model | ||
var LLMsetupModel = JsonConvert.DeserializeObject<LLM_SetupModel>(setupModel); | ||
LLM_SetupModel LLM = LLMsetupModel ?? throw new ArgumentNullException(nameof(setupModel)); | ||
IKernel kernel = LLM.Validate(); | ||
|
||
// Generate the output | ||
string prompt = PlanningPrompts.AbstractNodePrompt; | ||
var generate = kernel.CreateSemanticFunction(prompt, "abstractPlanner" ,"AbstractPlanner", "generates a tailored lesson plan", null, input.Temperature); | ||
var context = kernel.CreateNewContext(); | ||
context["language"] = input.Language; | ||
context["level"] = input.Level.ToString(); | ||
context["macro_subject"] = input.MacroSubject; | ||
context["title"] = input.Title; | ||
context["correction"] = input.Correction; | ||
|
||
context["format"] = FormatStrings.LessonPlanFormat; | ||
context["activities"] = UtilsStrings.ActivitiesListB; | ||
context["examples"] = ExamplesStrings.CustomPlanExamples; | ||
|
||
var result = await generate.InvokeAsync(context); | ||
output = result.ToString(); | ||
if (output.StartsWith("```json")){ | ||
output = output[7..]; | ||
if (output.EndsWith("```")){ | ||
output = output[..^3]; | ||
} | ||
output = output.TrimStart('\n').TrimEnd('\n'); | ||
output = output.TrimStart(' ').TrimEnd(' '); | ||
} | ||
Console.WriteLine("Result: "+ output); | ||
LessonPlan lessonPlan = new(output); | ||
foreach (var node in lessonPlan.Nodes){ | ||
if(node.Type){ | ||
//map the value into TypeOfExercise enum | ||
int exercise = (int)Enum.Parse<TypeOfExercise>(node.Details); | ||
node.Details = exercise.ToString(); | ||
} | ||
} | ||
string json = lessonPlan.ToJson(); | ||
|
||
if (input.Language.ToLower() != "english"){ | ||
var InternalFunctions = new InternalFunctions(); | ||
var translation = await InternalFunctions.Translate(kernel, json, input.Language); | ||
json = translation; | ||
} | ||
if (json.StartsWith("```json")){ | ||
json = json[7..]; | ||
if (json.EndsWith("```")){ | ||
json = json[..^3]; | ||
} | ||
// remove all nelines and tabulation | ||
json = json.Replace("\n", "").Replace("\t", "").Trim(); | ||
} | ||
return Ok(json.ToString()); | ||
} | ||
// Handle exceptions if something goes wrong during the material generation | ||
catch (Exception ex){ | ||
_logger.LogError(ex, "Error during abstract node generation"); | ||
return StatusCode(500, "Internal Server Error. \n" + output); | ||
} | ||
} | ||
|
||
} | ||
} |
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,88 @@ | ||
// Import necessary namespaces from the ASP.NET Core framework | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.SemanticKernel; | ||
using Newtonsoft.Json; | ||
|
||
// Declare the namespace for the CoursePlannerController | ||
namespace SK_API.Controllers{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public partial class CoursePlannerController : ControllerBase | ||
{ | ||
private readonly ILogger<CoursePlannerController> _logger; | ||
private readonly IConfiguration _configuration; | ||
private readonly Auth _auth; | ||
|
||
public CoursePlannerController(ILogger<CoursePlannerController> logger, IConfiguration configuration, Auth auth) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); | ||
_auth = auth; | ||
} | ||
|
||
// Define your Lesson POST action method here | ||
[HttpPost("plancourse")] | ||
public async Task<IActionResult> CoursePlannerInputAsync([FromHeader(Name = "ApiKey")] string token, [FromHeader(Name = "SetupModel")] string setupModel, [FromBody] CoursePlanRequestModel input){ | ||
string output = ""; | ||
try{ | ||
// Authentication with the token | ||
if (token == null) | ||
{ | ||
return Unauthorized("API token is required"); | ||
} | ||
else if (!_auth.Authenticate(token)){ | ||
return BadRequest("API token is required"); | ||
} | ||
else{ | ||
Console.WriteLine("Authenticated successfully"); | ||
} | ||
|
||
// Validate the setup model | ||
var LLMsetupModel = JsonConvert.DeserializeObject<LLM_SetupModel>(setupModel); | ||
LLM_SetupModel LLM = LLMsetupModel ?? throw new ArgumentNullException(nameof(setupModel)); | ||
IKernel kernel = LLM.Validate(); | ||
|
||
// Generate the output | ||
string prompt = PlanningPrompts.CoursePlanPrompt; | ||
var generate = kernel.CreateSemanticFunction(prompt, "coursePlanner" ,"CoursePlanner", "generates a course plan about the given topic", null, input.Temperature); | ||
var context = kernel.CreateNewContext(); | ||
context["language"] = input.Language; | ||
context["level"] = input.Level.ToString(); | ||
context["macro_subject"] = input.MacroSubject; | ||
context["topic"] = input.Topic; | ||
context["number_of_lessons"] = input.NumberOfLessons.ToString(); | ||
context["lesson_duration"] = input.LessonDuration.ToString(); | ||
context["last_lesson"] = ""; | ||
if(input.Title != "" && input.Title != null && input.Title != "null" && input.Title != " " && input.Title.ToLower() != "string") | ||
{ | ||
Console.WriteLine("Title: " + input.Title); | ||
string ll = @$"You already taught everything up to '{input.Title}', so you need to continue from there."; | ||
context["last_lesson"] = ll; | ||
} | ||
context["format"] = FormatStrings.CoursePlanFormat; | ||
context["example"] = ExamplesStrings.CoursePlanExamples; | ||
|
||
var result = await generate.InvokeAsync(context); | ||
output = result.ToString(); | ||
if (output.StartsWith("```json")){ | ||
output = output[7..]; | ||
if (output.EndsWith("```")){ | ||
output = output[..^3]; | ||
} | ||
output = output.TrimStart('\n').TrimEnd('\n'); | ||
output = output.TrimStart(' ').TrimEnd(' '); | ||
} | ||
Console.WriteLine("Result: "+ output); | ||
CoursePlan coursePlan = new(output); | ||
string json = coursePlan.ToJson(); | ||
return Ok(json.ToString()); | ||
} | ||
// Handle exceptions if something goes wrong during the material generation | ||
catch (Exception ex){ | ||
_logger.LogError(ex, "Error during course plan generation"); | ||
return StatusCode(500, "Internal Server Error. \n" + output); | ||
} | ||
} | ||
|
||
} | ||
} |
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
Empty file.
Oops, something went wrong.