Skip to content

Commit 5d276fc

Browse files
committed
Working C
1 parent c72cb30 commit 5d276fc

File tree

14 files changed

+327
-96
lines changed

14 files changed

+327
-96
lines changed

pgml-sdks/pgml/go/test.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,13 @@ int main() {
3333
printf("Result %u -> %s\n", i, results[i]);
3434
}
3535

36+
// Test the TransformerPipeline
37+
TransformerPipelineC * t_pipeline = TransformerPipelineC_new("text-generation", "TheBloke/zephyr-7B-beta-GPTQ", "{\"revision\": \"main\"}", "postgres://pg:ml@sql.cloud.postgresml.org:38042/pgml");
38+
GeneralJsonAsyncIteratorC * t_pipeline_iter = TransformerPipelineC_transform_stream(t_pipeline, "\"AI is going to\"", "{\"max_new_tokens\": 100}", NULL);
39+
while (!GeneralJsonAsyncIteratorC_done(t_pipeline_iter)) {
40+
char * res = GeneralJsonAsyncIteratorC_next(t_pipeline_iter);
41+
printf("Token -> %s\n", res);
42+
}
43+
3644
return 0;
3745
}

pgml-sdks/pgml/src/builtins.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use sqlx::Row;
33
use tracing::instrument;
44

55
/// Provides access to builtin database methods
6-
// #[derive(alias, Debug, Clone)]
7-
#[derive(Debug, Clone)]
6+
#[derive(alias, Debug, Clone)]
87
pub struct Builtins {
98
database_url: Option<String>,
109
}
@@ -14,7 +13,10 @@ use crate::{get_or_initialize_pool, query_runner::QueryRunner, types::Json};
1413
#[cfg(feature = "python")]
1514
use crate::{query_runner::QueryRunnerPython, types::JsonPython};
1615

