You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The SDK is written in asynchronous code, so you need to run it inside an async runtime. Both Pythonand JavaScript support async functions natively.
66
+
The SDK is written in asynchronous code, so you need to run it inside an async runtime. Both Python, JavaScript and Rust support async functions natively.
The above example imports the `pgml` module and creates a collection object. By itself, the collection only tracks document contents and identifiers, but once we add a pipeline, we can instruct the SDK to perform additional tasks when documents and are inserted and retrieved.
The pipeline configuration is a key/value object, where the key is the name of a column in a document, and the value is the action the SDK should perform on that column.
@@ -153,9 +229,36 @@ documents = [
153
229
await collection.upsert_documents(documents)
154
230
```
155
231
{% endtab %}
156
-
{% endtabs %}
157
232
158
-
If the same document `id` is used, the SDK computes the difference between existing and new documents and only updates the chunks that have changed.
233
+
{% tab title="Rust" %}
234
+
```rust
235
+
// Add this code to the end of the main function in the above example.
// Add this code to the end of the main function in the above example.
313
+
letresults=collection
314
+
.vector_search(
315
+
serde_json::json!({
316
+
"query": {
317
+
"fields": {
318
+
"text": {
319
+
"query":"Something about a document...",
320
+
},
321
+
},
322
+
},
323
+
"limit":2,
324
+
})
325
+
.into(),
326
+
&mutpipeline,
327
+
)
328
+
.await?;
329
+
330
+
println!("{:?}", results);
331
+
332
+
Ok(())
333
+
```
334
+
{% endtab %}
335
+
336
+
{% tab title="C" %}
337
+
```c
338
+
// Add this code to the end of the main function in the above example.
339
+
r_size = 0;
340
+
char** results = pgml_collectionc_vector_search(collection, "{\"query\": {\"fields\": {\"text\": {\"query\": \"Something about a document...\"}}}, \"limit\": 2}", pipeline, &r_size);
341
+
printf("\n\nPrinting results:\n");
342
+
for (i = 0; i < r_size; ++i) {
343
+
printf("Result %u -> %s\n", i, results[i]);
344
+
}
345
+
346
+
pgml_pipelinec_delete(pipeline);
347
+
pgml_collectionc_delete(collection);
348
+
```
349
+
{% endtab %}
206
350
{% endtabs %}
207
351
208
352
We are using built-in vector search, powered by embeddings and the PostgresML [pgml.embed()](../sql-extension/pgml.embed) function, which embeds the `query` argument, compares it to the embeddings stored in the database, and returns the top two results, ranked by cosine similarity.
@@ -228,6 +372,8 @@ if __name__ == "__main__":
228
372
{% endtab %}
229
373
{% endtabs %}
230
374
375
+
Note that `Rust` and `C` example do not require any additional code to run correctly.
376
+
231
377
Once you run the example, you should see something like this in the terminal:
0 commit comments