This commit is contained in:
70
docs/snippets/php/advanced/vector_database_integration.md
Normal file
70
docs/snippets/php/advanced/vector_database_integration.md
Normal file
@@ -0,0 +1,70 @@
|
||||
```php title="PHP"
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Kreuzberg\Kreuzberg;
|
||||
use Kreuzberg\ExtractionConfig;
|
||||
use Kreuzberg\ChunkingConfig;
|
||||
use Kreuzberg\EmbeddingConfig;
|
||||
|
||||
class VectorRecord {
|
||||
public function __construct(
|
||||
public string $id,
|
||||
public string $content,
|
||||
public array $embedding,
|
||||
public array $metadata
|
||||
) {}
|
||||
}
|
||||
|
||||
function extractAndVectorize(
|
||||
string $documentPath,
|
||||
string $documentId
|
||||
): array {
|
||||
$config = new ExtractionConfig(
|
||||
chunking: new ChunkingConfig(
|
||||
maxCharacters: 512,
|
||||
overlap: 50,
|
||||
embedding: new EmbeddingConfig(
|
||||
normalize: true,
|
||||
batchSize: 32
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$result = Kreuzberg::extractFileSync($documentPath, null, $config);
|
||||
|
||||
$records = [];
|
||||
if ($result->getChunks()) {
|
||||
foreach ($result->getChunks() as $index => $chunk) {
|
||||
$embedding = $chunk->getEmbedding();
|
||||
if ($embedding) {
|
||||
$metadata = [
|
||||
'document_id' => $documentId,
|
||||
'chunk_index' => (string)$index,
|
||||
'content_length' => (string)strlen($chunk->getContent()),
|
||||
];
|
||||
|
||||
$records[] = new VectorRecord(
|
||||
id: "{$documentId}_chunk_{$index}",
|
||||
content: $chunk->getContent(),
|
||||
embedding: $embedding,
|
||||
metadata: $metadata
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $records;
|
||||
}
|
||||
|
||||
// Usage
|
||||
$records = extractAndVectorize('research_paper.pdf', 'doc_123');
|
||||
|
||||
foreach ($records as $record) {
|
||||
echo "Vector ID: " . $record->id . "\n";
|
||||
echo "Content length: " . strlen($record->content) . " characters\n";
|
||||
echo "Embedding dimension: " . count($record->embedding) . "\n";
|
||||
echo "---\n";
|
||||
}
|
||||
?>
|
||||
```
|
||||
Reference in New Issue
Block a user