Skip to content

Commit 0d601b4

Browse files
committed
Add Google Gemini as a summary provider
1 parent fbd2cc5 commit 0d601b4

File tree

3 files changed

+101
-28
lines changed

3 files changed

+101
-28
lines changed

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,6 @@ REDDIT_CLIENT_ID=your_reddit_client_id
157157
# Default value: (empty)
158158
REDDIT_CLIENT_SECRET=your_reddit_client_secret
159159

160-
# OpenAI API key
161-
# Used to connect to OpenAI for article summaries when the "Include summary" checkbox is checked
162-
# Default value: (empty)
163-
OPENAI_API_KEY=your_openai_api_key
164-
165-
# OpenAI API model
166-
# Model used for article summaries
167-
# Default value: gpt-4o-mini
168-
OPENAI_API_MODEL=specified_openai_model
169-
170160
# Ollama URL
171161
# URL to your Ollama instance for article summaries
172162
# Default value: (empty)
@@ -177,6 +167,26 @@ OLLAMA_URL=your_ollama_url
177167
# Default value: (empty)
178168
OLLAMA_MODEL=specified_ollama_model
179169

170+
# Google Gemini API key
171+
# Used to connect to Google Gemini for article summaries when the "Include summary" checkbox is checked
172+
# Default value: (empty)
173+
GOOGLE_GEMINI_API_KEY=your_google_gemini_api_key
174+
175+
# Google Gemini API model
176+
# Model used for article summaries
177+
# Default value: gemini-2.5-flash
178+
GOOGLE_GEMINI_API_MODEL=specified_google_gemini_model
179+
180+
# OpenAI API key
181+
# Used to connect to OpenAI for article summaries when the "Include summary" checkbox is checked
182+
# Default value: (empty)
183+
OPENAI_API_KEY=your_openai_api_key
184+
185+
# OpenAI API model
186+
# Model used for article summaries
187+
# Default value: gpt-4o-mini
188+
OPENAI_API_MODEL=specified_openai_model
189+
180190
# Summary system prompt
181191
# The prompt that guides the LLM model to give accurate-ish and concise article summaries
182192
# Default value: You are web article summarizer. Use the following pieces of retrieved context to answer the question. Do not answer from your own knowledge base. If the answer isn't present in the knowledge base, refrain from providing an answer based on your own knowledge. Instead, say nothing. Output should be limited to one paragraph with a maximum of three sentences, and keep the answer concise. Always complete the last sentence. Do not hallucinate or make up information.

