Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instant vector search template #1

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions instant-vector-search/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Ignore macOS system files
.DS_Store

# Ignore environment variable files
.env
.env.*

# Ignore build output directories
build/

# Ignore node_modules folders
node_modules/

# Ignore logs generated by as-test
logs/
3 changes: 3 additions & 0 deletions instant-vector-search/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["assemblyscript-prettier"]
}
83 changes: 83 additions & 0 deletions instant-vector-search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# instant-vector-search

A simplified template demonstrating how to build instant vector search using Hypermode and Modus. This project showcases how to configure collections, embed data, and deploy APIs to perform real-time vector search with minimal setup.

## Guide

Check out the guide on how to create a project using this template:
[Instant Vector Search Guide](TODO)

## Semantic Search in Action

This template demonstrates how instant vector search can enhance your application by generating embeddings from user queries and performing vector searches in less than 200ms.

## How to Use the Template

Fork or clone this repository to get started:

```
git clone https://github.com/hypermodeinc/instant-vector-search.git
cd instant-vector-search
```

Install Modus CLI:

```
npm install -g @hypermode/modus-cli
```

## Initialize and deploy your app:

You can deploy using either the Hypermode Console or Hyp CLI.

Option 1: Hypermode Console

- Create a new project in the Hypermode Console.
- Connect your GitHub account and select your repository to import.

Option 2: Hyp CLI

- Run the following command to import and deploy your app:

```
hyp init
```

## Uploading Data

Once your app is deployed, upload your data to the texts collection to enable vector search.

From the Hypermode Console: Upload a CSV directly using the dashboard.

Using the `upsertTexts` function: Upload data programmatically by calling the function from your code.

Once your data is in place, the embeddings will be generated automatically, and your system will be ready to handle real-time vector searches.

## Making API Calls

You can test the API via the Query page in the Hypermode Console. Run a sample vector search query:

```
query {
search(query: "your search term here")
}
```

## Hypermode & Modus Overview

This template leverages Hypermode and Modus to power your backend functions:

[Hypermode](https://docs.hypermode.com/introduction) is a managed service that provides the infrastructure and tools for creating AI-powered applications, including assistants, APIs, and backend services. It offers:

- Automatic building and deployment of functions with each git push
- A live, scalable API for previewing, testing, and production
- Access to various AI models for experimentation
- Integrated console for observability and control
- GraphQL API generation for easy integration

[Modus](https://docs.hypermode.com/modus/overview) is an open-source, serverless framework that’s part of the Hypermode ecosystem. It focuses on:

- Building functions and APIs using WebAssembly
- Supporting multiple programming languages (currently Go and AssemblyScript)
- Providing features to integrate models, data, and external services
- Scaling from simple CRUD operations to complex AI systems
6 changes: 6 additions & 0 deletions instant-vector-search/asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./node_modules/@hypermode/modus-sdk-as/plugin.asconfig.json",
"options": {
"transform": ["@hypermode/modus-sdk-as/transform", "json-as/transform"]
}
}
57 changes: 57 additions & 0 deletions instant-vector-search/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { collections } from "@hypermode/modus-sdk-as";
import { models } from "@hypermode/modus-sdk-as";
import { EmbeddingsModel } from "@hypermode/modus-sdk-as/models/experimental/embeddings";

const textsCollection = "texts";
const searchMethod = "searchMethod1";
const embeddingModelName = "minilm";

export function upsertTexts(ids: string[], texts: string[]): string[] {
const errors: string[] = [];

if (ids.length !== texts.length) {
errors.push("Length of all arrays must be the same");
return errors;
}

let result = collections.upsertBatch(textsCollection, ids, texts);

if (!result.isSuccessful) {
errors.push(result.error);
return errors;
}

return ids;
}

export function search(query: string): string[] {
const searchResults = collections.search(
textsCollection,
searchMethod,
query,
10,
true,
);

if (!searchResults.isSuccessful) {
return [searchResults.error];
}
const searchTexts: string[] = [];

for (let i = 0; i < searchResults.objects.length; i++) {
const obj = searchResults.objects[i];
const text = collections.getText(textsCollection, obj.key);
if (text) {
searchTexts.push(text);
}
}
return searchTexts;
}

export function miniLMEmbed(texts: string[]): f32[][] {
const model = models.getModel<EmbeddingsModel>(embeddingModelName);
const input = model.createInput(texts);
const output = model.invoke(input);

return output.predictions;
}
4 changes: 4 additions & 0 deletions instant-vector-search/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": ["./**/*.ts"]
}
11 changes: 11 additions & 0 deletions instant-vector-search/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-check

import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import aseslint from "@hypermode/modus-sdk-as/tools/assemblyscript-eslint";

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
aseslint.config,
);
29 changes: 29 additions & 0 deletions instant-vector-search/modus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://schema.hypermode.com/modus.json",
"endpoints": {
"default": {
"type": "graphql",
"path": "/graphql",
"auth": "bearer-token"
}
},
"models": {
"minilm": {
"sourceModel": "sentence-transformers/all-MiniLM-L6-v2",
"provider": "hugging-face",
"connection": "hypermode"
}
},
"collections": {
"texts": {
"searchMethods": {
"searchMethod1": {
"embedder": "miniLMEmbed",
"index": {
"type": "sequential"
}
}
}
}
}
}
Loading
Loading