-
-
Save kiritocode1/f5ed00e61197564bf0a4a2491c097bee to your computer and use it in GitHub Desktop.
0$ Ai project
This file contains 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
//? npm i @huggingface/inference | |
//? npm i --save-dev dotenv | |
//? get the api key from here : https://huggingface.co/settings/tokens | |
// 💖 written by Aryan Kathawale :) | |
import { HfInference } from "@huggingface/inference"; | |
import { config } from "dotenv"; | |
config(); | |
// dot product function , 🤗 | |
// returns the similarity between two vectors | |
function dotProduct(a: number[], b: number[]) { | |
if (a.length !== b.length) { | |
throw new Error('Both arguments must have the same length'); | |
} | |
let result = 0; | |
for (let i = 0; i < a.length; i++) { | |
result += a[i] * b[i]; | |
} | |
return result; | |
} | |
// initialize hf inference api , 🤗 | |
const HF = new HfInference(process.env.Prod_token); | |
async function main () { | |
const result1 = await HF.featureExtraction({ | |
model: "intfloat/e5-small-v2", | |
inputs: "sky is blue" | |
}); | |
const result2 = await HF.featureExtraction({ | |
model: "intfloat/e5-small-v2", | |
inputs: "sky is blue" | |
}); | |
if(is1DArray(result1) && is1DArray(result2)) { | |
console.log(dotProduct(result1, result2)); | |
} | |
} | |
// typescript bullshittery 🤗 ifnore if doing pure js | |
function is1DArray<T>(value: (T | T[] | T[][])[]): value is T[] { | |
return !Array.isArray(value[0]); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
! important : Api is free and infinite , but is rate limited to 10 calls , please try this and try hugging face out