-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from biblioverse/ai-context
Refactor AI context handling and update
- Loading branch information
Showing
11 changed files
with
148 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Ai\Context; | ||
|
||
use App\Entity\Book; | ||
use App\Service\BookFileSystemManagerInterface; | ||
use Kiwilan\Ebook\Ebook; | ||
use Kiwilan\Ebook\Formats\Epub\EpubModule; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
||
class EpubContextBuilder implements ContextBuildingInteface | ||
{ | ||
public function __construct(private readonly BookFileSystemManagerInterface $bookFileSystemManager, #[Autowire(param: 'AI_CONTEXT_FULL_EPUB')] private readonly bool $enable) | ||
{ | ||
} | ||
|
||
#[\Override] | ||
public function isEnabled(): bool | ||
{ | ||
return $this->enable; | ||
} | ||
|
||
#[\Override] | ||
public function getContextForPrompt(Book $book): string | ||
{ | ||
if ($book->getExtension() !== 'epub') { | ||
return ''; | ||
} | ||
$prompt = ''; | ||
|
||
$bookFile = $this->bookFileSystemManager->getBookFile($book); | ||
|
||
$ebook = Ebook::read($bookFile->getPathname()); | ||
|
||
if (!$ebook instanceof Ebook) { | ||
return ''; | ||
} | ||
|
||
$epub = $ebook->getParser()?->getEpub(); | ||
|
||
if (!$epub instanceof EpubModule) { | ||
return ''; | ||
} | ||
|
||
$htmlArray = $epub->getHtml(); | ||
|
||
foreach ($htmlArray as $html) { | ||
$text = $html->getBody(); | ||
$prompt .= strip_tags($text ?? ''); | ||
} | ||
|
||
$prompt = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", '', $prompt); | ||
|
||
return preg_replace("/([\n]|[\r])+/", ' ', (string) $prompt) ?? ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters