Skip to content

Commit

Permalink
Merge pull request #92 from UN-OCHA/develop
Browse files Browse the repository at this point in the history
v1.7.0
  • Loading branch information
orakili authored Oct 22, 2024
2 parents 839856d + 507a2d0 commit 429d086
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ defaults:
format: 'plain_text'
feedback: ''
formatting: 'basic'
retrieval_mode: 'embeddings'
answers:
no_document: 'Sorry, no source documents were found.'
no_passage: 'Sorry, I could not find information to answer the question.'
no_answer: 'Sorry, I was unable to answer your question. Please try again in a short moment.'
invalid_answer: 'Sorry, I was unable to answer your question.'
document_embedding_error: 'Sorry, there was an error trying to retrieve the documents to the answer to your question.'
document_embedding_error: 'Sorry, there was an error trying to retrieve the documents to answer your question.'
question_embedding_error: 'Sorry, there was an error trying to process the qestion.'
plugins:
answer_validator:
Expand Down
3 changes: 3 additions & 0 deletions modules/ocha_ai_chat/config/schema/ocha_ai_chat.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ ocha_ai_chat.settings:
formatting:
type: string
label: 'Formatting mode.'
retrieval_mode:
type: string
label: 'Passage retrieval mode: embeddings or keywords.'
answers:
type: mapping
mapping:
Expand Down
6 changes: 6 additions & 0 deletions modules/ocha_ai_chat/drush.services.yml
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 }
1 change: 1 addition & 0 deletions modules/ocha_ai_chat/ocha_ai_chat.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
- '@current_user'
- '@database'
- '@datetime.time'
- '@http_client'
- '@plugin.manager.ocha_ai.answer_validator'
- '@plugin.manager.ocha_ai.completion'
- '@plugin.manager.ocha_ai.embedding'
Expand Down
82 changes: 82 additions & 0 deletions modules/ocha_ai_chat/src/Commands/ReliefwebAiChatCommands.php
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);
}

}
13 changes: 13 additions & 0 deletions modules/ocha_ai_chat/src/Form/OchaAiChatConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#description' => $this->t('Basic formatting means that the module takes the answer from the LLM and restores line breaks within HTML.'),
];

// Passage retrieval mode.
$form['defaults']['form']['retrieval_mode'] = [
'#type' => 'select',
'#title' => $this->t('Passage retrieval mode.'),
'#default_value' => $defaults['form']['retrieval_mode'] ?? '',
'#options' => [
'embeddings' => $this->t('Embeddings'),
'keywords' => $this->t('Keywords'),
],
'#description' => $this->t('Retrieve relevant passages using embeddings or keywords.'),
];

// Default answers when there is an error for example.
$form['defaults']['form']['answers'] = [
'#type' => 'details',
'#title' => $this->t('Answers'),
Expand Down
Loading

0 comments on commit 429d086

Please sign in to comment.