翻译配置
{!apiKeySetted &&
@@ -329,7 +315,8 @@ const Settings = () => {
onChange={setTranslateEnableValue}/>
-
diff --git a/src/chrome/openaiService.ts b/src/chrome/openaiService.ts
index 58bca3b..ae98d47 100644
--- a/src/chrome/openaiService.ts
+++ b/src/chrome/openaiService.ts
@@ -1,6 +1,8 @@
+import {DEFAULT_SERVER_URL_OPENAI} from '../const'
+
const getServerUrl = (serverUrl?: string) => {
if (!serverUrl) {
- return 'https://api.openai.com'
+ return DEFAULT_SERVER_URL_OPENAI
}
if (serverUrl.endsWith('/')) {
serverUrl = serverUrl.slice(0, -1)
diff --git a/src/const.tsx b/src/const.tsx
index 9a63dd2..6172897 100644
--- a/src/const.tsx
+++ b/src/const.tsx
@@ -169,7 +169,8 @@ export const SUMMARIZE_THRESHOLD = 100
export const SUMMARIZE_LANGUAGE_DEFAULT = 'cn'
export const SUMMARIZE_ALL_THRESHOLD = 5
export const ASK_ENABLED_DEFAULT = true
-export const SERVER_URL_OPENAI = 'https://api.openai.com'
+export const DEFAULT_SERVER_URL_OPENAI = 'https://api.openai.com'
+export const CUSTOM_MODEL_TOKENS = 16385
export const MODELS = [{
code: 'gpt-3.5-turbo',
@@ -183,6 +184,9 @@ export const MODELS = [{
code: 'gpt-3.5-turbo-1106',
name: 'gpt-3.5-turbo-1106',
tokens: 16385,
+}, {
+ code: 'custom',
+ name: '自定义',
}]
export const GEMINI_TOKENS = 32768
export const MODEL_DEFAULT = MODELS[1].code
diff --git a/src/hooks/useSubtitleService.ts b/src/hooks/useSubtitleService.ts
index e4bd42d..f88a0bb 100644
--- a/src/hooks/useSubtitleService.ts
+++ b/src/hooks/useSubtitleService.ts
@@ -15,18 +15,9 @@ import {
setUrl,
} from '../redux/envReducer'
import {EventBusContext} from '../Router'
-import {
- EVENT_EXPAND,
- GEMINI_TOKENS,
- MODEL_DEFAULT,
- MODEL_MAP,
- TOTAL_HEIGHT_MAX,
- TOTAL_HEIGHT_MIN,
- WORDS_MIN,
- WORDS_RATE
-} from '../const'
+import {EVENT_EXPAND, GEMINI_TOKENS, TOTAL_HEIGHT_MAX, TOTAL_HEIGHT_MIN, WORDS_MIN, WORDS_RATE} from '../const'
import {useInterval} from 'ahooks'
-import {getWholeText} from '../util/biz_util'
+import {getModelMaxTokens, getWholeText} from '../util/biz_util'
/**
* Service是单例,类似后端的服务概念
@@ -172,7 +163,7 @@ const useSubtitleService = () => {
if (envData.aiType === 'gemini') {
size = GEMINI_TOKENS*WORDS_RATE
} else {
- size = (MODEL_MAP[envData.model??MODEL_DEFAULT]?.tokens??4000)*WORDS_RATE
+ size = getModelMaxTokens(envData)*WORDS_RATE
}
}
size = Math.max(size, WORDS_MIN)
@@ -209,7 +200,7 @@ const useSubtitleService = () => {
}
}
dispatch(setSegments(segments))
- }, [data?.body, dispatch, envData.aiType, envData.model, envData.summarizeEnable, envData.words])
+ }, [data?.body, dispatch, envData])
// 每秒更新当前视频时间
useInterval(() => {
diff --git a/src/hooks/useTranslate.ts b/src/hooks/useTranslate.ts
index 12ae77a..dc4701e 100644
--- a/src/hooks/useTranslate.ts
+++ b/src/hooks/useTranslate.ts
@@ -16,7 +16,6 @@ import {
import {
LANGUAGE_DEFAULT,
LANGUAGES_MAP,
- MODEL_DEFAULT,
PROMPT_DEFAULTS,
PROMPT_TYPE_ASK,
PROMPT_TYPE_TRANSLATE,
@@ -28,7 +27,7 @@ import {
} from '../const'
import toast from 'react-hot-toast'
import {useMemoizedFn} from 'ahooks/es'
-import {extractJsonArray, extractJsonObject} from '../util/biz_util'
+import {extractJsonArray, extractJsonObject, getModel} from '../util/biz_util'
import {formatTime} from '../util/util'
const useTranslate = () => {
@@ -104,7 +103,7 @@ const useTranslate = () => {
}
}
:{
- model: envData.model??MODEL_DEFAULT,
+ model: getModel(envData),
messages: [
{
role: 'user',
@@ -137,7 +136,7 @@ const useTranslate = () => {
dispatch(addTaskId(task.id))
}
}
- }, [data?.body, envData.fetchAmount, envData.prompts, envData.aiType, envData.serverUrl, envData.model, envData.apiKey, envData.geminiApiKey, language.name, title, dispatch])
+ }, [data?.body, envData, language.name, title, dispatch])
const addSummarizeTask = useCallback(async (type: SummaryType, segment: Segment) => {
if (segment.text.length >= SUMMARIZE_THRESHOLD) {
@@ -173,7 +172,7 @@ const useTranslate = () => {
}
}
:{
- model: envData.model??MODEL_DEFAULT,
+ model: getModel(envData),
messages: [
{
role: 'user',
@@ -198,7 +197,7 @@ const useTranslate = () => {
const task = await chrome.runtime.sendMessage({type: 'addTask', taskDef})
dispatch(addTaskId(task.id))
}
- }, [dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey, envData.model, envData.prompts, envData.serverUrl, summarizeLanguage.name, title])
+ }, [dispatch, envData, summarizeLanguage.name, title])
const addAskTask = useCallback(async (segment: Segment, question: string) => {
if (segment.text.length >= SUMMARIZE_THRESHOLD) {
@@ -228,7 +227,7 @@ const useTranslate = () => {
}
}
:{
- model: envData.model??MODEL_DEFAULT,
+ model: getModel(envData),
messages: [
{
role: 'user',
@@ -251,7 +250,7 @@ const useTranslate = () => {
const task = await chrome.runtime.sendMessage({type: 'addTask', taskDef})
dispatch(addTaskId(task.id))
}
- }, [dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey, envData.model, envData.prompts, envData.serverUrl, summarizeLanguage.name, title])
+ }, [dispatch, envData, summarizeLanguage.name, title])
const handleTranslate = useMemoizedFn((task: Task, content: string) => {
let map: {[key: string]: string} = {}
diff --git a/src/redux/envReducer.ts b/src/redux/envReducer.ts
index d17096b..ca58af3 100644
--- a/src/redux/envReducer.ts
+++ b/src/redux/envReducer.ts
@@ -1,7 +1,7 @@
import {createSlice, PayloadAction} from '@reduxjs/toolkit'
import {find} from 'lodash-es'
import {getDevData} from '../util/biz_util'
-import {SERVER_URL_OPENAI, TOTAL_HEIGHT_DEF} from '../const'
+import {DEFAULT_SERVER_URL_OPENAI, TOTAL_HEIGHT_DEF} from '../const'
interface EnvState {
envData: EnvData
@@ -51,7 +51,7 @@ interface EnvState {
const initialState: EnvState = {
envData: {
- serverUrl: SERVER_URL_OPENAI,
+ serverUrl: DEFAULT_SERVER_URL_OPENAI,
translateEnable: true,
summarizeEnable: true,
autoExpand: true,
diff --git a/src/typings.d.ts b/src/typings.d.ts
index 58d5888..0493b81 100644
--- a/src/typings.d.ts
+++ b/src/typings.d.ts
@@ -7,6 +7,8 @@ interface EnvData {
apiKey?: string
serverUrl?: string
model?: string
+ customModel?: string
+ customModelTokens?: number
// gemini
geminiApiKey?: string
diff --git a/src/util/biz_util.ts b/src/util/biz_util.ts
index 4fad38a..295547d 100644
--- a/src/util/biz_util.ts
+++ b/src/util/biz_util.ts
@@ -1,5 +1,5 @@
import devData from '../data/data.json'
-import {APP_DOM_ID, SUMMARIZE_TYPES} from '../const'
+import {APP_DOM_ID, CUSTOM_MODEL_TOKENS, MODEL_DEFAULT, MODEL_MAP, SUMMARIZE_TYPES} from '../const'
import {isDarkMode} from '@kky002/kky-util'
import toast from 'react-hot-toast'
import {findIndex} from 'lodash-es'
@@ -123,6 +123,22 @@ export const getServerUrl = (serverUrl?: string) => {
return serverUrl
}
+export const getModel = (envData: EnvData) => {
+ if (envData.model === 'custom') {
+ return envData.customModel
+ } else {
+ return envData.model
+ }
+}
+
+export const getModelMaxTokens = (envData: EnvData) => {
+ if (envData.model === 'custom') {
+ return envData.customModelTokens??CUSTOM_MODEL_TOKENS
+ } else {
+ return MODEL_MAP[envData.model??MODEL_DEFAULT]?.tokens??4000
+ }
+}
+
export const setTheme = (theme: EnvData['theme']) => {
const appRoot = document.getElementById(APP_DOM_ID)
if (appRoot != null) {