Skip to content

Commit

Permalink
Add a new translator module. Add IOpenAiClient interface. Update to 2…
Browse files Browse the repository at this point in the history
….6.0.
  • Loading branch information
rodion-m committed Jun 17, 2023
1 parent 76fba21 commit 16d08e8
Show file tree
Hide file tree
Showing 29 changed files with 487 additions and 249 deletions.
2 changes: 1 addition & 1 deletion OpenAI.ChatGpt.AspNetCore/ChatGPTFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace OpenAI.ChatGpt.AspNetCore;
// ReSharper disable once InconsistentNaming
public class ChatGPTFactory : IDisposable
{
private readonly OpenAiClient _client;
private readonly IOpenAiClient _client;
private readonly ChatGPTConfig _config;
private readonly IChatHistoryStorage _chatHistoryStorage;
private readonly ITimeProvider _clock;
Expand Down
2 changes: 1 addition & 1 deletion OpenAI.ChatGpt.AspNetCore/OpenAI.ChatGpt.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageId>OpenAI.ChatGPT.AspNetCore</PackageId>
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
<Product>OpenAI ChatGPT integration for .NET with DI</Product>
<Version>2.5.0</Version>
<Version>2.6.0</Version>
<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>
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageId>OpenAI.ChatGPT.EntityFrameworkCore</PackageId>
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
<Product>OpenAI ChatGPT integration for .NET with EF Core storage</Product>
<Version>2.5.0</Version>
<Version>2.6.0</Version>
<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>
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
Expand Down
18 changes: 0 additions & 18 deletions OpenAI.ChatGpt.Extensions/OpenAI.ChatGpt.Extensions.csproj

This file was deleted.

14 changes: 0 additions & 14 deletions OpenAI.ChatGpt.Extensions/Program.cs

This file was deleted.

126 changes: 0 additions & 126 deletions OpenAI.ChatGpt.Extensions/SourceFiles.cs

This file was deleted.

62 changes: 0 additions & 62 deletions OpenAI.ChatGpt.Extensions/TestsGeneratorExtension.cs

This file was deleted.

75 changes: 75 additions & 0 deletions OpenAI.ChatGpt.Modules.Translator/ChatGPTTranslatorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using OpenAI.ChatGpt.Models.ChatCompletion;

namespace OpenAI.ChatGpt.Modules.Translator;

public class ChatGPTTranslatorService : IDisposable
{
private readonly IOpenAiClient _client;
private readonly string? _defaultSourceLanguage;
private readonly string? _defaultTargetLanguage;
private readonly string? _extraPrompt;
private readonly bool _isHttpClientInjected;

public ChatGPTTranslatorService(
IOpenAiClient client,
string? defaultSourceLanguage = null,
string? defaultTargetLanguage = null,
string? extraPrompt = null)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_isHttpClientInjected = true;
_defaultSourceLanguage = defaultSourceLanguage;
_defaultTargetLanguage = defaultTargetLanguage;
_extraPrompt = extraPrompt;
}

public ChatGPTTranslatorService(
string apiKey,
string? host,
string? defaultSourceLanguage = null,
string? defaultTargetLanguage = null,
string? extraPrompt = null)
{
ArgumentNullException.ThrowIfNull(apiKey);
_client = new OpenAiClient(apiKey, host);
_defaultSourceLanguage = defaultSourceLanguage;
_defaultTargetLanguage = defaultTargetLanguage;
_extraPrompt = extraPrompt;
}

public void Dispose()
{
if (!_isHttpClientInjected)
{
_client.Dispose();
}
}

public async Task<string> Translate(
string text,
string? sourceLanguage = null,
string? targetLanguage = null,
Action<ChatCompletionRequest>? requestModifier = null,
CancellationToken cancellationToken = default)
{
if (text == null) throw new ArgumentNullException(nameof(text));
var sourceLanguageOrDefault = sourceLanguage ?? _defaultSourceLanguage;
var targetLanguageOrDefault = targetLanguage ?? _defaultTargetLanguage;
var prompt = GetPrompt(sourceLanguageOrDefault, targetLanguageOrDefault);
var response = await _client.GetChatCompletions(
Dialog.StartAsSystem(prompt).ThenUser(text),
user: null,
requestModifier: requestModifier,
cancellationToken: cancellationToken
);
return response;
}

private string GetPrompt(string sourceLanguage, string targetLanguage)
{
return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " +
"I will provide you with an English sentence and you will translate it into Russian. " +
"In the response write ONLY translated text."
+ (_extraPrompt is not null ? "\n" + _extraPrompt : "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>11</LangVersion>
<Authors>Rodion Mostovoi</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>OpenAI.ChatGPT.Modules.Translator</PackageId>
<PackageProjectUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</PackageProjectUrl>
<Product>OpenAI ChatGPT based language translator</Product>
<Version>2.6.0</Version>
<Description>OpenAI ChatGPT based language translator.</Description>
<RepositoryUrl>https://github.com/rodion-m/ChatGPT_API_dotnet</RepositoryUrl>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<PackageTags>chatgpt, openai, sdk, api, chatcompletions, gpt3, gpt4, translator</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Title>OpenAI ChatGPT based language translator</Title>
<Copyright>Rodion Mostovoi</Copyright>
</PropertyGroup>

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\OpenAI.ChatGpt\OpenAI.ChatGpt.csproj" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions OpenAI.ChatGpt/ChatGPT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ChatGPT : IDisposable
private readonly IChatHistoryStorage _storage;
private readonly ITimeProvider _clock;
private readonly ChatGPTConfig? _config;
private readonly OpenAiClient _client;
private readonly IOpenAiClient _client;
private ChatService? _currentChat;

private static readonly string NoUser = Guid.Empty.ToString();
Expand All @@ -24,7 +24,7 @@ public class ChatGPT : IDisposable
/// Use this constructor to create chat conversation provider for the specific user.
/// </summary>
public ChatGPT(
OpenAiClient client,
IOpenAiClient client,
IChatHistoryStorage chatHistoryStorage,
ITimeProvider clock,
string userId,
Expand All @@ -42,7 +42,7 @@ public ChatGPT(
/// If you don't have users use this ChatGPT constructor.
/// </summary>
public ChatGPT(
OpenAiClient client,
IOpenAiClient client,
IChatHistoryStorage chatHistoryStorage,
ITimeProvider clock,
ChatGPTConfig? config)
Expand Down
Loading

0 comments on commit 16d08e8

Please sign in to comment.