Skip to content

Commit 22bf492

Browse files
committed
fix: gemini-1.0-pro model cannot use system instruction
1 parent a6dcd5d commit 22bf492

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Easydict/Swift/Service/Gemini/GeminiService.swift

+13-6
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@ public final class GeminiService: LLMStreamService {
3939
result.isStreamFinished = false
4040

4141
let queryType = queryType(text: text, from: from, to: to)
42-
let translationPrompt = promptContent(queryType: queryType, text: text, from: from, to: to)
43-
let systemInstruction = queryType == .dictionary ? LLMStreamService.dictSystemPrompt : LLMStreamService
42+
let chatHistory = promptContent(queryType: queryType, text: text, from: from, to: to)
43+
let systemPrompt = queryType == .dictionary ? LLMStreamService
44+
.dictSystemPrompt : LLMStreamService
4445
.translationSystemPrompt
46+
47+
var systemInstruction: ModelContent? = try ModelContent(role: "system", systemPrompt)
48+
49+
// !!!: gemini-1.0-pro model does not support system instruction https://github.com/google-gemini/generative-ai-python/issues/328
50+
if model == GeminiModel.gemini1_0_pro.rawValue {
51+
systemInstruction = nil
52+
}
53+
4554
let model = GenerativeModel(
4655
name: model,
4756
apiKey: apiKey,
@@ -58,7 +67,7 @@ public final class GeminiService: LLMStreamService {
5867

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

61-
let outputContentStream = model.generateContentStream(translationPrompt)
70+
let outputContentStream = model.generateContentStream(chatHistory)
6271
for try await outputContent in outputContentStream {
6372
guard let line = outputContent.text else {
6473
return
@@ -196,9 +205,7 @@ extension GeminiService {
196205
chats.append(chat)
197206
}
198207
}
199-
guard !chats.isEmpty else {
200-
return chats
201-
}
208+
202209
return chats
203210
}
204211
}

0 commit comments

Comments
 (0)