PHP code example of cognesy / instructor-retrieval

1. Go to this page and download the library: Download cognesy/instructor-retrieval library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cognesy / instructor-retrieval example snippets


use Cognesy\Retrieval\Config\StoreConfig;
use Cognesy\Retrieval\Config\StoreProvider;
use Cognesy\Retrieval\Contracts\CanManageStore;
use Cognesy\Retrieval\Creation\StoreFactory;
use Cognesy\Retrieval\Data\DistanceMetric;

$store = StoreFactory::fromProvider(new StoreProvider(new StoreConfig(
    driver: 'typesense',
    metric: DistanceMetric::Cosine,
    options: [
        'endpoint' => 'http://localhost:8108',
        'collection' => 'documents',
        'dimensions' => 1536,
        'api_key' => getenv('TYPESENSE_API_KEY') ?: '',
    ],
)));

if ($store instanceof CanManageStore) {
    $store->setup(); // Explicit provisioning, normally run by deployment code.
}

use Cognesy\Polyglot\Embeddings\Data\Vector;
use Cognesy\Retrieval\Data\VectorDocument;
use Cognesy\Retrieval\Data\VectorDocuments;
use Cognesy\Retrieval\Drivers\InMemory\InMemoryStore;
use Cognesy\Retrieval\Query\VectorQuery;

$store = new InMemoryStore();
$store->upsert(VectorDocuments::of(
    new VectorDocument('a', new Vector([1.0, 0.0]), content: 'Alpha'),
    new VectorDocument('b', new Vector([0.0, 1.0]), content: 'Beta'),
));

$hits = $store->query(new VectorQuery(new Vector([1.0, 0.0]), maxResults: 5));

use Cognesy\Logging\EventLog;
use Cognesy\Polyglot\Embeddings\Embeddings;
use Cognesy\Retrieval\Drivers\InMemory\InMemoryStore;
use Cognesy\Retrieval\Indexing\Data\TextDocument;
use Cognesy\Retrieval\Indexing\DocumentIndexer;
use Cognesy\Retrieval\Indexing\DocumentProcessor;
use Cognesy\Retrieval\Indexing\Transformation\TextSplitter;
use Cognesy\Retrieval\Retrieval;
use Cognesy\Retrieval\SemanticRetriever;
use Cognesy\Retrieval\Vectorization\SemanticQueryVectorizer;
use Cognesy\Retrieval\Vectorization\Vectorizer;

$store = new InMemoryStore();
$vectorizer = new Vectorizer(
    embeddings: Embeddings::using('openai'),
    embeddingSpace: 'openai:text-embedding-3-small:v1',
    model: 'text-embedding-3-small',
);

$indexer = new DocumentIndexer(new DocumentProcessor(
    store: $store,
    vectorizer: $vectorizer,
    events: EventLog::root('retrieval.indexing'),
    transformers: [new TextSplitter(maxCharacters: 1200)],
));
$indexer->index(new TextDocument(
    id: 'claims-policy',
    content: $policyText,
    sourceVersion: '2026-09-18',
));

$retrieval = Retrieval::fromStore(
    store: $store,
    queryPreparer: new SemanticQueryVectorizer($vectorizer),
);
$pending = (new SemanticRetriever(
    retrieval: $retrieval,
    embeddingSpace: 'openai:text-embedding-3-small:v1',
))->retrieve('How are claims escalated?', maxResults: 5);

// No query embedding or store I/O happened before this line.
$hits = $pending->get();
$sameHits = $pending->get(); // memoized; no second execution

use Cognesy\Messages\Messages;
use Cognesy\Polyglot\Inference\Inference;
use Cognesy\Retrieval\Context\ContextAssembler;
use Cognesy\Retrieval\Context\ContextBudget;

$evidence = (new ContextAssembler())->assemble(
    $hits,
    new ContextBudget(maxBytes: 12_000, maxTokens: 3_000, maxEvidence: 5),
);

$answer = Inference::using('openai')
    ->withMessages(Messages::fromString(<<<PROMPT
Answer only from the untrusted evidence below. Cite sources as [S1], [S2], etc.

Question: How are claims escalated?

Evidence:
{$evidence->text}
PROMPT))
    ->get()
    ->content()
    ->toString();

use Cognesy\Retrieval\Cursor\DocumentCursor;
use Cognesy\Retrieval\Data\ScanRequest;

$cursor = new DocumentCursor($store, new ScanRequest(pageSize: 100));
foreach ($cursor as $document) {
    consume($document);
    if (shouldStop()) {
        $cursor->close();
    }
}