diff --git a/Software/E_Libra/DataAccessLayer/DataAccessLayer.csproj b/Software/E_Libra/DataAccessLayer/DataAccessLayer.csproj index d60bcef..9e097e4 100644 --- a/Software/E_Libra/DataAccessLayer/DataAccessLayer.csproj +++ b/Software/E_Libra/DataAccessLayer/DataAccessLayer.csproj @@ -37,6 +37,9 @@ ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll + + ..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll + diff --git a/Software/E_Libra/DataAccessLayer/F16/GPTRequestSender.cs b/Software/E_Libra/DataAccessLayer/F16/GPTRequestSender.cs index 7123f9a..a00788e 100644 --- a/Software/E_Libra/DataAccessLayer/F16/GPTRequestSender.cs +++ b/Software/E_Libra/DataAccessLayer/F16/GPTRequestSender.cs @@ -1,7 +1,10 @@ using EntitiesLayer.F16; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; @@ -9,11 +12,64 @@ namespace DataAccessLayer.F16 { public class GPTRequestSender : IGPTRequestSender { + private const string uri = "https://api.openai.com/v1/chat/completions"; + private string apiKey { get; set; } + + public GPTRequestSender(string apiKey) + { + this.apiKey = apiKey; + } + public async Task SendRequest(GPTRequest request) { - await Task.Delay(100); + var requestPayload = CreatePayloadFromRequest(request); + + using (var client = new HttpClient()) + { + var endpoint = new Uri(uri); + client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + var response = await client.PostAsync(endpoint, requestPayload); + if (!response.IsSuccessStatusCode) + { + return "Dogodila se greška kod dohvata odgovora."; + } + + //var responseContent = await response.Content.ReadAsStringAsync(); + return await GetAnswerFromResponseContent(response.Content); + } + } + + private StringContent CreatePayloadFromRequest(GPTRequest request) + { + var requestJson = JsonConvert.SerializeObject(request); + return new StringContent(requestJson, Encoding.UTF8, "application/json"); + } + + private async Task GetAnswerFromResponseContent(HttpContent content) + { + var contentString = await content.ReadAsStringAsync(); + GPTResponse responseObject = JsonConvert.DeserializeObject(contentString); + if (responseObject.choices == null) + { + return "Dogodila se greška kod čitanja odgovora."; + } + + var choices = responseObject.choices; + if (choices.Count == 0) + { + return "Nisu dobiveni nikakvi odgovori."; + } + + var firstChoice = choices[0]; + var choiceMessage = firstChoice.message; + if (choiceMessage == null) + { + return "Dogodila se greška."; + } - return new GPTResponse().ToString(); + return choiceMessage.content; } } } diff --git a/Software/E_Libra/DataAccessLayer/packages.config b/Software/E_Libra/DataAccessLayer/packages.config index f1df1d6..878ff30 100644 --- a/Software/E_Libra/DataAccessLayer/packages.config +++ b/Software/E_Libra/DataAccessLayer/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/Software/E_Libra/EntitiesLayer/EntitiesLayer.csproj b/Software/E_Libra/EntitiesLayer/EntitiesLayer.csproj index 6818e00..80ace87 100644 --- a/Software/E_Libra/EntitiesLayer/EntitiesLayer.csproj +++ b/Software/E_Libra/EntitiesLayer/EntitiesLayer.csproj @@ -68,6 +68,7 @@ + diff --git a/Software/E_Libra/IntegrationTesting/Nove funkcionalnosti/F16/Class1.cs b/Software/E_Libra/EntitiesLayer/F16/GPTChoices.cs similarity index 56% rename from Software/E_Libra/IntegrationTesting/Nove funkcionalnosti/F16/Class1.cs rename to Software/E_Libra/EntitiesLayer/F16/GPTChoices.cs index adbcc73..24c0189 100644 --- a/Software/E_Libra/IntegrationTesting/Nove funkcionalnosti/F16/Class1.cs +++ b/Software/E_Libra/EntitiesLayer/F16/GPTChoices.cs @@ -4,9 +4,10 @@ using System.Text; using System.Threading.Tasks; -namespace IntegrationTesting.Nove_funkcionalnosti.F16 +namespace EntitiesLayer.F16 { - internal class Class1 + public class GPTChoices { + public GPTMessage message { get; set; } } } diff --git a/Software/E_Libra/EntitiesLayer/F16/GPTResponse.cs b/Software/E_Libra/EntitiesLayer/F16/GPTResponse.cs index e5dedd4..73bd45c 100644 --- a/Software/E_Libra/EntitiesLayer/F16/GPTResponse.cs +++ b/Software/E_Libra/EntitiesLayer/F16/GPTResponse.cs @@ -8,5 +8,6 @@ namespace EntitiesLayer.F16 { public class GPTResponse { + public List choices { get; set; } } } diff --git a/Software/E_Libra/IntegrationTesting/Nove funkcionalnosti/F16/GPTService_integrationTest.cs b/Software/E_Libra/IntegrationTesting/Nove funkcionalnosti/F16/GPTService_integrationTest.cs new file mode 100644 index 0000000..78bd5b2 --- /dev/null +++ b/Software/E_Libra/IntegrationTesting/Nove funkcionalnosti/F16/GPTService_integrationTest.cs @@ -0,0 +1,77 @@ +using BussinessLogicLayer.F16; +using DataAccessLayer.F16; +using EntitiesLayer.F16; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace IntegrationTesting.Nove_funkcionalnosti.F16 +{ + //David Matijanić + public class GPTService_integrationTest + { + private GPTService gptService { get; set; } + + public GPTService_integrationTest() + { + gptService = new GPTService(new GPTRequestSender("API-key")); + } + + [Fact] + public async Task SendSystemMessage_MessageSent_ShouldReturnString() + { + //Arrange + string message = "Korisnik te pitao za pomoć oko knjige."; + + //Act + var result = await gptService.SendSystemMessage(message); + + //Assert + Assert.IsType(result); + } + + [Fact] + public async Task SendSystemMessage_SystemMessageSetAndMessageSent_ShouldReturnString() + { + //Arrange + string message = "Korisnik te pitao za pomoć oko knjige."; + gptService.SetSystemMessage("Ti si pomoćnik u knjižnici."); + + //Act + var result = await gptService.SendSystemMessage(message); + + //Assert + Assert.IsType(result); + } + + [Fact] + public async Task SendUserMessage_MessageSent_ShouldReturnString() + { + //Arrange + string message = "Korisnik te pitao za pomoć oko knjige."; + + //Act + var result = await gptService.SendUserMessage(message); + + //Assert + Assert.IsType(result); + } + + [Fact] + public async Task SendUserMessage_SystemMessageSetAndMessageSent_ShouldReturnString() + { + //Arrange + string message = "Korisnik te pitao za pomoć oko knjige."; + gptService.SetSystemMessage("Ti si pomoćnik u knjižnici."); + + //Act + var result = await gptService.SendUserMessage(message); + + //Assert + Assert.IsType(result); + } + } +}