Skip to content

Commit

Permalink
Add MissingGradedAnswersTop1K and CreateRankingTasks APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed May 10, 2024
1 parent 8f54e1b commit 4dd9527
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 26 deletions.
49 changes: 40 additions & 9 deletions MyApp.ServiceInterface/AiServerServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,62 @@ public class AiServerServices(ILogger<AiServerServices> log,
public async Task<object> Any(CreateAnswersForModel request)
{
var command = executor.Command<CreateAnswerTasksCommand>();
var to = new CreateAnswersForModelResponse();

int completed = 0;
var sb = StringBuilderCache.Local();
foreach (var postId in request.PostIds)
{
var post = await questions.GetQuestionFileAsPostAsync(postId);
if (post == null)
{
sb.Append("Missing: ").Append(postId).Append('\n');
to.Errors[postId] = "Missing QuestionFile";
continue;
}

completed++;
await command.ExecuteAsync(new CreateAnswerTasks
{
Post = post,
ModelUsers = [request.Model],
});
to.Results.Add(post.Id);
}
return to;
}

public async Task<object> Any(CreateRankingTasks request)
{
var command = executor.Command<CreateRankAnswerTaskCommand>();

var to = new CreateRankingTasksResponse();

var uniqueUserNames = request.AnswerIds.Select(x => x.RightPart('-')).ToSet();
var userIdMap = await Db.DictionaryAsync<string, string>(
Db.From<ApplicationUser>().Where(x => uniqueUserNames.Contains(x.UserName!))
.Select(x => new { x.UserName, x.Id }));

sb.Append("Completed: ").Append(completed).Append('\n');

return new StringResponse { Result = sb.ToString() };
foreach (var id in request.AnswerIds)
{
try
{
var postId = id.LeftPart('-').ToInt();
var userName = id.RightPart('-');
if (!userIdMap.TryGetValue(userName, out var userId))
{
to.Errors[id] = "Unknown User";
continue;
}
await command.ExecuteAsync(new CreateRankAnswerTask
{
UserId = userId,
AnswerId = id,
});
}
catch (Exception e)
{
to.Errors[id] = e.Message;
}
}
return to;
}

public async Task Any(CreateAnswerCallback request)
{
var modelUser = appConfig.GetModelUserById(request.UserId);
Expand Down
32 changes: 31 additions & 1 deletion MyApp.ServiceInterface/StatServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class QuestionGroup
public PostGroup Group { get; set; }
}

public class StatServices(AppConfig appConfig) : Service
public class StatServices(AppConfig appConfig, QuestionsProvider questions) : Service
{
public async Task<object> Any(MissingTop1K request)
{
Expand All @@ -36,4 +36,34 @@ SELECT PostId from StatTotals S WHERE Id LIKE '%-{model.UserName}'

return new MissingTop1KResponse { Results = missingIds };
}

public async Task<object> Any(MissingGradedAnswersTop1K request)
{
var top1kIds = await Db.ColumnAsync<int>(Db.From<QuestionGroup>()
.Where(x => x.Group == PostGroup.Top1K));

var to = new MissingGradedAnswersTop1KResponse();
foreach (var postId in top1kIds)
{
var id = $"{postId}";
try
{
var meta = await questions.GetMetaAsync(postId);
foreach (var entry in meta.ModelVotes.Safe())
{
if (meta.GradedBy == null || !meta.GradedBy.ContainsKey(entry.Key))
{
var answerId = $"{postId}-{entry.Key}";
to.Results.Add(answerId);
}
}
}
catch (Exception e)
{
to.Errors[id] = e.Message;
}
}

return to;
}
}
21 changes: 20 additions & 1 deletion MyApp.ServiceModel/OpenAi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,30 @@ public class CreateOpenAiChatResponse
}

[ValidateHasRole(Roles.Moderator)]
public class CreateAnswersForModel : IPost, IReturn<StringResponse>
public class CreateAnswersForModel : IPost, IReturn<CreateAnswersForModelResponse>
{
[ValidateNotEmpty]
public string Model { get; set; }

[Input(Type = "tag"), FieldCss(Field = "col-span-12")]
public List<int> PostIds { get; set; }
}
public class CreateAnswersForModelResponse
{
public Dictionary<int, string> Errors { get; set; } = new();
public List<int> Results { get; set; } = [];
public ResponseStatus? ResponseStatus { get; set; }
}

