🌐 AI搜索 & 代理 主页
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add pgml.embed() to the builtins
  • Loading branch information
levkk committed May 22, 2024
commit 51d005775fad372bfb27d22308ed3e4754b22579
2 changes: 1 addition & 1 deletion pgml-sdks/pgml/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pgml-sdks/pgml/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pgml"
version = "1.0.3"
version = "1.0.4"
edition = "2021"
authors = ["PosgresML <team@postgresml.org>"]
homepage = "https://postgresml.org/"
Expand Down
18 changes: 17 additions & 1 deletion pgml-sdks/pgml/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{get_or_initialize_pool, query_runner::QueryRunner, types::Json};
#[cfg(feature = "python")]
use crate::{query_runner::QueryRunnerPython, types::JsonPython};

#[alias_methods(new, query, transform)]
#[alias_methods(new, query, transform, embed)]
impl Builtins {
pub fn new(database_url: Option<String>) -> Self {
Self { database_url }
Expand Down Expand Up @@ -87,6 +87,22 @@ impl Builtins {
let results = results.first().unwrap().get::<serde_json::Value, _>(0);
Ok(Json(results))
}

/// Run the built-in `pgml.embed()` function.
///
/// # Arguments
///
/// * `model` - The model to use.
/// * `text` - The text to embed.
///
pub async fn embed(&self, model: &str, text: &str) -> anyhow::Result<Json> {
let pool = get_or_initialize_pool(&self.database_url).await?;
let query = sqlx::query("SELECT pgml.embed($1, $2)");
let result = query.bind(model).bind(text).fetch_one(&pool).await?;
let result = result.get::<Vec<f32>, _>(0);
let result = serde_json::to_value(result)?;
Ok(Json(result))
}
}

#[cfg(test)]
Expand Down