Skip to content

Commit

Permalink
first RAG based response
Browse files Browse the repository at this point in the history
  • Loading branch information
mhughes2k committed Mar 18, 2024
1 parent c4abe25 commit 1978c36
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
19 changes: 15 additions & 4 deletions mod/xaichat/view.php

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion search/engine/solr/classes/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,8 @@ protected function add_solr_document($doc) {
debugging('Solr client error adding document with id ' . $doc['id'] . ': ' . $e->getMessage(), DEBUG_DEVELOPER);
} catch (\SolrServerException $e) {
// We only use the first line of the message, as it's a fully java stacktrace behind it.
$msg = strtok($e->getMessage(), "\n");
// $msg = strtok($e->getMessage(), "\n");
$msg = $e->getMessage();
debugging('Solr server error adding document with id ' . $doc['id'] . ': ' . $msg, DEBUG_DEVELOPER);
}

Expand Down
2 changes: 1 addition & 1 deletion search/engine/solrrag/classes/ai/aiclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function embed_query($content): array {
$rawresult = $this->post($this->get_embeddings_url(), $params);
// var_dump($rawresult);
$result = json_decode($rawresult, true);
var_dump($result);
// var_dump($result);
$usage = $result['usage'];
$this->provider->increment_prompt_usage($usage['prompt_tokens']);
$this->provider->increment_total_tokens($usage['total_tokens']);
Expand Down
39 changes: 25 additions & 14 deletions search/engine/solrrag/classes/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function execute_query($filters, $accessinfo, $limit = 0) {
) {
// Do a vector similarity search.
debugging("Running similarity search", DEBUG_DEVELOPER);
$this->execute_solr_knn_query($filters, $accessinfo, $limit);
return $this->execute_solr_knn_query($filters, $accessinfo, $limit);
} else {
debugging("Running regular search", DEBUG_DEVELOPER);
print_r($filters);
Expand All @@ -353,27 +353,26 @@ public function execute_solr_knn_query($filters, $accessinfo, $limit) {
$topK = 3; // Nearest neighbours to retrieve.
$field = "solr_vector_" . count($vector);
$requestbody = "{!knn f={$field} topK={$topK}}[" . implode(",", $vector) . "]";

$filters->mainquery = $requestbody;
if (empty($limit)) {
$limit = \core_search\manager::MAX_RESULTS;
}

$curl = $this->get_curl_object();
$requesturl = $this->get_connection_url('/select');
$requesturl->param('fl', 'id,areaid,score');
$requesturl->param('fl', 'id,areaid,score,content');
$requesturl->param('wt', 'xml');

$body = [
'query' => $requestbody
// $requesturl->param('query', $requestbody)
$params = [
"query" => $requestbody,
];
echo $requesturl->out(false);
$result = $curl->post($requesturl->out(false),
json_encode($body)
);
$curl->setHeader('Content-type: application/json');
$result = $curl->post($requesturl->out(false), json_encode($params));

// Probably have to duplicate error handling code from the add_stored_file() function.
$code = $curl->get_errno();
$info = $curl->get_info();

// Now error handling. It is just informational, since we aren't tracking per file/doc results.
if ($code != 0) {
// This means an internal cURL error occurred error is in result.
Expand Down Expand Up @@ -410,10 +409,22 @@ public function execute_solr_knn_query($filters, $accessinfo, $limit) {
// echo htmlentities($result);
// debugging("Got SOLR update/extract response");
$xml = simplexml_load_string($result);
print_r($xml->result);
print_r($xml->result['numFound']);
print_r($xml->result['maxScore']);

// echo "<pre>";
// var_dump($xml->result);
// echo "</pre>";
$results = $xml->result->doc;
$docs = [];
foreach($results as $doc) {
$docs[] = (object)[
'id' => (string)$doc->str[0],
'areaid' => (string)$doc->str[1],
'content' => (string)$doc->str[2],
'score' => (string)$doc->float,
];
}
return $docs;
// [0][1][2] as defined in the fl attribute above

}
} else {
// We received an unprocessable response.
Expand Down
1 change: 1 addition & 0 deletions search/engine/solrrag/classes/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ protected function validate_add_field_result($result) {

// It comes as error when fetching fields data.
if (!empty($results->error)) {
var_dump($results);
throw new \moodle_exception('errorcreatingschema', 'search_solrrag', '', $results->error);
}

Expand Down

0 comments on commit 1978c36

Please sign in to comment.