|
| 1 | +import { createClient } from "@/ai/client"; |
| 2 | +import PostHogClient from "@/lib/postHogServer"; |
| 3 | +import { shouldUseAuth } from "@/lib/shouldUseAuth"; |
| 4 | +import { currentUser } from "@clerk/nextjs/server"; |
| 5 | +import { NextRequest } from "next/server"; |
| 6 | + |
| 7 | +import { ChatCompletionCreateParams } from "openai/resources/index.mjs"; |
| 8 | + |
| 9 | +const makePrompt = ( |
| 10 | + p1: string, |
| 11 | + p2: string |
| 12 | +) => `You will be acting as a website breeder. Your task is to generate a new website name based on two existing websites, as if the new website were the offspring of the two parent sites. |
| 13 | +
|
| 14 | +Here are the two URLs you will be working with: |
| 15 | +<url1> |
| 16 | +${p1} |
| 17 | +</url1> |
| 18 | +<url2> |
| 19 | +${p2} |
| 20 | +</url2> |
| 21 | +
|
| 22 | +To generate the offspring website name, follow these steps: |
| 23 | +
|
| 24 | +1. Analyze each website and identify their key elements, themes, and characteristics. Consider factors such as the website's purpose, target audience, design style, and content focus. |
| 25 | +
|
| 26 | +2. Brainstorm potential offspring website names that combine elements from both parent websites. The new name should reflect a blend of the two sites' themes and characteristics. |
| 27 | +
|
| 28 | +3. When creating the offspring website name, keep these guidelines in mind: |
| 29 | + - The name should be concise and memorable. |
| 30 | + - It should maintain a coherent theme that relates to both parent websites. |
| 31 | + - The name should hint at the potential purpose or content of the hypothetical offspring website. |
| 32 | + - Feel free to use wordplay, puns, or creative combinations of words from the parent website names or their key elements. |
| 33 | +
|
| 34 | +4. After brainstorming and refining your ideas, select the best offspring website name. |
| 35 | +
|
| 36 | +Please output the final offspring website name inside <offspring_url> tags. |
| 37 | +`; |
| 38 | + |
| 39 | +export async function POST(req: NextRequest) { |
| 40 | + const { urls } = await req.json(); |
| 41 | + |
| 42 | + if (shouldUseAuth) { |
| 43 | + const user = await currentUser(); |
| 44 | + const posthog = PostHogClient(); |
| 45 | + |
| 46 | + if (!user) { |
| 47 | + return new Response(`<h1>Unauthorized</h1><p>Log in to continue</p>`, { |
| 48 | + status: 401, |
| 49 | + headers: { "Content-Type": "text/html" }, |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + posthog.capture({ |
| 54 | + distinctId: user.id, |
| 55 | + event: "breed url", |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + const offspringUrl = await genResponse({ urls }); |
| 60 | + |
| 61 | + return new Response(JSON.stringify(offspringUrl), { |
| 62 | + headers: { |
| 63 | + "Content-Type": "application/json", |
| 64 | + }, |
| 65 | + status: 200, |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +async function genResponse({ urls }: { urls: string[] }): Promise<string> { |
| 70 | + const params: ChatCompletionCreateParams = { |
| 71 | + messages: [ |
| 72 | + { |
| 73 | + role: "user", |
| 74 | + content: makePrompt(urls[0], urls[1]), |
| 75 | + }, |
| 76 | + ], |
| 77 | + |
| 78 | + model: "claude-3-haiku-20240307", |
| 79 | + max_tokens: 4000, |
| 80 | + }; |
| 81 | + |
| 82 | + const client = createClient(process.env.ANTHROPIC_API_KEY!); |
| 83 | + |
| 84 | + const content = (await client.chat.completions.create(params)).choices[0] |
| 85 | + .message.content!; |
| 86 | + const offspringUrlMatch = content.match( |
| 87 | + /<offspring_url>(.*?)<\/offspring_url>/ |
| 88 | + ); |
| 89 | + return offspringUrlMatch ? offspringUrlMatch[1] : ""; |
| 90 | +} |
0 commit comments