Skip to content

Commit

Permalink
fix: maybe this will finally work
Browse files Browse the repository at this point in the history
  • Loading branch information
zzulanas committed Jun 19, 2023
1 parent 4e89b0f commit 6f33d87
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 41 deletions.
22 changes: 9 additions & 13 deletions .github/workflows/gen-embeddings.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Generate Embeddings
name: Call Generate Embeddings Edge Function

on:
push:
Expand All @@ -7,20 +7,16 @@ on:
workflow_dispatch:

jobs:
generate_embeddings:
call:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Checkout code
uses: actions/checkout@v2

- uses: actions/setup-node@v2
- name: Call Edge Function
uses: actions/setup-node@v2
with:
node-version: 14

- run: npm install

- run: npm install -g typescript

- run: tsc -p . # Transpile TypeScript files

- run: node lib/generate-embeddings.js # Generate embeddings
node-version: "14"
- run: npm install @supabase/supabase-js
- run: node call-edge-function.js
39 changes: 11 additions & 28 deletions lib/generate-embeddings.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
import { Configuration, OpenAIApi } from "openai";
import { supabaseClient } from "./supabase-client";
import supabaseClient from "./supabase-client.ts";

async function generateEmbeddings() {
const configuration = new Configuration({ apiKey: process.env.OPEN_AI_KEY! });
const openAi = new OpenAIApi(configuration);
async function callEdgeFunction() {
const { data, error } = await supabase.functions.invoke("create-embeddings", {
body: { name: "Functions" },
});

const documents = [
"I love long walks on the beach",
"I love ice cream, especially vanilla ice cream",
"In my free time I like to rock climb, the highest grade I can do is around a V5, I'm still learning!",
];

// Assuming each document is a string
for (const document of documents) {
// OpenAI recommends replacing newlines with spaces for best results
const input = document.replace(/\n/g, " ");

const embeddingResponse = await openAi.createEmbedding({
model: "text-embedding-ada-002",
input,
});

const [{ embedding }] = embeddingResponse.data.data;

// In production we should handle possible errors
await supabaseClient.from("documents").insert({
content: document,
embedding,
});
if (error) {
console.error(error);
} else {
console.log(data);
}
}

callEdgeFunction();
47 changes: 47 additions & 0 deletions supabase/functions/create-embeddings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import "https://deno.land/x/[email protected]/mod.ts";
import { createClient } from "https://esm.sh/@supabase/[email protected]";
import { Configuration, OpenAIApi } from "https://esm.sh/[email protected]";
import { supabaseClient } from "https://esm.sh/@supabase/supabase-js@2";

export const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers":
"authorization, x-client-info, apikey, content-type",
};

serve(async (req) => {
// Handle CORS
if (req.method === "OPTIONS") {
return new Response("ok", { headers: corsHeaders });
}

const configuration = new Configuration({ apiKey: process.env.OPEN_AI_KEY! });
const openAi = new OpenAIApi(configuration);

const documents = [
"I love long walks on the beach",
"I love ice cream, especially vanilla ice cream",
"In my free time I like to rock climb, the highest grade I can do is around a V5, I'm still learning!",
"I'm currently lost in the Arctic Circle, send help!",
];

// Assuming each document is a string
for (const document of documents) {
// OpenAI recommends replacing newlines with spaces for best results
const input = document.replace(/\n/g, " ");

const embeddingResponse = await openAi.createEmbedding({
model: "text-embedding-ada-002",
input,
});

const [{ embedding }] = embeddingResponse.data.data;

// In production we should handle possible errors
await supabaseClient.from("documents").insert({
content: document,
embedding,
});
}
});

0 comments on commit 6f33d87

Please sign in to comment.