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

Add functions #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions supabase/functions/hf-nomic-ai-nomic-embed-text-v1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
env,
pipeline,
} from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]";

// Create a feature extraction pipeline
const extractor = await pipeline(
"feature-extraction",
"nomic-ai/nomic-embed-text-v1",
{
device: "auto",
quantized: true, // TODO: make it configurable
},
);

// Compute sentence embeddings

Deno.serve(async (req) => {
const { input } = await req.json();

const texts = [input];
const embeddings = await extractor(texts, {
// TODO: make these params configurable
pooling: "mean",
normalize: true,
});

return new Response(JSON.stringify(embeddings), {
headers: { "Content-Type": "application/json" },
status: 200,
});
});
54 changes: 25 additions & 29 deletions supabase/functions/hf-supabase-gte-small/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import {
env,
pipeline,
} from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]";

interface WebhookPayload {
text: string;
}
// Create a feature extraction pipeline
const extractor = await pipeline(
"feature-extraction",
"supabase/gte-small",
{
device: "auto",
quantized: true, // TODO: make it configurable
},
);

const model = new Supabase.ai.Session("gte-small");
// Compute sentence embeddings

Deno.serve(async (req) => {
try {
// Parse request body
const payload: WebhookPayload = await req.json();
const { text } = payload;
const { input } = await req.json();

if (!text) {
throw new Error("Missing required parameter: text");
}
const texts = [input];
const embeddings = await extractor(texts, {
// TODO: make these params configurable
pooling: "mean",
normalize: true,
});

// Generate embedding
const embedding = await model.run(text, {
mean_pool: true,
normalize: true,
});

// Return embedding
return new Response(JSON.stringify({ embedding }), {
status: 200,
});

// Handle error
} catch (error) {
return new Response(error.message, {
status: 500,
});
}
return new Response(JSON.stringify(embeddings), {
headers: { "Content-Type": "application/json" },
status: 200,
});
});
30 changes: 30 additions & 0 deletions supabase/functions/hf-xenova-jina-embeddings-v2-base-en/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
env,
pipeline,
} from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]";

// Create a feature extraction pipeline
const extractor = await pipeline(
"feature-extraction",
"Xenova/jina-embeddings-v2-base-en",
{
device: "auto",
quantized: true, // TODO: make it configurable
},
);

Deno.serve(async (req) => {
const { input } = await req.json();

const texts = [input];
const embeddings = await extractor(texts, {
// TODO: make these params configurable
pooling: "mean",
normalize: true,
});

return new Response(JSON.stringify(embeddings), {
headers: { "Content-Type": "application/json" },
status: 200,
});
});