Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Supabase Edge Functions で最近追加された AI 機能を使ってみた

Supabase Edge Functions で最近追加された AI 機能を使ってみた

toranoana.deno #16 での登壇資料です。
https://yumenosora.connpass.com/event/316556/

4geru sakisaka

May 14, 2024
Tweet

More Decks by 4geru sakisaka

Other Decks in Technology

Transcript

  1. 1 // / <reference types="https://esm.sh/@supabase/functions-js/src/edge- runtime.d.ts" /> 2 // const

    session = new Supabase.ai.Session('mistral') 3 const session = new Supabase.ai.Session('llama2') 4 import { notifyMessage } from './messages.ts' 5 6 Deno.serve(async (req: Request) => { 7 const params = new URL(req.url).searchParams 8 const prompt = params.get('prompt') ?? '' 9 notifyMessage(`user prompt: ${prompt}`) 10 11 // Get the output as a stream 12 const output = await session.run(prompt, { stream: true }) NPEFMͷઃఆ͕؆୯
  2. 1 // / <reference types="https://esm.sh/@supabase/functions-js/src/edge- runtime.d.ts" /> 2 // const

    session = new Supabase.ai.Session('mistral') 3 const session = new Supabase.ai.Session('llama2') 4 import { notifyMessage } from './messages.ts' 5 6 Deno.serve(async (req: Request) => { 7 const params = new URL(req.url).searchParams 8 const prompt = params.get('prompt') ?? '' 9 notifyMessage(`user prompt: ${prompt}`) 10 11 // Get the output as a stream 12 const output = await session.run(prompt, { stream: true }) NPEFMͷݺͼग़͕͠؆୯
  3. 18 // Create a stream 19 const stream = new

    ReadableStream({ 20 async start(controller) { 21 const encoder = new TextEncoder() 22 23 try { 24 let message = '' 25 for await (const chunk of output) { 26 controller.enqueue(encoder.encode(chunk.response ?? '')) 27 message = [...message, chunk.response].join('') 28 } 29 notifyMessage(message) 30 } catch (err) { 31 console.error('Stream error:', err) 32 } finally { 33 controller.close() 34 } 35 }, 36 }) 37 DIVOL͝ͱʹ෼ׂͯ͠ड͚औΔ
  4. 18 // Create a stream 19 const stream = new

    ReadableStream({ 20 async start(controller) { 21 const encoder = new TextEncoder() 22 23 try { 24 let message = '' 25 for await (const chunk of output) { 26 controller.enqueue(encoder.encode(chunk.response ?? '')) 27 message = [...message, chunk.response].join('') 28 } 29 notifyMessage(message) 30 } catch (err) { 31 console.error('Stream error:', err) 32 } finally { 33 controller.close() 34 } 35 }, 36 }) 37 DPOUSPMMFSͰॲཧΛ͢Δ
  5. 10 const session = new Supabase.ai.Session('gte-small'); 11 12 serve(async (req)

    => { 13 const { events } = await req.json() 14 console.log(events) 15 if (events && events[0]?.type === "message") { 16 const input = events[0].message.text 17 // Generate the embedding from the user input 18 const embedding = await session.run(input, { 19 mean_pool: true, 20 normalize: true, 21 }); 22 23 // จࣈྻԽͨ͠ϝοηʔδσʔλ 24 let messages:any = [ 25 { 26 "type": "text", 27 "text": events[0].message.text 28 }, 29 ] NPEFMͷݺͼग़͕͠؆୯ ϕΫτϧ৘ใ͕SFUVSO͞ΕΔ
  6. 31 // Store the vector in Postgres 32 const supabase

    = supabaseClient() 33 const { data: documents } = await supabase.rpc('match_documents', { 34 query_embedding: embedding, // Pass the embedding you want to compare 35 match_threshold: 0.78, // Choose an appropriate threshold for your data 36 match_count: 3, // Choose the number of matches 37 }) 38 console.log({documents}) 39 documents.map((doc) => { 40 messages.push({ 41 "type": "text", 42 "text": `Matched: ${doc.body}` 43 }) 44 }) 45 const { data, error } = await supabase.from('documents').insert({ 46 body: events[0].message.text, 47 embedding, 48 }) 49 replyMessage(events, messages) 50 } Ұக͢ΔυΩϡϝϯτΛݕࡧ
  7. 31 // Store the vector in Postgres 32 const supabase

    = supabaseClient() 33 const { data: documents } = await supabase.rpc('match_documents', { 34 query_embedding: embedding, // Pass the embedding you want to compare 35 match_threshold: 0.78, // Choose an appropriate threshold for your data 36 match_count: 3, // Choose the number of matches 37 }) 38 console.log({documents}) 39 documents.map((doc) => { 40 messages.push({ 41 "type": "text", 42 "text": `Matched: ${doc.body}` 43 }) 44 }) 45 const { data, error } = await supabase.from('documents').insert({ 46 body: events[0].message.text, 47 embedding, 48 }) 49 replyMessage(events, messages) 50 } ೖྗͨ͠ϕΫτϧͷ৘ใΛอଘ
  8. 31 // Store the vector in Postgres 32 const supabase

    = supabaseClient() 33 const { data: documents } = await supabase.rpc('match_documents', { 34 query_embedding: embedding, // Pass the embedding you want to compare 35 match_threshold: 0.78, // Choose an appropriate threshold for your dat 36 match_count: 3, // Choose the number of matches 37 }) 38 console.log({documents}) 39 documents.map((doc) => { 40 messages.push({ 41 "type": "text", 42 "text": `Matched: ${doc.body}` 43 }) 44 }) 45 const { data, error } = await supabase.from('documents').insert({ 46 body: events[0].message.text, 47 embedding, 48 }) 49 replyMessage(events, messages) 50 } ϩάͷ֬ೝ͕؆୯ʂ