This starter demonstrates how to implement semantic search using EdgeDB's vector types and Next.js. It provides a foundation for building applications with semantic search capabilities by leveraging EdgeDB's ai
extension.
using extension ai;
module default {
type Post {
required title: str;
required content: str;
required author: str;
created: datetime {
rewrite insert using (datetime_of_statement());
}
updated: datetime {
rewrite insert using (datetime_of_statement());
rewrite update using (datetime_of_statement());
}
deferred index ext::ai::index(embedding_model := 'text-embedding-3-small')
on (.content);
}
}
- AI extension: Uses EdgeDB's
ai
extension which automatically handles vector embeddings - AI index: The
ext::ai::index
automatically generates and stores embeddings for the content field using OpenAI'stext-embedding-3-small
model - Automatic Timestamps:
created
field is automatically set on insertupdated
field is automatically set on both insert and update
- Required Fields:
title
: The post's titlecontent
: The main text content that will be vectorized for semantic searchauthor
: The post's author name
- Clone & Install
git clone <repository-url>
cd <project-directory>
pnpm install
- Initialize EdgeDB
npx edgedb project init
- Generate Types
pnpm generate
- Set up OpenAI API key in
.env.local
OPENAI_API_KEY=<your-api-key>
- Set up OpenAI API key in EdgeDB UI
- Start Development
pnpm dev
The template uses OpenAI's embeddings API to generate vector representations of the search query.
We then use EdgeDB's ai
extension to search for posts that are semantically similar to the query.
const searchResults = await client.query(`
with query := <array<float32>><json>$query
select ext::ai::search(Post, query);
`, {
query: queryEmbedding
});
Check out the implementation in app/api/posts/route.ts
.