Gathering detailed insights and metrics for cosinity
Gathering detailed insights and metrics for cosinity
Gathering detailed insights and metrics for cosinity
Gathering detailed insights and metrics for cosinity
npm install cosinity
Typescript
Module System
Node Version
NPM Version
66.6
Supply Chain
98
Quality
78.5
Maintenance
100
Vulnerability
100
License
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
No dependencies detected.
Cosinity is a lightweight, zero-dependency NPM package for calculating the cosine similarity between two vectors. It's particularly useful for working with vector embeddings, such as those obtained from the OpenAI Embedding API, enabling applications in semantic similarity, recommendation systems, and more.
import
) and CommonJS (require
).You can install Cosinity via NPM:
1npm install cosinity
Or with Yarn:
1yarn add cosinity
1import cosineSimilarity from "cosinity"; 2 3const vectorA = [1, 2, 3]; 4const vectorB = [4, 5, 6]; 5 6const similarity = cosineSimilarity(vectorA, vectorB); 7console.log("Cosine Similarity:", similarity);
1const cosineSimilarity = require("cosinity"); 2 3const vectorA = [1, 2, 3]; 4const vectorB = [4, 5, 6]; 5 6const similarity = cosineSimilarity(vectorA, vectorB); 7console.log("Cosine Similarity:", similarity);
cosineSimilarity(vectorA, vectorB)
Calculates the cosine similarity between two vectors.
vectorA
number[]
: The first vector.vectorB
number[]
: The second vector.number
: The cosine similarity between vectorA
and vectorB
. The value ranges from -1
(exact opposite) to 1
(exact same), where 0
indicates orthogonality (no similarity).Error
: If the input vectors are not of the same length or are empty.1import cosineSimilarity from "cosinity"; 2 3const vectorA = [0, 1]; 4const vectorB = [1, 0]; 5 6const similarity = cosineSimilarity(vectorA, vectorB); 7console.log("Cosine Similarity:", similarity); // Output: 0
Cosinity can be integrated with OpenAI's Embedding API to calculate the similarity between text snippets.
1import OpenAI from "openai"; 2import cosineSimilarity from "cosinity"; 3 4const openai = new OpenAI({ 5 apiKey: "YOUR_OPENAI_API_KEY", // Replace with your OpenAI API key 6}); 7 8async function getEmbedding(text) { 9 const response = await openai.embeddings.create({ 10 model: "text-embedding-3-small", 11 input: text, 12 encoding_format: "float", 13 }); 14 return response.data[0].embedding; 15} 16 17async function compareTexts(text1, text2) { 18 const [embedding1, embedding2] = await Promise.all([ 19 getEmbedding(text1), 20 getEmbedding(text2), 21 ]); 22 23 const similarity = cosineSimilarity(embedding1, embedding2); 24 25 console.log( 26 `Cosine Similarity between "${text1}" and "${text2}":`, 27 similarity 28 ); 29} 30 31compareTexts("Hello, world!", "Hi, universe!");
Cosine Similarity between "Hello, world!" and "Hi, universe!": 0.87654321
The cosineSimilarity
function performs input validation:
vectorA
and vectorB
are not of the same length.Example:
1try { 2 cosineSimilarity([1, 2], [1, 2, 3]); 3} catch (error) { 4 console.error(error.message); // Output: Vectors must be of the same length and not empty. 5}
Contributions are welcome!
Fork the repository.
Clone your fork:
1git clone https://github.com/develanet/cosinity.git
Create a new branch:
1git checkout -b feature/my-new-feature
Commit your changes:
1git commit -am 'Add new feature'
Push to the branch:
1git push origin feature/my-new-feature
Submit a Pull Request.
Please make sure your code passes existing tests and add new tests for your features.
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by Zay
For any questions or suggestions, feel free to open an issue or contact me at isaias@develanet.com.
No vulnerabilities found.
No security vulnerabilities found.