ที่เก็บนี้มีรหัสสำหรับวิธีการจัดเก็บและสอบถามข้อมูลของคุณเองโดยใช้ OpenAI EMBEDDINGS และ SUPABASE โดยใช้ JavaScript
ก่อนที่คุณจะเริ่มตรวจสอบให้แน่ใจว่าคุณมีการตั้งค่าต่อไปนี้:
โคลนที่เก็บ:
git clone https://github.com/muzammildafedar/custom-chatgpt-using-js.git
cd your-repoติดตั้งการพึ่งพา:
npm install openai
npm install @supabase/supabase-jsแทนที่ค่าตัวยึดในรหัสด้วยข้อมูลรับรอง Supabase และ OpenAI ที่แท้จริงของคุณ:
const supabaseUrl = 'Your Supabase URL' ;
const supabaseKey = 'Your Supabase API Key' ;
const openaiConfig = {
apiKey : 'Your OpenAI API Key' ,
} ; StoreEmbedding (ชื่อ, ร่างกาย, การฝัง) คำอธิบาย: เก็บชื่อเรื่องร่างกายและการฝังในฐานข้อมูล Supabase
QueryEmbeddings (Query, MatchThreshold, MatchCount) คำอธิบาย: Queries Supabase สำหรับ Embeddings ที่ตรงกับการสืบค้น, Threshold และ Count
Getanswer (คำถาม, โพสต์) คำอธิบาย: สร้างคำตอบจาก OpenAI GPT-3.5 ตามแบบสอบถามที่ให้ไว้และเอกสารที่จับคู่
สร้างตารางเพื่อจัดเก็บ embeddings
create table posts (
id serial primary key,
title text not null,
body text not null,
embedding vector(1536)
);
เพื่อที่จะ "เชื่อมต่อ" openai กับการฝังตัวของเราเราจำเป็นต้องสร้างฟังก์ชั่นใน postgres ค้นหาค่าการจับคู่ที่ใกล้เคียงที่สุดเมื่อได้รับเวกเตอร์
create or replace function match_posts (
query_embedding vector(1536),
match_threshold float,
match_count int
)
returns table (
id bigint,
body text,
title text,
similarity float
)
language sql stable
as $$
select
posts.id,
posts.body,
posts.title,
1 - (posts.embedding <=> query_embedding) as similarity
from posts
where 1 - (posts.embedding <=> query_embedding) > match_threshold
order by similarity desc
limit match_count;
$$;
นอกจากนี้เราสามารถสร้างดัชนีบนตารางโพสต์ของเราเพื่อเร่งความเร็วแบบสอบถาม
create index on posts using ivfflat (embedding vector_cosine_ops)
with
(lists = 100);