[ValidateHasRole(Roles.Moderator)]
public class CreateRankingTasks : IPost, IReturn<CreateRankingTasksResponse>
{
[Input(Type = "tag"), FieldCss(Field = "col-span-12")]
public List<string> AnswerIds { get; set; }
}
public class CreateRankingTasksResponse
{
public Dictionary<string, string> Errors { get; set; } = new();
public List<string> Results { get; set; } = [];
public ResponseStatus? ResponseStatus { get; set; }
}
12 changes: 12 additions & 0 deletions MyApp.ServiceModel/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,16 @@ public class MissingTop1K : IGet, IReturn<MissingTop1KResponse>
public class MissingTop1KResponse
{
public List<int> Results { get; set; }
public ResponseStatus? ResponseStatus { get; set; }
}

[ValidateHasRole(Roles.Moderator)]
public class MissingGradedAnswersTop1K : IGet, IReturn<MissingGradedAnswersTop1KResponse>
{
}
public class MissingGradedAnswersTop1KResponse
{
public List<string> Results { get; set; } = [];
public Dictionary<string, string> Errors { get; set; } = new();
public ResponseStatus? ResponseStatus { get; set; }
}
27 changes: 27 additions & 0 deletions MyApp.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,31 @@ public static string GetHostDir()
// var appSettings = JSON.parse(File.ReadAllText(Path.GetFullPath("appsettings.json")));
// return appSettings.ToObjectDictionary()["HostDir"].ToString()!;
}

public static JsonApiClient CreateDevClient() => new("https://localhost:5001");
public static async Task<JsonApiClient> CreateAuthenticatedDevClientAsync()
{
var client = CreateDevClient();
await client.ApiAsync(new Authenticate
{
provider = "credentials",
UserName = "mythz",
Password = Environment.GetEnvironmentVariable("AUTH_SECRET")
});
return client;
}

public static JsonApiClient CreateProdClient() => new("https://pvq.app");
public static async Task<JsonApiClient> CreateAuthenticatedProdClientAsync()
{
var client = CreateProdClient();
await client.ApiAsync(new Authenticate
{
provider = "credentials",
UserName = "mythz",
Password = Environment.GetEnvironmentVariable("AUTH_SECRET")
});
return client;
}

}
45 changes: 30 additions & 15 deletions MyApp.Tests/Top1KQuestionTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ public class Top1KQuestionTasks
"deepseek-coder",
"gemma-2b",
];

JsonApiClient CreateLiveClient() => new("https://pvq.app");


[Test]
public async Task Find_Missing_Top1K_Questions_For_Model()
{
var model = "mixtral";

var client = CreateLiveClient();
var client = TestUtils.CreateProdClient();

var api = await client.ApiAsync(new MissingTop1K
{
Expand All @@ -43,7 +41,7 @@ public async Task Create_missing_Top1K_Answers_for_Models()
{
// var model = "mixtral";

var client = CreateLiveClient();
var client = TestUtils.CreateProdClient();
await client.ApiAsync(new Authenticate
{
provider = "credentials",
Expand All @@ -62,14 +60,7 @@ public async Task Create_missing_Top1K_Answers_for_Adhoc_Model()
{
var model = "command-r-plus";

var client = CreateLiveClient();
await client.ApiAsync(new Authenticate
{
provider = "credentials",
UserName = "mythz",
Password = Environment.GetEnvironmentVariable("AUTH_SECRET")
});

var client = await TestUtils.CreateAuthenticatedProdClientAsync();
await CreateMissing1KModelsForModelAsync(client, model);
}

Expand Down Expand Up @@ -98,12 +89,36 @@ private static async Task CreateMissing1KModelsForModelAsync(JsonApiClient clien

apiCreate.Error.PrintDump();
apiCreate.ThrowIfError();
apiCreate.Response!.Result.Print();
apiCreate.Response!.Errors.PrintDump();
apiCreate.Response!.Results.PrintDump();
}

[Test]
public void Find_answers_that_have_not_been_individually_graded()
public async Task Find_answers_that_have_not_been_individually_graded()
{
var client = await TestUtils.CreateAuthenticatedProdClientAsync();
// var client = await TestUtils.CreateAuthenticatedDevClientAsync();

var api = await client.ApiAsync(new MissingGradedAnswersTop1K());

api.Error.PrintDump();
api.ThrowIfError();
api.Response.PrintDump();

if (api.Response!.Results.Count == 0)
{
$"No more ungraded answers".Print();
return;
}

var apiCreate = await client.ApiAsync(new CreateRankingTasks
{
AnswerIds = api.Response!.Results,
});

apiCreate.Error.PrintDump();
apiCreate.ThrowIfError();
apiCreate.Response!.Errors.PrintDump();
apiCreate.Response!.Results.PrintDump();
}
}

0 comments on commit 4dd9527

Please sign in to comment.