- Notifications
You must be signed in to change notification settings - Fork352
Add LangChain splitters#655
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletionspgml-extension/examples/chunking.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| --- Chunk text for LLM embeddings and vectorization. | ||
| DROP TABLE documents CASCADE; | ||
| CREATE TABLE documents ( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| document TEXT NOT NULL, | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
| ); | ||
| DROP TABLE splitters CASCADE; | ||
| CREATE TABLE splitters ( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| splitter VARCHAR NOT NULL DEFAULT 'recursive_character' | ||
| ); | ||
| DROP TABLE document_chunks CASCADE; | ||
| CREATE TABLE document_chunks( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| document_id BIGINT NOT NULL REFERENCES documents(id), | ||
| splitter_id BIGINT NOT NULL REFERENCES splitters(id), | ||
| chunk_index BIGINT NOT NULL, | ||
| chunk VARCHAR | ||
| ); | ||
| INSERT INTO documents VALUES ( | ||
| 1, | ||
| 'It was the best of times, it was the worst of times, it was the age of wisdom, | ||
| it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, | ||
| it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, | ||
| we had nothing before us, we were all going direct to Heaven, we were all going direct the other way—in short, the period was so far like | ||
| the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.', | ||
| NOW() | ||
| ); | ||
| INSERT INTO splitters VALUES (1, 'recursive_character'); | ||
| WITH document AS ( | ||
| SELECT id, document | ||
| FROM documents | ||
| WHERE id = 1 | ||
| ), | ||
| splitter AS ( | ||
| SELECT id, splitter | ||
| FROM splitters | ||
| WHERE id = 1 | ||
| ) | ||
| INSERT INTO document_chunks SELECT | ||
| nextval('document_chunks_id_seq'::regclass), | ||
| (SELECT id FROM document), | ||
| (SELECT id FROM splitter), | ||
| chunk_index, | ||
| chunk | ||
| FROM | ||
| pgml.chunk( | ||
| (SELECT splitter FROM splitter), | ||
| (SELECT document FROM document), | ||
| '{"chunk_size": 2, "chunk_overlap": 2}' | ||
| ); | ||
| SELECT * FROM document_chunks LIMIT 5; |
1 change: 1 addition & 0 deletionspgml-extension/requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,3 +18,4 @@ torchvision==0.15.2 | ||
| tqdm==4.65.0 | ||
| transformers==4.29.2 | ||
| xgboost==1.7.5 | ||
| langchain==0.0.180 | ||
22 changes: 19 additions & 3 deletionspgml-extension/src/api.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletionspgml-extension/src/bindings/langchain.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from langchain.text_splitter import ( | ||
| CharacterTextSplitter, | ||
| LatexTextSplitter, | ||
| MarkdownTextSplitter, | ||
| NLTKTextSplitter, | ||
| PythonCodeTextSplitter, | ||
| RecursiveCharacterTextSplitter, | ||
| SpacyTextSplitter, | ||
| ) | ||
| import json | ||
| SPLITTERS = { | ||
| "character": CharacterTextSplitter, | ||
| "latex": LatexTextSplitter, | ||
| "markdown": MarkdownTextSplitter, | ||
| "nltk": NLTKTextSplitter, | ||
| "python": PythonCodeTextSplitter, | ||
| "recursive_character": RecursiveCharacterTextSplitter, | ||
| "spacy": SpacyTextSplitter, | ||
| } | ||
| def chunk(splitter, text, args): | ||
| kwargs = json.loads(args) | ||
| if splitter in SPLITTERS: | ||
| return SPLITTERS[splitter](**kwargs).split_text(text) | ||
| else: | ||
| raise ValueError("Unknown splitter: {}".format(splitter)) |
37 changes: 37 additions & 0 deletionspgml-extension/src/bindings/langchain.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| use once_cell::sync::Lazy; | ||
| use pgrx::*; | ||
| use pyo3::prelude::*; | ||
| use pyo3::types::PyTuple; | ||
| static PY_MODULE: Lazy<Py<PyModule>> = Lazy::new(|| { | ||
| Python::with_gil(|py| -> Py<PyModule> { | ||
| let src = include_str!(concat!( | ||
| env!("CARGO_MANIFEST_DIR"), | ||
| "/src/bindings/langchain.py" | ||
| )); | ||
| PyModule::from_code(py, src, "", "").unwrap().into() | ||
| }) | ||
| }); | ||
| pub fn chunk(splitter: &str, text: &str, kwargs: &serde_json::Value) -> Vec<String> { | ||
| crate::bindings::venv::activate(); | ||
| let kwargs = serde_json::to_string(kwargs).unwrap(); | ||
| Python::with_gil(|py| -> Vec<String> { | ||
| let chunk: Py<PyAny> = PY_MODULE.getattr(py, "chunk").unwrap().into(); | ||
| chunk | ||
| .call1( | ||
| py, | ||
| PyTuple::new( | ||
| py, | ||
| &[splitter.into_py(py), text.into_py(py), kwargs.into_py(py)], | ||
| ), | ||
| ) | ||
| .unwrap() | ||
| .extract(py) | ||
| .unwrap() | ||
| }) | ||
| } |
2 changes: 2 additions & 0 deletionspgml-extension/src/bindings/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
6 changes: 1 addition & 5 deletionspgml-extension/src/bindings/transformers.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionspgml-extension/tests/test.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.