Skip to content

Commit

Permalink
#46 Implementiran GPTRequestSender
Browse files Browse the repository at this point in the history
  • Loading branch information
dmatijani committed Jun 15, 2024
1 parent 42efb15 commit e6343ba
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Software/E_Libra/DataAccessLayer/DataAccessLayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
Expand Down
60 changes: 58 additions & 2 deletions Software/E_Libra/DataAccessLayer/F16/GPTRequestSender.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,75 @@
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;

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<string> 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<string> GetAnswerFromResponseContent(HttpContent content)
{
var contentString = await content.ReadAsStringAsync();
GPTResponse responseObject = JsonConvert.DeserializeObject<GPTResponse>(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;
}
}
}
1 change: 1 addition & 0 deletions Software/E_Libra/DataAccessLayer/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.2.0" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
</packages>
1 change: 1 addition & 0 deletions Software/E_Libra/EntitiesLayer/EntitiesLayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Entities\Member.cs" />
<Compile Include="Entities\MostPopularBooks.cs" />
<Compile Include="Entities\MostPopularGenres.cs" />
<Compile Include="F16\GPTChoices.cs" />
<Compile Include="F16\GPTMessage.cs" />
<Compile Include="F16\GPTRequest.cs" />
<Compile Include="F16\GPTResponse.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
1 change: 1 addition & 0 deletions Software/E_Libra/EntitiesLayer/F16/GPTResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ namespace EntitiesLayer.F16
{
public class GPTResponse
{
public List<GPTChoices> choices { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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<string>(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<string>(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<string>(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<string>(result);
}
}
}

0 comments on commit e6343ba

Please sign in to comment.