| 1 | import fetch from 'node-fetch'; |
| 2 | import { SECRET_KEYS, readSecret } from '../endpoints/secrets.js'; |
| 3 | |
| 4 | const SOURCES = { |
| 5 | 'nomicai': { |
| 6 | secretKey: SECRET_KEYS.NOMICAI, |
| 7 | url: 'api-atlas.nomic.ai/v1/embedding/text', |
| 8 | model: 'nomic-embed-text-v1.5', |
| 9 | }, |
| 10 | }; |
| 11 | |
| 12 | /** |
| 13 | * Gets the vector for the given text batch from an OpenAI compatible endpoint. |
| 14 | * @param {string[]} texts - The array of texts to get the vector for |
| 15 | * @param {string} source - The source of the vector |
| 16 | * @param {import('../users.js').UserDirectoryList} directories - The directories object for the user |
| 17 | * @returns {Promise<number[][]>} - The array of vectors for the texts |
| 18 | */ |
| 19 | export async function getNomicAIBatchVector(texts, source, directories) { |
| 20 | const config = SOURCES[source]; |
| 21 | |
| 22 | if (!config) { |
| 23 | console.error('Unknown source', source); |
| 24 | throw new Error('Unknown source'); |
| 25 | } |
| 26 | |
| 27 | const key = readSecret(directories, config.secretKey); |
| 28 | |
| 29 | if (!key) { |
| 30 | console.warn('No API key found'); |
| 31 | throw new Error('No API key found'); |
| 32 | } |
| 33 | |
| 34 | const url = config.url; |
| 35 | let response; |
| 36 | response = await fetch(`https://${url}`, { |
| 37 | method: 'POST', |
| 38 | headers: { |
| 39 | 'Content-Type': 'application/json', |
| 40 | Authorization: `Bearer ${key}`, |
| 41 | }, |
| 42 | body: JSON.stringify({ |
| 43 | texts: texts, |
| 44 | model: config.model, |
| 45 | }), |
| 46 | }); |
| 47 | |
| 48 | if (!response.ok) { |
| 49 | const text = await response.text(); |
| 50 | console.warn('API request failed', response.statusText, text); |
| 51 | throw new Error('API request failed'); |
| 52 | } |
| 53 | |
| 54 | /** @type {any} */ |
| 55 | const data = await response.json(); |
| 56 | if (!Array.isArray(data?.embeddings)) { |
| 57 | console.warn('API response was not an array'); |
| 58 | throw new Error('API response was not an array'); |
| 59 | } |
| 60 | |
| 61 | return data.embeddings; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets the vector for the given text from an OpenAI compatible endpoint. |
| 66 | * @param {string} text - The text to get the vector for |
| 67 | * @param {string} source - The source of the vector |
| 68 | * @param {import('../users.js').UserDirectoryList} directories - The directories object for the user |
| 69 | * @returns {Promise<number[]>} - The vector for the text |
| 70 | */ |
| 71 | export async function getNomicAIVector(text, source, directories) { |
| 72 | const vectors = await getNomicAIBatchVector([text], source, directories); |
| 73 | return vectors[0]; |
| 74 | } |