Skip to content

Commit fbd2cc5

Browse files
committed
Validate summary temperature and provide better logging for summary retrieval from Ollama and OpenAI APIs
1 parent 3ccbedf commit fbd2cc5

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

classes/webpage-analyzer.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ public function getSummary() {
507507
!empty(OLLAMA_URL) &&
508508
!empty(OLLAMA_MODEL)
509509
) {
510+
$this->log->info('Trying to get summary from Ollama API for ' . $this->url);
510511
$model_is_available = false;
511512
$ollama_tags_url = OLLAMA_URL . '/api/tags';
512513
$curl_options = array(
@@ -551,19 +552,24 @@ public function getSummary() {
551552
$curl_response = curlURL($ollama_api_url, $curl_options) ?? '';
552553
if (!$curl_response) {
553554
$this->log->error('Ollama API response is empty or invalid');
554-
$summary = '';
555-
}
556-
$response = json_decode($curl_response, true);
557-
if (!empty($response['response']) && strlen($response['response']) > 100) {
558-
$summary = $response['response'];
559-
$summary_provider = 'ollama';
560-
$summary_model = OLLAMA_MODEL;
561-
$this->log->info("Summary for URL $this->url generated by Ollama model $summary_model");
555+
} else {
556+
$response = json_decode($curl_response, true);
557+
if (!$response) {
558+
$this->log->error('Ollama API response is not valid JSON');
559+
} elseif (!empty($response['response']) && strlen($response['response']) > 100) {
560+
$summary = $response['response'];
561+
$summary_provider = 'ollama';
562+
$summary_model = OLLAMA_MODEL;
563+
$this->log->info("Summary for URL $this->url generated by Ollama model $summary_model");
564+
} else {
565+
$this->log->error('Ollama API response does not contain valid summary content');
566+
}
562567
}
563568
}
564569
}
565570
}
566571
if(!$summary && !empty(OPENAI_API_KEY)) {
572+
$this->log->info('Trying to get summary from OpenAI API for ' . $this->url);
567573
$openai_url = 'https://api.openai.com/v1/chat/completions';
568574
$openai_model = OPENAI_API_MODEL ?? 'gpt-4o-mini';
569575
$curl_options = array(
@@ -599,6 +605,12 @@ public function getSummary() {
599605
$summary_provider = 'openai';
600606
$summary_model = $openai_model;
601607
$this->log->info("Summary for URL $this->url generated by OpenAI model $summary_model");
608+
} else {
609+
if(!empty($response['error']['message'])) {
610+
$this->log->error('OpenAI API response contains an error: ' . $response['error']['message']);
611+
} else {
612+
$this->log->error('OpenAI API response does not contain valid summary content');
613+
}
602614
}
603615
}
604616
if (!$summary) {

config.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
// Set defaults
14-
const UPVOTE_RSS_VERSION = '1.2.4';
14+
const UPVOTE_RSS_VERSION = '1.2.5';
1515
const DEFAULT_PLATFORM = 'lemmy';
1616
const DEFAULT_HACKER_NEWS_INSTANCE = 'news.ycombinator.com';
1717
const DEFAULT_HACKER_NEWS_COMMUNITY = 'beststories';
@@ -295,6 +295,10 @@
295295

296296
// Summary temperature
297297
$summary_temperature = $_SERVER["SUMMARY_TEMPERATURE"] ?? $_ENV["SUMMARY_TEMPERATURE"] ?? 0.4;
298+
$summary_temperature = floatval($summary_temperature);
299+
if (!($summary_temperature > 0 && $summary_temperature <= 1)) {
300+
$summary_temperature = 0.4;
301+
}
298302
define('SUMMARY_TEMPERATURE', $summary_temperature);
299303

300304

0 commit comments

Comments
 (0)