Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit746953e

Browse files
committed
add vLLM to the transform API
1 parentb212ee0 commit746953e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

‎pgml-extension/src/api.rs‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ use std::fmt::Write;
22
use std::str::FromStr;
33

44
use ndarray::Zip;
5+
use once_cell::sync::OnceCell;
56
use pgrx::iter::{SetOfIterator,TableIterator};
67
use pgrx::*;
8+
use serde_json::Value;
79

810
#[cfg(feature ="python")]
911
use serde_json::json;
1012

13+
usecrate::bindings::vllm::{LLMBuilder,LLM};
1114
#[cfg(feature ="python")]
1215
usecrate::orm::*;
1316

@@ -610,7 +613,7 @@ pub fn transform_json(
610613
inputs:default!(Vec<&str>,"ARRAY[]::TEXT[]"),
611614
cache:default!(bool,false),
612615
) ->JsonB{
613-
matchcrate::bindings::transformers::transform(&task.0,&args.0, inputs){
616+
matchtransform(task.0, args.0, inputs){
614617
Ok(output) =>JsonB(output),
615618
Err(e) =>error!("{e}"),
616619
}
@@ -632,6 +635,34 @@ pub fn transform_string(
632635
}
633636
}
634637

638+
fntransform(muttask:Value,args:Value,inputs:Vec<&str>) -> anyhow::Result<Value>{
639+
// use vLLM if model present in task and backend is set to vllm
640+
let use_vllm = task.as_object_mut().is_some_and(|obj|{
641+
obj.contains_key("model") &&matches!(obj.get("backend"),Some(Value::String(backend))if backend.to_string().to_ascii_lowercase() =="vllm")
642+
});
643+
644+
if use_vllm{
645+
crate::bindings::python::activate().unwrap();
646+
647+
staticLAZY_LLM:OnceCell<LLM> =OnceCell::new();
648+
let llm =LAZY_LLM.get_or_init(move ||{
649+
let builder =matchLLMBuilder::try_from(task){
650+
Ok(b) => b,
651+
Err(e) =>error!("{e}"),
652+
};
653+
builder.build().unwrap()
654+
});
655+
656+
Ok(json!(llm.generate(&inputs,None)?))
657+
}else{
658+
ifletSome(map) = task.as_object_mut(){
659+
// pop backend keyword, if present
660+
let _ = map.remove("backend");
661+
}
662+
crate::bindings::transformers::transform(&task,&args, inputs)
663+
}
664+
}
665+
635666
#[cfg(feature ="python")]
636667
#[pg_extern(immutable, parallel_safe, name ="generate")]
637668
fngenerate(project_name:&str,inputs:&str,config:default!(JsonB,"'{}'")) ->String{

‎pgml-extension/src/bindings/vllm/llm.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use pyo3::{prelude::*, types::PyDict};
2+
use serde_json::Value;
23

34
usesuper::SamplingParams;
45

@@ -213,6 +214,28 @@ impl ToPyObject for Quantization {
213214
}
214215
}
215216

217+
implTryFrom<Value>forLLMBuilder{
218+
typeError =&'staticstr;
219+
220+
fntry_from(value:Value) ->Result<Self,Self::Error>{
221+
match value.as_object(){
222+
Some(map) =>{
223+
let model = map
224+
.get("model")
225+
.ok_or("Json object must have `model` key")?
226+
.as_str()
227+
.ok_or("`model` key must be a str")?;
228+
229+
Ok(LLMBuilder::new(model))
230+
}
231+
None =>match value.as_str(){
232+
Some(model) =>Ok(LLMBuilder::new(model)),
233+
None =>Err("Json value expected as str or object"),
234+
},
235+
}
236+
}
237+
}
238+
216239
#[cfg(test)]
217240
mod tests{
218241
usecrate::SamplingParamsBuilder;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp