-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new translator module. Add IOpenAiClient interface. Update to 2…
….6.0.
- Loading branch information
Showing
29 changed files
with
487 additions
and
249 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
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
18 changes: 0 additions & 18 deletions
18
OpenAI.ChatGpt.Extensions/OpenAI.ChatGpt.Extensions.csproj
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
OpenAI.ChatGpt.Modules.Translator/ChatGPTTranslatorService.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,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 : ""); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
OpenAI.ChatGpt.Modules.Translator/OpenAI.ChatGpt.Modules.Translator.csproj
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,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> |
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
Oops, something went wrong.