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

generate prompt on the server #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 1 addition & 4 deletions components/DropDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import {
ChevronUpIcon,
} from "@heroicons/react/20/solid";
import { Fragment } from "react";
import { vibes, type VibeType } from "../utils/prompt";

function classNames(...classes: string[]) {
return classes.filter(Boolean).join(" ");
}

export type VibeType = "Professional" | "Casual" | "Funny";

interface DropDownProps {
vibe: VibeType;
setVibe: (vibe: VibeType) => void;
}

let vibes: VibeType[] = ["Professional", "Casual", "Funny"];

export default function DropDown({ vibe, setVibe }: DropDownProps) {
return (
<Menu as="div" className="relative block text-left w-full">
Expand Down
24 changes: 19 additions & 5 deletions pages/api/generate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { NextRequest } from "next/server";
import { OpenAIStream, OpenAIStreamPayload } from "../../utils/OpenAIStream";
import { generatePrompt, vibes, VibeType } from "../../utils/prompt";

if (!process.env.OPENAI_API_KEY) {
throw new Error("Missing env var from OpenAI");
Expand All @@ -9,15 +10,28 @@ export const config = {
runtime: "edge",
};

type GenerateRequestBody = {
bio?: string;
vibe?: VibeType;
};

const handler = async (req: NextRequest): Promise<Response> => {
const { prompt } = (await req.json()) as {
prompt?: string;
};
if (req.method !== "POST") {
return new Response("Method not allowed", { status: 405 });
}

if (!prompt) {
return new Response("No prompt in the request", { status: 400 });
const { bio, vibe } = (await req.json()) as GenerateRequestBody;

if (!bio) {
return new Response("No bio in the request", { status: 400 });
}

if (!vibe || !vibes.includes(vibe)) {
return new Response("Invalid vibe", { status: 400 });
}

const prompt = generatePrompt(bio, vibe);

const payload: OpenAIStreamPayload = {
model: "text-davinci-003",
prompt,
Expand Down
17 changes: 5 additions & 12 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import type { NextPage } from "next";
import Head from "next/head";
import Image from "next/image";
import { useState } from "react";
import { Toaster, toast } from "react-hot-toast";
import DropDown, { VibeType } from "../components/DropDown";
import { toast, Toaster } from "react-hot-toast";
import DropDown from "../components/DropDown";
import Footer from "../components/Footer";
import Github from "../components/GitHub";
import Header from "../components/Header";
import LoadingDots from "../components/LoadingDots";
import ResizablePanel from "../components/ResizablePanel";
import { type VibeType } from "../utils/prompt";

const Home: NextPage = () => {
const [loading, setLoading] = useState(false);
Expand All @@ -19,15 +20,6 @@ const Home: NextPage = () => {

console.log("Streamed response: ", generatedBios);

const prompt =
vibe === "Funny"
? `Generate 2 funny twitter bios with no hashtags and clearly labeled "1." and "2.". Make sure there is a joke in there and it's a little ridiculous. Make sure each generated bio is at max 20 words and base it on this context: ${bio}${
bio.slice(-1) === "." ? "" : "."
}`
: `Generate 2 ${vibe} twitter bios with no hashtags and clearly labeled "1." and "2.". Make sure each generated bio is at least 14 words and at max 20 words and base them on this context: ${bio}${
bio.slice(-1) === "." ? "" : "."
}`;

const generateBio = async (e: any) => {
e.preventDefault();
setGeneratedBios("");
Expand All @@ -38,7 +30,8 @@ const Home: NextPage = () => {
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt,
bio,
vibe,
}),
});
console.log("Edge function returned.");
Expand Down
15 changes: 15 additions & 0 deletions utils/prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const vibes = ["Professional", "Casual", "Funny"] as const;

export type VibeType = typeof vibes[number];

export function generatePrompt(bio: string, vibe: VibeType) {
if (vibe === "Funny") {
return `Generate 2 funny twitter bios with no hashtags and clearly labeled "1." and "2.". Make sure there is a joke in there and it's a little ridiculous. Make sure each generated bio is at max 20 words and base it on this context: ${bio}${
bio.slice(-1) === "." ? "" : "."
}`;
}

return `Generate 2 ${vibe} twitter bios with no hashtags and clearly labeled "1." and "2.". Make sure each generated bio is at least 14 words and at max 20 words and base them on this context: ${bio}${
bio.slice(-1) === "." ? "" : "."
}`;
}