@dev-fastn-ai/ucl-sdk
Version:
Fastn UCL SDK - A robust TypeScript SDK for integrating AI agents with Fastn UCL
237 lines • 8.15 kB
JavaScript
"use strict";
// import { pipeline, env } from '@xenova/transformers';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbeddingService = exports.EmbeddingCache = void 0;
// Utility to detect environment
function isBrowser() {
// @ts-ignore
return (typeof window !== 'undefined') && (typeof window.document !== 'undefined');
}
// Polyfill fetch for Node.js if needed
let fetchFn;
if (typeof fetch === 'undefined') {
// @ts-ignore
fetchFn = (...args) => Promise.resolve().then(() => __importStar(require('node-fetch'))).then(({ default: fetch }) => fetch(...args));
}
else {
fetchFn = fetch;
}
// Abstracted cache
class EmbeddingCache {
constructor(storageKey) {
this.memoryCache = {};
this.storageKey = storageKey;
if (!isBrowser()) {
this.memoryCache = {};
}
}
get(text) {
if (isBrowser()) {
if (typeof globalThis.localStorage !== 'undefined') {
const raw = globalThis.localStorage.getItem(this.storageKey);
if (!raw)
return null;
try {
const cache = JSON.parse(raw);
return cache[text] || null;
}
catch {
return null;
}
}
else {
return null;
}
}
else {
return this.memoryCache[text] || null;
}
}
set(text, embedding) {
if (isBrowser()) {
if (typeof globalThis.localStorage !== 'undefined') {
const raw = globalThis.localStorage.getItem(this.storageKey);
let cache = {};
if (raw) {
try {
cache = JSON.parse(raw);
}
catch { }
}
cache[text] = embedding;
globalThis.localStorage.setItem(this.storageKey, JSON.stringify(cache));
}
}
else {
this.memoryCache[text] = embedding;
}
}
clear() {
if (isBrowser()) {
if (typeof globalThis.localStorage !== 'undefined') {
globalThis.localStorage.removeItem(this.storageKey);
}
}
else {
this.memoryCache = {};
}
}
getAll() {
if (isBrowser()) {
if (typeof globalThis.localStorage !== 'undefined') {
const raw = globalThis.localStorage.getItem(this.storageKey);
if (!raw)
return {};
try {
return JSON.parse(raw);
}
catch {
return {};
}
}
else {
return {};
}
}
else {
return { ...this.memoryCache };
}
}
}
exports.EmbeddingCache = EmbeddingCache;
class EmbeddingService {
constructor(options) {
this.storageKey = 'local-embeddings';
this.refreshModel = async () => {
if (this.provider === 'xenova') {
// this.extractor = await pipeline('feature-extraction', this.modelName);
}
};
this.clearCache = () => {
this.cache.clear();
};
this.getCache = () => {
return this.cache.getAll();
};
this.provider = options.provider;
this.openaiApiKey = options.openaiApiKey || '';
this.cache = new EmbeddingCache(this.storageKey);
if (this.provider === 'xenova') {
this.initModel();
}
}
async initModel() {
try {
// this.extractor = await pipeline('feature-extraction', this.modelName);
}
catch (error) {
console.error("Error loading Xenova model:", error);
this.extractor = null;
}
}
async embed(text) {
console.log("Embedding text:", text);
if (this.provider === 'xenova') {
if (!this.extractor) {
await this.initModel();
}
const cached = this.cache.get(text);
if (cached) {
return cached;
}
// const result = await this.extractor(text, { pooling: 'mean', normalize: true });
// const embedding = result.data as number[];
// const embedding = [];
// this.cache.set(text, embedding);
// return embedding;
return [];
}
if (this.provider === 'openai') {
return await this.embedWithOpenAI(text);
}
throw new Error('Invalid embedding provider.');
}
async embedWithOpenAI(text) {
if (!this.openaiApiKey) {
throw new Error('OpenAI API key is required for OpenAI embeddings.');
}
const response = await fetchFn('https://api.openai.com/v1/embeddings', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.openaiApiKey}`,
},
body: JSON.stringify({
input: text,
model: 'text-embedding-3-small' // You can customize the model
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`OpenAI Embedding failed: ${response.status} ${errorText}`);
}
const data = await response.json();
return data.data[0].embedding;
}
async embedBatch(texts) {
if (this.provider !== 'openai') {
throw new Error('Batch embedding is only supported for OpenAI provider in this implementation.');
}
if (!this.openaiApiKey) {
throw new Error('OpenAI API key is required for OpenAI embeddings.');
}
const response = await fetchFn('https://api.openai.com/v1/embeddings', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.openaiApiKey}`,
},
body: JSON.stringify({
input: texts,
model: 'text-embedding-3-small'
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`OpenAI Embedding failed: ${response.status} ${errorText}`);
}
const data = await response.json();
// data.data is an array of { embedding: number[], ... }
return data.data.map((item) => item.embedding);
}
}
exports.EmbeddingService = EmbeddingService;
//# sourceMappingURL=embedding-service.js.map