From 27d7c3f5b43f2f99fcd902a1ad8cdd3f48d9a3a9 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Sat, 16 Mar 2024 20:26:22 +0800 Subject: [PATCH] Add support for rendering human answers --- MyApp.ServiceInterface/Data/AppConfig.cs | 2 +- MyApp.ServiceInterface/Data/R2Extensions.cs | 23 ++++++++++++++++++++- MyApp/Configure.AppHost.cs | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/MyApp.ServiceInterface/Data/AppConfig.cs b/MyApp.ServiceInterface/Data/AppConfig.cs index e339020..94acc72 100644 --- a/MyApp.ServiceInterface/Data/AppConfig.cs +++ b/MyApp.ServiceInterface/Data/AppConfig.cs @@ -17,7 +17,7 @@ public class AppConfig public ApplicationUser GetApplicationUser(string model) { - var user = ModelUsers.FirstOrDefault(x => x.Model == model); + var user = ModelUsers.FirstOrDefault(x => x.Model == model || x.UserName == model); return user ?? DefaultUser; } } diff --git a/MyApp.ServiceInterface/Data/R2Extensions.cs b/MyApp.ServiceInterface/Data/R2Extensions.cs index 6c6c222..c1fe2fe 100644 --- a/MyApp.ServiceInterface/Data/R2Extensions.cs +++ b/MyApp.ServiceInterface/Data/R2Extensions.cs @@ -6,6 +6,8 @@ namespace MyApp.Data; public static class R2Extensions { + public const int MostVotedScore = 10; + public const int AcceptedScore = 9; public static Dictionary ModelScores = new() { ["starcoder2:3b"] = 1, //3B @@ -56,12 +58,31 @@ public static async Task GetQuestionFilesAsync(this R2VirtualFiles r2, { to.Answers.Add(entry.Value.FromJson()); } + else if (entry.Key.StartsWith(idFiles.FileId + ".h.")) + { + var post = entry.Value.FromJson(); + var answer = new Answer + { + Id = $"{post.Id}", + Model = "human", + UpVotes = entry.Key.Contains("h.most-voted") ? MostVotedScore : AcceptedScore, + Choices = [ + new() + { + Index = 1, + Message = new() { Role = "human", Content = post.Body ?? "" } + } + ] + }; + if (to.Answers.All(x => x.Id != answer.Id)) + to.Answers.Add(answer); + } } if (to.Post == null) return null; - to.Answers.Each(x => x.UpVotes = ModelScores.GetValueOrDefault(x.Model, 1)); + to.Answers.Each(x => x.UpVotes = x.UpVotes == 0 ? ModelScores.GetValueOrDefault(x.Model, 1) : x.UpVotes); to.Answers.Sort((a, b) => b.Votes - a.Votes); return to; } diff --git a/MyApp/Configure.AppHost.cs b/MyApp/Configure.AppHost.cs index 5b3f92c..008ebc7 100644 --- a/MyApp/Configure.AppHost.cs +++ b/MyApp/Configure.AppHost.cs @@ -34,7 +34,7 @@ public override void Configure() FileSystemVirtualFiles.AssertDirectory(HostingEnvironment.ContentRootPath.CombineWith(AppConfig.Instance.CacheDir)); using var db = GetDbConnection(); - AppConfig.Instance.ModelUsers = db.Select(db.From().Where(x => x.Model != null)); + AppConfig.Instance.ModelUsers = db.Select(db.From().Where(x => x.Model != null || x.UserName == "human")); } private string? ResolveGitBlobBaseUrl(IVirtualDirectory contentDir)