Skip to content

Commit

Permalink
118: Task 02
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmatys committed Dec 1, 2024
1 parent 444c5b0 commit fdda693
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
12 changes: 12 additions & 0 deletions API/ASSISTENTE.Playground/Models/HumanCaptchaModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace ASSISTENTE.Playground.Models;

internal class HumanCaptchaModel
{
[JsonPropertyName("msgID")]
public required int MessageId { get; set; }

[JsonPropertyName("text")]
public required string Text { get; set; }
}
79 changes: 75 additions & 4 deletions API/ASSISTENTE.Playground/Playground.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Text;
using System.Text.Json;
using ASSISTENTE.Application.Abstractions.Interfaces;
using ASSISTENTE.Application.Handlers.Knowledge.Commands;
using ASSISTENTE.Infrastructure.Firecrawl.Contracts;
using ASSISTENTE.Infrastructure.LLM.Contracts;
using ASSISTENTE.Playground.Models;
using CSharpFunctionalExtensions;
using MediatR;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -41,13 +44,24 @@ public async Task LearnAsync()
.Log("Learning completed!", logger)
.LogError(logger);
}

public async Task RunAsync()
{
var result = await Task_02();

result
.Log("Task completed!", logger)
.LogError(logger);
}

private async Task<Result> Task_01()
{
const string url = "https://xyz.ag3nts.org";

var result = await firecrawlService.ScrapeAsync(url)
.Bind(markdownContent => Prompt.Create($"Answer the question, extract only date without any extra infomation: {markdownContent}"))
return await firecrawlService.ScrapeAsync(url)
.Bind(markdownContent =>
Prompt.Create(
$"Answer the question, extract only date without any extra infomation: {markdownContent}"))
.Bind(async prompt => await llmClient.GenerateAnswer(prompt))
.Bind(async answer =>
{
Expand All @@ -64,10 +78,67 @@ public async Task RunAsync()
var response = await httpClient.PostAsync(url, new FormUrlEncodedContent(formData));

var responseContent = await response.Content.ReadAsStringAsync();

return response.IsSuccessStatusCode
? Result.Success(responseContent)
: Result.Failure("");
});
}

private async Task<Result> Task_02()
{
const string memory = "- The capital of Poland is Kraków." +
"- A well-known number from the book The Hitchhiker's Guide to the Galaxy is 69." +
"- The current year is 1999.";

const string instruction = "Answer the question, extract only date without any extra infomation.";

return await VerifyRequest()
.Bind(verifyResponse =>
{
return Prompt
.Create($"{instruction} <memory>{memory}</memory> {verifyResponse.Text}")
.Bind(async prompt => await llmClient.GenerateAnswer(prompt))
.Bind(async answer => await VerifyRequest(verifyResponse.MessageId, answer.Text));
})
.Tap(result => Console.WriteLine(result.Text));
}

private async Task<Result<HumanCaptchaModel>> VerifyRequest(
int? messageId = null,
string? text = null)
{
const string url = "https://xyz.ag3nts.org/verify";

var requestBody = messageId == null
? new HumanCaptchaModel
{
MessageId = 0,
Text = "READY"
}
: new HumanCaptchaModel
{
MessageId = messageId.Value,
Text = text ?? ""
};

var response = await httpClient.PostAsync(
url,
new StringContent(
JsonSerializer.Serialize(requestBody),
Encoding.UTF8, "application/json"
)
);

if (!response.IsSuccessStatusCode)
return Result.Failure<HumanCaptchaModel>("");

var responseContent = await response.Content.ReadAsStringAsync();

var parsedResponse = JsonSerializer.Deserialize<HumanCaptchaModel>(responseContent);

return parsedResponse is null
? Result.Failure<HumanCaptchaModel>("")
: Result.Success(parsedResponse);
}
}
4 changes: 3 additions & 1 deletion API/global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"sdk": {
"version": "9.0.100"
"version": "9.0.0",
"rollForward": "latestMajor",
"allowPrerelease": true
}
}

0 comments on commit fdda693

Please sign in to comment.