Skip to content

Commit 56b64bd

Browse files
authored
Merge pull request #4 from 01Dri/development
0.1.0
2 parents 73a44ce + 9415b48 commit 56b64bd

47 files changed

Lines changed: 390 additions & 573 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

MapperAI.sln.DotSettings.user

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AHttpClient_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc439425da351c75ac7d966a1cc8324b51a9c471865af79d2f2f3fcb65e392_003FHttpClient_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
23
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=5ca51f47_002De3fa_002D40db_002D8e16_002D940aee47c197/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Test_Should_Create_4_Files_With_CSharp_Extension" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
34
&lt;TestAncestor&gt;&#xD;
45
&lt;TestId&gt;xUnit::8B3E109D-96CA-4B6D-B379-6AF70646DC25::net8.0::MapperAI.Test.FileMapperTests.Test_Should_Create_4_Files_With_CSharp_Extension&lt;/TestId&gt;&#xD;
6+
&lt;TestId&gt;xUnit::8B3E109D-96CA-4B6D-B379-6AF70646DC25::net8.0::MapperAI.Test.ClassMapperTests.Test1&lt;/TestId&gt;&#xD;
7+
&lt;TestId&gt;xUnit::8B3E109D-96CA-4B6D-B379-6AF70646DC25::net8.0::MapperAI.Test.PdfMapperTests.Test1&lt;/TestId&gt;&#xD;
8+
&lt;TestId&gt;xUnit::8B3E109D-96CA-4B6D-B379-6AF70646DC25::net8.0::MapperAI.Test.FileMapperTests.Test_Should_Create_4_Files_With_Go_Extension&lt;/TestId&gt;&#xD;
59
&lt;/TestAncestor&gt;&#xD;
610
&lt;/SessionState&gt;</s:String>
711
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=ee3fe810_002Dfb62_002D402b_002Db9c5_002D8dab803c815e/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Test1" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;

src/MapperAI.Core/Clients/ClientBaseAI.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/MapperAI.Core/Clients/ClientFactoryAI.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/MapperAI.Core/Clients/GeminiClientAI.cs

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Text.Json;
2+
using MapperAI.Core.Clients.Interfaces;
3+
using MapperAI.Core.Clients.Models;
4+
using MapperAI.Core.Exceptions;
5+
using MapperAI.Core.Serializers.Interfaces;
6+
7+
namespace MapperAI.Core.Clients;
8+
9+
public class GeminiMapperMapperClient : MapperClientBase, IMapperClient
10+
{
11+
private const string EndpointBase = "https://generativelanguage.googleapis.com/v1beta";
12+
13+
public GeminiMapperMapperClient(MapperClientConfiguration mapperClientConfiguration, IMapperSerializer serializer) : base(
14+
mapperClientConfiguration, serializer)
15+
{
16+
}
17+
18+
public async Task<MapperClientResponse> SendAsync(string prompt, CancellationToken cancellationToken)
19+
{
20+
dynamic payload = CreatePayload(prompt);
21+
string endpoint =
22+
$"{EndpointBase}/models/{MapperClientConfiguration.Model}:generateContent?key={MapperClientConfiguration.ApiKey}";
23+
using JsonDocument result = await GetAsync(endpoint, payload, cancellationToken);
24+
string? resultParsed = ParseResponse(result);
25+
if (resultParsed == null) throw new MapperException("Result request from AI is null.");
26+
string cleanedResult = resultParsed
27+
.Replace("```json", string.Empty)
28+
.Replace("```", string.Empty)
29+
.Replace("\\$", "\\\\$")
30+
.Trim();
31+
32+
return new MapperClientResponse(cleanedResult);
33+
}
34+
35+
private dynamic CreatePayload(string promptText)
36+
{
37+
return new
38+
{
39+
contents = new[]
40+
{
41+
new
42+
{
43+
parts = new[]
44+
{
45+
new { text = promptText }
46+
}
47+
}
48+
}
49+
};
50+
}
51+
52+
53+
private string? ParseResponse(JsonDocument response)
54+
=> response
55+
.RootElement
56+
.GetProperty("candidates")[0]
57+
.GetProperty("content")
58+
.GetProperty("parts")[0]
59+
.GetProperty("text")
60+
.GetString();
61+
62+
63+
}

src/MapperAI.Core/Clients/Interfaces/IClientAI.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/MapperAI.Core/Clients/Interfaces/IClientFactoryAI.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using MapperAI.Core.Clients.Models;
2+
3+
namespace MapperAI.Core.Clients.Interfaces;
4+
5+
public interface IMapperClient
6+
{
7+
Task<MapperClientResponse> SendAsync(string prompt, CancellationToken cancellationToken);
8+
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using MapperAI.Core.Clients.Models;
2+
3+
namespace MapperAI.Core.Clients.Interfaces;
4+
5+
public interface IMapperClientFactory
6+
{
7+
IMapperClient CreateClient(MapperClientConfiguration configuration);
8+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Text;
2+
using System.Text.Json;
3+
using MapperAI.Core.Clients.Models;
4+
using MapperAI.Core.Exceptions;
5+
using MapperAI.Core.Serializers.Interfaces;
6+
7+
namespace MapperAI.Core.Clients;
8+
9+
public abstract class MapperClientBase
10+
{
11+
protected MapperClientConfiguration MapperClientConfiguration;
12+
private readonly HttpClient _httpClient;
13+
private readonly IMapperSerializer _serializer;
14+
15+
protected MapperClientBase(MapperClientConfiguration mapperClientConfiguration, IMapperSerializer serializer)
16+
{
17+
MapperClientConfiguration = mapperClientConfiguration;
18+
_httpClient = new HttpClient();
19+
_serializer = serializer;
20+
}
21+
22+
protected async Task<JsonDocument> GetAsync(string endpoint, object body, CancellationToken cancellationToken = default)
23+
{
24+
try
25+
{
26+
string json = _serializer.Serialize(body);
27+
StringContent mediaTypeRequest = new(json, Encoding.UTF8, "application/json");
28+
HttpResponseMessage response = await _httpClient.PostAsync(endpoint, mediaTypeRequest, cancellationToken);
29+
if (!response.IsSuccessStatusCode)
30+
{
31+
throw new MapperRequestStatusException($"Request failed with status: {response.StatusCode}");
32+
}
33+
34+
string result = await response.Content.ReadAsStringAsync();
35+
return JsonDocument.Parse(result);
36+
}
37+
catch (Exception ex)
38+
{
39+
throw new MapperException($"An error occurred during processing: {ex.Message}");
40+
}
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)