classes/webpage-analyzer.php

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,60 @@ public function getSummary() {
568568
}
569569
}
570570
}
571+
if(!$summary && !empty(GOOGLE_GEMINI_API_KEY)) {
572+
$this->log->info('Trying to get summary from Google Gemini API for ' . $this->url);
573+
$gemini_url = 'https://generativelanguage.googleapis.com/v1beta/openai/chat/completions';
574+
$gemini_model = GOOGLE_GEMINI_API_MODEL ?? 'gemini-2.5-flash';
575+
$curl_options = array(
576+
CURLOPT_CUSTOMREQUEST => 'POST',
577+
CURLOPT_POSTFIELDS => json_encode(array(
578+
'max_tokens' => 1000,
579+
'temperature' => SUMMARY_TEMPERATURE,
580+
'model' => $gemini_model,
581+
'messages' => [
582+
array(
583+
'role' => 'system',
584+
'content' => SUMMARY_SYSTEM_PROMPT
585+
),
586+
array(
587+
'role' => 'user',
588+
'content' => $parsed_content
589+
)
590+
]
591+
)),
592+
CURLOPT_HTTPHEADER => array(
593+
'Content-Type: application/json',
594+
'Authorization: Bearer ' . GOOGLE_GEMINI_API_KEY
595+
)
596+
);
597+
$curl_response = curlURL($gemini_url, $curl_options);
598+
if (!$curl_response) {
599+
$this->log->error('Google Gemini API response is empty or invalid');
600+
} else {
601+
$response = json_decode($curl_response, true);
602+
if (!$response) {
603+
$this->log->error('Google Gemini API response is not valid JSON');
604+
} elseif (!empty($response['choices'][0]['message']['content']) && strlen($response['choices'][0]['message']['content']) > 100) {
605+
$summary = $response['choices'][0]['message']['content'];
606+
$summary_provider = 'google_gemini';
607+
$summary_model = $gemini_model;
608+
$this->log->info("Summary for URL $this->url generated by Google Gemini model $summary_model");
609+
} else {
610+
if(!empty($response['error']['message'])) {
611+
$this->log->error('Google Gemini API response contains an error: ' . $response['error']['message']);
612+
} elseif (!empty($response['choices'][0]['finish_reason']) && $response['choices'][0]['finish_reason'] == 'length') {
613+
$total_tokens = $response['usage']['total_tokens'] ?? 'unknown';
614+
$this->log->error(
615+
'Google Gemini API response was cut off due to length limit. ' .
616+
'Try increasing SUMMARY_MAX_TOKENS. ' .
617+
'total_tokens: ' . $total_tokens . ', SUMMARY_MAX_TOKENS: ' . SUMMARY_MAX_TOKENS
618+
);
619+
} else {
620+
$this->log->error('Google Gemini API response does not contain valid summary content');
621+
}
622+
}
623+
}
624+
}
571625
if(!$summary && !empty(OPENAI_API_KEY)) {
572626
$this->log->info('Trying to get summary from OpenAI API for ' . $this->url);
573627
$openai_url = 'https://api.openai.com/v1/chat/completions';
@@ -597,19 +651,21 @@ public function getSummary() {
597651
$curl_response = curlURL($openai_url, $curl_options);
598652
if (!$curl_response) {
599653
$this->log->error('OpenAI API response is empty or invalid');
600-
$summary = '';
601-
}
602-
$response = json_decode($curl_response, true);
603-
if (!empty($response['choices'][0]['message']['content']) && strlen($response['choices'][0]['message']['content']) > 100){
604-
$summary = $response['choices'][0]['message']['content'];
605-
$summary_provider = 'openai';
606-
$summary_model = $openai_model;
607-
$this->log->info("Summary for URL $this->url generated by OpenAI model $summary_model");
608654
} else {
609-
if(!empty($response['error']['message'])) {
610-
$this->log->error('OpenAI API response contains an error: ' . $response['error']['message']);
655+
$response = json_decode($curl_response, true);
656+
if (!$response) {
657+
$this->log->error('OpenAI API response is not valid JSON');
658+
} elseif (!empty($response['choices'][0]['message']['content']) && strlen($response['choices'][0]['message']['content']) > 100) {
659+
$summary = $response['choices'][0]['message']['content'];
660+
$summary_provider = 'openai';
661+
$summary_model = $openai_model;
662+
$this->log->info("Summary for URL $this->url generated by OpenAI model $summary_model");
611663
} else {
612-
$this->log->error('OpenAI API response does not contain valid summary content');
664+
if(!empty($response['error']['message'])) {
665+
$this->log->error('OpenAI API response contains an error: ' . $response['error']['message']);
666+
} else {
667+
$this->log->error('OpenAI API response does not contain valid summary content');
668+
}
613669
}
614670
}
615671
}

config.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,27 @@
260260
define('INCLUDE_CONTENT', $include_content);
261261

262262

263-
// OpenAI API
264-
$openai_api_key = $_SERVER["OPENAI_API_KEY"] ?? $_ENV["OPENAI_API_KEY"] ?? null;
265-
define('OPENAI_API_KEY', $openai_api_key);
266-
$openai_api_model = $_SERVER["OPENAI_API_MODEL"] ?? $_ENV["OPENAI_API_MODEL"] ?? 'gpt-4o-mini';
267-
define('OPENAI_API_MODEL', $openai_api_model);
268-
269-
270263
// Ollama API
271264
$ollama_url = $_SERVER["OLLAMA_URL"] ?? $_ENV["OLLAMA_URL"] ?? null;
272265
define('OLLAMA_URL', $ollama_url);
273266
$ollama_model = $_SERVER["OLLAMA_MODEL"] ?? $_ENV["OLLAMA_MODEL"] ?? null;
274267
define('OLLAMA_MODEL', $ollama_model);
275268

276269

270+
// Google Gemini API
271+
$gemini_api_key = $_SERVER["GOOGLE_GEMINI_API_KEY"] ?? $_ENV["GOOGLE_GEMINI_API_KEY"] ?? null;
272+
define('GOOGLE_GEMINI_API_KEY', $gemini_api_key);
273+
$gemini_api_model = $_SERVER["GOOGLE_GEMINI_API_MODEL"] ?? $_ENV["GOOGLE_GEMINI_API_MODEL"] ?? 'gemini-1';
274+
define('GOOGLE_GEMINI_API_MODEL', $gemini_api_model);
275+
276+
277+
// OpenAI API
278+
$openai_api_key = $_SERVER["OPENAI_API_KEY"] ?? $_ENV["OPENAI_API_KEY"] ?? null;
279+
define('OPENAI_API_KEY', $openai_api_key);
280+
$openai_api_model = $_SERVER["OPENAI_API_MODEL"] ?? $_ENV["OPENAI_API_MODEL"] ?? 'gpt-4o-mini';
281+
define('OPENAI_API_MODEL', $openai_api_model);
282+
283+
277284
// Summary enabled
278285
$summary_enabled = false;
279286
if (OPENAI_API_KEY !== null) $summary_enabled = true;

0 commit comments

Comments
 (0)