Skip to content

Commit

Permalink
update readme with latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hkulekci committed Aug 26, 2024
1 parent 411d60b commit fff3445
Showing 1 changed file with 64 additions and 17 deletions.
81 changes: 64 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,76 @@ You can install the client in your PHP project using composer:
composer require hkulekci/qdrant
```

An example to create a collection :
### Connecting to Qdrant

```php
use Qdrant\Endpoints\Collections;
use Qdrant\Http\GuzzleClient;
use Qdrant\Models\Request\CreateCollection;
use Qdrant\Models\Request\VectorParams;

include __DIR__ . "/../vendor/autoload.php";
include_once 'config.php';

$config = new \Qdrant\Config(QDRANT_HOST);
use Qdrant\Config;
use Qdrant\Http\Builder;

$config = new Config(QDRANT_HOST);
$config->setApiKey(QDRANT_API_KEY);

$client = new Qdrant(new GuzzleClient($config));
$transport = (new Builder())->build($config);
$client = new Qdrant($transport);
```

### Creating a Collection

```php
use Qdrant\Endpoints\Collections;
use Qdrant\Models\Request\CreateCollection;
use Qdrant\Models\Request\VectorParams;

$createCollection = new CreateCollection();
$createCollection->addVector(new VectorParams(1024, VectorParams::DISTANCE_COSINE), 'image');
$response = $client->collections('images')->create($createCollection);
$createCollection->addVector(new VectorParams(1536, VectorParams::DISTANCE_COSINE), 'content');
$response = $client->collections('contents')->create($createCollection);
```

So now, we can insert a point :
### Inserting Points Into Collection

```php
use Qdrant\Models\PointsStruct;
use Qdrant\Models\PointStruct;
use Qdrant\Models\VectorStruct;

$openai = OpenAI::client(OPENAI_API_KEY);

$query = 'sustainable agricultural startups';
$response = $openai->embeddings()->create([
'model' => 'text-embedding-ada-002',
'input' => $query,
]);
$embedding = array_values($response->embeddings[0]->embedding);

$points = new PointsStruct();
$points->addPoint(
new PointStruct(
(int) $imageId,
new VectorStruct($data['embeddings'][0], 'image'),
new VectorStruct($embedding, 'content'),
[
'id' => 1,
'meta' => 'Meta data'
]
)
);
$client->collections('images')->points()->upsert($points);
$client->collections('contents')->points()->upsert($points);
```

While upsert data, if you want to wait for upsert to actually happen, you can use query paramaters:
### Wait for Acknowledges

```
$client->collections('images')->points()->upsert($points, ['wait' => 'true']);
While upsert data, if you want to wait for upsert to actually happen, you can use query parameters:

```php
$client->collections('contents')->points()->upsert($points, ['wait' => 'true']);
```

You can check for more parameters : https://qdrant.github.io/qdrant/redoc/index.html#tag/points/operation/upsert_points

### Search on Points

Search with a filter :

```php
Expand All @@ -87,5 +107,32 @@ $searchRequest = (new SearchRequest(new VectorStruct($embedding, 'elev_pitch')))
])
->setWithPayload(true);

$response = $client->collections('images')->points()->search($searchRequest);
$response = $client->collections('contents')->points()->search($searchRequest);
```

### Search on Points with OpenAI Embeddings

```php
$openai = OpenAI::client(OPENAI_API_KEY);

$query = 'lorem ipsum dolor sit amed';
$response = $openai->embeddings()->create([
'model' => 'text-embedding-ada-002',
'input' => $query,
]);
$embedding = array_values($response->embeddings[0]->embedding);

$searchRequest = (new SearchRequest(new VectorStruct($embedding, 'content')))
->setLimit(10)
->setParams([
'hnsw_ef' => 128,
'exact' => false,
])
->setWithPayload(true);

$response = $client->collections('contents')->points()->search($searchRequest);

foreach ($response['result'] as $item) {
echo $item['score'] . ';' . $item['payload']['id'] . ';' . $item['payload']['meta_data'] . PHP_EOL;
}
```

0 comments on commit fff3445

Please sign in to comment.