Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions storage/postgres-pgvector/app/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { db } from '@/drizzle/db'
import { SelectPokemon, pokemons } from '@/drizzle/schema'
import { openai } from '@/lib/openai'
import { desc, sql, cosineDistance, gt } from 'drizzle-orm'
import { asc, sql, cosineDistance, lt } from 'drizzle-orm'
import { embed } from 'ai'

export async function searchPokedex(
Expand All @@ -15,16 +15,16 @@ export async function searchPokedex(
const embedding = await generateEmbedding(query)
const vectorQuery = `[${embedding.join(',')}]`

const similarity = sql<number>`1 - (${cosineDistance(
const similarity = sql<number>`${cosineDistance(
pokemons.embedding,
vectorQuery
)})`
)}`

const pokemon = await db
.select({ id: pokemons.id, name: pokemons.name, similarity })
.from(pokemons)
.where(gt(similarity, 0.5))
.orderBy((t) => desc(t.similarity))
.where(lt(similarity, 0.5))
.orderBy((t) => asc(t.similarity))
.limit(8)

return pokemon
Expand Down
2 changes: 1 addition & 1 deletion storage/postgres-pgvector/components/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function Search({ searchPokedex }: SearchProps) {
<div className="text-sm text-gray-800">
{pokemon.similarity ? (
<div className="text-xs font-mono p-0.5 rounded bg-zinc-100">
{pokemon.similarity.toFixed(3)}
{(1 - pokemon.similarity).toFixed(3)}
</div>
) : (
<div />
Expand Down