-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
468 additions
and
25 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
198 changes: 198 additions & 0 deletions
198
STranslate/ViewModels/Preference/Services/TranslatorGemini.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,198 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Security.Policy; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using STranslate.Model; | ||
using STranslate.Util; | ||
|
||
namespace STranslate.ViewModels.Preference.Services | ||
{ | ||
public partial class TranslatorGemini : ObservableObject, ITranslator | ||
{ | ||
public TranslatorGemini() | ||
: this(Guid.NewGuid(), "https://generativelanguage.googleapis.com", "Gemini") { } | ||
|
||
public TranslatorGemini( | ||
Guid guid, | ||
string url, | ||
string name = "", | ||
IconType icon = IconType.Gemini, | ||
string appID = "", | ||
string appKey = "", | ||
bool isEnabled = true, | ||
ServiceType type = ServiceType.GeminiService | ||
) | ||
{ | ||
Identify = guid; | ||
Url = url; | ||
Name = name; | ||
Icon = icon; | ||
AppID = appID; | ||
AppKey = appKey; | ||
IsEnabled = isEnabled; | ||
Type = type; | ||
} | ||
|
||
[ObservableProperty] | ||
private Guid _identify = Guid.Empty; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
private ServiceType _type = 0; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
public bool _isEnabled = true; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
private string _name = string.Empty; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
private IconType _icon = IconType.Gemini; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
public string _url = string.Empty; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
public string _AppID = string.Empty; | ||
|
||
[JsonIgnore] | ||
[ObservableProperty] | ||
public string _appKey = string.Empty; | ||
|
||
[JsonIgnore] | ||
public object _data = string.Empty; | ||
|
||
[JsonIgnore] | ||
public object Data | ||
{ | ||
get => _data; | ||
set | ||
{ | ||
if (_data != value) | ||
{ | ||
OnPropertyChanging(nameof(Data)); | ||
_data = value; | ||
OnPropertyChanged(nameof(Data)); | ||
} | ||
} | ||
} | ||
|
||
[JsonIgnore] | ||
public List<IconType> Icons { get; private set; } = Enum.GetValues(typeof(IconType)).OfType<IconType>().ToList(); | ||
|
||
#region Show/Hide Encrypt Info | ||
|
||
[JsonIgnore] | ||
private bool _keyHide = true; | ||
|
||
[JsonIgnore] | ||
public bool KeyHide | ||
{ | ||
get => _keyHide; | ||
set | ||
{ | ||
if (_keyHide != value) | ||
{ | ||
OnPropertyChanging(nameof(KeyHide)); | ||
_keyHide = value; | ||
OnPropertyChanged(nameof(KeyHide)); | ||
} | ||
} | ||
} | ||
|
||
|
||
private void ShowEncryptInfo() => KeyHide = !KeyHide; | ||
|
||
private RelayCommand? showEncryptInfoCommand; | ||
[JsonIgnore] | ||
public IRelayCommand ShowEncryptInfoCommand => showEncryptInfoCommand ??= new RelayCommand(new Action(ShowEncryptInfo)); | ||
|
||
#endregion Show/Hide Encrypt Info | ||
|
||
[Obsolete] | ||
public async Task<object> TranslateAsync(object request, CancellationToken token) | ||
{ | ||
try | ||
{ | ||
if (string.IsNullOrEmpty(Url) || string.IsNullOrEmpty(AppKey)) | ||
throw new Exception("请先完善配置"); | ||
|
||
if (!Url.EndsWith("completions")) | ||
{ | ||
Url = Url.TrimEnd('/') + "/completions"; | ||
} | ||
|
||
if (request != null) | ||
{ | ||
var jsonData = JsonConvert.SerializeObject(request); | ||
|
||
// 构建请求 | ||
var client = new HttpClient(new SocketsHttpHandler()); | ||
var req = new HttpRequestMessage | ||
{ | ||
Method = HttpMethod.Post, | ||
RequestUri = new Uri(Url), | ||
Content = new StringContent(jsonData, Encoding.UTF8, "application/json") | ||
}; | ||
req.Headers.Add("Authorization", $"Bearer {AppKey}"); | ||
|
||
// 发送请求 | ||
using var response = await client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, token); | ||
// 获取响应流 | ||
using var responseStream = await response.Content.ReadAsStreamAsync(token); | ||
using var reader = new System.IO.StreamReader(responseStream); | ||
// 逐行读取并输出结果 | ||
while (!reader.EndOfStream || token.IsCancellationRequested) | ||
{ | ||
var line = await reader.ReadLineAsync(token); | ||
|
||
if (string.IsNullOrEmpty(line?.Trim())) | ||
continue; | ||
|
||
var preprocessString = line.Replace("data:", "").Trim(); | ||
|
||
// 结束标记 | ||
if (preprocessString.Equals("[DONE]")) | ||
break; | ||
|
||
// 解析JSON数据 | ||
var parsedData = JsonConvert.DeserializeObject<JObject>(preprocessString); | ||
|
||
if (parsedData is null) | ||
continue; | ||
|
||
// 提取content的值 | ||
var contentValue = parsedData["choices"]?.FirstOrDefault()?["delta"]?["content"]?.ToString(); | ||
|
||
if (string.IsNullOrEmpty(contentValue)) | ||
continue; | ||
|
||
// 输出 | ||
Data += contentValue; | ||
//Debug.Write(contentValue); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Data = ex.Message; | ||
} | ||
|
||
return Task.FromResult<string?>(null); | ||
} | ||
} | ||
} |
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.