Extract code examples into snippets

This commit is contained in:
Bastian Hofmann
2025-07-14 16:56:53 +02:00
parent 80c00bd3ab
commit 6cac0e2e79
9 changed files with 302 additions and 306 deletions
@@ -31,312 +31,7 @@ Inference is billed based on the number of tokens processed by the model. The co
Inference can be easily used through the Qdrant SDKs and the REST or GRPC APIs. Inference is available when upserting points as well as when querying the database. Inference can be easily used through the Qdrant SDKs and the REST or GRPC APIs. Inference is available when upserting points as well as when querying the database.
```bash {{< code-snippet path="/documentation/headless/snippets/cloud-inference/simple/" >}}
# Create a new vector
curl -X PUT "https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6333/collections/<your-collection>/points?wait=true" \\
-H "Content-Type: application/json" \\
-H "api-key: <paste-your-api-key-here>" \\
-d '{
"points": [
{
"id": 1,
"payload": { "topic": "cooking", "type": "dessert" },
"vector": {
"text": "Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
"model": "<the-model-to-use>"
}
}
]
}'
# Perform a search query
curl -X POST "https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6333/collections/<your-collection>/points/query" \\
-H "Content-Type: application/json" \\
-H "api-key: <paste-your-api-key-here>" \\
-d '{
"query": {
"text": "Recipe for baking chocolate chip cookies",
"model": "<the-model-to-use>"
}
}'
```
```python
from qdrant_client import QdrantClient
from qdrant_client.http.models import PointStruct, Document
client = QdrantClient(
url="https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6333",
api_key="<paste-your-api-key-here>",
cloud_inference=True,
)
points = [
PointStruct(
id=1,
payload={"topic": "cooking", "type": "dessert"},
vector=Document(
text="Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
model="<the-model-to-use>"
)
)
]
client.upsert(collection_name="<your-collection>", points=points)
points = client.query_points(collection_name="<your-collection>", query=Document(
text="Recipe for baking chocolate chip cookies requires flour",
model="<the-model-to-use>"
))
print(points)
```
```typescript
import {QdrantClient} from "@qdrant/js-client-rest";
const client = new QdrantClient({
url: 'https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6333',
apiKey: '<paste-your-api-key-here>',
});
const points = [
{
id: 1,
payload: { topic: "cooking", type: "dessert" },
vector: {
text: "Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
model: "<the-model-to-use>"
}
}
];
await client.upsert("<your-collection>", { wait: true, points });
const result = await client.query(
"<your-collection>",
{
query: {
text: "What ingredients are needed for baking chocolate chip cookies?",
model: "<the-model-to-use>"
},
}
)
console.log(result);
```
```rust
use qdrant_client::qdrant::vector;
use qdrant_client::qdrant::vector_input;
use qdrant_client::qdrant::QueryPointsBuilder;
use qdrant_client::qdrant::Vector;
use qdrant_client::qdrant::VectorInput;
use qdrant_client::Payload;
use qdrant_client::Qdrant;
use qdrant_client::qdrant::{Document};
use qdrant_client::qdrant::{PointStruct, UpsertPointsBuilder};
#[tokio::main]
async fn main() {
let client = Qdrant::from_url("https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6334")
.api_key("<paste-your-api-key-here>")
.build()
.unwrap();
let mut points = Vec::new();
let vector = Vector {
vector: Some(vector::Vector::Document(Document {
text: "Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.".to_string(),
model: "<the-model-to-use>".to_string(),
options: Default::default(),
})),
..Default::default()
};
points.push(PointStruct::new(1, vector, Payload::default()));
let _ = client
.upsert_points(UpsertPointsBuilder::new("<your-collection>", points).wait(true))
.await;
let document = Document {
text: "Recipe for baking chocolate chip cookies".to_string(),
model: "<the-model-to-use>".to_string(),
options: Default::default(),
};
let query = VectorInput {
variant: Some(vector_input::Variant::Document(document)),
};
let query_request = QueryPointsBuilder::new("<your-collection>").query(query);
let result = client.query(query_request).await.unwrap();
println!("Result: {:?}", result);
```
```java
package org.example;
import static io.qdrant.client.PointIdFactory.id;
import static io.qdrant.client.QueryFactory.nearest;
import static io.qdrant.client.ValueFactory.value;
import static io.qdrant.client.VectorsFactory.vectors;
import io.qdrant.client.grpc.Points;
import io.qdrant.client.grpc.Points.Document;
import io.qdrant.client.grpc.Points.PointStruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
QdrantClient client =
new QdrantClient(
QdrantGrpcClient.newBuilder("xyz-example.cloud-region.cloud-provider.cloud.qdrant.io", 6334, true)
.withApiKey("<paste-your-api-key-here>")
.build());
client
.upsertAsync(
"<your-collection>",
List.of(
PointStruct.newBuilder()
.setId(id(1))
.setVectors(
vectors(
Document.newBuilder()
.setText(
"Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.")
.setModel("<the-model-to-use>")
.build()))
.putAllPayload(Map.of("topic", value("cooking"), "type", value("dessert")))
.build()))
.get();
List <Points.ScoredPoint> points =
client
.queryAsync(
Points.QueryPoints.newBuilder()
.setCollectionName("<your-collection>")
.setQuery(
nearest(
Document.newBuilder()
.setText("Recipe for baking chocolate chip cookies")
.setModel("<the-model-to-use>")
.build()))
.build())
.get();
System.out.printf(points.toString());
}
}
```
```csharp
using Qdrant.Client;
using Qdrant.Client.Grpc;
using Value = Qdrant.Client.Grpc.Value;
var client = new QdrantClient(
host: "xyz-example.cloud-region.cloud-provider.cloud.qdrant.io",
port: 6334,
https: true,
apiKey: "<paste-your-api-key-here>"
);
await client.UpsertAsync(
collectionName: "<your-collection>",
points: new List <PointStruct> {
new() {
Id = 1,
Vectors = new Document() {
Text =
"Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
Model = "<the-model-to-use>",
},
Payload = {
["topic"] = "cooking",
["type"] = "dessert"
},
},
}
);
var points = await client.QueryAsync(
collectionName: "<your-collection>",
query: new Document() {
Text = "Recipe for baking chocolate chip cookies", Model = "<the-model-to-use>"
}
);
foreach(var point in points) {
Console.WriteLine(point);
}
```
```go
package main
import (
"context"
"log"
"time"
"github.com/qdrant/go-client/qdrant"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := qdrant.NewClient(&qdrant.Config{
Host: "xyz-example.cloud-region.cloud-provider.cloud.qdrant.io",
Port: 6334,
APIKey: "<paste-your-api-key-here>",
UseTLS: true,
})
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer client.Close()
_, err = client.GetPointsClient().Upsert(ctx, &qdrant.UpsertPoints{
CollectionName: "<your-collection>",
Points: []*qdrant.PointStruct{
{
Id: qdrant.NewIDNum(uint64(1)),
Vectors: qdrant.NewVectorsDocument(&qdrant.Document{
Text: "Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
Model: "<the-model-to-use>",
}),
Payload: qdrant.NewValueMap(map[string]any{
"topic": "cooking",
"type": "dessert",
}),
},
},
})
if err != nil {
log.Fatalf("error creating point: %v", err)
}
points, err := client.Query(ctx, &qdrant.QueryPoints{
CollectionName: "<your-collection>",
Query: qdrant.NewQueryNearest(
qdrant.NewVectorInputDocument(&qdrant.Document{
Text: "Recipe for baking chocolate chip cookies",
Model: "<the-model-to-use>",
}),
),
})
log.Printf("List of points: %s", points)
}
})
```
Usage examples, specific to each cluster and model, can also be found in the Inference tab of the Cluster Detail page in the Qdrant Cloud Console. Usage examples, specific to each cluster and model, can also be found in the Inference tab of the Cluster Detail page in the Qdrant Cloud Console.
@@ -0,0 +1 @@
This code snippet demonstrates how to use cloud inference in Qdrant Cloud to automatically create vector embeddings from text or image documents during upseart and query operations.
@@ -0,0 +1,29 @@
```bash
# Create a new vector
curl -X PUT "https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6333/collections/<your-collection>/points?wait=true" \\
-H "Content-Type: application/json" \\
-H "api-key: <paste-your-api-key-here>" \\
-d '{
"points": [
{
"id": 1,
"payload": { "topic": "cooking", "type": "dessert" },
"vector": {
"text": "Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
"model": "<the-model-to-use>"
}
}
]
}'
# Perform a search query
curl -X POST "https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6333/collections/<your-collection>/points/query" \\
-H "Content-Type: application/json" \\
-H "api-key: <paste-your-api-key-here>" \\
-d '{
"query": {
"text": "Recipe for baking chocolate chip cookies",
"model": "<the-model-to-use>"
}
}'
```
@@ -0,0 +1,41 @@
```csharp
using Qdrant.Client;
using Qdrant.Client.Grpc;
using Value = Qdrant.Client.Grpc.Value;
var client = new QdrantClient(
host: "xyz-example.cloud-region.cloud-provider.cloud.qdrant.io",
port: 6334,
https: true,
apiKey: "<paste-your-api-key-here>"
);
await client.UpsertAsync(
collectionName: "<your-collection>",
points: new List <PointStruct> {
new() {
Id = 1,
Vectors = new Document() {
Text =
"Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
Model = "<the-model-to-use>",
},
Payload = {
["topic"] = "cooking",
["type"] = "dessert"
},
},
}
);
var points = await client.QueryAsync(
collectionName: "<your-collection>",
query: new Document() {
Text = "Recipe for baking chocolate chip cookies", Model = "<the-model-to-use>"
}
);
foreach(var point in points) {
Console.WriteLine(point);
}
```
@@ -0,0 +1,59 @@
```go
package main
import (
"context"
"log"
"time"
"github.com/qdrant/go-client/qdrant"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := qdrant.NewClient(&qdrant.Config{
Host: "xyz-example.cloud-region.cloud-provider.cloud.qdrant.io",
Port: 6334,
APIKey: "<paste-your-api-key-here>",
UseTLS: true,
})
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer client.Close()
_, err = client.GetPointsClient().Upsert(ctx, &qdrant.UpsertPoints{
CollectionName: "<your-collection>",
Points: []*qdrant.PointStruct{
{
Id: qdrant.NewIDNum(uint64(1)),
Vectors: qdrant.NewVectorsDocument(&qdrant.Document{
Text: "Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
Model: "<the-model-to-use>",
}),
Payload: qdrant.NewValueMap(map[string]any{
"topic": "cooking",
"type": "dessert",
}),
},
},
})
if err != nil {
log.Fatalf("error creating point: %v", err)
}
points, err := client.Query(ctx, &qdrant.QueryPoints{
CollectionName: "<your-collection>",
Query: qdrant.NewQueryNearest(
qdrant.NewVectorInputDocument(&qdrant.Document{
Text: "Recipe for baking chocolate chip cookies",
Model: "<the-model-to-use>",
}),
),
})
log.Printf("List of points: %s", points)
}
})
```
@@ -0,0 +1,58 @@
```java
package org.example;
import static io.qdrant.client.PointIdFactory.id;
import static io.qdrant.client.QueryFactory.nearest;
import static io.qdrant.client.ValueFactory.value;
import static io.qdrant.client.VectorsFactory.vectors;
import io.qdrant.client.grpc.Points;
import io.qdrant.client.grpc.Points.Document;
import io.qdrant.client.grpc.Points.PointStruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
QdrantClient client =
new QdrantClient(
QdrantGrpcClient.newBuilder("xyz-example.cloud-region.cloud-provider.cloud.qdrant.io", 6334, true)
.withApiKey("<paste-your-api-key-here>")
.build());
client
.upsertAsync(
"<your-collection>",
List.of(
PointStruct.newBuilder()
.setId(id(1))
.setVectors(
vectors(
Document.newBuilder()
.setText(
"Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.")
.setModel("<the-model-to-use>")
.build()))
.putAllPayload(Map.of("topic", value("cooking"), "type", value("dessert")))
.build()))
.get();
List <Points.ScoredPoint> points =
client
.queryAsync(
Points.QueryPoints.newBuilder()
.setCollectionName("<your-collection>")
.setQuery(
nearest(
Document.newBuilder()
.setText("Recipe for baking chocolate chip cookies")
.setModel("<the-model-to-use>")
.build()))
.build())
.get();
System.out.printf(points.toString());
}
}
```
@@ -0,0 +1,30 @@
```python
from qdrant_client import QdrantClient
from qdrant_client.http.models import PointStruct, Document
client = QdrantClient(
url="https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6333",
api_key="<paste-your-api-key-here>",
cloud_inference=True,
)
points = [
PointStruct(
id=1,
payload={"topic": "cooking", "type": "dessert"},
vector=Document(
text="Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
model="<the-model-to-use>"
)
)
]
client.upsert(collection_name="<your-collection>", points=points)
points = client.query_points(collection_name="<your-collection>", query=Document(
text="Recipe for baking chocolate chip cookies requires flour",
model="<the-model-to-use>"
))
print(points)
```
@@ -0,0 +1,50 @@
```rust
use qdrant_client::qdrant::vector;
use qdrant_client::qdrant::vector_input;
use qdrant_client::qdrant::QueryPointsBuilder;
use qdrant_client::qdrant::Vector;
use qdrant_client::qdrant::VectorInput;
use qdrant_client::Payload;
use qdrant_client::Qdrant;
use qdrant_client::qdrant::{Document};
use qdrant_client::qdrant::{PointStruct, UpsertPointsBuilder};
#[tokio::main]
async fn main() {
let client = Qdrant::from_url("https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6334")
.api_key("<paste-your-api-key-here>")
.build()
.unwrap();
let mut points = Vec::new();
let vector = Vector {
vector: Some(vector::Vector::Document(Document {
text: "Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.".to_string(),
model: "<the-model-to-use>".to_string(),
options: Default::default(),
})),
..Default::default()
};
points.push(PointStruct::new(1, vector, Payload::default()));
let _ = client
.upsert_points(UpsertPointsBuilder::new("<your-collection>", points).wait(true))
.await;
let document = Document {
text: "Recipe for baking chocolate chip cookies".to_string(),
model: "<the-model-to-use>".to_string(),
options: Default::default(),
};
let query = VectorInput {
variant: Some(vector_input::Variant::Document(document)),
};
let query_request = QueryPointsBuilder::new("<your-collection>").query(query);
let result = client.query(query_request).await.unwrap();
println!("Result: {:?}", result);
```
@@ -0,0 +1,33 @@
```typescript
import {QdrantClient} from "@qdrant/js-client-rest";
const client = new QdrantClient({
url: 'https://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6333',
apiKey: '<paste-your-api-key-here>',
});
const points = [
{
id: 1,
payload: { topic: "cooking", type: "dessert" },
vector: {
text: "Recipe for baking chocolate chip cookies requires flour, sugar, eggs, and chocolate chips.",
model: "<the-model-to-use>"
}
}
];
await client.upsert("<your-collection>", { wait: true, points });
const result = await client.query(
"<your-collection>",
{
query: {
text: "What ingredients are needed for baking chocolate chip cookies?",
model: "<the-model-to-use>"
},
}
)
console.log(result);
```