Skip to content

Commit

Permalink
Merge pull request #9 from localgovdrupal/feature/4-properly-handle-a…
Browse files Browse the repository at this point in the history
…pi-key

Feature/4 properly handle api key
  • Loading branch information
rupertj authored Dec 19, 2024
2 parents 049cdce + 07d1d52 commit c280b0b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
}
],
"require": {
"openai-php/client": "^0.8.4",
"smalot/pdfparser": "^2.7.0"
"openai-php/client": "^0.10.3",
"smalot/pdfparser": "^2.7.0",
"drupal/ai": "^1.0@dev",
"drupal/ai_provider_openai": "^1.0@dev"
}
}
1 change: 1 addition & 0 deletions localgov_publications_importer.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ core_version_requirement: ^9 || ^10

dependencies:
- localgov_publications:localgov_publications
- ai_provider_openai:ai_provider_openai
2 changes: 1 addition & 1 deletion localgov_publications_importer.services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
services:
localgov_publications_importer.importer:
class: Drupal\localgov_publications_importer\Service\Importer
arguments: ['@entity_type.manager']
arguments: ['@entity_type.manager','@ai.provider']
29 changes: 13 additions & 16 deletions src/Service/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Drupal\paragraphs\Entity\Paragraph;
use Smalot\PdfParser\Config as PdfParserConfig;
use Smalot\PdfParser\Parser as PdfParser;
use Drupal\ai\OperationType\Chat\ChatInput;
use Drupal\ai\OperationType\Chat\ChatMessage;
use Drupal\ai\AiProviderPluginManager;

/**
* Imports content from uploaded files.
Expand All @@ -18,6 +21,7 @@ class Importer {
*/
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected AiProviderPluginManager $aiProvider,
) {
}

Expand Down Expand Up @@ -81,23 +85,16 @@ public function importPdf($pathToFile): ?NodeInterface {
// of words. Swop these for spaces.
$content = str_replace("\t\n", ' ', $page->getText());

$client = \OpenAI::client('');

$result = $client->chat()->create([
'model' => 'gpt-3.5-turbo',
'messages' => [
[
'role' => 'user',
'content' => $content,
],
[
'role' => 'system',
'content' => 'This plain text document has been stripped of its formatting. Please add the formatting back in, and give me the whole document back as valid HTML.',
],
],
]);
// Find the default selected LLM:
$sets = $this->aiProvider->getDefaultProviderForOperationType('chat');

$content = $result->choices[0]->message->content;
$provider = $this->aiProvider->createInstance($sets['provider_id']);
$messages = new ChatInput([
new chatMessage('system', 'This plain text document has been stripped of its formatting. Please add the formatting back in, and give me the whole document back as valid HTML.'),
new chatMessage('user', $content),
]);
$message = $provider->chat($messages, $sets['model_id'])->getNormalized();
$content = $message->getText();

$this->addBodyAsParagraph($publicationPage, $content);

Expand Down

0 comments on commit c280b0b

Please sign in to comment.