From 6877a1b7bcfaae64dc9ec59913f997a3a82b5801 Mon Sep 17 00:00:00 2001 From: josc146 Date: Mon, 5 Aug 2024 19:01:06 +0800 Subject: [PATCH] improve 48817006 --- src/_locales/de/main.json | 6 +- src/_locales/en/main.json | 6 +- src/_locales/es/main.json | 6 +- src/_locales/fr/main.json | 6 +- src/_locales/in/main.json | 6 +- src/_locales/it/main.json | 6 +- src/_locales/ja/main.json | 6 +- src/_locales/ko/main.json | 6 +- src/_locales/pt/main.json | 6 +- src/_locales/ru/main.json | 6 +- src/_locales/tr/main.json | 6 +- src/_locales/zh-hans/main.json | 4 +- src/_locales/zh-hant/main.json | 6 +- src/background/index.mjs | 8 +-- src/config/index.mjs | 3 +- src/popup/sections/AdvancedPart.jsx | 45 ------------- src/popup/sections/GeneralPart.jsx | 41 ++++++++++++ src/popup/styles.scss | 15 ----- src/services/apis/ollama-api.mjs | 100 ++++++++-------------------- src/services/apis/openai-api.mjs | 2 + src/utils/fetch-ollama.mjs | 27 -------- src/utils/fetch-sse.mjs | 1 - 22 files changed, 134 insertions(+), 184 deletions(-) delete mode 100644 src/utils/fetch-ollama.mjs diff --git a/src/_locales/de/main.json b/src/_locales/de/main.json index 87bdb3d9..0b9bbc77 100644 --- a/src/_locales/de/main.json +++ b/src/_locales/de/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "Immer das schwebende Fenster anzeigen, die Seitenleiste für alle Website-Adapter deaktivieren", "Allow ESC to close all floating windows": "ESC-Taste zum Schließen aller schwebenden Fenster zulassen", "Export All Data": "Alle Daten exportieren", - "Import All Data": "Alle Daten importieren" + "Import All Data": "Alle Daten importieren", + "Keep-Alive Time": "Keep-Alive-Zeit", + "5m": "5m", + "30m": "30m", + "Forever": "Für immer" } diff --git a/src/_locales/en/main.json b/src/_locales/en/main.json index ffb7bbf2..2b7d9972 100644 --- a/src/_locales/en/main.json +++ b/src/_locales/en/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "Always display floating window, disable sidebar for all site adapters", "Allow ESC to close all floating windows": "Allow ESC to close all floating windows", "Export All Data": "Export All Data", - "Import All Data": "Import All Data" + "Import All Data": "Import All Data", + "Keep-Alive Time": "Keep-Alive Time", + "5m": "5m", + "30m": "30m", + "Forever": "Forever" } diff --git a/src/_locales/es/main.json b/src/_locales/es/main.json index 4a4f2cd6..6055a767 100644 --- a/src/_locales/es/main.json +++ b/src/_locales/es/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "Mostrar siempre la ventana flotante, desactivar la barra lateral para todos los adaptadores de sitios", "Allow ESC to close all floating windows": "Permitir que ESC cierre todas las ventanas flotantes", "Export All Data": "Exportar todos los datos", - "Import All Data": "Importar todos los datos" + "Import All Data": "Importar todos los datos", + "Keep-Alive Time": "Tiempo de mantenimiento de la conexión", + "5m": "5m", + "30m": "30m", + "Forever": "Siempre" } diff --git a/src/_locales/fr/main.json b/src/_locales/fr/main.json index 6a03f760..7727d151 100644 --- a/src/_locales/fr/main.json +++ b/src/_locales/fr/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "Toujours afficher la fenêtre flottante, désactiver la barre latérale pour tous les adaptateurs de site", "Allow ESC to close all floating windows": "Autoriser la touche ESC pour fermer toutes les fenêtres flottantes", "Export All Data": "Exporter toutes les données", - "Import All Data": "Importer toutes les données" + "Import All Data": "Importer toutes les données", + "Keep-Alive Time": "Temps de maintien de la connexion", + "5m": "5m", + "30m": "30m", + "Forever": "Toujours" } diff --git a/src/_locales/in/main.json b/src/_locales/in/main.json index 50ee5044..045acc07 100644 --- a/src/_locales/in/main.json +++ b/src/_locales/in/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "Selalu tampilkan jendela mengambang, nonaktifkan sidebar untuk semua adapter situs", "Allow ESC to close all floating windows": "Izinkan ESC untuk menutup semua jendela mengambang", "Export All Data": "Ekspor Semua Data", - "Import All Data": "Impor Semua Data" + "Import All Data": "Impor Semua Data", + "Keep-Alive Time": "Waktu Tetap Hidup", + "5m": "5m", + "30m": "30m", + "Forever": "Selamanya" } diff --git a/src/_locales/it/main.json b/src/_locales/it/main.json index 8c85f4fa..e48b55bc 100644 --- a/src/_locales/it/main.json +++ b/src/_locales/it/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "Mostra sempre la finestra flottante, disabilita la barra laterale per tutti gli adattatori del sito", "Allow ESC to close all floating windows": "Consenti ESC per chiudere tutte le finestre flottanti", "Export All Data": "Esporta tutti i dati", - "Import All Data": "Importa tutti i dati" + "Import All Data": "Importa tutti i dati", + "Keep-Alive Time": "Tempo di mantenimento", + "5m": "5m", + "30m": "30m", + "Forever": "Per sempre" } diff --git a/src/_locales/ja/main.json b/src/_locales/ja/main.json index ea759bf3..3137c30f 100644 --- a/src/_locales/ja/main.json +++ b/src/_locales/ja/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "常にフローティングウィンドウを表示し、すべてのサイトアダプターでサイドバーを無効にします", "Allow ESC to close all floating windows": "ESCキーですべてのフローティングウィンドウを閉じる", "Export All Data": "すべてのデータをエクスポート", - "Import All Data": "すべてのデータをインポート" + "Import All Data": "すべてのデータをインポート", + "Keep-Alive Time": "Keep-Alive時間", + "5m": "5分", + "30m": "30分", + "Forever": "永久" } diff --git a/src/_locales/ko/main.json b/src/_locales/ko/main.json index 29ff50fe..56cca928 100644 --- a/src/_locales/ko/main.json +++ b/src/_locales/ko/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "항상 떠다니는 창을 표시하고 모든 사이트 어댑터의 사이드바를 비활성화합니다", "Allow ESC to close all floating windows": "ESC를 눌러 모든 떠다니는 창을 닫도록 허용", "Export All Data": "모든 데이터 내보내기", - "Import All Data": "모든 데이터 가져오기" + "Import All Data": "모든 데이터 가져오기", + "Keep-Alive Time": "Keep-Alive 시간", + "5m": "5분", + "30m": "30분", + "Forever": "영원히" } diff --git a/src/_locales/pt/main.json b/src/_locales/pt/main.json index 0d4d4012..c88971e9 100644 --- a/src/_locales/pt/main.json +++ b/src/_locales/pt/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "Sempre exibir janela flutuante, desativar barra lateral para todos os adaptadores de site", "Allow ESC to close all floating windows": "Permitir ESC para fechar todas as janelas flutuantes", "Export All Data": "Exportar Todos os Dados", - "Import All Data": "Importar Todos os Dados" + "Import All Data": "Importar Todos os Dados", + "Keep-Alive Time": "Tempo de Manutenção de Conexão", + "5m": "5m", + "30m": "30m", + "Forever": "Para sempre" } diff --git a/src/_locales/ru/main.json b/src/_locales/ru/main.json index 47da012b..ec4e06c8 100644 --- a/src/_locales/ru/main.json +++ b/src/_locales/ru/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "Всегда отображать плавающее окно, отключить боковую панель для всех адаптеров сайтов", "Allow ESC to close all floating windows": "Разрешить ESC для закрытия всех плавающих окон", "Export All Data": "Экспорт всех данных", - "Import All Data": "Импорт всех данных" + "Import All Data": "Импорт всех данных", + "Keep-Alive Time": "Время поддержания активности", + "5m": "5m", + "30m": "30m", + "Forever": "Вечно" } diff --git a/src/_locales/tr/main.json b/src/_locales/tr/main.json index c3798e43..68ea5bd8 100644 --- a/src/_locales/tr/main.json +++ b/src/_locales/tr/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "Her zaman kayan pencereyi görüntüle, tüm site adaptörleri için kenar çubuğunu devre dışı bırak", "Allow ESC to close all floating windows": "ESC tuşuyla tüm kayan pencereleri kapatmaya izin ver", "Export All Data": "Tüm Verileri Dışa Aktar", - "Import All Data": "Tüm Verileri İçe Aktar" + "Import All Data": "Tüm Verileri İçe Aktar", + "Keep-Alive Time": "Canlı Tutma Süresi", + "5m": "5m", + "30m": "30m", + "Forever": "Sonsuza dek" } diff --git a/src/_locales/zh-hans/main.json b/src/_locales/zh-hans/main.json index 29796c37..ff41ec53 100644 --- a/src/_locales/zh-hans/main.json +++ b/src/_locales/zh-hans/main.json @@ -77,7 +77,6 @@ "ChatGPT (GPT-4-8k)": "ChatGPT (GPT-4-8k)", "ChatGPT (GPT-4-32k)": "ChatGPT (GPT-4-32k)", "GPT-3.5": "GPT-3.5", - "Ollama API": "Ollama API", "Custom Model": "自定义模型", "Balanced": "平衡", "Creative": "有创造力", @@ -148,8 +147,7 @@ "Allow ESC to close all floating windows": "允许按ESC关闭所有浮动窗口", "Export All Data": "导出所有数据", "Import All Data": "导入所有数据", - "Temperature": "温度", - "keep-alive Time": "保活时间", + "Keep-Alive Time": "保活时间", "5m": "5分钟", "30m": "半小时", "Forever": "永久" diff --git a/src/_locales/zh-hant/main.json b/src/_locales/zh-hant/main.json index 0236dc85..65246abe 100644 --- a/src/_locales/zh-hant/main.json +++ b/src/_locales/zh-hant/main.json @@ -146,5 +146,9 @@ "Always display floating window, disable sidebar for all site adapters": "總是顯示浮動視窗,停用所有網站適配器的側邊欄", "Allow ESC to close all floating windows": "允許按 ESC 關閉所有浮動視窗", "Export All Data": "匯出所有資料", - "Import All Data": "匯入所有資料" + "Import All Data": "匯入所有資料", + "Keep-Alive Time": "保持連線時間", + "5m": "5 分鐘", + "30m": "30 分鐘", + "Forever": "永遠" } diff --git a/src/background/index.mjs b/src/background/index.mjs index efbd928e..2639c70b 100644 --- a/src/background/index.mjs +++ b/src/background/index.mjs @@ -127,13 +127,7 @@ async function executeApi(session, port, config) { config.customModelName, ) } else if (ollamaApiModelKeys.includes(session.modelName)) { - await generateAnswersWithOllamaApi( - port, - session.question, - session, - config.ollamaApiKey, - config.ollamaModelName, - ) + await generateAnswersWithOllamaApi(port, session.question, session) } else if (azureOpenAiApiModelKeys.includes(session.modelName)) { await generateAnswersWithAzureOpenaiApi(port, session.question, session) } else if (claudeApiModelKeys.includes(session.modelName)) { diff --git a/src/config/index.mjs b/src/config/index.mjs index d0fa1b87..23ce1f76 100644 --- a/src/config/index.mjs +++ b/src/config/index.mjs @@ -251,7 +251,8 @@ export const defaultConfig = { ollamaEndpoint: 'http://127.0.0.1:11434', ollamaModelName: 'llama3.1', - keepAliveTime: '5m', + ollamaApiKey: '', + ollamaKeepAliveTime: '5m', // advanced diff --git a/src/popup/sections/AdvancedPart.jsx b/src/popup/sections/AdvancedPart.jsx index 4623fd75..4149528b 100644 --- a/src/popup/sections/AdvancedPart.jsx +++ b/src/popup/sections/AdvancedPart.jsx @@ -1,7 +1,5 @@ -import '../styles.scss' import { useTranslation } from 'react-i18next' import { parseFloatWithClamp, parseIntWithClamp } from '../../utils/index.mjs' -import { isUsingOllamaModel } from '../../config/index.mjs' import PropTypes from 'prop-types' import { Tab, TabList, TabPanel, Tabs } from 'react-tabs' import Browser from 'webextension-polyfill' @@ -58,49 +56,6 @@ function ApiParams({ config, updateConfig }) { }} /> - {isUsingOllamaModel(config) && ( - - )} ) } diff --git a/src/popup/sections/GeneralPart.jsx b/src/popup/sections/GeneralPart.jsx index ef6d1b03..7b566f80 100644 --- a/src/popup/sections/GeneralPart.jsx +++ b/src/popup/sections/GeneralPart.jsx @@ -350,6 +350,47 @@ export function GeneralPart({ config, updateConfig }) { }} /> )} + {isUsingOllamaModel(config) && ( +
+ {t('Keep-Alive Time') + ':'} + + + +
+ )} {isUsingOllamaModel(config) && ( { - finished = true - pushRecord(session, question, answer) - console.debug('conversation history', { content: session.conversationRecords }) - port.postMessage({ answer: null, done: true, session: session }) - } - await fetchSSE(`${apiUrl}/api/chat`, { - method: 'POST', - signal: controller.signal, - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${apiKey}`, + return generateAnswersWithChatgptApiCompat( + config.ollamaEndpoint + '/v1', + port, + question, + session, + config.ollamaApiKey, + 'ollamaModel', + { + model: config.ollamaModelName, }, - body: JSON.stringify({ - messages: prompt, - model: modelName, - stream: true, - max_tokens: config.maxResponseTokenLength, - temperature: config.temperature, - keep_alive: config.keepAliveTime === '-1' ? -1 : config.keepAliveTime, + ).then(() => + fetch(config.ollamaEndpoint + '/api/generate', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${config.ollamaApiKey}`, + }, + body: JSON.stringify({ + model: config.ollamaModelName, + prompt: 't', + options: { + num_predict: 1, + }, + keep_alive: config.ollamaKeepAliveTime === '-1' ? -1 : config.ollamaKeepAliveTime, + }), }), - onMessage(message) { - console.debug('sse message', message) - if (finished) return - let data = message - const delta = data.message?.content - if (delta) { - answer += delta - port.postMessage({ answer: answer, done: false, session: null }) - } - if (data.done_reason) { - finish() - return - } - }, - async onStart() {}, - async onEnd() { - port.postMessage({ done: true }) - port.onMessage.removeListener(messageListener) - port.onDisconnect.removeListener(disconnectListener) - }, - async onError(resp) { - port.onMessage.removeListener(messageListener) - port.onDisconnect.removeListener(disconnectListener) - if (resp instanceof Error) throw resp - const error = await resp.json().catch(() => ({})) - throw new Error(!isEmpty(error) ? JSON.stringify(error) : `${resp.status} ${resp.statusText}`) - }, - }) + ) } diff --git a/src/services/apis/openai-api.mjs b/src/services/apis/openai-api.mjs index 43d6b820..2b594207 100644 --- a/src/services/apis/openai-api.mjs +++ b/src/services/apis/openai-api.mjs @@ -125,6 +125,7 @@ export async function generateAnswersWithChatgptApiCompat( session, apiKey, modelName, + extraBody = {}, ) { const { controller, messageListener, disconnectListener } = setAbortController(port) @@ -157,6 +158,7 @@ export async function generateAnswersWithChatgptApiCompat( stream: true, max_tokens: config.maxResponseTokenLength, temperature: config.temperature, + ...extraBody, }), onMessage(message) { console.debug('sse message', message) diff --git a/src/utils/fetch-ollama.mjs b/src/utils/fetch-ollama.mjs deleted file mode 100644 index 49fbd331..00000000 --- a/src/utils/fetch-ollama.mjs +++ /dev/null @@ -1,27 +0,0 @@ -export async function fetchSSE(resource, options) { - const { onMessage, onStart, onEnd, onError, ...fetchOptions } = options - const resp = await fetch(resource, fetchOptions).catch(async (err) => { - await onError(err) - }) - if (!resp) return - if (!resp.ok) { - await onError(resp) - return - } - let hasStarted = false - const reader = resp.body.getReader() - let result - while (!(result = await reader.read()).done) { - const chunk = result.value - const str = new TextDecoder().decode(chunk) - if (!hasStarted) { - const str = new TextDecoder().decode(chunk) - hasStarted = true - await onStart(str) - } - let data = JSON.parse(str) - onMessage(data) - if (data.done) break - } - await onEnd() -} diff --git a/src/utils/fetch-sse.mjs b/src/utils/fetch-sse.mjs index 13df99f7..059cc043 100644 --- a/src/utils/fetch-sse.mjs +++ b/src/utils/fetch-sse.mjs @@ -33,7 +33,6 @@ export async function fetchSSE(resource, options) { console.debug('not common response', error) } if (fakeSseData) { - console.log('FAKE') parser.feed(new TextEncoder().encode(fakeSseData)) break }