Skip to content

Commit

Permalink
Tweaking similariy search results
Browse files Browse the repository at this point in the history
  • Loading branch information
mhughes2k committed May 12, 2024
1 parent 32728fe commit 22d66cc
Show file tree
Hide file tree
Showing 17 changed files with 798 additions and 218 deletions.
18 changes: 18 additions & 0 deletions ai/classes/LogLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace core_ai;

/**
* Describes log levels.
*/
class LogLevel
{
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
}
7 changes: 7 additions & 0 deletions ai/classes/LoggerAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace core_ai;

interface LoggerAwareInterface {
public function setLogger(LoggerInterface $logger);
}
13 changes: 13 additions & 0 deletions ai/classes/LoggerAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace core_ai;

trait LoggerAwareTrait {
private $logger;
public function setLogger(LoggerInterface $logger) {
$this->logger = $logger;
}
public function log($message, array $context = [], $level = LogLevel::INFO) {
$this->logger->log($level, $message, $context);
}
}
114 changes: 114 additions & 0 deletions ai/classes/LoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace core_ai;

/**
* Describes a logger instance.
*
* The message MUST be a string or object implementing __toString().
*
* The message MAY contain placeholders in the form: {foo} where foo
* will be replaced by the context data in key "foo".
*
* The context array can contain arbitrary data, the only assumption that
* can be made by implementors is that if an Exception instance is given
* to produce a stack trace, it MUST be in a key named "exception".
*
* See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
* for the full interface specification.
*/
interface LoggerInterface
{
/**
* System is unusable.
*
* @param string $message
* @param array $context
* @return void
*/
public function emergency($message, array $context = array());

/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param array $context
* @return void
*/
public function alert($message, array $context = array());

/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param array $context
* @return void
*/
public function critical($message, array $context = array());

/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param array $context
* @return void
*/
public function error($message, array $context = array());

/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param array $context
* @return void
*/
public function warning($message, array $context = array());

/**
* Normal but significant events.
*
* @param string $message
* @param array $context
* @return void
*/
public function notice($message, array $context = array());

/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function info($message, array $context = array());

/**
* Detailed debug information.
*
* @param string $message
* @param array $context
* @return void
*/
public function debug($message, array $context = array());

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = array());
}
73 changes: 50 additions & 23 deletions ai/classes/aiclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/**
* Base client for AI providers that uses simple http request.
*/
class AIClient extends \curl {
class AIClient extends \curl implements LoggerAwareInterface {
use LoggerAwareTrait;
/**
* @var AIProvider
*/
Expand All @@ -14,12 +15,15 @@ public function __construct(
AIProvider $provider
) {
$this->provider = $provider;
$this->setLogger($provider->get_logger());
$settings = [];
parent::__construct($settings);
$this->setHeader('Authorization: Bearer ' . $this->provider->get('apikey'));
$this->setHeader('Content-Type: application/json');
}



public function get_embeddings_url(): string {
return $this->provider->get('baseurl') . $this->provider->get('embeddingsurl');
}
Expand All @@ -40,21 +44,31 @@ public function chat($messages) {
];
$params = json_encode($params);
$rawresult = $this->post($this->get_chat_completions_url(), $params);
$this->logger->info("Response rescieved from AI provider: {name}", ['name' => $this->provider->get('name')]);
$jsonresult = json_decode($rawresult);
if (isset($jsonresult->error)) {
$this->logger->error("Error: " . $jsonresult->error->message . ":". print_r($messages, true));
throw new AiException("Error: " . $jsonresult->error->message . ":". print_r($messages, true));
//return "Error: " . $jsonresult->error->message . ":". print_r($messages, true);
}
$result = [];
if (isset($jsonresult->choices)) {
$this->logger->info("Starting Processing completions");
$result = $this->convert_chat_completion($jsonresult->choices);
if (isset($jsonresult->usage)) {
$this->provider->increment_prompt_usage($jsonresult->usage->prompt_tokens);
$this->provider->increment_completion_tokens($jsonresult->usage->completion_tokens);
$this->provider->increment_total_tokens($jsonresult->usage->total_tokens);
}
$this->logger->info("Finished completions");
}

if (isset($jsonresult->usage)) {
$this->logger->info("Updating token usage");
$usage = $jsonresult->usage;
$updated = [
$this->provider->increment_prompt_usage($usage->prompt_tokens),
$this->provider->increment_completion_tokens($usage->completion_tokens),
$this->provider->increment_total_tokens($usage->total_tokens)
];
$this->logger->info("Request Tokens-{prompt_tokens}. Total tokens: {total_tokens}", (array)$usage);
$this->logger->info("Tokens-Prompt:{$updated[0]}, Completion:{$updated[1]}, Total:{$updated[2]}");
}
//$this->logger->info($result);
return $result;
}

Expand All @@ -79,30 +93,43 @@ public function embed_query($content): array {
$usedptokens = $this->provider->get_usage('prompt_tokens');
$totaltokens = $this->provider->get_usage('total_tokens');
// mtrace("Prompt tokens: $usedptokens. Total tokens: $totaltokens");
$content = $content ?? ""; // Fix "null" content to be "empty" string.
$params = [
"input" => htmlentities($content), // TODO need to do some length checking here!
"model" => $this->provider->get('embeddingmodel')
"model" => $this->provider->get('embeddingmodel'),
];
$params = json_encode($params);
// var_dump($this->get_embeddings_url());
$embeddingsurl = $this->get_embeddings_url();
$this->logger->info("Embeddings URL: " . $embeddingsurl);
$urlisblocked = $this->check_securityhelper_blocklist($embeddingsurl);
if (!is_null($urlisblocked)) {
$this->logger->warning($urlisblocked);
throw new \moodle_exception("{$embeddingsurl} is blocked by policy");
}
$rawresult = $this->post($embeddingsurl, $params);

$rawresult = $this->post($this->get_embeddings_url(), $params);
// var_dump($rawresult);
$result = json_decode($rawresult, true);
// var_dump($result);
$usage = $result['usage'];
$this->provider->increment_prompt_usage($usage['prompt_tokens']);
$this->provider->increment_total_tokens($usage['total_tokens']);
// mtrace("Used Prompt tokens: {$usage['prompt_tokens']}. Total tokens: {$usage['total_tokens']}");
$data = $result['data'];
foreach($data as $d) {
if ($d['object'] == "embedding") {
return $d['embedding'];
if (is_null($result)) {
throw new \moodle_exception('Failed to decode response from AI provider: {$a}', "", "", $rawresult);
}
if (isset($result['usage'])) {
$usage = $result['usage'];
$updated = [
$this->provider->increment_prompt_usage($usage['prompt_tokens']),
$this->provider->increment_total_tokens($usage['total_tokens'])
];
$this->logger->info("Used Prompt tokens: {prompt_tokens}. Total tokens: {total_tokens}", $usage);
$this->logger->info("Tokens-Prompt:{$updated[0]}, Total:{$updated[1]}");
}
if (isset($result['data'])) {
$data = $result['data'];
foreach ($data as $d) {
if ($d['object'] == "embedding") {
return $d['embedding'];
}
}
}
$usedptokens = $this->provider->get_usage('prompt_tokens');
$totaltokens = $this->provider->get_usage('total_tokens');
// mtrace("Total Used: Prompt tokens: $usedptokens. Total tokens: $totaltokens");

return [];
}
public function embed_documents(array $documents) {
Expand Down
Loading

0 comments on commit 22d66cc

Please sign in to comment.