AGENT SKILL

ndarray-vector-math

暂无简介

License 许可
Apache-2.0
最近更新
2026-11-14
Stars
★ 13
Forks
2

03 SKILL.md 正文

/ndarray-vector-math

When to Use

  • Computing cosine similarity between embedding vectors in agent L1 memory
  • Batch nearest-neighbor search without an external vector DB
  • Matrix operations for PCA/dimensionality reduction on small embedding sets
  • Fast typed-array math without Python/WASM dependency

Do NOT use for

  • 100k vectors (use [[in-memory-vector-storage]] or pgvector instead)

  • GPU-accelerated matrix math (use ONNX runtime or TensorFlow.js)

Cosine similarity

import ndarray from 'ndarray'
import ops     from 'ndarray-ops'

type Vec = Float32Array

function cosine(a: Vec, b: Vec): number {
  if (a.length !== b.length) throw new Error('dimension mismatch')
  const n = a.length
  let dot = 0, normA = 0, normB = 0
  for (let i = 0; i < n; i++) {
    dot   += a[i] * b[i]
    normA += a[i] * a[i]
    normB += b[i] * b[i]
  }
  if (normA === 0 || normB === 0) return 0
  return dot / (Math.sqrt(normA) * Math.sqrt(normB))
}

// Usage
const queryVec  = new Float32Array([0.1, 0.3, 0.8, ...])
const memVec    = new Float32Array([0.2, 0.2, 0.9, ...])
const similarity = cosine(queryVec, memVec)
// 1.0 = identical, 0.0 = orthogonal, -1.0 = opposite

ndarray batch operations

import ndarray from 'ndarray'
import ops     from 'ndarray-ops'

// Matrix of N embedding vectors, each of dim D
const N = 1000, D = 1536
const matrix = ndarray(new Float32Array(N * D), [N, D])

// Fill with embeddings
for (let i = 0; i < N; i++) {
  for (let d = 0; d < D; d++) {
    matrix.set(i, d, embeddings[i][d])
  }
}

// L2 normalize all rows in-place
for (let i = 0; i < N; i++) {
  const row  = matrix.pick(i, null)
  let norm   = 0
  for (let d = 0; d < D; d++) norm += row.get(d) ** 2
  norm = Math.sqrt(norm)
  if (norm > 0) ops.divseq(row, norm)
}

Top-K nearest neighbors (brute force)

function topK(
  query:     Float32Array,
  corpus:    Float32Array[],
  k         = 5,
  threshold = 0.75
): { index: number; score: number }[] {
  return corpus
    .map((vec, index) => ({ index, score: cosine(query, vec) }))
    .filter(r => r.score >= threshold)
    .sort((a, b) => b.score - a.score)
    .slice(0, k)
}

Anti-Fake-Pass Checklist

❌ Float64Array instead of Float32Array → 2× memory, no precision benefit for embeddings
❌ cosine computed on non-normalized vectors → results not in [-1, 1] range
❌ norm === 0 not checked → NaN propagates through all downstream comparisons
❌ N > 100k with brute force → O(N*D) scan too slow (use HNSW index)
❌ ndarray-ops not imported → ops.divseq silently undefined
❌ Dimension mismatch not caught → silent wrong results (no runtime error)

04 同仓库更多 Skills

Horonomos-laps
Horonomos-laps/Yana-AI

openai--sentry--sentry

Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

ito-basket-compare

Compare Itô prediction-market baskets against a user's knowledge base, portfolio notes, financial context, watchlist, or research thesis. Use for read-only basket comparison and gap analysis without investment advice or live trading.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

implementing-network-traffic-baselining

Build network traffic baselines from NetFlow/IPFIX data using Python pandas for statistical analysis, z-score anomaly detection, and hourly/daily traffic pattern profiling

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

detecting-evasion-techniques-in-endpoint-logs

Detects defense evasion techniques used by adversaries in endpoint logs including log tampering, timestomping, process injection, and security tool disabling. Use when investigating suspicious endpoint behavior, building detection rules for evasion tactics, or conducting threat hunting for stealthy adversary activity. Activates for requests involving evasion detection, defense evasion analysis, log tampering detection, or MITRE ATT&CK TA0005.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

implementing-microsegmentation-with-guardicore

Implementing microsegmentation using Akamai Guardicore Segmentation to map application dependencies, create granular network policies, visualize east-west traffic flows, and enforce least-privilege communication between workloads across data centers and cloud.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

ramdisk-tmpfs-patterns

暂无简介

★ 13