Local-MT-Extension/ExampleExtension/ExtensionImpl.cpp

81 lines
3.2 KiB
C++
Raw Normal View History

2018-10-12 03:23:59 +08:00
#include "extension.h"
2024-09-10 00:13:16 +08:00
#include "httplib.h"
#include <json/json.h>
#include <locale>
#include <codecvt>
#include <string>
2018-10-12 03:23:59 +08:00
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
2019-06-29 17:58:12 +08:00
/*
You shouldn't mess with this or even look at it unless you're certain you know what you're doing.
Param sentence: pointer to sentence received by Textractor (UTF-16).
This can be modified. Textractor uses the modified sentence for future processing and display. If empty (starts with null terminator), Textractor will destroy it.
Textractor will display the sentence after all extensions have had a chance to process and/or modify it.
The buffer is allocated using HeapAlloc(). If you want to make it larger, please use HeapReAlloc().
2019-09-05 00:54:55 +08:00
Param sentenceInfo: pointer to array containing misc info about the sentence. End of array is marked with name being nullptr.
2019-06-29 17:58:12 +08:00
Return value: the buffer used for the sentence. Remember to return a new pointer if HeapReAlloc() gave you one.
This function may be run concurrently with itself: please make sure it's thread safe.
It will not be run concurrently with DllMain.
2018-10-12 03:23:59 +08:00
*/
2024-09-10 00:13:16 +08:00
std::string WideStringToString(const std::wstring& text)
{
std::vector<char> buffer((text.size() + 1) * 4);
WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, buffer.data(), buffer.size(), nullptr, nullptr);
return buffer.data();
}
2019-09-05 00:54:55 +08:00
extern "C" __declspec(dllexport) wchar_t* OnNewSentence(wchar_t* sentence, const InfoForExtension* sentenceInfo)
2018-10-12 03:23:59 +08:00
{
2024-09-10 00:13:16 +08:00
try
{
std::wstring sentenceCopy(sentence);
size_t oldSize = sentenceCopy.size();
if (ProcessSentence(sentenceCopy, SentenceInfo{ sentenceInfo }))
{
Json::Value data;
data["model"] = "qwen2:1.5b";
data["prompt"] = "Please translate the following content into Chinese! (Return to Chinese translation directly): " + WideStringToString(sentence);
data["stream"] = false;
Json::StreamWriterBuilder writer;
std::string json_data = Json::writeString(writer, data);
httplib::Client cli("http://localhost:11434");
auto res = cli.Post("/api/generate", json_data, "application/json");
if (res && res->status == 200)
{
Json::CharReaderBuilder reader;
Json::Value json_response;
std::istringstream s(res->body);
std::string errs;
if (Json::parseFromStream(reader, s, &json_response, &errs))
{
std::string translated_text = json_response["response"].asString();
std::wstring translated_sentence = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(translated_text);
if (translated_sentence.size() > oldSize)
{
sentence = (wchar_t*)HeapReAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sentence, (translated_sentence.size() + 1) * sizeof(wchar_t));
}
wcscpy_s(sentence, translated_sentence.size() + 1, translated_sentence.c_str());
}
}
else
{
*sentence = L'\0';
}
}
}
catch (SKIP)
{
*sentence = L'\0';
}
return sentence;
2018-10-12 03:23:59 +08:00
}
2024-09-10 00:13:16 +08:00