Skip to content

Commit 16d08e8

Browse files
committed
Add a new translator module. Add IOpenAiClient interface. Update to 2.6.0.
1 parent 76fba21 commit 16d08e8

File tree

29 files changed

+487
-249
lines changed

29 files changed

+487
-249
lines changed

OpenAI.ChatGpt.AspNetCore/ChatGPTFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace OpenAI.ChatGpt.AspNetCore;
1818
// ReSharper disable once InconsistentNaming
1919
public class ChatGPTFactory : IDisposable
2020
{
21-
private readonly OpenAiClient _client;
21+
private readonly IOpenAiClient _client;
2222
private readonly ChatGPTConfig _config;
2323
private readonly IChatHistoryStorage _chatHistoryStorage;
2424
private readonly ITimeProvider _clock;

OpenAI.ChatGpt.AspNetCore/OpenAI.ChatGpt.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageId>OpenAI.ChatGPT.AspNetCore</PackageId>
99
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
1010
<Product>OpenAI ChatGPT integration for .NET with DI</Product>
11-
<Version>2.5.0</Version>
11+
<Version>2.6.0</Version>
1212
<Description>OpenAI Chat Completions API (ChatGPT) integration with easy DI supporting (Microsoft.Extensions.DependencyInjection). It allows you to use the API in your .NET applications. Also, the client supports streaming responses (like ChatGPT) via async streams.</Description>
1313
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
1414
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>

OpenAI.ChatGpt.EntityFrameworkCore/OpenAI.ChatGpt.EntityFrameworkCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PackageId>OpenAI.ChatGPT.EntityFrameworkCore</PackageId>
1010
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
1111
<Product>OpenAI ChatGPT integration for .NET with EF Core storage</Product>
12-
<Version>2.5.0</Version>
12+
<Version>2.6.0</Version>
1313
<Description>OpenAI Chat Completions API (ChatGPT) integration with DI and EF Core supporting. It allows you to use the API in your .NET applications. Also, the client supports streaming responses (like ChatGPT) via async streams.</Description>
1414
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
1515
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>

OpenAI.ChatGpt.Extensions/OpenAI.ChatGpt.Extensions.csproj

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

OpenAI.ChatGpt.Extensions/Program.cs

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

OpenAI.ChatGpt.Extensions/SourceFiles.cs

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

OpenAI.ChatGpt.Extensions/TestsGeneratorExtension.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using OpenAI.ChatGpt.Models.ChatCompletion;
2+
3+
namespace OpenAI.ChatGpt.Modules.Translator;
4+
5+
public class ChatGPTTranslatorService : IDisposable
6+
{
7+
private readonly IOpenAiClient _client;
8+
private readonly string? _defaultSourceLanguage;
9+
private readonly string? _defaultTargetLanguage;
10+
private readonly string? _extraPrompt;
11+
private readonly bool _isHttpClientInjected;
12+
13+
public ChatGPTTranslatorService(
14+
IOpenAiClient client,
15+
string? defaultSourceLanguage = null,
16+
string? defaultTargetLanguage = null,
17+
string? extraPrompt = null)
18+
{
19+
_client = client ?? throw new ArgumentNullException(nameof(client));
20+
_isHttpClientInjected = true;
21+
_defaultSourceLanguage = defaultSourceLanguage;
22+
_defaultTargetLanguage = defaultTargetLanguage;
23+
_extraPrompt = extraPrompt;
24+
}
25+
26+
public ChatGPTTranslatorService(
27+
string apiKey,
28+
string? host,
29+
string? defaultSourceLanguage = null,
30+
string? defaultTargetLanguage = null,
31+
string? extraPrompt = null)
32+
{
33+
ArgumentNullException.ThrowIfNull(apiKey);
34+
_client = new OpenAiClient(apiKey, host);
35+
_defaultSourceLanguage = defaultSourceLanguage;
36+
_defaultTargetLanguage = defaultTargetLanguage;
37+
_extraPrompt = extraPrompt;
38+
}
39+
40+
public void Dispose()
41+
{
42+
if (!_isHttpClientInjected)
43+
{
44+
_client.Dispose();
45+
}
46+
}
47+
48+
public async Task<string> Translate(
49+
string text,
50+
string? sourceLanguage = null,
51+
string? targetLanguage = null,
52+
Action<ChatCompletionRequest>? requestModifier = null,
53+
CancellationToken cancellationToken = default)
54+
{
55+
if (text == null) throw new ArgumentNullException(nameof(text));
56+
var sourceLanguageOrDefault = sourceLanguage ?? _defaultSourceLanguage;
57+
var targetLanguageOrDefault = targetLanguage ?? _defaultTargetLanguage;
58+
var prompt = GetPrompt(sourceLanguageOrDefault, targetLanguageOrDefault);
59+
var response = await _client.GetChatCompletions(
60+
Dialog.StartAsSystem(prompt).ThenUser(text),
61+
user: null,
62+
requestModifier: requestModifier,
63+
cancellationToken: cancellationToken
64+
);
65+
return response;
66+
}
67+
68+
private string GetPrompt(string sourceLanguage, string targetLanguage)
69+
{
70+
return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " +
71+
"I will provide you with an English sentence and you will translate it into Russian. " +
72+
"In the response write ONLY translated text."
73+
+ (_extraPrompt is not null ? "\n" + _extraPrompt : "");
74+
}
75+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
<LangVersion>11</LangVersion>
7+
<Authors>Rodion Mostovoi</Authors>
8+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
9+
<PackageId>OpenAI.ChatGPT.Modules.Translator</PackageId>
10+
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
11+
<Product>OpenAI ChatGPT based language translator</Product>
12+
<Version>2.6.0</Version>
13+
<Description>OpenAI ChatGPT based language translator.</Description>
14+
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
15+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
16+
<PackageTags>chatgpt, openai, sdk, api, chatcompletions, gpt3, gpt4, translator</PackageTags>
17+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
18+
<Title>OpenAI ChatGPT based language translator</Title>
19+
<Copyright>Rodion Mostovoi</Copyright>
20+
</PropertyGroup>
21+
22+
<PropertyGroup>
23+
<ImplicitUsings>enable</ImplicitUsings>
24+
<Nullable>enable</Nullable>
25+
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
26+
</PropertyGroup>
27+
28+
<ItemGroup>
29+
<ProjectReference Include="..\OpenAI.ChatGpt\OpenAI.ChatGpt.csproj" />
30+
</ItemGroup>
31+
32+
</Project>

OpenAI.ChatGpt/ChatGPT.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ChatGPT : IDisposable
1414
private readonly IChatHistoryStorage _storage;
1515
private readonly ITimeProvider _clock;
1616
private readonly ChatGPTConfig? _config;
17-
private readonly OpenAiClient _client;
17+
private readonly IOpenAiClient _client;
1818
private ChatService? _currentChat;
1919

2020
private static readonly string NoUser = Guid.Empty.ToString();
@@ -24,7 +24,7 @@ public class ChatGPT : IDisposable
2424
/// Use this constructor to create chat conversation provider for the specific user.
2525
/// </summary>
2626
public ChatGPT(
27-
OpenAiClient client,
27+
IOpenAiClient client,
2828
IChatHistoryStorage chatHistoryStorage,
2929
ITimeProvider clock,
3030
string userId,
@@ -42,7 +42,7 @@ public ChatGPT(
4242
/// If you don't have users use this ChatGPT constructor.
4343
/// </summary>
4444
public ChatGPT(
45-
OpenAiClient client,
45+
IOpenAiClient client,
4646
IChatHistoryStorage chatHistoryStorage,
4747
ITimeProvider clock,
4848
ChatGPTConfig? config)

0 commit comments

Comments
 (0)