Skip to content

Commit 14904d0

Browse files
committed
Init to win it
1 parent 5cee942 commit 14904d0

File tree

7 files changed

+3182
-130
lines changed

7 files changed

+3182
-130
lines changed

README.md

+64-20
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,80 @@
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
22

3-
## Getting Started
3+
### Understand how data is converted to embeddings
44

5-
First, run the development server:
5+
![Embeddings demo](./docs/embeddings-demo.png)
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.
634

735
```bash
836
npm run dev
9-
# or
10-
yarn dev
11-
# or
12-
pnpm dev
13-
# or
14-
bun dev
1537
```
1638

17-
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
39+
## Project structure
1840

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.
2042

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**
2244

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.
2447

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.
2649

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**
2951

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`:
3153

32-
## Deploy on Vercel
54+
```typescript
55+
import { NextRequest, NextResponse } from 'next/server';
56+
import OpenAI from 'openai';
3357

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+
});
3561

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+
```

docs/embeddings-demo.png

142 KB
Loading

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12+
"next": "14.2.3",
13+
"openai": "^4.47.1",
1214
"react": "^18",
13-
"react-dom": "^18",
14-
"next": "14.2.3"
15+
"react-dom": "^18"
1516
},
1617
"devDependencies": {
17-
"typescript": "^5",
1818
"@types/node": "^20",
1919
"@types/react": "^18",
2020
"@types/react-dom": "^18",
21+
"eslint": "^8",
22+
"eslint-config-next": "14.2.3",
2123
"postcss": "^8",
2224
"tailwindcss": "^3.4.1",
23-
"eslint": "^8",
24-
"eslint-config-next": "14.2.3"
25+
"typescript": "^5"
2526
}
2627
}

0 commit comments

Comments
 (0)