- Notifications
You must be signed in to change notification settings - Fork328
Added new examples for JavaScript#953
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
Show all changes
2 commits Select commitHold shift + click to select a range
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
14 changes: 10 additions & 4 deletionspgml-sdks/rust/pgml/javascript/examples/README.md
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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
## Examples | ||
### [Semantic Search](./semantic_search.js) | ||
This is a basic example to perform semantic search on a collection of documents. Embeddings are created using `intfloat/e5-small` model. The results are semantically similar documemts to the query. Finally, the collection is archived. | ||
### [Question Answering](./question_answering.js) | ||
This is an example to find documents relevant to a question from the collection of documents. The query is passed to vector search to retrieve documents that match closely in the embeddings space. A score is returned with each of the search result. | ||
### [Question Answering using Instructore Model](./question_answering_instructor.js) | ||
In this example, we will use `hknlp/instructor-base` model to build text embeddings instead of the default `intfloat/e5-small` model. | ||
### [Extractive Question Answering](./extractive_question_answering.js) | ||
In this example, we will show how to use `vector_recall` result as a `context` to a HuggingFace question answering model. We will use `Builtins.transform()` to run the model on the database. |
62 changes: 62 additions & 0 deletionspgml-sdks/rust/pgml/javascript/examples/extractive_question_answering.js
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 @@ | ||
const pgml = require("pgml"); | ||
require("dotenv").config(); | ||
pgml.js_init_logger(); | ||
const main = async () => { | ||
// Initialize the collection | ||
const collection = pgml.newCollection("my_javascript_eqa_collection_2"); | ||
// Add a pipeline | ||
const model = pgml.newModel(); | ||
const splitter = pgml.newSplitter(); | ||
const pipeline = pgml.newPipeline( | ||
"my_javascript_eqa_pipeline_1", | ||
model, | ||
splitter, | ||
); | ||
await collection.add_pipeline(pipeline); | ||
// Upsert documents, these documents are automatically split into chunks and embedded by our pipeline | ||
const documents = [ | ||
{ | ||
id: "Document One", | ||
text: "PostgresML is the best tool for machine learning applications!", | ||
}, | ||
{ | ||
id: "Document Two", | ||
text: "PostgresML is open source and available to everyone!", | ||
}, | ||
]; | ||
await collection.upsert_documents(documents); | ||
const query = "What is the best tool for machine learning?"; | ||
// Perform vector search | ||
const queryResults = await collection | ||
.query() | ||
.vector_recall(query, pipeline) | ||
.limit(1) | ||
.fetch_all(); | ||
// Construct context from results | ||
const context = queryResults | ||
.map((result) => { | ||
return result[1]; | ||
}) | ||
.join("\n"); | ||
// Query for answer | ||
const builtins = pgml.newBuiltins(); | ||
const answer = await builtins.transform("question-answering", [ | ||
JSON.stringify({ question: query, context: context }), | ||
]); | ||
// Archive the collection | ||
await collection.archive(); | ||
return answer; | ||
}; | ||
main().then((results) => { | ||
console.log("Question answer: \n", results); | ||
}); |
12 changes: 0 additions & 12 deletionspgml-sdks/rust/pgml/javascript/examples/getting-started/README.md
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletionspgml-sdks/rust/pgml/javascript/examples/question_answering.js
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,55 @@ | ||
const pgml = require("pgml"); | ||
require("dotenv").config(); | ||
const main = async () => { | ||
// Initialize the collection | ||
const collection = pgml.newCollection("my_javascript_qa_collection"); | ||
// Add a pipeline | ||
const model = pgml.newModel(); | ||
const splitter = pgml.newSplitter(); | ||
const pipeline = pgml.newPipeline( | ||
"my_javascript_qa_pipeline", | ||
model, | ||
splitter, | ||
); | ||
await collection.add_pipeline(pipeline); | ||
// Upsert documents, these documents are automatically split into chunks and embedded by our pipeline | ||
const documents = [ | ||
{ | ||
id: "Document One", | ||
text: "PostgresML is the best tool for machine learning applications!", | ||
}, | ||
{ | ||
id: "Document Two", | ||
text: "PostgresML is open source and available to everyone!", | ||
}, | ||
]; | ||
await collection.upsert_documents(documents); | ||
// Perform vector search | ||
const queryResults = await collection | ||
.query() | ||
.vector_recall("What is the best tool for machine learning?", pipeline) | ||
.limit(1) | ||
.fetch_all(); | ||
// Convert the results to an array of objects | ||
const results = queryResults.map((result) => { | ||
const [similarity, text, metadata] = result; | ||
return { | ||
similarity, | ||
text, | ||
metadata, | ||
}; | ||
}); | ||
// Archive the collection | ||
await collection.archive(); | ||
return results; | ||
}; | ||
main().then((results) => { | ||
console.log("Vector search Results: \n", results); | ||
}); |
60 changes: 60 additions & 0 deletionspgml-sdks/rust/pgml/javascript/examples/question_answering_instructor.js
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,60 @@ | ||
const pgml = require("pgml"); | ||
require("dotenv").config(); | ||
const main = async () => { | ||
// Initialize the collection | ||
const collection = pgml.newCollection("my_javascript_qai_collection"); | ||
// Add a pipeline | ||
const model = pgml.newModel("hkunlp/instructor-base", "pgml", { | ||
instruction: "Represent the Wikipedia document for retrieval: ", | ||
}); | ||
const splitter = pgml.newSplitter(); | ||
const pipeline = pgml.newPipeline( | ||
"my_javascript_qai_pipeline", | ||
model, | ||
splitter, | ||
); | ||
await collection.add_pipeline(pipeline); | ||
// Upsert documents, these documents are automatically split into chunks and embedded by our pipeline | ||
const documents = [ | ||
{ | ||
id: "Document One", | ||
text: "PostgresML is the best tool for machine learning applications!", | ||
}, | ||
{ | ||
id: "Document Two", | ||
text: "PostgresML is open source and available to everyone!", | ||
}, | ||
]; | ||
await collection.upsert_documents(documents); | ||
// Perform vector search | ||
const queryResults = await collection | ||
.query() | ||
.vector_recall("What is the best tool for machine learning?", pipeline, { | ||
instruction: | ||
"Represent the Wikipedia question for retrieving supporting documents: ", | ||
}) | ||
.limit(1) | ||
.fetch_all(); | ||
// Convert the results to an array of objects | ||
const results = queryResults.map((result) => { | ||
const [similarity, text, metadata] = result; | ||
return { | ||
similarity, | ||
text, | ||
metadata, | ||
}; | ||
}); | ||
// Archive the collection | ||
await collection.archive(); | ||
return results; | ||
}; | ||
main().then((results) => { | ||
console.log("Vector search Results: \n", results); | ||
}); |
6 changes: 5 additions & 1 deletion...ascript/examples/getting-started/index.js → ...ml/javascript/examples/semantic_search.js
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
2 changes: 1 addition & 1 deletionpgml-sdks/rust/pgml/python/examples/README.md
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.