Xano MCP Server
by SarimSiddd
- docs
/**
* @fileovertitle Enhanced Titan Memory Model with Transformer-based Memory Encoding
*/
import * as tf from '@tensorflow/tfjs-node';
import { ITensor, IMemoryState, ISurpriseMetrics, IAttentionBlock, IMemoryUpdateResult, IModelGradients, TensorContainer, unwrapTensor, wrapTensor } from './types.js';
import * as fs from 'fs/promises';
import { Tokenizer } from '@tensorflow-models/universal-sentence-encoder';
// Enhanced configuration with transformer parameters
export interface TitanMemoryConfig {
inputDim?: number;
hiddenDim?: number;
outputDim?: number;
learningRate?: number;
transformerLayers?: number;
numHeads?: number;
ffDimension?: number;
dropoutRate?: number;
maxSequenceLength?: number;
memorySlots?: number;
similarityThreshold?: number;
}
export class TitanMemoryModel implements IMemoryModel {
private config: TitanMemoryConfig;
private tokenizer!: Tokenizer;
private transformerLayers: tf.LayersModel[];
private memoryProjection!: tf.LayersModel;
private similarityNetwork!: tf.LayersModel;
// Memory state with timestamps and attention weights
private memoryState: IMemoryState & {
timestamps: tf.Tensor;
accessCounts: tf.Tensor;
};
constructor(config: TitanMemoryConfig = {}) {
this.config = {
inputDim: 768,
hiddenDim: 512,
outputDim: 768,
transformerLayers: 4,
numHeads: 8,
ffDimension: 2048,
dropoutRate: 0.1,
maxSequenceLength: 512,
memorySlots: 1000,
similarityThreshold: 0.7,
...config
};
this.transformerLayers = this.buildTransformerStack();
this.buildSimilarityNetwork();
this.initializeMemoryState();
}
private buildTransformerStack(): tf.LayersModel[] {
return Array.from({ length: this.config.transformerLayers! }, (_, i) =>
tf.sequential({
layers: [
tf.layers.multiHeadAttention({
numHeads: this.config.numHeads,
keyDim: this.config.hiddenDim! / this.config.numHeads!,
}),
tf.layers.layerNormalization(),
tf.layers.dense({ units: this.config.ffDimension!, activation: 'gelu' }),
tf.layers.dropout({ rate: this.config.dropoutRate }),
tf.layers.dense({ units: this.config.hiddenDim! }),
tf.layers.layerNormalization()
]
})
);
}
private buildSimilarityNetwork(): void {
this.similarityNetwork = tf.sequential({
layers: [
tf.layers.dense({
units: this.config.hiddenDim!,
activation: 'relu',
inputShape: [this.config.hiddenDim! * 2]
}),
tf.layers.dense({ units: 1, activation: 'sigmoid' })
]
});
}
private initializeMemoryState(): void {
const initialMemory = tf.zeros([this.config.memorySlots!, this.config.hiddenDim!]);
this.memoryState = {
shortTerm: initialMemory,
longTerm: initialMemory.clone(),
meta: initialMemory.clone(),
timestamps: tf.zeros([this.config.memorySlots!]),
accessCounts: tf.zeros([this.config.memorySlots!])
};
}
public async encodeText(text: string): Promise<tf.Tensor> {
const tokens = await this.tokenizer.tokenize(text);
const padded = tokens.slice(0, this.config.maxSequenceLength!);
const input = tf.tensor2d([padded], [1, this.config.maxSequenceLength!]);
let encoding = input;
for (const layer of this.transformerLayers) {
encoding = layer.apply(encoding) as tf.Tensor;
}
return tf.mean(encoding, 1);
}
public async storeMemory(text: string): Promise<void> {
const embedding = await this.encodeText(text);
const similarityScores = tf.matMul(
this.memoryState.shortTerm,
embedding.reshape([-1, 1])
).flatten();
const { values, indices } = tf.topk(similarityScores, 1);
if (values.dataSync()[0] < this.config.similarityThreshold!) {
// Add new memory
const newMemory = tf.concat([
this.memoryState.shortTerm,
embedding.reshape([1, -1])
], 0).slice(0, this.config.memorySlots!);
this.memoryState.shortTerm.dispose();
this.memoryState.shortTerm = newMemory;
}
}
public async recallMemory(query: string, topK: number = 5): Promise<tf.Tensor[]> {
const queryEmbedding = await this.encodeText(query);
const similarities = tf.matMul(
this.memoryState.shortTerm,
queryEmbedding.reshape([-1, 1])
).flatten();
const { indices } = tf.topk(similarities, topK);
return indices.dataSync().map(i =>
this.memoryState.shortTerm.slice([i, 0], [1, -1])
);
}
public computeMemoryRelevance(): tf.Tensor {
return tf.tidy(() => {
const recency = tf.sub(tf.scalar(Date.now()), this.memoryState.timestamps);
const frequency = tf.log(tf.add(this.memoryState.accessCounts, tf.scalar(1)));
return tf.mul(recency, frequency);
});
}
public adaptivePruning(): void {
const relevance = this.computeMemoryRelevance();
const { values, indices } = tf.topk(relevance, this.config.memorySlots!);
this.memoryState.shortTerm = tf.gather(this.memoryState.shortTerm, indices);
this.memoryState.longTerm = tf.gather(this.memoryState.longTerm, indices);
this.memoryState.meta = tf.gather(this.memoryState.meta, indices);
}
// Updated forward pass with transformer-based processing
public forward(x: ITensor, memoryState: IMemoryState): { predicted: ITensor; memoryUpdate: IMemoryUpdateResult } {
return tf.tidy(() => {
const input = unwrapTensor(x);
let transformed = input;
// Process through transformer layers
for (const layer of this.transformerLayers) {
transformed = layer.apply(transformed) as tf.Tensor;
}
// Dynamic memory integration
const memoryQuery = tf.matMul(transformed, this.memoryProjection.predict(transformed));
const attention = this.computeAttention(memoryQuery, memoryState.shortTerm, memoryState.longTerm);
// Memory update with surprise-based gating
const surprise = this.computeSurprise(transformed, attention.values);
const updateGate = tf.sigmoid(tf.mul(surprise.immediate, tf.scalar(0.5)));
const newShortTerm = tf.add(
tf.mul(memoryState.shortTerm, tf.sub(tf.scalar(1), updateGate)),
tf.mul(attention.values, updateGate)
);
return {
predicted: transformed,
memoryUpdate: {
newState: {
shortTerm: newShortTerm,
longTerm: memoryState.longTerm,
meta: memoryState.meta
},
attention,
surprise
}
};
});
}