**feat(similarity): add similarity calculation between vectors and text inputs**
- Introduced `calculateSimilarity` method in `types.d.ts` for comparing vectors or textual inputs: - Added support for custom options, including `forceEmbed` and a custom `distanceFunction`. - Enhanced functionality in `embed` to convert text inputs into vector representations. - Added new test cases in `vector-operations.test.ts`: - Validated similarity calculations between identical and different vectors. - Tested similarity scoring for similar and dissimilar text inputs. - Updated `README.md`: - Documented `calculateSimilarity` usage examples, including advanced options. - Clarified the integration of the similarity function into workflows. **Purpose**: Enable calculation of similarity scores for vectors and text inputs to facilitate advanced data comparison and retrieval tasks.
This commit is contained in:
parent
90cbccb1da
commit
d05d381a5d
3 changed files with 77 additions and 0 deletions
19
README.md
19
README.md
|
|
@ -731,6 +731,25 @@ await threadedDb.init()
|
|||
|
||||
// Directly embed text to vectors
|
||||
const vector = await db.embed("Some text to convert to a vector")
|
||||
|
||||
// Calculate similarity between two texts or vectors
|
||||
const similarity = await db.calculateSimilarity(
|
||||
"Cats are furry pets",
|
||||
"Felines make good companions"
|
||||
)
|
||||
console.log(`Similarity score: ${similarity}`) // Higher value means more similar
|
||||
|
||||
// Calculate similarity with custom options
|
||||
const vectorA = await db.embed("First text")
|
||||
const vectorB = await db.embed("Second text")
|
||||
const customSimilarity = await db.calculateSimilarity(
|
||||
vectorA, // Can use pre-computed vectors
|
||||
vectorB,
|
||||
{
|
||||
forceEmbed: false, // Skip embedding if inputs are already vectors
|
||||
distanceFunction: cosineDistance // Optional custom distance function
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
The threaded embedding function runs in a separate thread (Web Worker in browsers, Worker Thread in Node.js) to improve
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue