store GetQuery vectors openai js
1.0.0
該存儲庫包含有關如何使用OpenAI嵌入式和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(查詢,MatchThreshold,MatchCount)描述:查詢supabase的嵌入式,與提供的查詢,閾值和計數相匹配。
getanswer(查詢,帖子)描述:根據提供的查詢和匹配的文檔從OpenAI GPT-3.5生成答案。
創建表存儲嵌入的表
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);