Skip to content

Commit

Permalink
123: Task 4
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmatys committed Dec 21, 2024
1 parent e694530 commit 84f1f27
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 47 deletions.
38 changes: 4 additions & 34 deletions API/ASSISTENTE.Playground/ASSISTENTE.Playground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,11 @@
</ItemGroup>

<ItemGroup>
<None Update="Data\data.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\Audio\adam.m4a">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Audio\agnieszka.m4a">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Audio\ardian.m4a">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Audio\michal.m4a">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Audio\monika.m4a">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Audio\rafal.m4a">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Images\fragment_1.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Images\fragment_2.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Images\fragment_3.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Images\fragment_4.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\**\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<Target Name="CreateDirectories" AfterTargets="Build">
<MakeDir Directories="$(OutputPath)Resources\Repositories" />
<MakeDir Directories="$(OutputPath)Resources\Notes" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 22:43. Wykryto jednostkę organiczną w pobliżu północnego skrzydła fabryki. Osobnik przedstawił się jako Aleksander Ragowski. Przeprowadzono skan biometryczny, zgodność z bazą danych potwierdzona. Jednostka przekazana do działu kontroli. Patrol kontynuowany.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 03:26. Wstępny alarm wykrycia – ruch organiczny. Analiza wizualna i sensoryczna wykazała obecność lokalnej zwierzyny leśnej. Fałszywy alarm. Obszar bezpieczny, wracam na trasę patrolu. Spokój przywrócony.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 02:15. Obszar patrolu nocnego cichy, bez wykrycia aktywności organicznej ani mechanicznej. Prowadzony monitoring peryferii obiektu. Kontynuacja zadań.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 01:30. Przebieg patroli nocnych na poziomie ściśle monitorowanym. Czujniki pozostają aktywne, a wytyczne dotyczące wykrywania życia organicznego – bez rezultatów. Stan patrolu bez zakłóceń.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 23:45. Patroluje zachodnią część terenu; brak anomalii ani odchyleń od normy. Sektor bezpieczny, wszystkie kanały komunikacyjne czyste. Przechodzę do następnego punktu.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 04:02. Bez wykrycia aktywności organicznej lub technologicznej. Sensor dźwiękowy i detektory ruchu w pełnej gotowości. Bez niepokojących sygnałów w trakcie patrolu. Kontynuuję monitorowanie.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 22:50. Sektor północno-zachodni spokojny, stan obszaru stabilny. Skanery temperatury i ruchu wskazują brak wykrycia. Jednostka w pełni operacyjna, powracam do dalszego patrolu.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 00:11. Czujniki dźwięku wykryły ultradźwiękowy sygnał, pochodzenie: nadajnik ukryty w zielonych krzakach, nieopodal lasu. Przeprowadzono analizę obiektu. Analiza odcisków palców wskazuje osobę o imieniu Barbara Zawadzka, skorelowano z bazą urodzeń. Nadajnik przekazany do działu śledczego. Obszar zabezpieczony, patrol zakończony bez dalszych incydentów.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 01:00. Monitoring obszaru patrolowego: całkowity brak ruchu. Względna cisza, czujniki nie wykazały aktywności. Kontynuuję obserwację terenu według wyznaczonych wytycznych.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Godzina 03:45. Patrol na peryferiach zachodnich zakończony. Czujniki nie wykazały żadnych niepokojących sygnałów. Obszar bez anomalii, kończę bieżący cykl i przechodzę do kolejnego sektora.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion API/ASSISTENTE.Playground/Playground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task LearnAsync()

