Skip to content

MOSS API Examples

This page demonstrates practical usage examples of the InferEdge MOSS SDK for semantic search operations.

Basic Setup

First, import and initialize the MossClient:

typescript
import { MossClient } from "@inferedge/moss";

const client = new MossClient("your-api-key");

Creating an Index

Create a semantic search index with your preferred model:

typescript
// Create an index with the fast model
await client.createIndex("documents", "moss-minilm");

// Or use the more accurate model
await client.createIndex("research", "moss-mediumlm");

Adding Documents

Add individual documents or batch insert multiple items:

typescript
// Add a single document
await client.addItem("documents", {
  id: "doc-001",
  text: "Introduction to machine learning algorithms and neural networks",
});

// Add multiple documents at once
await client.addItems("documents", [
  { id: "doc-002", text: "Deep learning fundamentals and applications" },
  { id: "doc-003", text: "Natural language processing with transformers" },
  { id: "doc-004", text: "Computer vision and image recognition techniques" },
]);

Performing Searches

Search for semantically similar content:

typescript
// Basic search
const results = await client.query("documents", "machine learning");

console.log(`Found ${results.matches.length} results`);
results.matches.forEach((match) => {
  console.log(`${match.id}: ${match.text} (score: ${match.score.toFixed(3)})`);
});

// Search with options
const detailedResults = await client.query("documents", "neural networks", {
  topResultsCount: 5,
  measurePerformance: true,
});

if (detailedResults.metrics) {
  console.log(`Search completed in ${detailedResults.metrics.totalTime}ms`);
}

Managing Index Content

Update and remove documents as needed:

typescript
// Update an existing document
await client.updateItem("documents", "doc-001", {
  id: "doc-001",
  text: "Advanced machine learning algorithms and deep neural networks",
});

// Remove a document
const removed = client.removeItem("documents", "doc-002");
console.log(`Document removed: ${removed}`);

// Get document count
const count = client.getItemCount("documents");
console.log(`Index contains ${count} documents`);

Index Persistence

Export and import indexes for backup or transfer:

typescript
// Export index to binary format
const binaryData = client.exportIndexAsBinary("documents");

// Save to file (Node.js example)
import { writeFileSync } from "fs";
writeFileSync("documents-backup.moss", Buffer.from(binaryData));

// Load index from binary data
import { readFileSync } from "fs";
const loadedData = readFileSync("documents-backup.moss").buffer;
const indexId = await client.loadIndexFromBinary(loadedData);
console.log(`Loaded index: ${indexId}`);

Complete Example

Here's a complete example putting it all together:

typescript
import { MossClient } from "@inferedge/moss";

async function main() {
  const client = new MossClient(process.env.MOSS_API_KEY);

  // Create and populate index
  await client.createIndex("knowledge-base", "moss-minilm");

  await client.addItems("knowledge-base", [
    {
      id: "1",
      text: "Photosynthesis is the process plants use to convert sunlight into energy",
    },
    {
      id: "2",
      text: "DNA contains the genetic instructions for all living organisms",
    },
    {
      id: "3",
      text: "The theory of relativity revolutionized our understanding of space and time",
    },
    {
      id: "4",
      text: "Machine learning algorithms can recognize patterns in large datasets",
    },
  ]);

  // Search for content
  const results = await client.query(
    "knowledge-base",
    "how do plants make energy"
  );

  console.log("Search Results:");
  results.matches.forEach((match, index) => {
    console.log(
      `${index + 1}. ${match.text} (${(match.score * 100).toFixed(1)}% match)`
    );
  });

  // Clean up
  client.deleteIndex("knowledge-base");
}

main().catch(console.error);

Type Definitions

For full type information, see the API reference: