Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否添加多个翻译引擎? #16

Open
pilgrimlyieu opened this issue Jul 16, 2023 · 10 comments
Open

能否添加多个翻译引擎? #16

pilgrimlyieu opened this issue Jul 16, 2023 · 10 comments
Assignees
Labels
enhancement New feature or request

Comments

@pilgrimlyieu
Copy link

pilgrimlyieu commented Jul 16, 2023

例如 Google, Bing, DeepL 甚至 GPT 等。单个翻译引擎似乎有点单薄,而且有时是会多个结果进行比对的。非常感谢!

@N0I0C0K
Copy link
Owner

N0I0C0K commented Jul 16, 2023

当然可以!我正打算加入多个接口选择,目前的加入顺序是:

  1. Google
  2. Bing

当然如果你想帮助我完成其他接口的接入,我预留了接口在 src/Youdao/ITranslater.cs ,可以很方便接入一个新的翻译引擎!

@pilgrimlyieu
Copy link
Author

pilgrimlyieu commented Jul 16, 2023

当然可以!我正打算加入多个接口选择,目前的加入顺序是:

  1. Google 谷歌
  2. Bing 必应

当然如果你想帮助我完成其他接口的接入,我预留了接口在 src/Youdao/ITranslater.cs ,可以很方便接入一个新的翻译引擎!

感谢!可惜我心有余而力不足,并不了解相关内容,恐怕很难向您提供有益的帮助。

另外我在使用过程中发现一个点,使用 Ctrl + Enter 进行朗读时直接关闭了 PT,但是朗读这个功能并不适合立刻关闭 PT 吧?当然只是我个人的看法,如果有方法可供调整我认为就更好了。

@N0I0C0K
Copy link
Owner

N0I0C0K commented Jul 16, 2023

对,之前我也发现了这个问题,按道理确实不应该关,我在代码里的实现也是朗读不关PT,但是好像不论我是否设置他关闭,只要有enter按下,始终都会关闭,我会在后面持续关注这个问题!

@pilgrimlyieu
Copy link
Author

对,之前我也发现了这个问题,按道理确实不应该关,我在代码里的实现也是朗读不关PT,但是好像不论我是否设置他关闭,只要有enter按下,始终都会关闭,我会在后面持续关注这个问题!

好的,再次感谢,期待插件的后续更新!

@N0I0C0K N0I0C0K added the enhancement New feature or request label Jul 18, 2023
@yinleiCoder
Copy link

期待添加google翻译

@LeXwDeX
Copy link

LeXwDeX commented Jan 9, 2024

提供Azure的ChatGPT的对接方式参考,这两家本质上一样,Azure多了一些请求头,以及自定义API地址,但它们返回和请求结构都一样的,但好在微软搞了SDK,C#写起来结构简约些。

也可以不用自己开发那么多,统一使用OPENAI的API,但提供自定义URL的方式。然后用户可以通过使用https://github.com/songquanpeng/one-api等类似项目自行适配。

提示词可以通过一个文件配置配置提示词,纯示范:

===
Author: N0I0C0K
Name: "PowerTranslator"
Version: 0.01
===

你是一个翻译器。我会输入任何一种语言,你会通过如下的结构告知我{英语}的翻译结果,并给出不超过15个字的使用示范。如果有多个对应的翻译结果,可以在list中依次添加。最终使用统一的JSON结构回复,不需要除JSON以外的内容。

{
    "翻译结果": [],
    "属性": [
        {
            "词性": "",
            "词根": "",
            "含义": ""
        }
    ]
}

image


参考文档:https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line%2Cpython&pivots=programming-language-csharp

// API客户端工厂接口
public interface IApiClientFactory
{
    IApiClient CreateClient();
}

// API客户端接口
public interface IApiClient
{
    string GetResponse(string prompt);
}
using Azure;
using Azure.AI.OpenAI;
using System;

// Azure 客户端实现
public class AzureOpenAIClient : IApiClient
{
    private OpenAIClient _client;

    public AzureOpenAIClient(string endpoint, string key)
    {
        _client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
    }

