Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pgvector example to use Vercel AI SDK #920

Merged
merged 3 commits into from
May 25, 2024
Merged
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
8 changes: 4 additions & 4 deletions storage/postgres-pgvector/app/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import prisma from '@/lib/prisma'
import { openai } from '@/lib/openai'
import { type Pokemon } from '@prisma/client'
import { embed } from 'ai'

export async function searchPokedex(
query: string
Expand Down Expand Up @@ -33,10 +34,9 @@ export async function searchPokedex(
async function generateEmbedding(raw: string) {
// OpenAI recommends replacing newlines with spaces for best results
const input = raw.replace(/\n/g, ' ')
const embeddingData = await openai.embeddings.create({
model: 'text-embedding-ada-002',
input,
const { embedding } = await embed({
model: openai.embedding('text-embedding-ada-002'),
value: input,
})
const [{ embedding }] = (embeddingData as any).data
return embedding
}
6 changes: 1 addition & 5 deletions storage/postgres-pgvector/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import './globals.css'
import { Inter } from 'next/font/google'
import { HotToaster } from '../components/hot-toaster'

export const metadata = {
metadataBase: new URL('https://postgres-pgvector.vercel.app'),
Expand All @@ -22,10 +21,7 @@ export default function RootLayout({
}) {
return (
<html lang="en">
<body className={inter.variable}>
{children}
<HotToaster />
</body>
<body className={inter.variable}>{children}</body>
</html>
)
}
2 changes: 1 addition & 1 deletion storage/postgres-pgvector/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function Home() {
</Link>
, and{' '}
<Link
href="https://platform.openai.com/docs/guides/embeddings"
href="https://sdk.vercel.ai/providers/ai-sdk-providers/openai#embedding-models"
className="font-medium underline underline-offset-4 hover:text-black transition-colors"
>
OpenAI
Expand Down
26 changes: 0 additions & 26 deletions storage/postgres-pgvector/components/hot-toaster.tsx

This file was deleted.

25 changes: 0 additions & 25 deletions storage/postgres-pgvector/components/refresh-button.tsx

This file was deleted.

5 changes: 0 additions & 5 deletions storage/postgres-pgvector/components/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '@/components/command'
import { type Pokemon } from '@prisma/client'
import { useEffect, useState } from 'react'
import { toast } from 'react-hot-toast'
import { useDebounce } from 'use-debounce'

export interface SearchProps {
Expand Down Expand Up @@ -55,10 +54,6 @@ export function Search({ searchPokedex }: SearchProps) {
key={pokemon.id}
value={pokemon.name}
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">
Expand Down
4 changes: 2 additions & 2 deletions storage/postgres-pgvector/lib/openai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import OpenAI from 'openai'
import { createOpenAI } from '@ai-sdk/openai'

export const openai = new OpenAI({
export const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY || '',
organization: process.env.OPENAI_ORG_ID,
})
3 changes: 2 additions & 1 deletion storage/postgres-pgvector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
"dependencies": {
"@ai-sdk/openai": "^0.0.14",
"@prisma/client": "^5.14.0",
"@types/node": "20.12.12",
"@types/react": "18.3.2",
"@types/react-dom": "18.3.0",
"ai": "^3.1.14",
"autoprefixer": "10.4.19",
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
Expand All @@ -30,7 +32,6 @@
"prisma": "^5.14.0",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-hot-toast": "^2.4.1",
"tailwind-merge": "^2.3.0",
"tailwindcss": "3.4.3",
"ts-node": "^10.9.2",
Expand Down
Loading