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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
The SDK is written in asynchronous code, so you need to run it inside an async runtime. Both PythonandJavaScript support async functions natively.
The SDK is written in asynchronous code, so you need to run it inside an async runtime. Both Python, JavaScriptandRust support async functions natively.
{% tabs %}
{% tab title="JavaScript" %}
{% tab title="JavaScript" %}
```javascript
const pgml = require("pgml");
Expand All
@@ -63,6 +85,29 @@ async def main():
collection = Collection("sample_collection")
```
{% endtab %}
{% tab title="Rust" %}
```rust
use pgml::{Collection, Pipeline};
use anyhow::Error;
#[tokio::main]
async fn main() -> Result<(), Error> {
let mut collection = Collection::new("sample_collection", None)?;
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.
Expand DownExpand Up
@@ -153,9 +229,36 @@ documents = [
await collection.upsert_documents(documents)
```
{% endtab %}
{% endtabs %}
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.
{% tab title="Rust" %}
```rust
// 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.
let results = collection
.vector_search(
serde_json::json!({
"query": {
"fields": {
"text": {
"query": "Something about a document...",
},
},
},
"limit": 2,
})
.into(),
&mut pipeline,
)
.await?;
println!("{:?}", results);
Ok(())
```
{% endtab %}
{% tab title="C" %}
```c
// Add this code to the end of the main function in the above example.
r_size = 0;
char** results = pgml_collectionc_vector_search(collection, "{\"query\": {\"fields\": {\"text\": {\"query\": \"Something about a document...\"}}}, \"limit\": 2}", pipeline, &r_size);
printf("\n\nPrinting results:\n");
for (i = 0; i < r_size; ++i) {
printf("Result %u -> %s\n", i, results[i]);
}
pgml_pipelinec_delete(pipeline);
pgml_collectionc_delete(collection);
```
{% endtab %}
{% endtabs %}
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.
Expand All
@@ -228,6 +372,8 @@ if __name__ == "__main__":
{% endtab %}
{% endtabs %}
Note that `Rust` and `C` example do not require any additional code to run correctly.
Once you run the example, you should see something like this in the terminal:
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.