mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
[FIXED] Add PaLM vectorization source (#1182)
* Add PaLM vectorization source * FIX * Add API key check. Fix comment --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
This commit is contained in:
43
src/palm-vectors.js
Normal file
43
src/palm-vectors.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const fetch = require('node-fetch').default;
|
||||
const { SECRET_KEYS, readSecret } = require('./secrets');
|
||||
|
||||
/**
|
||||
* Gets the vector for the given text from PaLM gecko model
|
||||
* @param {string} text - The text to get the vector for
|
||||
* @returns {Promise<number[]>} - The vector for the text
|
||||
*/
|
||||
async function getPaLMVector(text) {
|
||||
const key = readSecret(SECRET_KEYS.PALM);
|
||||
|
||||
if (!key) {
|
||||
console.log('No PaLM key found');
|
||||
throw new Error('No PaLM key found');
|
||||
}
|
||||
|
||||
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta2/models/embedding-gecko-001:embedText?key=${key}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text: text,
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
console.log('PaLM request failed', response.statusText, text);
|
||||
throw new Error('PaLM request failed');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Access the "value" dictionary
|
||||
const vector = data.embedding.value;
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getPaLMVector,
|
||||
};
|
@@ -15,6 +15,8 @@ async function getVector(source, text) {
|
||||
return require('./openai-vectors').getOpenAIVector(text);
|
||||
case 'transformers':
|
||||
return require('./embedding').getTransformersVector(text);
|
||||
case 'palm':
|
||||
return require('./palm-vectors').getPaLMVector(text);
|
||||
}
|
||||
|
||||
throw new Error(`Unknown vector source ${source}`);
|
||||
|
Reference in New Issue
Block a user