-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from UN-OCHA/develop
v1.7.0
- Loading branch information
Showing
10 changed files
with
478 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
ocha_ai_chat.commands: | ||
class: \Drupal\ocha_ai_chat\Commands\ReliefwebAiChatCommands | ||
arguments: ['@entity_type.manager', '@ocha_ai_chat.chat'] | ||
tags: | ||
- { name: drush.command } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
modules/ocha_ai_chat/src/Commands/ReliefwebAiChatCommands.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Drupal\ocha_ai_chat\Commands; | ||
|
||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\ocha_ai_chat\Services\OchaAiChat; | ||
use Drush\Commands\DrushCommands; | ||
|
||
/** | ||
* Analyze AI Chat logs. | ||
*/ | ||
class ReliefwebAiChatCommands extends DrushCommands { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct( | ||
protected EntityTypeManagerInterface $entityTypeManager, | ||
protected OchaAiChat $ochaChat, | ||
) {} | ||
|
||
/** | ||
* Analyze logs. | ||
* | ||
* @command ocha-ai-chat:analyze-logs | ||
* | ||
* @usage ocha-ai-chat:analyze-logs | ||
* Analyze logs. | ||
* | ||
* @validate-module-enabled ocha_ai_chat | ||
*/ | ||
public function analyzeLogs(string $filename = '/var/www/html/rw-chat-logs.tsv', string $filename_out = '/var/www/html/rw-chat-rerun.tsv') { | ||
if (!file_exists($filename)) { | ||
$this->output->writeln('File not found: ' . $filename); | ||
} | ||
|
||
$out = fopen($filename_out, 'w'); | ||
|
||
$f = fopen($filename, 'r'); | ||
$header = fgetcsv($f, NULL, "\t"); | ||
$header_lowercase = array_map('strtolower', $header); | ||
$header_lowercase[] = 'url'; | ||
$header_lowercase[] = 'new answer'; | ||
$header_lowercase[] = 'new status'; | ||
$header_lowercase[] = 'original_answer'; | ||
|
||
fputcsv($out, $header_lowercase, "\t"); | ||
|
||
// Get data. | ||
$count = 0; | ||
while (($row = fgetcsv($f, NULL, "\t")) && $count < 1000) { | ||
$data = []; | ||
for ($i = 0; $i < count($row); $i++) { | ||
$data[$header_lowercase[$i]] = $row[$i]; | ||
} | ||
|
||
if (!isset($data['status']) || $data['status'] == 'success') { | ||
continue; | ||
} | ||
|
||
$source_data = json_decode($data['source_data'], TRUE); | ||
$data['url'] = $source_data['url']; | ||
$data['url'] = str_replace('https://reliefweb.int/updates?search=url_alias:', '', $data['url']); | ||
$data['url'] = str_replace('"', '', $data['url']); | ||
|
||
$count++; | ||
$this->output->writeln($count . '. ' . $data['url']); | ||
|
||
$question = $data['question']; | ||
$answer = $this->ochaChat->answer($question, $source_data); | ||
|
||
$data['new answer'] = $answer['answer']; | ||
$data['new status'] = $answer['status']; | ||
$data['new original_answer'] = $answer['original_answer']; | ||
fputcsv($out, $data, "\t"); | ||
} | ||
|
||
fclose($f); | ||
fclose($out); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.