tisfeng / Easydict

一个简洁优雅的词典翻译 macOS App。开箱即用,支持离线 OCR 识别,支持有道词典,🍎 苹果系统词典,🍎 苹果系统翻译,OpenAI,Gemini,DeepL,Google,Bing,腾讯,百度,阿里,小牛,彩云和火山翻译。A concise and elegant Dictionary and Translator macOS App for looking up words and translating text.
GNU General Public License v3.0
6.38k stars 324 forks source link

feat: add new models for gemini translation #571

Closed Jerry23011 closed 4 weeks ago

Jerry23011 commented 1 month ago

closes #559

The settings pane is now finished and working.

I set the default model as gemini-1.5-flash as it has the same quota with 1.0 and offer better performance.

However, I don't know how to realize query type and display available models in the query view

tisfeng commented 1 month ago

Actually, we can easily enable LLMStreamService to change models quickly cc781170

image
Jerry23011 commented 1 month ago

Actually, we can easily enable LLMStreamService to change models quickly cc78117

image

That's nice

Jerry23011 commented 1 month ago

Speaking of built-in ai, I noticed it's using gemini pro 1.5 flash latest. Do we use latest for gemini too? Or do we stick with the stable build?

tisfeng commented 1 month ago

Currently Gemini has only one translationPrompt, which can only be used for translation.

Please refer to BaseOpenAIService to add prompt for querying sentences and words.

image
tisfeng commented 1 month ago

Speaking of built-in ai, I noticed it's using gemini pro 1.5 flash latest. Do we use latest for gemini too? Or do we stick with the stable build?

Just keep it this way. I noticed that gemini-1.5-flash is also used in the official documentation.

I use gemini-1.5-flash-latest in the built-in AI service because the gemini-1.5-flash API from Google was not yet available before, and using it directly would result in an error 😓

Jerry23011 commented 1 month ago

It's generally working but sometimes it doens't 100% comform to the format in the prompt in word translate

截屏2024-05-29 02 47 33 截屏2024-05-29 02 49 16
tisfeng commented 1 month ago

Although using a single prompt seems to work for now, I recommend referring to the official docs and switching to using ModelContent to record few-shot examples. Note that the roles are "user" and "model," which is different from OpenAI.

Additionally, Gemini also supports systemInstruction https://github.com/google-gemini/generative-ai-swift/pull/152 , please add that as well.

Jerry23011 commented 1 month ago

I'm confused at the moment. If we are using history, what are we inputing into the systemInstructions?

Also, Chat doesn't seem to support streaming? If we were to initialize model with startChat(history: ...), chat will become a Chat instead of GenerativeModel, and Chat doesn't support streaming.

截屏2024-05-30 00 56 37

So I'm thinking if we can just use systemInstructions for the few shots as it accepts a list of ModelContent Turns out systemInstructions only accepts String

tisfeng commented 1 month ago

No, the parameter is actually quite flexible, here is a simple example:

                let model = GenerativeModel(
                    name: model,
                    apiKey: apiKey,
                    safetySettings: [
                        harassmentBlockNone,
                        hateSpeechBlockNone,
                        sexuallyExplicitBlockNone,
                        dangerousContentBlockNone,
                    ],
+                    systemInstruction: LLMStreamService.translationSystemPrompt
                )

                var resultString = ""

                // Gemini Docs: https://github.com/google/generative-ai-swift

                let chatHistory = [
                    ModelContent(role: "user", parts: "Hello, I have 2 dogs in my house."),
                    ModelContent(role: "model", parts: "Great to meet you. What would you like to know?"),
                ]

+                let outputContentStream = model.generateContentStream(chatHistory)
Jerry23011 commented 1 month ago

Now it works pretty well

截屏2024-05-30 14 08 22
Jerry23011 commented 1 month ago

The streaming feature is broken. Let me take another look

Jerry23011 commented 1 month ago

I'm pretty sure it has something to do with handleResult(), but I don't know how to fix it yet.

Jerry23011 commented 1 month ago

It should be working now.

Jerry23011 commented 1 month ago

I'll take a look at these

tisfeng commented 1 month ago

I have improved the code, replace all AI const stored keys with dynamic variables a6dcd5d3 .

This code can be optimized later, as well as removing unused const keys.

Jerry23011 commented 1 month ago

So we need to replace all the strings 😂

tisfeng commented 1 month ago

Note that gemini-1.0-pro model does not support system instruction https://github.com/google-gemini/generative-ai-python/issues/328

So I change system prompt to user prompt for gemini-1.0-pro model, see in 22bf492d and 67a63f15 .

image
Jerry23011 commented 4 weeks ago

Note that gemini-1.0-pro model does not support system instruction google-gemini/generative-ai-python#328

So I change system prompt to user prompt for gemini-1.0-pro model, see in 22bf492 and 67a63f1 .

Got it, thank you :)

tisfeng commented 4 weeks ago

@Jerry23011 I haven't looked closely at the Gemini API, does it support canceling stream requests? Like this https://github.com/tisfeng/Easydict/pull/577

Jerry23011 commented 4 weeks ago

@Jerry23011 I haven't looked closely at the Gemini API, does it support canceling stream requests? Like this #577

I didn't find any, so I submitted an issue https://github.com/google-gemini/generative-ai-swift/issues/178

tisfeng commented 4 weeks ago

Good, their developers are very proactive, and next, we just need to wait for them to implement this feature 😃