Merge pull request #2847 from ceruleandeep/staging
Update makersuite-vectors.js to use Gemini API text-embedding-004 model
This commit is contained in:
commit
34a1b5586a
|
@ -1,5 +1,6 @@
|
||||||
const fetch = require('node-fetch').default;
|
const fetch = require('node-fetch').default;
|
||||||
const { SECRET_KEYS, readSecret } = require('../endpoints/secrets');
|
const { SECRET_KEYS, readSecret } = require('../endpoints/secrets');
|
||||||
|
const API_MAKERSUITE = 'https://generativelanguage.googleapis.com';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the vector for the given text from gecko model
|
* Gets the vector for the given text from gecko model
|
||||||
|
@ -9,12 +10,11 @@ const { SECRET_KEYS, readSecret } = require('../endpoints/secrets');
|
||||||
*/
|
*/
|
||||||
async function getMakerSuiteBatchVector(texts, directories) {
|
async function getMakerSuiteBatchVector(texts, directories) {
|
||||||
const promises = texts.map(text => getMakerSuiteVector(text, directories));
|
const promises = texts.map(text => getMakerSuiteVector(text, directories));
|
||||||
const vectors = await Promise.all(promises);
|
return await Promise.all(promises);
|
||||||
return vectors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the vector for the given text from PaLM gecko model
|
* 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 {string} text - The text to get the vector for
|
||||||
* @param {import('../users').UserDirectoryList} directories - The directories object for the user
|
* @param {import('../users').UserDirectoryList} directories - The directories object for the user
|
||||||
* @returns {Promise<number[]>} - The vector for the text
|
* @returns {Promise<number[]>} - The vector for the text
|
||||||
|
@ -27,14 +27,23 @@ async function getMakerSuiteVector(text, directories) {
|
||||||
throw new Error('No Google AI Studio key found');
|
throw new Error('No Google AI Studio key found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/embedding-gecko-001:embedText?key=${key}`, {
|
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',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
|
||||||
text: text,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
@ -44,11 +53,8 @@ async function getMakerSuiteVector(text, directories) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
// noinspection JSValidateTypes
|
||||||
// Access the "value" dictionary
|
return data['embedding']['values'];
|
||||||
const vector = data.embedding.value;
|
|
||||||
|
|
||||||
return vector;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
Loading…
Reference in New Issue