This commit is contained in:
dogcraft 2025-03-22 12:00:20 +08:00
parent 9ef78e2b86
commit 9256eb3638
No known key found for this signature in database
GPG Key ID: 23087E69E76F9682
4 changed files with 23 additions and 26 deletions

View File

@ -187,7 +187,7 @@ export const paramDef = {
}, },
enableLlmTranslator: { type: 'boolean' }, enableLlmTranslator: { type: 'boolean' },
enableLlmTranslatorRedisCache: { type: 'boolean' }, enableLlmTranslatorRedisCache: { type: 'boolean' },
llmTranslatorRedisCacheTtl: { type: 'integer' , nullable: true}, llmTranslatorRedisCacheTtl: { type: 'integer' },
llmTranslatorBaseUrl: { type: 'string', nullable: true }, llmTranslatorBaseUrl: { type: 'string', nullable: true },
llmTranslatorApiKey: { type: 'string', nullable: true }, llmTranslatorApiKey: { type: 'string', nullable: true },
llmTranslatorModel: { type: 'string', nullable: true }, llmTranslatorModel: { type: 'string', nullable: true },

View File

@ -5,16 +5,16 @@
import { URLSearchParams } from 'node:url'; import { URLSearchParams } from 'node:url';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { OpenAI } from 'openai';
import * as Redis from 'ioredis';
import { Endpoint } from '@/server/api/endpoint-base.js'; import { Endpoint } from '@/server/api/endpoint-base.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js'; import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { HttpRequestService } from '@/core/HttpRequestService.js'; import { HttpRequestService } from '@/core/HttpRequestService.js';
import { GetterService } from '@/server/api/GetterService.js'; import { GetterService } from '@/server/api/GetterService.js';
import { RoleService } from '@/core/RoleService.js'; import { RoleService } from '@/core/RoleService.js';
import { ApiError } from '../../error.js';
import { MiMeta } from '@/models/_.js'; import { MiMeta } from '@/models/_.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { OpenAI } from "openai"; import { ApiError } from '../../error.js';
import * as Redis from 'ioredis';
export const meta = { export const meta = {
tags: ['notes'], tags: ['notes'],
@ -96,7 +96,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
const res = await this.llmTranslate(note.text, ps.targetLang, note.id); const res = await this.llmTranslate(note.text, ps.targetLang, note.id);
return { return {
text: res, text: res,
} };
} }
if (this.serverSettings.deeplAuthKey == null) { if (this.serverSettings.deeplAuthKey == null) {
@ -136,7 +136,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}); });
} }
private async llmTranslate(text: string, targetLang: string, noteId: string): Promise<string | null> { private async llmTranslate(text: string, targetLang: string, noteId: string): Promise<string> {
if (this.serverSettings.enableLlmTranslatorRedisCache) { if (this.serverSettings.enableLlmTranslatorRedisCache) {
const key = `llmTranslate:${targetLang}:${noteId}`; const key = `llmTranslate:${targetLang}:${noteId}`;
const cached = await this.redisClient.get(key); const cached = await this.redisClient.get(key);
@ -145,17 +145,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
return cached; return cached;
} }
const res = await this.getLlmRes(text, targetLang); const res = await this.getLlmRes(text, targetLang);
await this.redisClient.set(key, res ?? ''); await this.redisClient.set(key, res);
this.redisClient.expire(key, this.serverSettings.llmTranslatorRedisCacheTtl * 60); this.redisClient.expire(key, this.serverSettings.llmTranslatorRedisCacheTtl * 60);
return res; return res;
} } else {
else {
return this.getLlmRes(text, targetLang); return this.getLlmRes(text, targetLang);
} }
} }
private async getLlmRes(text: string, targetLang: string): Promise<string> {
private async getLlmRes(text: string, targetLang: string): Promise<string | null> {
const client = new OpenAI({ const client = new OpenAI({
baseURL: this.serverSettings.llmTranslatorBaseUrl, baseURL: this.serverSettings.llmTranslatorBaseUrl,
apiKey: this.serverSettings.llmTranslatorApiKey ?? '', apiKey: this.serverSettings.llmTranslatorApiKey ?? '',
@ -173,8 +171,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
temperature: this.serverSettings.llmTranslatorTemperature, temperature: this.serverSettings.llmTranslatorTemperature,
max_tokens: this.serverSettings.llmTranslatorMaxTokens, max_tokens: this.serverSettings.llmTranslatorMaxTokens,
top_p: this.serverSettings.llmTranslatorTopP, top_p: this.serverSettings.llmTranslatorTopP,
}) });
return completion.choices[0].message.content return completion.choices[0].message.content ?? '';
} }
} }

View File

@ -125,7 +125,6 @@ const llmTranslatorUserPrompt = ref<string>('');
const llmTranslatorRedisCacheEnabled = ref<boolean>(false); const llmTranslatorRedisCacheEnabled = ref<boolean>(false);
const llmTranslatorRedisCacheTtl = ref<number>(0); const llmTranslatorRedisCacheTtl = ref<number>(0);
async function init() { async function init() {
const meta = await misskeyApi('admin/meta'); const meta = await misskeyApi('admin/meta');
deeplAuthKey.value = meta.deeplAuthKey ?? ''; deeplAuthKey.value = meta.deeplAuthKey ?? '';