Skip to content

Commit

Permalink
feat: add ollama service #55
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Apr 14, 2024
1 parent afa6b58 commit c3491b5
Show file tree
Hide file tree
Showing 12 changed files with 642 additions and 26 deletions.
4 changes: 4 additions & 0 deletions src/STranslate.Model/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public enum ServiceType
STranslateService,
EcdictService,
ChatglmService,
OllamaService,
}

public enum TTSType
Expand Down Expand Up @@ -330,6 +331,9 @@ public enum IconType

[Description("腾讯OCR")]
TencentOCR,

[Description("Ollama")]
Ollama,
}

/// <summary>
Expand Down
Binary file added src/STranslate.Style/Resources/ollama.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/STranslate.Style/STranslate.Style.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
<Resource Include="Resources\niutrans.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Resource>
<Resource Include="Resources\ollama.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Resource>
<Resource Include="Resources\openai.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Resource>
Expand Down
1 change: 1 addition & 0 deletions src/STranslate.Style/Styles/IconStyle.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
<BitmapImage x:Key="PaddleOCR" UriSource="pack://application:,,,/STranslate.Style;component/Resources/paddleocr.png" />
<BitmapImage x:Key="BaiduOCR" UriSource="pack://application:,,,/STranslate.Style;component/Resources/baidu2.png" />
<BitmapImage x:Key="TencentOCR" UriSource="pack://application:,,,/STranslate.Style;component/Resources/tencent2.png" />
<BitmapImage x:Key="Ollama" UriSource="pack://application:,,,/STranslate.Style;component/Resources/ollama.png" />
</ResourceDictionary>
1 change: 1 addition & 0 deletions src/STranslate/Helper/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ public override ITranslator ReadJson(JsonReader reader, Type objectType, ITransl
(int)ServiceType.VolcengineService => new TranslatorVolcengine(),
(int)ServiceType.EcdictService => new TranslatorEcdict(),
(int)ServiceType.ChatglmService => new TranslatorChatglm(),
(int)ServiceType.OllamaService => new TranslatorOllama(),
//TODO: 新接口需要适配
_ => throw new NotSupportedException($"Unsupported ServiceType: {type}")
};
Expand Down
2 changes: 2 additions & 0 deletions src/STranslate/ViewModels/InputViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ private bool PreviousHandle()
case ServiceType.GeminiService:
case ServiceType.OpenAIService:
case ServiceType.ChatglmService:
case ServiceType.OllamaService:
{
//流式处理目前给AI使用,所以可以传递识别语言给AI做更多处理
//Auto则转换为识别语种
Expand Down Expand Up @@ -502,6 +503,7 @@ public override ITranslator ReadJson(JsonReader reader, Type objectType, ITransl
(int)ServiceType.VolcengineService => new TranslatorVolcengine(),
(int)ServiceType.EcdictService => new TranslatorEcdict(),
(int)ServiceType.ChatglmService => new TranslatorChatglm(),
(int)ServiceType.OllamaService => new TranslatorOllama(),
_ => new TranslatorApi()
};

Expand Down
81 changes: 55 additions & 26 deletions src/STranslate/ViewModels/OutputViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
using CommunityToolkit.Mvvm.Input;
using GongSolutions.Wpf.DragDrop;
using STranslate.Helper;
using STranslate.Log;
using STranslate.Model;
using STranslate.Util;
using STranslate.ViewModels.Preference;
using System;
using System.ComponentModel;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -21,37 +24,63 @@ public partial class OutputViewModel : ObservableObject, IDropTarget
[RelayCommand(IncludeCancelCommand = true)]
private async Task SingleTranslateAsync(ITranslator service, CancellationToken token)
{
var inputVM = Singleton<InputViewModel>.Instance;
var sourceLang = Singleton<MainViewModel>.Instance.SourceLang;
var targetLang = Singleton<MainViewModel>.Instance.TargetLang;

var idetify = LangEnum.auto;
//如果是自动则获取自动识别后的目标语种
if (targetLang == LangEnum.auto)
try
{
var autoRet = StringUtil.AutomaticLanguageRecognition(inputVM.InputContent);
idetify = autoRet.Item1;
targetLang = autoRet.Item2;
var inputVM = Singleton<InputViewModel>.Instance;
var sourceLang = Singleton<MainViewModel>.Instance.SourceLang;
var targetLang = Singleton<MainViewModel>.Instance.TargetLang;

var idetify = LangEnum.auto;
//如果是自动则获取自动识别后的目标语种
if (targetLang == LangEnum.auto)
{
var autoRet = StringUtil.AutomaticLanguageRecognition(inputVM.InputContent);
idetify = autoRet.Item1;
targetLang = autoRet.Item2;
}

//根据不同服务类型区分-默认非流式请求数据,若走此种方式请求则无需添加
//TODO: 新接口需要适配
switch (service.Type)
{
case ServiceType.GeminiService:
case ServiceType.OpenAIService:
case ServiceType.ChatglmService:
case ServiceType.OllamaService:
{
//流式处理目前给AI使用,所以可以传递识别语言给AI做更多处理
//Auto则转换为识别语种
sourceLang = sourceLang == LangEnum.auto ? idetify : sourceLang;
await inputVM.StreamHandlerAsync(service, inputVM.InputContent, sourceLang, targetLang, token);
break;
}

default:
await inputVM.NonStreamHandlerAsync(service, inputVM.InputContent, sourceLang, targetLang, token);
break;
}
}

//根据不同服务类型区分-默认非流式请求数据,若走此种方式请求则无需添加
//TODO: 新接口需要适配
switch (service.Type)
catch (Exception exception)
{
case ServiceType.GeminiService:
case ServiceType.OpenAIService:
case ServiceType.ChatglmService:
{
//流式处理目前给AI使用,所以可以传递识别语言给AI做更多处理
//Auto则转换为识别语种
sourceLang = sourceLang == LangEnum.auto ? idetify : sourceLang;
await inputVM.StreamHandlerAsync(service, inputVM.InputContent, sourceLang, targetLang, token);
var errorMessage = "";
var isCancelMsg = false;
switch (exception)
{
case TaskCanceledException:
errorMessage = token.IsCancellationRequested ? "请求取消" : "请求超时";
isCancelMsg = token.IsCancellationRequested;
break;
}
case HttpRequestException:
errorMessage = "请求出错";
break;
}

service.Data = TranslationResult.Fail($"{errorMessage}: {exception.Message}", exception);

default:
await inputVM.NonStreamHandlerAsync(service, inputVM.InputContent, sourceLang, targetLang, token);
break;
if (isCancelMsg)
LogService.Logger.Debug($"[{service.Name}({service.Identify})] {errorMessage}, 请求API: {service.Url}, 异常信息: {exception.Message}");
else
LogService.Logger.Error($"[{service.Name}({service.Identify})] {errorMessage}, 请求API: {service.Url}, 异常信息: {exception.Message}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public override ITranslator ReadJson(JsonReader reader, Type objectType, ITransl
(int)ServiceType.VolcengineService => new TranslatorVolcengine(),
(int)ServiceType.EcdictService => new TranslatorEcdict(),
(int)ServiceType.ChatglmService => new TranslatorChatglm(),
(int)ServiceType.OllamaService => new TranslatorOllama(),
//TODO: 新接口需要适配
_ => throw new NotSupportedException($"Unsupported ServiceType: {type}")
};
Expand Down
3 changes: 3 additions & 0 deletions src/STranslate/ViewModels/Preference/ServiceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public ServiceViewModel()
TransServices.Add(new TranslatorCaiyun());
TransServices.Add(new TranslatorVolcengine());
TransServices.Add(new TranslatorChatglm());
TransServices.Add(new TranslatorOllama());

ResetView();
}
Expand Down Expand Up @@ -105,6 +106,7 @@ private void TogglePage(ITranslator service)
ServiceType.VolcengineService => string.Format("{0}TextVolcengineServicesPage", head),
ServiceType.EcdictService => string.Format("{0}TextEcdictServicesPage", head),
ServiceType.ChatglmService => string.Format("{0}TextChatglmServicesPage", head),
ServiceType.OllamaService => string.Format("{0}TextOllamaServicesPage", head),
_ => string.Format("{0}TextApiServicePage", head)
};

Expand Down Expand Up @@ -136,6 +138,7 @@ private void Add(List<object> list)
TranslatorVolcengine volcengine => volcengine.Clone(),
TranslatorEcdict ecdict => ecdict.Clone(),
TranslatorChatglm chatglm => chatglm.Clone(),
TranslatorOllama ollama => ollama.Clone(),
_ => throw new InvalidOperationException($"Unsupported service type: {service.GetType().Name}")
});

Expand Down
Loading

0 comments on commit c3491b5

Please sign in to comment.