|
import fetch from 'node-fetch'; |
|
import { SECRET_KEYS, readSecret } from '../endpoints/secrets.js'; |
|
const API_MAKERSUITE = 'https://generativelanguage.googleapis.com'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function getMakerSuiteBatchVector(texts, directories) { |
|
const promises = texts.map(text => getMakerSuiteVector(text, directories)); |
|
return await Promise.all(promises); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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'); |
|
} |
|
|
|
|
|
const data = await response.json(); |
|
|
|
return data['embedding']['values']; |
|
} |
|
|