Skip to content

Commit

Permalink
feat(slb-495): update logger service and minor refactor ai service
Browse files Browse the repository at this point in the history
  • Loading branch information
dspachos committed Jan 3, 2025
1 parent b7942d5 commit 871febe
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use Drupal\silverback_ai_import\ContentImportAiService;
*/
function silverback_ai_import_form_alter(&$form, &$form_state, $form_id) {
// @todo
$logger = Drupal::service('silverback_ai_imoprt.logger');
dpm($logger->getEntries());
//$logger = Drupal::service('silverback_ai_imoprt.logger');
//dpm($logger->getEntries());

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
silverback_ai_import.content:
class: Drupal\silverback_ai_import\ContentImportAiService
arguments: ['@current_route_match', '@current_user', '@entity_type.manager', '@logger.factory', '@config.factory', '@silverback_ai.openai_http_client', '@plugin.manager.ai.import', '@plugin.manager.ai.post.import', '@silverback_ai.request', '@silverback_ai.token.usage']
arguments: ['@current_route_match', '@current_user', '@entity_type.manager', '@logger.factory', '@config.factory', '@silverback_ai.openai_http_client', '@plugin.manager.ai.import', '@plugin.manager.ai.post.import', '@silverback_ai.service', '@silverback_ai.token.usage']
silverback_ai_import.batch.import:
class: 'Drupal\silverback_ai_import\ContentImportBatch'
arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ final class ContentImportAiService {
public const URL = 'url';
public const PDF = 'pdf';


/**
* Constructs a ContentImportAiService object.
*/
Expand Down Expand Up @@ -111,7 +110,7 @@ private function getAstFromFilePath(FileInterface $file) {
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager')->getViaUri($uri);
$file_path = $stream_wrapper_manager->realpath();
$parse_service_url = $this->configFactory->get('silverback_ai_import.settings')->get('converter_service_url');

// @todo Add DI.
$client = \Drupal::httpClient();
try {
// @todo For now this is working only for docx files.
Expand All @@ -124,7 +123,7 @@ private function getAstFromFilePath(FileInterface $file) {
$response = json_decode($body);
} catch (RequestException $e) {
// Handle any errors.
\Drupal::logger('silverback_ai_import')->error($e->getMessage());
$this->loggerFactory->get('silverback_ai_import')->error($e->getMessage());
}
return $response;
}
Expand Down Expand Up @@ -165,7 +164,7 @@ private function getAstFromUrl(string $url) {
$response = json_decode($body);
} catch (RequestException $e) {
// Handle any errors.
\Drupal::logger('silverback_ai_import')->error($e->getMessage());
$this->loggerFactory->get('silverback_ai_import')->error($e->getMessage());
}
return $response;
}
Expand Down Expand Up @@ -211,7 +210,7 @@ private function getAstFromPdfFile(FileInterface $file) {
$response = json_decode($body);
} catch (RequestException $e) {
// Handle any errors.
\Drupal::logger('silverback_ai_import')->error($e->getMessage());
$this->loggerFactory->get('silverback_ai_import')->error($e->getMessage());
}
return $response;
}
Expand Down Expand Up @@ -381,10 +380,10 @@ public function getPlugin(array $chunk) {
* The ID of the parent node (used in recursion)
*
* @return array An array of flattened nodes, where each node contains:
* - type: The capitalized node type
* - id: A unique identifier
* - parent: Reference to the parent node's ID
* - Additional properties specific to each node type
* - type: The capitalized node type
* - id: A unique identifier
* - parent: Reference to the parent node's ID
* - Additional properties specific to each node type
*/
public function flattenAst($ast, $parent = NULL) {

Expand Down Expand Up @@ -494,7 +493,6 @@ public function extractPageDataFromUrl($url) {
'path' => NULL,
'metatags' => [],
'language' => NULL,
// Add an error key.
'error' => NULL,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function createEntry($ast, EntityInterface $entity, string $source): void
'target_entity_revision_id' => NULL,
'source' => $source ?? '',
'output_folder' => $ast->outputDirectory,
'data' => '-', // @todo serialise ast perhaps?
'data' => serialize($ast),
])
->execute();
} catch (\Exception $e) {
Expand Down Expand Up @@ -102,7 +102,7 @@ public function getEntries() {
foreach ($rsc->fetchAll() as $row) {
$rows[] = $this->buildRow($row);
}
dpm($rows);

return $rows;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/drupal/silverback_ai/silverback_ai.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ services:
silverback_ai.openai_http_client:
class: Drupal\silverback_ai\HttpClient\OpenAiHttpClient
arguments: ['@http_client_factory', '@config.factory']
silverback_ai.request:
silverback_ai.service:
class: Drupal\silverback_ai\AiService
arguments: ['@current_route_match', '@current_user', '@entity_type.manager', '@logger.factory', '@config.factory', '@silverback_ai.openai_http_client','@silverback_ai.token.usage']
25 changes: 24 additions & 1 deletion packages/drupal/silverback_ai/src/AiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ public function __construct(
}

/**
* @todo
* Makes a chat completion request to the OpenAI API.
*
* @param string $prompt The text prompt to send to the AI model
* @param string $model The AI model to use for the request (defaults to class constant DEFAULT_AI_MODEL)
* @param array $context Additional context data for logging purposes
*
* @return array The decoded JSON response from the API
*
* @throws \Exception If the HTTP request fails or JSON decoding fails
*/
public function request(string $prompt, string $model = self::DEFAULT_AI_MODEL, array $context = []) {

Expand Down Expand Up @@ -69,6 +77,21 @@ public function request(string $prompt, string $model = self::DEFAULT_AI_MODEL,
return $response;
}

/**
* ...
*/
public function listModels() {
try {
$response = $this->silverbackAiOpenaiHttpClient->get('models');
$responseBodyContents = $response->getBody()->getContents();
$response = json_decode($responseBodyContents, TRUE, 512, JSON_THROW_ON_ERROR);
return $response['data'];
} catch (\Exception $e) {
throw new \Exception('HTTP request failed: ' . $e->getMessage());
}
return [];
}

/**
* Logs the tokens usage.
*
Expand Down

0 comments on commit 871febe

Please sign in to comment.