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
# Open Source Alternative for Building End-to-End Vector Search Applications without OpenAI & Pinecone
2
+
# How to use this crate
3
+
4
+
Here is a brief outline of how to use this crate and specifically add new Python classes.
5
+
6
+
There are three main macros to know about:
7
+
-`custom_derive`
8
+
-`custom_methods`
9
+
-`custom_into_py`
10
+
11
+
## custom_derive
12
+
`custom_derive` is used when defining a new struct that you want to be available as a Python class. This macro automatically creates a wrapper for the struct postfixing the name with `Python`. For example, the following code:
13
+
```
14
+
#[derive(custom_derive, Debug, Clone)]
15
+
pub struct TestStruct {
16
+
pub name: String
17
+
}
18
+
```
19
+
20
+
Creates another struct:
21
+
22
+
```
23
+
pub struct TestStructPython {
24
+
pub wrapped: TestStruct
25
+
}
26
+
```
27
+
28
+
You must currently implement `Debug` and `Clone` on the structs you use `custom_derive` on.
29
+
30
+
## custom_methods
31
+
`custom_methods` is used on the impl block for a struct you want to be available as a Python class. This macro automatically creates methods that work seamlessly with pyO3. For example, the following code:
.expect("Error setting python value in custom_into_py proc_macro");
100
+
dict.set_item("name", self.name)
101
+
.expect("Error setting python value in custom_into_py proc_macro");
102
+
dict.set_item("parameters", self.parameters.0)
103
+
.expect("Error setting python value in custom_into_py proc_macro");
104
+
dict.into()
105
+
}
106
+
}
107
+
```
108
+
109
+
Implementing `IntoPy` allows pyo3 to seamlessly convert between Rust and python. Note that Python users calling `get_text_splitters` will receive a list of dictionaries.
110
+
111
+
## Other Noteworthy Things
112
+
113
+
Be aware that the only pyo3 specific code in this crate is the `pymodule` invocation in `lib.rs`. Everything else is handled by `pgml-macros`. If you want to expose a Python Class directly on the Python module you have to add it in the `pymodule` invocation. For example, if you wanted to expose `TestStruct` so Python module users could access it directly on `pgml`, you could do the following:
0 commit comments