Skip to content

Commit

Permalink
Add Stream Option to Gemini Model (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
MUTED64 authored Oct 22, 2024
1 parent 7a6aa86 commit 7be42e2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions addon/locale/en-US/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ service-gemini-secret-fail=Config
service-gemini-dialog-title=Gemini Config
service-gemini-dialog-endPoint=EndPoint
service-gemini-dialog-prompt=Prompt
service-gemini-dialog-stream=Stream
service-gemini-dialog-save=Save
service-gemini-dialog-close=Close
Expand Down
1 change: 1 addition & 0 deletions addon/locale/it-IT/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ service-gemini-secret-fail=Configura
service-gemini-dialog-title=Configura Gemini
service-gemini-dialog-endPoint=EndPoint
service-gemini-dialog-prompt=Prompt
service-gemini-dialog-stream=Stream
service-gemini-dialog-save=Salva
service-gemini-dialog-close=Chiudi
Expand Down
1 change: 1 addition & 0 deletions addon/locale/zh-CN/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ service-gemini-secret-fail=配置
service-gemini-dialog-title=Gemini 配置
service-gemini-dialog-endPoint=接口
service-gemini-dialog-prompt=Prompt
service-gemini-dialog-stream=流式输出
service-gemini-dialog-save=保存
service-gemini-dialog-close=关闭
Expand Down
1 change: 1 addition & 0 deletions addon/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pref(
"__prefsPrefix__.gemini.prompt",
"As an academic expert with specialized knowledge in various fields, please provide a proficient and precise translation translation from ${langFrom} to ${langTo} of the academic text enclosed in 🔤. It is crucial to maintaining the original phrase or sentence and ensure accuracy while utilizing the appropriate language. The text is as follows: 🔤 ${sourceText} 🔤 Please provide the translated result without any additional explanation and remove 🔤.",
);
pref("__prefsPrefix__.gemini.stream", true);
pref(
"__prefsPrefix__.cnkiRegex",
"(查看名企职位.+?https://dict.cnki.net[a-zA-Z./]+.html?)",
Expand Down
11 changes: 10 additions & 1 deletion src/modules/services/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ const geminiTranslate = async function (
.replaceAll("${sourceText}", sourceText);
}

function getGenContentAPI(data: Required<TranslateTask>) {
const stream = getPref("gemini.stream") as boolean;
if (stream) {
return apiURL + `:streamGenerateContent?alt=sse&key=${data.secret}`;
} else {
return apiURL + `:generateContent?key=${data.secret}`;
}
}

const xhr = await Zotero.HTTP.request(
"POST",
apiURL + `:streamGenerateContent?alt=sse&key=${data.secret}`,
getGenContentAPI(data),
{
headers: {
"Content-Type": "application/json",
Expand Down
27 changes: 26 additions & 1 deletion src/modules/settings/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ async function gptStatusCallback(prefix: "gemini", status: boolean) {
const dialogData: { [key: string | number]: any } = {
endPoint: getPref(`${prefix}.endPoint`),
prompt: getPref(`${prefix}.prompt`),
stream: getPref(`${prefix}.stream`),
};

dialog
Expand All @@ -20,8 +21,11 @@ async function gptStatusCallback(prefix: "gemini", status: boolean) {
styles: {
display: "grid",
gridTemplateColumns: "1fr 4fr",
gridTemplateRows: "auto 1fr auto",
rowGap: "10px",
columnGap: "5px",
minWidth: "400px",
minHeight: "200px",
},
children: [
{
Expand Down Expand Up @@ -55,13 +59,33 @@ async function gptStatusCallback(prefix: "gemini", status: boolean) {
},
},
{
tag: "input",
tag: "textarea",
id: "prompt",
attributes: {
"data-bind": "prompt",
"data-prop": "value",
},
},

{
tag: "label",
namespace: "html",
attributes: {
for: "stream",
},
properties: {
innerHTML: getString(`service-${addonPrefix}-dialog-stream`),
},
},
{
tag: "input",
id: "stream",
attributes: {
"data-bind": "stream",
"data-prop": "checked",
type: "checkbox",
},
},
],
},
false,
Expand All @@ -77,6 +101,7 @@ async function gptStatusCallback(prefix: "gemini", status: boolean) {
{
setPref(`${prefix}.endPoint`, dialogData.endPoint);
setPref(`${prefix}.prompt`, dialogData.prompt);
setPref(`${prefix}.stream`, dialogData.stream);
}
break;
default:
Expand Down

0 comments on commit 7be42e2

Please sign in to comment.