    // 实现获取响应的方法
    public string GetResponse(string prompt)
    {
        var chatCompletionsOptions = new ChatCompletionsOptions()
        {
            DeploymentName = "gpt-35-turbo",
            Messages =
            {
                new ChatRequestUserMessage(prompt),
            },
            MaxTokens = 100
        };

        Response<ChatCompletions> response = _client.GetChatCompletions(chatCompletionsOptions);
        return response.Value.Choices[0].Message.Content;
    }
}

// Azure 客户端工厂
public class AzureOpenAIClientFactory : IApiClientFactory
{
    private string _endpoint;
    private string _key;

    public AzureOpenAIClientFactory(string endpoint, string key)
    {
        _endpoint = endpoint;
        _key = key;
    }

    // 创建Azure API客户端
    public IApiClient CreateClient()
    {
        return new AzureOpenAIClient(_endpoint, _key);
    }
}
using System;

class Program
{
    static void Main(string[] args)
    {
        // 获取API终端和密钥(URL请求地址和KEY)
        string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
        string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");

        // 创建客户端
        IApiClientFactory factory = new AzureOpenAIClientFactory(endpoint, key);
        IApiClient client = factory.CreateClient();

        // 获取并打印响应
        string response = client.GetResponse("Does Azure OpenAI support customer managed keys?");
        Console.WriteLine(response);
    }
}

@N0I0C0K
Copy link
Owner

N0I0C0K commented Jan 9, 2024

提供Azure的ChatGPT的对接方式参考,这两家本质上一样,Azure多了一些请求头,以及自定义API地址,但它们返回和请求结构都一样的,但好在微软搞了SDK,C#写起来结构简约些。

也可以不用自己开发那么多,统一使用OPENAI的API,但提供自定义URL的方式。然后用户可以通过使用https://github.com/songquanpeng/one-api等类似项目自行适配。

提示词可以通过一个文件配置配置提示词,纯示范:

===
Author: N0I0C0K
Name: "PowerTranslator"
Version: 0.01
===

你是一个翻译器。我会输入任何一种语言,你会通过如下的结构告知我{英语}的翻译结果,并给出不超过15个字的使用示范。如果有多个对应的翻译结果,可以在list中依次添加。最终使用统一的JSON结构回复,不需要除JSON以外的内容。

{
    "翻译结果": [],
    "属性": [
        {
            "词性": "",
            "词根": "",
            "含义": ""
        }
    ]
}

image

参考文档:https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line%2Cpython&pivots=programming-language-csharp

// API客户端工厂接口
public interface IApiClientFactory
{
    IApiClient CreateClient();
}

// API客户端接口
public interface IApiClient
{
    string GetResponse(string prompt);
}
using Azure;
using Azure.AI.OpenAI;
using System;

// Azure 客户端实现
public class AzureOpenAIClient : IApiClient
{
    private OpenAIClient _client;

    public AzureOpenAIClient(string endpoint, string key)
    {
        _client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
    }

    // 实现获取响应的方法
    public string GetResponse(string prompt)
    {
        var chatCompletionsOptions = new ChatCompletionsOptions()
        {
            DeploymentName = "gpt-35-turbo",
            Messages =
            {
                new ChatRequestUserMessage(prompt),
            },
            MaxTokens = 100
        };

        Response<ChatCompletions> response = _client.GetChatCompletions(chatCompletionsOptions);
        return response.Value.Choices[0].Message.Content;
    }
}

// Azure 客户端工厂
public class AzureOpenAIClientFactory : IApiClientFactory
{
    private string _endpoint;
    private string _key;

    public AzureOpenAIClientFactory(string endpoint, string key)
    {
        _endpoint = endpoint;
        _key = key;
    }

    // 创建Azure API客户端
    public IApiClient CreateClient()
    {
        return new AzureOpenAIClient(_endpoint, _key);
    }
}
using System;

class Program
{
    static void Main(string[] args)
    {
        // 获取API终端和密钥(URL请求地址和KEY)
        string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
        string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");

        // 创建客户端
        IApiClientFactory factory = new AzureOpenAIClientFactory(endpoint, key);
        IApiClient client = factory.CreateClient();

        // 获取并打印响应
        string response = client.GetResponse("Does Azure OpenAI support customer managed keys?");
        Console.WriteLine(response);
    }
}