public async Task RunAsync()
{
var result = await weekTwo.Task_03();
var result = await weekTwo.Task_04();

result
.Log("Task completed!", logger)
Expand Down
138 changes: 126 additions & 12 deletions API/ASSISTENTE.Playground/Tasks/WeekTwo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class WeekTwo(
IVisionClient visionClient,
IImageClient imageClient)
{
private const string ApiKey = "<API-KEY>";
private const string ApiKey = "<API_KEY>";

public async Task<Result> Task_01()
{
Expand Down Expand Up @@ -58,9 +58,7 @@ public async Task<Result> Task_01()

var answer = await Prompt.Create($"<INSTRUKCJE>{instuctions}</INSTRUKCJE>\n\n <ZEZNANIA>{context}</ZEZNANIA>")
.Bind(async prompt => await llmClient.GenerateAnswer(prompt))
.Bind(answer =>
Prompt.Create(
$"Na podstawie otrzymanych informacji: \n <INFORMACJE>{answer.Text}</INFORMACJE>. \n\n Zwróć tylko i wyłącznie nazwę ulicy przy której znajduje się zidentifikowane miejsce."))
.Bind(answer => Prompt.Create($"Na podstawie otrzymanych informacji: \n <INFORMACJE>{answer.Text}</INFORMACJE>. \n\n Zwróć tylko i wyłącznie nazwę ulicy przy której znajduje się zidentifikowane miejsce."))
.Bind(async prompt => await llmClient.GenerateAnswer(prompt));

var request = new TaskRequestModel
Expand Down Expand Up @@ -134,13 +132,9 @@ public async Task<Result> Task_02()

var answer = await Prompt.Create($"<INSTRUKCJE>{instuctions}</INSTRUKCJE>\n\n <OPIS>{context}</OPIS>")
.Bind(async prompt => await llmClient.GenerateAnswer(prompt))
.Bind(answer =>
Prompt.Create(
$"Na podstawie otrzymanych informacji {step1} \n\n <INFORMACJE>{answer.Text}</INFORMACJE>."))
.Bind(answer => Prompt.Create($"Na podstawie otrzymanych informacji {step1} \n\n <INFORMACJE>{answer.Text}</INFORMACJE>."))
.Bind(async prompt => await llmClient.GenerateAnswer(prompt))
.Bind(answer =>
Prompt.Create(
$"Na podstawie otrzymanych informacji {step2} \n\n <INFORMACJE>{answer.Text}</INFORMACJE>."))
.Bind(answer => Prompt.Create($"Na podstawie otrzymanych informacji {step2} \n\n <INFORMACJE>{answer.Text}</INFORMACJE>."))
.Bind(async prompt => await llmClient.GenerateAnswer(prompt));

Console.WriteLine($"Miasto: {answer.GetValueOrDefault(x => x.Text)}");
Expand All @@ -156,10 +150,10 @@ public async Task<Result> Task_03()

var fileResponse = await httpClient.GetAsync(robotDescriptionUrl);
var robotDescription = await fileResponse.Content.ReadAsStringAsync();

var image = await ImagePrompt.Create(robotDescription)
.Bind(async prompt => await imageClient.GenerateImage(prompt));

var request = new TaskRequestModel
{
Task = taskName,
Expand All @@ -181,4 +175,124 @@ public async Task<Result> Task_03()
? Result.Success(responseContent)
: Result.Failure(responseContent);
}

public async Task<Result> Task_04()
{
const string url = "https://centrala.ag3nts.org/report";
const string taskName = "kategorie";
const string filesPath = "Data/Files";

const string masterPrompt = "Twoim zadaniem jest na podstawie treści podanej w <OPIS> zwrócić odpowiednie " +
"słowo kluczowe: HARDWARE, PEOPLE, MISSING.\n\n" +
"Przykład: 'Czujniki dźwięku wykryły ultradźwiękowy sygnał, pochodzenie: nadajnik ukryty w zielonych krzakach'\n" +
"Zwracasz: HARDWARE\n\n" +
"Przykład: Osobnik przedstawił się jako Aleksander Ragowski." +
"Zwracasz: PEOPLE\n\n";

const string rules = "1. Jeżeli w danym pliku znajdują się informacje o sprzęcie to ZWRACASZ SŁOWO: HARDWARE" +
"2. Jeżeli w danym pliku znajdują się informacje o ludziach to ZWRACASZ SŁOWO: PEOPLE" +
"3. Istnieje szansa, że znaleziona informacja nie dotyczy ani ludzi, ani maszyn! ZWRACASZ SŁOWO: MISSING";

var filePaths = Directory.GetFiles(filesPath);

var peopleList = new List<string>();
var hardwareList = new List<string>();

foreach (var filePath in filePaths)
{
var fileName = Path.GetFileName(filePath);
var fileExtension = Path.GetExtension(filePath);

if (fileExtension == ".txt")
{
var fileText = await File.ReadAllTextAsync(filePath);

var promptText = $"<ZADANIE>{masterPrompt}</ZADANIE\n\n" +
$"<ZASADY>{rules}</ZASADY>\n\n" +
$"<OPIS>{fileText}</OPIS>";

var answer = await Prompt.Create(promptText)
.Bind(async prompt => await llmClient.GenerateAnswer(prompt))
.GetValueOrDefault(x => x.Text);

if (answer == "PEOPLE")
peopleList.Add(fileName);

if (answer == "HARDWARE")
hardwareList.Add(fileName);
}

if (fileExtension == ".mp3")
{
await using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

var answer = await AudioFile.Create(fileName, fileStream)
.Bind(async prompt => await audioClient.GenerateTranscription(prompt))
.Map(transcription => $"<ZADANIE>{masterPrompt}</ZADANIE\n\n" +
$"<ZASADY>{rules}</ZASADY>\n\n" +
$"<OPIS>{transcription.Text}</OPIS>")
.Bind(Prompt.Create)
.Bind(async prompt => await llmClient.GenerateAnswer(prompt))
.GetValueOrDefault(x => x.Text);

if (answer == "PEOPLE")
peopleList.Add(fileName);

if (answer == "HARDWARE")
hardwareList.Add(fileName);
}

if (fileExtension == ".png")
{
await using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

using var memoryStream = new MemoryStream();
await fileStream.CopyToAsync(memoryStream);
var byteImage = memoryStream.ToArray();

var base64Image = Convert.ToBase64String(byteImage);
var extension = Path.GetExtension(filePath);

var answer = await VisionImage.Create("Zwróć treść z obrazka", base64Image, extension)
.Bind(async prompt => await visionClient.Recognize(prompt))
.Map(recognition => $"<ZADANIE>{masterPrompt}</ZADANIE\n\n" +
$"<ZASADY>{rules}</ZASADY>\n\n" +
$"<OPIS>{recognition.Text}</OPIS>")
.Bind(Prompt.Create)
.Bind(async prompt => await llmClient.GenerateAnswer(prompt))
.GetValueOrDefault(x => x.Text);

if (answer == "PEOPLE")
peopleList.Add(fileName);

if (answer == "HARDWARE")
hardwareList.Add(fileName);
}
}

var request = new TaskRequestModel
{
Task = taskName,
ApiKey = ApiKey,
Answer = new
{
people = peopleList,
hardware = hardwareList
}
};

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

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

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

0 comments on commit 84f1f27

Please sign in to comment.