-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f8aa3a
commit 8c82e9e
Showing
11 changed files
with
143 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
OPENAI_API_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
# Playground Template | ||
## Environment Variables | ||
Copy the example env file: | ||
|
||
A comprehensive playground template for building rich-text editors with [Plate](https://platejs.org/) and Next.js 14. | ||
``` | ||
cp ./.env.example ./.env.local | ||
``` | ||
|
||
## Usage | ||
- `OPENAI_API_KEY` – Your OpenAI API key (obtain one [here](https://platform.openai.com/account/api-keys)) | ||
|
||
```bash | ||
pnpm install | ||
pnpm dev | ||
``` | ||
|
||
## Features | ||
## Running the App | ||
|
||
- Next.js 14 App Directory | ||
- [Plate](https://platejs.org/) Editor | ||
- [shadcn/ui](https://ui.shadcn.com/) | ||
- Radix UI Primitives | ||
- Tailwind CSS | ||
- Icons from [Lucide](https://lucide.dev) | ||
- Dark mode with `next-themes` | ||
- Tailwind CSS class sorting, merging and linting. | ||
To run the app locally, you can run the following commands: | ||
|
||
``` | ||
pnpm i | ||
pnpm dev | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
templates/plate-playground-template/src/app/api/ai/command/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { openai } from '@ai-sdk/openai'; | ||
import { convertToCoreMessages, streamText } from 'ai'; | ||
|
||
import { limitTotalCharacters } from '../utils/limitTotalCharacters'; | ||
import { truncateSystemPrompt } from '../utils/truncateSystemPrompt'; | ||
|
||
import type { NextRequest } from 'next/server'; | ||
|
||
export async function POST(req: NextRequest) { | ||
const { messages, system } = await req.json(); | ||
const limitedMessages = limitTotalCharacters(messages, 8000); | ||
|
||
const result = await streamText({ | ||
maxTokens: 2048, | ||
messages: convertToCoreMessages(limitedMessages), | ||
model: openai('gpt-4o-mini'), | ||
system: system ? truncateSystemPrompt(system, 12_000) : undefined, | ||
}); | ||
|
||
return result.toDataStreamResponse(); | ||
} |
32 changes: 32 additions & 0 deletions
32
templates/plate-playground-template/src/app/api/ai/copilot/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { openai } from '@ai-sdk/openai'; | ||
import { generateText } from 'ai'; | ||
|
||
import type { NextRequest } from 'next/server'; | ||
|
||
export async function POST(req: NextRequest) { | ||
const { prompt, system } = await req.json(); | ||
|
||
try { | ||
const result = await generateText({ | ||
abortSignal: req.signal, | ||
maxTokens: 50, | ||
model: openai('gpt-4o-mini'), | ||
prompt: prompt, | ||
system, | ||
temperature: 0.7, | ||
}); | ||
|
||
return new Response(JSON.stringify(result), { | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
} catch (error: any) { | ||
if (error.name === 'AbortError') { | ||
return new Response(null, { status: 408 }); | ||
} | ||
|
||
return new Response(JSON.stringify({ error: error.message }), { | ||
status: 500, | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
templates/plate-playground-template/src/app/api/ai/utils/limitTotalCharacters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Message } from 'ai'; | ||
|
||
export function limitTotalCharacters( | ||
messages: Message[], | ||
maxTotalChars: number | ||
) { | ||
let totalChars = 0; | ||
const limitedMessages: Message[] = []; | ||
|
||
for (let i = messages.length - 1; i >= 0; i--) { | ||
const msgChars = messages[i].content.length; | ||
|
||
if (totalChars + msgChars > maxTotalChars) break; | ||
|
||
totalChars += msgChars; | ||
limitedMessages.unshift(messages[i]); | ||
} | ||
|
||
return limitedMessages; | ||
} |
33 changes: 33 additions & 0 deletions
33
templates/plate-playground-template/src/app/api/ai/utils/truncateSystemPrompt.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export function truncateSystemPrompt(systemPrompt: string, maxChars: number) { | ||
if (systemPrompt.length <= maxChars) return systemPrompt; | ||
|
||
// Find the position of <Block> and <Selection> tags | ||
const blockStart = systemPrompt.indexOf('<Block>'); | ||
const selectionStart = systemPrompt.indexOf('<Selection>'); | ||
|
||
if (blockStart === -1 || selectionStart === -1) { | ||
// If tags are not found, simple truncation | ||
return systemPrompt.slice(0, maxChars - 3) + '...'; | ||
} | ||
|
||
// Preserve the structure and truncate content within tags if necessary | ||
const prefix = systemPrompt.slice(0, blockStart); | ||
const blockContent = systemPrompt.slice(blockStart, selectionStart); | ||
const selectionContent = systemPrompt.slice(selectionStart); | ||
|
||
const availableChars = maxChars - prefix.length - 6; // 6 for '...' in both block and selection | ||
const halfAvailable = availableChars / 2; | ||
|
||
const truncatedBlock = | ||
blockContent.length > halfAvailable | ||
? blockContent.slice(0, halfAvailable - 3) + '...' | ||
: blockContent; | ||
|
||
const truncatedSelection = | ||
selectionContent.length > availableChars - truncatedBlock.length | ||
? selectionContent.slice(0, availableChars - truncatedBlock.length - 3) + | ||
'...' | ||
: selectionContent; | ||
|
||
return prefix + truncatedBlock + truncatedSelection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters