Skip to content

Create embedding API #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pgml-extension/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ RUN apt update && apt-fast install -y \
python3-pip \
libpython3.10-dev \
python3.10-dev
RUN pip3 install xgboost scikit-learn torch lightgbm transformers datasets
RUN pip3 install xgboost scikit-learn torch lightgbm transformers datasets sentence_transformers
RUN useradd postgresml -m -s /bin/bash -G sudo
RUN echo 'postgresml ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
USER postgresml
Expand Down
2 changes: 1 addition & 1 deletion pgml-extension/Dockerfile.local
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN cat /etc/apt/sources.list
RUN apt-get update && apt-get install -y postgresql-pgml-14

# Cache this, quicker
RUN pip3 install xgboost scikit-learn diptest torch lightgbm transformers datasets sentencepiece sacremoses sacrebleu rouge
RUN pip3 install xgboost scikit-learn diptest torch lightgbm transformers datasets sentencepiece sentence_transformers sacremoses sacrebleu rouge

COPY --chown=postgres:postgres . /app
WORKDIR /app
Expand Down
5 changes: 5 additions & 0 deletions pgml-extension/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ fn load_dataset(
TableIterator::new(vec![(name, rows)].into_iter())
}

#[pg_extern]
pub fn embed(transformer: &str, text: &str, kwargs: default!(JsonB, "'{}'")) -> Vec<f32> {
crate::bindings::transformers::embed(transformer, &text, &kwargs.0)
}

#[cfg(feature = "python")]
#[pg_extern(name = "transform")]
pub fn transform_json(
Expand Down
9 changes: 9 additions & 0 deletions pgml-extension/src/bindings/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import datasets
from rouge import Rouge
from sacrebleu.metrics import BLEU
from sentence_transformers import SentenceTransformer
from sklearn.metrics import (
mean_squared_error,
r2_score,
Expand Down Expand Up @@ -37,6 +38,7 @@
)

__cache_transformer_by_model_id = {}
__cache_sentence_transformer_by_name = {}

def transform(task, args, inputs):
task = json.loads(task)
Expand All @@ -50,6 +52,13 @@ def transform(task, args, inputs):

return json.dumps(pipe(inputs, **args))

def embed(transformer, text, kwargs):
kwargs = json.loads(kwargs)
if transformer not in __cache_sentence_transformer_by_name:
__cache_sentence_transformer_by_name[transformer] = SentenceTransformer(transformer)
model = __cache_sentence_transformer_by_name[transformer]
return model.encode(text, **kwargs)

def load_dataset(name, subset, limit: None, kwargs: "{}"):
kwargs = json.loads(kwargs)

Expand Down
22 changes: 22 additions & 0 deletions pgml-extension/src/bindings/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ pub fn transform(
serde_json::from_str(&results).unwrap()
}

pub fn embed(transformer: &str, text: &str, kwargs: &serde_json::Value) -> Vec<f32> {
let kwargs = serde_json::to_string(kwargs).unwrap();
Python::with_gil(|py| -> Vec<f32> {
let embed: Py<PyAny> = PY_MODULE.getattr(py, "embed").unwrap().into();
embed
.call1(
py,
PyTuple::new(
py,
&[
transformer.to_string().into_py(py),
text.to_string().into_py(py),
kwargs.into_py(py),
],
),
)
.unwrap()
.extract(py)
.unwrap()
})
}

pub fn tune(
task: &Task,
dataset: TextDataset,
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy