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);