Skip to content

Commit 6beed32

Browse files
committed
print score in chat
This makes it easier to tweak the similarity threshold
1 parent c9f3c70 commit 6beed32

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

action.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3+
use dokuwiki\ErrorHandler;
34
use dokuwiki\Extension\ActionPlugin;
4-
use dokuwiki\Extension\EventHandler;
55
use dokuwiki\Extension\Event;
6+
use dokuwiki\Extension\EventHandler;
67
use dokuwiki\Logger;
7-
use dokuwiki\ErrorHandler;
88
use dokuwiki\plugin\aichat\Chunk;
99

1010
/**
@@ -41,7 +41,7 @@ public function handleQuestion(Event $event, mixed $param)
4141
$helper = plugin_load('helper', 'aichat');
4242

4343
$question = $INPUT->post->str('question');
44-
$history = json_decode((string) $INPUT->post->str('history'), null, 512, JSON_THROW_ON_ERROR);
44+
$history = json_decode((string)$INPUT->post->str('history'), null, 512, JSON_THROW_ON_ERROR);
4545
header('Content-Type: application/json');
4646

4747
if (!$helper->userMayAccess()) {
@@ -58,15 +58,21 @@ public function handleQuestion(Event $event, mixed $param)
5858
$sources = [];
5959
foreach ($result['sources'] as $source) {
6060
/** @var Chunk $source */
61-
$sources[wl($source->getPage())] = p_get_first_heading($source->getPage()) ?: $source->getPage();
61+
if(isset($sources[$source->getPage()])) continue; // only show the first occurrence per page
62+
$sources[$source->getPage()] = [
63+
'page' => $source->getPage(),
64+
'url' => wl($source->getPage()),
65+
'title' => p_get_first_heading($source->getPage()) ?: $source->getPage(),
66+
'score' => sprintf("%.2f%%", $source->getScore()*100),
67+
];
6268
}
6369
$parseDown = new Parsedown();
6470
$parseDown->setSafeMode(true);
6571

6672
echo json_encode([
6773
'question' => $result['question'],
6874
'answer' => $parseDown->text($result['answer']),
69-
'sources' => $sources,
75+
'sources' => array_values($sources),
7076
], JSON_THROW_ON_ERROR);
7177

7278
if ($this->getConf('logging')) {

script/AIChatChat.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,14 @@ class AIChatChat extends HTMLElement {
288288
div.textContent = message;
289289
}
290290

291-
if (sources !== null && Object.keys(sources).length > 0) {
291+
if (sources !== null && sources.length > 0) {
292292
const ul = document.createElement('ul');
293-
Object.entries(sources).forEach(([url, title]) => {
293+
sources.forEach((source) => {
294294
const li = document.createElement('li');
295295
const a = document.createElement('a');
296-
a.href = url;
297-
a.textContent = title;
296+
a.href = source.url;
297+
a.textContent = source.title;
298+
a.title = `${source.page} (${source.score})`;
298299
li.appendChild(a);
299300
ul.appendChild(li);
300301
});

0 commit comments

Comments
 (0)