File size: 2,074 Bytes
4342d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import fetch from 'node-fetch';
import { SECRET_KEYS, readSecret } from '../endpoints/secrets.js';
const API_MAKERSUITE = 'https://generativelanguage.googleapis.com';

/**
 * Gets the vector for the given text from gecko model
 * @param {string[]} texts - The array of texts to get the vector for
 * @param {import('../users.js').UserDirectoryList} directories - The directories object for the user
 * @returns {Promise<number[][]>} - The array of vectors for the texts
 */
export async function getMakerSuiteBatchVector(texts, directories) {
    const promises = texts.map(text => getMakerSuiteVector(text, directories));
    return await Promise.all(promises);
}

/**
 * Gets the vector for the given text from Gemini API text-embedding-004 model
 * @param {string} text - The text to get the vector for
 * @param {import('../users.js').UserDirectoryList} directories - The directories object for the user
 * @returns {Promise<number[]>} - The vector for the text
 */
export async function getMakerSuiteVector(text, directories) {
    const key = readSecret(directories, SECRET_KEYS.MAKERSUITE);

    if (!key) {
        console.warn('No Google AI Studio key found');
        throw new Error('No Google AI Studio key found');
    }

    const apiUrl = new URL(API_MAKERSUITE);
    const model = 'text-embedding-004';
    const url = `${apiUrl.origin}/v1beta/models/${model}:embedContent?key=${key}`;
    const body = {
        content: {
            parts: [
                { text: text },
            ],
        },
    };

    const response = await fetch(url, {
        body: JSON.stringify(body),
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
    });

    if (!response.ok) {
        const text = await response.text();
        console.warn('Google AI Studio request failed', response.statusText, text);
        throw new Error('Google AI Studio request failed');
    }

    /** @type {any} */
    const data = await response.json();
    // noinspection JSValidateTypes
    return data['embedding']['values'];
}