这个方法是可行的,很多翻译平台都提供了接口,但是无一例外他们都需要输入凭证,比如代码里面的AZURE_OPENAI_ENDPOINT,这会加大下载人员的初始化难度,在插件里,我是选择了逆向了一些平台的web端翻译api,以此来给插件提供免费的翻译服务访问。

比较犹豫要不要在插件里加入这种需要自己输入api key的接口🫥

@LeXwDeX
Copy link

LeXwDeX commented Jan 10, 2024

提供Azure的ChatGPT的对接方式参考,这两家本质上一样,Azure多了一些请求头,以及自定义API地址,但它们返回和请求结构都一样的,但好在微软搞了SDK,C#写起来结构简约些。
也可以不用自己开发那么多,统一使用OPENAI的API,但提供自定义URL的方式。然后用户可以通过使用https://github.com/songquanpeng/one-api等类似项目自行适配。
提示词可以通过一个文件配置配置提示词,纯示范:

===
Author: N0I0C0K
Name: "PowerTranslator"
Version: 0.01
===

你是一个翻译器。我会输入任何一种语言,你会通过如下的结构告知我{英语}的翻译结果,并给出不超过15个字的使用示范。如果有多个对应的翻译结果,可以在list中依次添加。最终使用统一的JSON结构回复,不需要除JSON以外的内容。

{
    "翻译结果": [],
    "属性": [
        {
            "词性": "",
            "词根": "",
            "含义": ""
        }
    ]
}

image
参考文档:https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line%2Cpython&pivots=programming-language-csharp

// API客户端工厂接口
public interface IApiClientFactory
{
    IApiClient CreateClient();
}

// API客户端接口
public interface IApiClient
{
    string GetResponse(string prompt);
}
using Azure;
using Azure.AI.OpenAI;
using System;

// Azure 客户端实现
public class AzureOpenAIClient : IApiClient
{
    private OpenAIClient _client;

    public AzureOpenAIClient(string endpoint, string key)
    {
        _client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
    }

    // 实现获取响应的方法
    public string GetResponse(string prompt)
    {
        var chatCompletionsOptions = new ChatCompletionsOptions()
        {
            DeploymentName = "gpt-35-turbo",
            Messages =
            {
                new ChatRequestUserMessage(prompt),
            },
            MaxTokens = 100
        };

        Response<ChatCompletions> response = _client.GetChatCompletions(chatCompletionsOptions);
        return response.Value.Choices[0].Message.Content;
    }
}

// Azure 客户端工厂
public class AzureOpenAIClientFactory : IApiClientFactory
{
    private string _endpoint;
    private string _key;

    public AzureOpenAIClientFactory(string endpoint, string key)
    {
        _endpoint = endpoint;
        _key = key;
    }

    // 创建Azure API客户端
    public IApiClient CreateClient()
    {
        return new AzureOpenAIClient(_endpoint, _key);
    }
}
using System;

class Program
{
    static void Main(string[] args)
    {
        // 获取API终端和密钥(URL请求地址和KEY)
        string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
        string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");

        // 创建客户端
        IApiClientFactory factory = new AzureOpenAIClientFactory(endpoint, key);
        IApiClient client = factory.CreateClient();

        // 获取并打印响应
        string response = client.GetResponse("Does Azure OpenAI support customer managed keys?");
        Console.WriteLine(response);
    }
}

这个方法是可行的,很多翻译平台都提供了接口,但是无一例外他们都需要输入凭证,比如代码里面的AZURE_OPENAI_ENDPOINT,这会加大下载人员的初始化难度,在插件里,我是选择了逆向了一些平台的web端翻译api,以此来给插件提供免费的翻译服务访问。

比较犹豫要不要在插件里加入这种需要自己输入api key的接口🫥

我是这么想的

如果有人用这个的话,那必然要有API,接触到这个的想必大部分人员应该是有开发能力的。

@N0I0C0K
Copy link
Owner

N0I0C0K commented Jan 10, 2024

确实,考虑在下一个更新中添加一个需要提供api key的接口试一下🐱‍👓

@q349980363
Copy link

你好 可否增加 deepl 翻译呢? 非常感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants