-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
}); |