This commit is contained in:
39
docs/snippets/php/advanced/chunking_rag.md
Normal file
39
docs/snippets/php/advanced/chunking_rag.md
Normal file
@@ -0,0 +1,39 @@
|
||||
```php title="PHP"
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Kreuzberg\Kreuzberg;
|
||||
use Kreuzberg\ExtractionConfig;
|
||||
use Kreuzberg\ChunkingConfig;
|
||||
use Kreuzberg\EmbeddingConfig;
|
||||
|
||||
$config = new ExtractionConfig(
|
||||
chunking: new ChunkingConfig(
|
||||
maxCharacters: 500,
|
||||
overlap: 50,
|
||||
embedding: new EmbeddingConfig(
|
||||
normalize: true,
|
||||
batchSize: 32
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$result = Kreuzberg::extractFileSync('research_paper.pdf', null, $config);
|
||||
|
||||
if ($result->getChunks()) {
|
||||
foreach ($result->getChunks() as $chunk) {
|
||||
$metadata = $chunk->getMetadata();
|
||||
if ($metadata) {
|
||||
echo "Chunk " . ($metadata->getChunkIndex() + 1) . "/" . $metadata->getTotalChunks() . "\n";
|
||||
echo "Position: " . $metadata->getByteStart() . "-" . $metadata->getByteEnd() . "\n";
|
||||
echo "Content: " . substr($chunk->getContent(), 0, 100) . "...\n";
|
||||
|
||||
if ($chunk->getEmbedding()) {
|
||||
echo "Embedding: " . count($chunk->getEmbedding()) . " dimensions\n";
|
||||
}
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
}
|
||||
?>
|
||||
```
|
||||
Reference in New Issue
Block a user