Skip to content

Commit

Permalink
fix: gemini-1.0-pro model cannot use system instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Jun 4, 2024
1 parent a6dcd5d commit 22bf492
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions Easydict/Swift/Service/Gemini/GeminiService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ public final class GeminiService: LLMStreamService {
result.isStreamFinished = false

let queryType = queryType(text: text, from: from, to: to)
let translationPrompt = promptContent(queryType: queryType, text: text, from: from, to: to)
let systemInstruction = queryType == .dictionary ? LLMStreamService.dictSystemPrompt : LLMStreamService
let chatHistory = promptContent(queryType: queryType, text: text, from: from, to: to)
let systemPrompt = queryType == .dictionary ? LLMStreamService
.dictSystemPrompt : LLMStreamService
.translationSystemPrompt

var systemInstruction: ModelContent? = try ModelContent(role: "system", systemPrompt)

// !!!: gemini-1.0-pro model does not support system instruction https://github.com/google-gemini/generative-ai-python/issues/328
if model == GeminiModel.gemini1_0_pro.rawValue {
systemInstruction = nil
}

let model = GenerativeModel(
name: model,
apiKey: apiKey,
Expand All @@ -58,7 +67,7 @@ public final class GeminiService: LLMStreamService {

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

let outputContentStream = model.generateContentStream(translationPrompt)
let outputContentStream = model.generateContentStream(chatHistory)
for try await outputContent in outputContentStream {
guard let line = outputContent.text else {
return
Expand Down Expand Up @@ -196,9 +205,7 @@ extension GeminiService {
chats.append(chat)
}
}
guard !chats.isEmpty else {
return chats
}

return chats
}
}

0 comments on commit 22bf492

Please sign in to comment.