bbepis / XUnity.AutoTranslator

MIT License
2k stars 293 forks source link

How can I communicate with a local LLM model? #581

Open Luckzq opened 2 months ago

Luckzq commented 2 months ago

I want to use KoboldCpp or Ollama to provide translation services, believing that many LLM models can offer better results than existing online translation websites. What are the ways to achieve this?

https://github.com/KoboldAI/KoboldAI-Client https://github.com/ollama/ollama

ManlyMarco commented 2 months ago

You have to make your own translation plugin.

Luckzq commented 2 months ago

You have to make your own translation plugin.你必须制作自己的翻译插件。

Thank you, I believe this plugin should be able to accomplish the task. I just wanted to ask whether I should implement it using CustomTranslate or by Implementing a Translator. Maybe someone has already done this?

ag2s20150909 commented 1 week ago

This is Ollama's translation plugin. You need to adjust the Model and Prompt according to your needs.

using System;
using XUnity.AutoTranslator.Plugin.Core.Endpoints;
using XUnity.AutoTranslator.Plugin.Core.Endpoints.Http;
using SimpleJSON;
using XUnity.AutoTranslator.Plugin.Core.Utilities;
using XUnity.AutoTranslator.Plugin.Core.Web;
using System.Net;

namespace OllamaTranslator
{
    public class OllamaTranslator : HttpEndpoint
    {
        private string _Url;
        private string _Model;
        private string _Prompt;
        private string _KeepAlive;

        public OllamaTranslator()
        {
        }

        public override string Id => "Ollama";

        public override string FriendlyName => "Ollama Translator";

        public override void Initialize(IInitializationContext context)
        {

            _Url = context.GetOrCreateSetting("Ollama", "Url", "http://localhost:11434/api/generate");
            _Model = context.GetOrCreateSetting("Ollama", "Model", "qwen2.5");
            _KeepAlive = context.GetOrCreateSetting("Ollama", "KeepAlive", "15m");
            _Prompt = context.GetOrCreateSetting("Ollama", "Prompt", "You are a professional RPG game translator. Please translate the user's input directly into English, keeping the original format.");
            if (string.IsNullOrEmpty(_Url)) throw new Exception("The OllamaTranslator endpoint requires an Url which has not been provided.");
            if (string.IsNullOrEmpty(_Model)) throw new Exception("The OllamaTranslator endpoint requires an Model which has not been provided.");
            if (string.IsNullOrEmpty(_Prompt)) throw new Exception("The OllamaTranslator endpoint requires an Prompt which has not been provided.");
        }

        public override void OnCreateRequest(IHttpRequestCreationContext context)
        {

            var json = new JSONObject();
            json["model"] = _Model;
            json["system"] = _Prompt;
            json["stream"] = false;
            json["keep_alive"] = _KeepAlive;
            json["prompt"] = context.UntranslatedText;

            var data = json.ToString();

            var request = new XUnityWebRequest("POST", _Url, data);
            request.Headers[HttpRequestHeader.ContentType] = "application/json";
            context.Complete(request);
        }

        public override void OnExtractTranslation(IHttpTranslationExtractionContext context)
        {
            var data = context.Response.Data;

            var obj = JSON.Parse(data);
            var token = obj.AsObject["response"].Value;
            var Translated = JsonHelper.Unescape(token);
            if (string.IsNullOrEmpty(Translated)) context.Fail("Received no translation.");
            context.Complete(Translated);

        }
    }
}

This is the prebuilt dll

OllamaTranslator.zip