mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Migrate Palm secret key, fix vector source key access
This commit is contained in:
@@ -44,6 +44,17 @@ function writeSecret(key, value) {
|
|||||||
writeFileAtomicSync(SECRETS_FILE, JSON.stringify(secrets, null, 4), 'utf-8');
|
writeFileAtomicSync(SECRETS_FILE, JSON.stringify(secrets, null, 4), 'utf-8');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteSecret(key) {
|
||||||
|
if (!fs.existsSync(SECRETS_FILE)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileContents = fs.readFileSync(SECRETS_FILE, 'utf-8');
|
||||||
|
const secrets = JSON.parse(fileContents);
|
||||||
|
delete secrets[key];
|
||||||
|
writeFileAtomicSync(SECRETS_FILE, JSON.stringify(secrets, null, 4), 'utf-8');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a secret from the secrets file
|
* Reads a secret from the secrets file
|
||||||
* @param {string} key Secret key
|
* @param {string} key Secret key
|
||||||
@@ -119,6 +130,14 @@ function migrateSecrets(settingsFile) {
|
|||||||
modified = true;
|
modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const palmKey = readSecret('api_key_palm');
|
||||||
|
if (palmKey) {
|
||||||
|
console.log('Migrating Palm key...');
|
||||||
|
writeSecret(SECRET_KEYS.MAKERSUITE, palmKey);
|
||||||
|
deleteSecret('api_key_palm');
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (modified) {
|
if (modified) {
|
||||||
console.log('Writing updated settings.json...');
|
console.log('Writing updated settings.json...');
|
||||||
const settingsContent = JSON.stringify(settings, null, 4);
|
const settingsContent = JSON.stringify(settings, null, 4);
|
||||||
|
@@ -17,7 +17,7 @@ async function getVector(source, text) {
|
|||||||
case 'transformers':
|
case 'transformers':
|
||||||
return require('../embedding').getTransformersVector(text);
|
return require('../embedding').getTransformersVector(text);
|
||||||
case 'palm':
|
case 'palm':
|
||||||
return require('../palm-vectors').getPaLMVector(text);
|
return require('../makersuite-vectors').getMakerSuiteVector(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`Unknown vector source ${source}`);
|
throw new Error(`Unknown vector source ${source}`);
|
||||||
@@ -196,7 +196,7 @@ router.post('/purge', jsonParser, async (req, res) => {
|
|||||||
|
|
||||||
const collectionId = String(req.body.collectionId);
|
const collectionId = String(req.body.collectionId);
|
||||||
|
|
||||||
const sources = ['transformers', 'openai'];
|
const sources = ['transformers', 'openai', 'palm'];
|
||||||
for (const source of sources) {
|
for (const source of sources) {
|
||||||
const index = await getIndex(collectionId, source, false);
|
const index = await getIndex(collectionId, source, false);
|
||||||
|
|
||||||
|
@@ -6,12 +6,12 @@ const { SECRET_KEYS, readSecret } = require('./endpoints/secrets');
|
|||||||
* @param {string} text - The text to get the vector for
|
* @param {string} text - The text to get the vector for
|
||||||
* @returns {Promise<number[]>} - The vector for the text
|
* @returns {Promise<number[]>} - The vector for the text
|
||||||
*/
|
*/
|
||||||
async function getPaLMVector(text) {
|
async function getMakerSuiteVector(text) {
|
||||||
const key = readSecret(SECRET_KEYS.PALM);
|
const key = readSecret(SECRET_KEYS.MAKERSUITE);
|
||||||
|
|
||||||
if (!key) {
|
if (!key) {
|
||||||
console.log('No PaLM key found');
|
console.log('No MakerSuite key found');
|
||||||
throw new Error('No PaLM key found');
|
throw new Error('No MakerSuite key found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/embedding-gecko-001:embedText?key=${key}`, {
|
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/embedding-gecko-001:embedText?key=${key}`, {
|
||||||
@@ -26,8 +26,8 @@ async function getPaLMVector(text) {
|
|||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
console.log('PaLM request failed', response.statusText, text);
|
console.log('MakerSuite request failed', response.statusText, text);
|
||||||
throw new Error('PaLM request failed');
|
throw new Error('MakerSuite request failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
@@ -39,5 +39,5 @@ async function getPaLMVector(text) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getPaLMVector,
|
getMakerSuiteVector,
|
||||||
};
|
};
|
Reference in New Issue
Block a user