forked from steven-tey/novel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Built WYSYWIG editor with Tiptap, integrated slash commands and AI au…
…tocomplete
- Loading branch information
1 parent
fbf4246
commit e8453ea
Showing
20 changed files
with
4,853 additions
and
5,133 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 |
---|---|---|
@@ -1,44 +1,48 @@ | ||
import { OpenAIStream, OpenAIStreamPayload } from "@/lib/openai/stream"; | ||
import { Configuration, OpenAIApi } from "openai-edge"; | ||
import { OpenAIStream, StreamingTextResponse } from "ai"; | ||
|
||
if (!process.env.OPENAI_API_KEY) { | ||
throw new Error("Missing env var from OpenAI"); | ||
} | ||
// Create an OpenAI API client (that's edge friendly!) | ||
const config = new Configuration({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}); | ||
const openai = new OpenAIApi(config); | ||
|
||
export const config = { | ||
runtime: "edge", | ||
}; | ||
export const runtime = "edge"; | ||
|
||
export async function POST(req: Request): Promise<Response> { | ||
let { content } = (await req.json()) as { | ||
content?: string; | ||
}; | ||
|
||
if (!content) { | ||
return new Response("No prompt in the request", { status: 400 }); | ||
} | ||
let { prompt: content } = await req.json(); | ||
|
||
// truncate content to the last 500 characters | ||
content = content.slice(-500); | ||
// remove line breaks, | ||
// remove trailing slash | ||
// limit to 500 characters | ||
content = content.replace(/\n/g, " ").replace(/\/$/, "").slice(0, 500); | ||
|
||
const payload: OpenAIStreamPayload = { | ||
const response = await openai.createChatCompletion({ | ||
model: "gpt-3.5-turbo", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: | ||
"You are an AI writing assistant that autocompletes existing text based on context from prior text. Give more weight/priority to the later characters than the beginning ones, and add leading spaces accordingly.", | ||
"You are an AI writing assistant that autocompletes existing text based on context from prior text. " + | ||
"Give more weight/priority to the later characters than the beginning ones.", | ||
}, | ||
{ | ||
role: "user", | ||
content, | ||
}, | ||
{ role: "user", content }, | ||
], | ||
max_tokens: 50, | ||
temperature: 0.7, | ||
top_p: 1, | ||
frequency_penalty: 0, | ||
presence_penalty: 0, | ||
max_tokens: 50, | ||
stream: true, | ||
n: 1, | ||
}; | ||
}); | ||
|
||
// Convert the response into a friendly text-stream | ||
const stream = OpenAIStream(response); | ||
|
||
const stream = await OpenAIStream(payload); | ||
return new Response(stream); | ||
// Respond with the stream | ||
return new StreamingTextResponse(stream); | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use client"; | ||
|
||
import { ReactNode } from "react"; | ||
import { Toaster } from "sonner"; | ||
|
||
export default function Providers({ children }: { children: ReactNode }) { | ||
return ( | ||
<> | ||
<Toaster /> | ||
{children} | ||
</> | ||
); | ||
} |
Oops, something went wrong.