17-
// #[alias_methods(new, query, transform)]
16+
#[cfg(feature = "c")]
17+
use crate::{languages::c::JsonC, query_runner::QueryRunnerC};
18+
19+
#[alias_methods(new, query, transform)]
1820
impl Builtins {
1921
pub fn new(database_url: Option<String>) -> Self {
2022
Self { database_url }

pgml-sdks/pgml/src/languages/c.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use crate::types::{DateTime, GeneralJsonAsyncIterator, GeneralJsonIterator, Json};
2+
use futures::pin_mut;
3+
use futures::stream::Stream;
24
use rust_bridge::c::CustomInto;
5+
use std::pin::Pin;
36

47
pub type JsonC = std::ffi::c_char;
58

69
unsafe impl CustomInto<Json> for *mut JsonC {
710
unsafe fn custom_into(self) -> Json {
811
let s = std::ffi::CStr::from_ptr(self).to_str().unwrap();
12+
eprintln!("\nABOU TO DECODE: {}\n", s);
913
serde_json::from_str::<serde_json::Value>(s).unwrap().into()
1014
}
1115
}
@@ -16,3 +20,81 @@ unsafe impl CustomInto<*mut JsonC> for Json {
1620
std::ffi::CString::new(s).unwrap().into_raw()
1721
}
1822
}
23+
24+
#[repr(C)]
25+
pub struct GeneralJsonIteratorC {
26+
pub wrapped:
27+
*mut std::iter::Peekable<Box<dyn Iterator<Item = Result<Json, anyhow::Error>> + Send>>,
28+
}
29+
30+
unsafe impl CustomInto<*mut GeneralJsonIteratorC> for GeneralJsonIterator {
31+
unsafe fn custom_into(self) -> *mut GeneralJsonIteratorC {
32+
Box::into_raw(Box::new(GeneralJsonIteratorC {
33+
wrapped: Box::into_raw(Box::new(self.0.peekable())),
34+
}))
35+
}
36+
}
37+
38+
#[no_mangle]
39+
pub unsafe extern "C" fn GeneralJsonIteratorC_done(iterator: *mut GeneralJsonIteratorC) -> bool {
40+
let mut c = Box::leak(Box::from_raw(iterator));
41+
if let Some(_) = (*c.wrapped).peek() {
42+
false
43+
} else {
44+
true
45+
}
46+
}
47+
48+
#[no_mangle]
49+
pub unsafe extern "C" fn GeneralJsonIteratorC_next(
50+
iterator: *mut GeneralJsonIteratorC,
51+
) -> *mut JsonC {
52+
let c = Box::leak(Box::from_raw(iterator));
53+
let b = Box::leak(Box::from_raw(c.wrapped));
54+
(*b).next().unwrap().unwrap().custom_into()
55+
}
56+
57+
#[repr(C)]
58+
pub struct GeneralJsonAsyncIteratorC {
59+
pub wrapped: *mut futures::stream::Peekable<
60+
Pin<Box<dyn Stream<Item = Result<Json, anyhow::Error>> + Send>>,
61+
>,
62+
}
63+
64+
unsafe impl CustomInto<*mut GeneralJsonAsyncIteratorC> for GeneralJsonAsyncIterator {
65+
unsafe fn custom_into(self) -> *mut GeneralJsonAsyncIteratorC {
66+
use futures::stream::StreamExt;
67+
Box::into_raw(Box::new(GeneralJsonAsyncIteratorC {
68+
wrapped: Box::into_raw(Box::new(self.0.peekable())),
69+
}))
70+
}
71+
}
72+
73+
#[no_mangle]
74+
pub unsafe extern "C" fn GeneralJsonAsyncIteratorC_done(
75+
iterator: *mut GeneralJsonAsyncIteratorC,
76+
) -> bool {
77+
crate::get_or_set_runtime().block_on(async move {
78+
use futures::stream::StreamExt;
79+
let c = Box::leak(Box::from_raw(iterator));
80+
let s = Box::leak(Box::from_raw(c.wrapped));
81+
let mut s = Pin::new(s);
82+
let res = s.as_mut().peek_mut().await;
83+
if let Some(res) = res {
84+
false
85+
} else {
86+
true
87+
}
88+
})
89+
}
90+
91+
#[no_mangle]
92+
pub unsafe extern "C" fn GeneralJsonAsyncIteratorC_next(
93+
iterator: *mut GeneralJsonAsyncIteratorC,
94+
) -> *mut JsonC {
95+
crate::get_or_set_runtime().block_on(async move {
96+
use futures::stream::StreamExt;
97+
let mut c = Box::leak(Box::from_raw(iterator));
98+
(*c.wrapped).next().await.unwrap().unwrap().custom_into()
99+
})
100+
}

pgml-sdks/pgml/src/model.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use crate::{
1111
#[cfg(feature = "python")]
1212
use crate::types::JsonPython;
1313

14+
#[cfg(feature = "c")]
15+
use crate::languages::c::JsonC;
16+
1417
/// A few notes on the following enums:
1518
/// - Sqlx does provide type derivation for enums, but it's not very good
1619
/// - Queries using these enums require a number of additional queries to get their oids and
@@ -52,8 +55,7 @@ pub(crate) struct ModelDatabaseData {
5255
}
5356

5457
/// A model used for embedding, inference, etc...
55-
// #[derive(alias, Debug, Clone)]
56-
#[derive(Debug, Clone)]
58+
#[derive(alias, Debug, Clone)]
5759
pub struct Model {
5860
pub(crate) name: String,
5961
pub(crate) runtime: ModelRuntime,
@@ -67,7 +69,7 @@ impl Default for Model {
6769
}
6870
}
6971

70-
// #[alias_methods(new, transform)]
72+
#[alias_methods(new, transform)]
7173
impl Model {
7274
/// Creates a new [Model]
7375
pub fn new(name: Option<String>, source: Option<String>, parameters: Option<Json>) -> Self {

pgml-sdks/pgml/src/open_source_ai.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ use crate::{
1313
#[cfg(feature = "python")]
1414
use crate::types::{GeneralJsonAsyncIteratorPython, GeneralJsonIteratorPython, JsonPython};
1515

16+
#[cfg(feature = "c")]
17+
use crate::{
18+
languages::c::JsonC,
19+
languages::c::{GeneralJsonAsyncIteratorC, GeneralJsonIteratorC},
20+
};
21+
1622
/// A drop in replacement for OpenAI
17-
// #[derive(alias, Debug, Clone)]
18-
#[derive(Debug, Clone)]
23+
#[derive(alias, Debug, Clone)]
1924
pub struct OpenSourceAI {
2025
database_url: Option<String>,
2126
}
@@ -163,13 +168,13 @@ impl Iterator for AsyncToSyncJsonIterator {
163168
}
164169
}
165170

166-
// #[alias_methods(
167-
// new,
168-
// chat_completions_create,
169-
// chat_completions_create_async,
170-
// chat_completions_create_stream,
171-
// chat_completions_create_stream_async
172-
// )]
171+
#[alias_methods(
172+
new,
173+
chat_completions_create,
174+
chat_completions_create_async,
175+
chat_completions_create_stream,
176+
chat_completions_create_stream_async
177+
)]
173178
impl OpenSourceAI {
174179
/// Creates a new [OpenSourceAI]
175180
///

pgml-sdks/pgml/src/query_builder.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ use crate::{pipeline::Pipeline, types::Json, Collection};
1212
#[cfg(feature = "python")]
1313
use crate::{pipeline::PipelinePython, types::JsonPython};
1414

15+
#[cfg(feature = "c")]
16+
use crate::{languages::c::JsonC, pipeline::PipelineC};
17+
1518
#[derive(alias, Clone, Debug)]
1619
pub struct QueryBuilder {
1720
collection: Collection,
1821
query: Json,
1922
pipeline: Option<Pipeline>,
2023
}
2124

22-
#[alias_methods(limit, filter, vector_recall, to_full_string, fetch_all)]
25+
#[alias_methods(limit, filter, vector_recall, to_full_string, fetch_all(skip = "C"))]
2326
impl QueryBuilder {
2427
pub fn new(collection: Collection) -> Self {
2528
let query = json!({

pgml-sdks/pgml/src/query_runner.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use crate::{get_or_initialize_pool, types::Json};
88
#[cfg(feature = "python")]
99
use crate::types::JsonPython;
1010

11+
#[cfg(feature = "c")]
12+
use crate::languages::c::JsonC;
13+
1114
#[derive(Clone, Debug)]
1215
enum BindValue {
1316
String(String),
@@ -17,23 +20,22 @@ enum BindValue {
1720
Json(Json),
1821
}
1922

20-
// #[derive(alias, Clone, Debug)]
21-
#[derive(Clone, Debug)]
23+
#[derive(alias, Clone, Debug)]
2224
pub struct QueryRunner {
2325
query: String,
2426
bind_values: Vec<BindValue>,
2527
database_url: Option<String>,
2628
}
2729

28-
// #[alias_methods(
29-
// fetch_all,
30-
// execute,
31-
// bind_string,
32-
// bind_int,
33-
// bind_float,
34-
// bind_bool,
35-
// bind_json
36-
// )]
30+
#[alias_methods(
31+
fetch_all,
32+
execute,
33+
bind_string,
34+
bind_int,
35+
bind_float,
36+
bind_bool,
37+
bind_json
38+
)]
3739
impl QueryRunner {
3840
pub fn new(query: &str, database_url: Option<String>) -> Self {
3941
Self {

pgml-sdks/pgml/src/splitter.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use crate::{
1111
#[cfg(feature = "python")]
1212
use crate::types::JsonPython;
1313

14+
#[cfg(feature = "c")]
15+
use crate::languages::c::JsonC;
16+
1417
#[allow(dead_code)]
1518
#[derive(Debug, Clone)]
1619
pub(crate) struct SplitterDatabaseData {
@@ -19,8 +22,7 @@ pub(crate) struct SplitterDatabaseData {
1922
}
2023

2124
/// A text splitter
22-
// #[derive(alias, Debug, Clone)]
23-
#[derive(Debug, Clone)]
25+
#[derive(alias, Debug, Clone)]
2426
pub struct Splitter {
2527
pub(crate) name: String,
2628
pub(crate) parameters: Json,
@@ -33,7 +35,7 @@ impl Default for Splitter {
3335
}
3436
}
3537

36-
// #[alias_methods(new)]
38+
#[alias_methods(new)]
3739
impl Splitter {
3840
/// Creates a new [Splitter]
3941
///

pgml-sdks/pgml/src/transformer_pipeline.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use sqlx::Row;
44
use tracing::instrument;
55

66
/// Provides access to builtin database methods
7-
// #[derive(alias, Debug, Clone)]
8-
#[derive(Debug, Clone)]
7+
#[derive(alias, Debug, Clone)]
98
pub struct TransformerPipeline {
109
task: Json,
1110
database_url: Option<String>,
@@ -17,7 +16,10 @@ use crate::{get_or_initialize_pool, types::Json};
1716
#[cfg(feature = "python")]
1817
use crate::types::{GeneralJsonAsyncIteratorPython, JsonPython};
1918

20-
// #[alias_methods(new, transform, transform_stream)]
19+
#[cfg(feature = "c")]
20+
use crate::{languages::c::GeneralJsonAsyncIteratorC, languages::c::JsonC};
21+
22+
#[alias_methods(new, transform, transform_stream)]
2123
impl TransformerPipeline {
2224
/// Creates a new [TransformerPipeline]
2325
///

0 commit comments

Comments
 (0)