Skip to content

Commit

Permalink
Fix pgvector example + upgrade (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
leerob committed May 22, 2024
1 parent 62c700e commit 635d628
Show file tree
Hide file tree
Showing 8 changed files with 1,381 additions and 1,142 deletions.
4 changes: 0 additions & 4 deletions storage/postgres-pgvector/app/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
import prisma from '@/lib/prisma'
import { openai } from '@/lib/openai'
import { type Pokemon } from '@prisma/client'
import { ratelimit } from '@/lib/utils'

export async function searchPokedex(
query: string
): Promise<Array<Pokemon & { similarity: number }>> {
try {
if (query.trim().length === 0) return []

const { success } = await ratelimit.limit('generations')
if (!success) throw new Error('Rate limit exceeded')

const embedding = await generateEmbedding(query)
const vectorQuery = `[${embedding.join(',')}]`
const pokemon = await prisma.$queryRaw`
Expand Down
2 changes: 0 additions & 2 deletions storage/postgres-pgvector/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { Search } from '@/components/search'
import Image from 'next/image'
import Link from 'next/link'

export const dynamic = 'force-dynamic'

export default function Home() {
return (
<main className="relative flex min-h-screen flex-col items-center justify-center">
Expand Down
7 changes: 4 additions & 3 deletions storage/postgres-pgvector/components/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function Search({ searchPokedex }: SearchProps) {
current = false
}
}, [debouncedQuery, searchPokedex])

return (
<div className="w-full">
<Command label="Command Menu" shouldFilter={false} className="h-[200px]">
Expand All @@ -53,20 +54,20 @@ export function Search({ searchPokedex }: SearchProps) {
<CommandItem
key={pokemon.id}
value={pokemon.name}
className="data-[selected='true']:bg-zinc-50 flex items-center justify-between py-3"
className="flex items-center justify-between py-3"
onSelect={(p) => {
console.log(p)
toast.success(`You selected ${p}!`)
}}
>
<div className="flex items-center space-x-4">
<div className="space-y-1">
<p className="text-sm text-gray-500">
<p className="text-sm text-gray-800">
{pokemon.name.substring(0, 90)}
</p>
</div>
</div>
<div className="text-sm text-gray-500">
<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)}
Expand Down
1 change: 1 addition & 0 deletions storage/postgres-pgvector/lib/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import OpenAI from 'openai'

export const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || '',
organization: process.env.OPENAI_ORG_ID,
})
15 changes: 0 additions & 15 deletions storage/postgres-pgvector/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'
import { Ratelimit } from '@upstash/ratelimit'
import { kv } from '@vercel/kv'

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

// Create a new ratelimiter, that allows 10 requests per 10 seconds
export const ratelimit = new Ratelimit({
redis: kv as any,
limiter: Ratelimit.slidingWindow(10, '10 s'),
analytics: true,
/**
* Optional prefix for the keys used in redis. This is useful if you want to share a redis
* instance with other applications and want to avoid key collisions. The default prefix is
* "@upstash/ratelimit"
*/
prefix: '@upstash/ratelimit',
})
8 changes: 0 additions & 8 deletions storage/postgres-pgvector/next.config.js

This file was deleted.

42 changes: 20 additions & 22 deletions storage/postgres-pgvector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,27 @@
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
"dependencies": {
"@prisma/client": "^5.4.1",
"@types/node": "20.8.3",
"@types/react": "18.2.25",
"@types/react-dom": "18.2.11",
"@upstash/ratelimit": "^0.4.4",
"@vercel/kv": "^0.2.3",
"autoprefixer": "10.4.16",
"clsx": "^2.0.0",
"cmdk": "^0.2.0",
"eslint": "8.51.0",
"eslint-config-next": "13.5.4",
"@prisma/client": "^5.14.0",
"@types/node": "20.12.12",
"@types/react": "18.3.2",
"@types/react-dom": "18.3.0",
"autoprefixer": "10.4.19",
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
"eslint": "9.3.0",
"eslint-config-next": "14.2.3",
"gpt3-tokenizer": "^1.1.5",
"next": "13.5.4",
"openai": "^4.11.1",
"postcss": "8.4.31",
"prisma": "^5.4.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"next": "14.2.3",
"openai": "^4.47.1",
"postcss": "8.4.38",
"prisma": "^5.14.0",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-hot-toast": "^2.4.1",
"tailwind-merge": "^1.14.0",
"tailwindcss": "3.3.3",
"ts-node": "^10.9.1",
"typescript": "5.2.2",
"use-debounce": "^9.0.4"
"tailwind-merge": "^2.3.0",
"tailwindcss": "3.4.3",
"ts-node": "^10.9.2",
"typescript": "5.4.5",
"use-debounce": "^10.0.0"
}
}

0 comments on commit 635d628

Please sign in to comment.