|
1 |
| -This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). |
| 1 | +# Embeddings Demo |
2 | 2 |
|
3 |
| -## Getting Started |
| 3 | +### Understand how data is converted to embeddings |
4 | 4 |
|
5 |
| -First, run the development server: |
| 5 | + |
| 6 | + |
| 7 | +### Built With |
| 8 | + |
| 9 | +- Next.js + tailwind |
| 10 | +- OpenAI's `text-embedding-3-small` model |
| 11 | +- Node version 20 or higher |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +### Start the project |
| 16 | + |
| 17 | +**Requires Node version 20+** |
| 18 | + |
| 19 | +From the project root directory, run the following command. |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install |
| 23 | +``` |
| 24 | + |
| 25 | +### Set environment variables |
| 26 | + |
| 27 | +Make sure you have populated a `.env.local` file with your OpenAI API key: |
| 28 | + |
| 29 | +``` |
| 30 | +OPENAI_API_KEY="your_openai_api_key_here" |
| 31 | +``` |
| 32 | + |
| 33 | +Start the app. |
6 | 34 |
|
7 | 35 | ```bash
|
8 | 36 | npm run dev
|
9 |
| -# or |
10 |
| -yarn dev |
11 |
| -# or |
12 |
| -pnpm dev |
13 |
| -# or |
14 |
| -bun dev |
15 | 37 | ```
|
16 | 38 |
|
17 |
| -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. |
| 39 | +## Project structure |
18 | 40 |
|
19 |
| -You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. |
| 41 | +In this example we opted to use Next.js and the app router, which colocates the frontend and backend code in a single repository. |
20 | 42 |
|
21 |
| -This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. |
| 43 | +**Frontend Client** |
22 | 44 |
|
23 |
| -## Learn More |
| 45 | +The frontend uses Next.js and tailwind to allow users to enter free form text. This text is split by word on the client-side and then converted to tokens by the `tiktoken` library |
| 46 | +when the user clicks the `Convert text to embeddings` button. |
24 | 47 |
|
25 |
| -To learn more about Next.js, take a look at the following resources: |
| 48 | +OpenAI's `text-embedding-3-small` model extracts the features of the text you provide it and returns vectors representing these features. |
26 | 49 |
|
27 |
| -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. |
28 |
| -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. |
| 50 | +**Backend API route** |
29 | 51 |
|
30 |
| -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! |
| 52 | +This project exposes an API route: `/api/embeddings`: |
31 | 53 |
|
32 |
| -## Deploy on Vercel |
| 54 | +```typescript |
| 55 | +import { NextRequest, NextResponse } from 'next/server'; |
| 56 | +import OpenAI from 'openai'; |
33 | 57 |
|
34 |
| -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. |
| 58 | +const openai = new OpenAI({ |
| 59 | + apiKey: process.env.OPENAI_API_KEY, |
| 60 | +}); |
35 | 61 |
|
36 |
| -Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. |
| 62 | +export async function POST(req: NextRequest) { |
| 63 | + const { inputText } = await req.json(); |
| 64 | + |
| 65 | + try { |
| 66 | + const response = await openai.embeddings.create({ |
| 67 | + model: 'text-embedding-3-small', |
| 68 | + input: inputText, |
| 69 | + }); |
| 70 | + |
| 71 | + const generatedEmbeddings = response.data[0].embedding; |
| 72 | + console.log(`Generated embeddings: %o`, generatedEmbeddings); |
| 73 | + |
| 74 | + return NextResponse.json({ embeddings: generatedEmbeddings }, { status: 200 }); |
| 75 | + } catch (error) { |
| 76 | + console.error('Error generating embeddings:', error); |
| 77 | + return NextResponse.json({ error }, { status: 500 }); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
0 commit comments