-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ namespace EntitiesLayer.F16 | |
{ | ||
public class GPTResponse | ||
{ | ||
public List<GPTChoices> choices { get; set; } | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
Software/E_Libra/IntegrationTesting/Nove funkcionalnosti/F16/GPTService_integrationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |