Last active
February 11, 2025 07:05
-
-
Save toy-crane/c92121c1e64ad702c1c1e1fc1f6bc558 to your computer and use it in GitHub Desktop.
How to create a title
This file contains hidden or 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
import { createClient } from "https://esm.sh/@supabase/[email protected]"; | |
import { generateObject, generateText, Output } from "npm:ai"; | |
import { openai } from "npm:@ai-sdk/openai"; | |
import { z } from "npm:zod"; | |
const TitleOutput = z.object({ | |
summary: z.string().describe( | |
"대화에서 다룬 내용을 바탕으로 간단한 요약을 알려주세요", | |
), | |
partner: z.string().describe( | |
"첨부된 대화 캡처 이미지 속에서 상대방의 이름을 알려주세요. 첨부된 이미지가 없다면 대화 내용을 보고 누구에 대해 이야기하는지 유추해 보세요.", | |
), | |
}); | |
type TitleSchema = z.infer<typeof TitleOutput>; | |
const system = ` | |
당신은 채팅 대화의 제목을 생성하는 AI입니다. | |
`; | |
interface UIMessage { | |
id: string; | |
createdAt?: Date; | |
content: string; | |
experimental_attachments?: Attachment[]; | |
role: "system" | "user" | "assistant" | "data"; | |
} | |
interface Attachment { | |
name?: string; | |
contentType?: string; | |
url: string; | |
} | |
export interface GenerateTitleRequest { | |
chatId: string; | |
} | |
Deno.serve(async (req) => { | |
const supabase = createClient( | |
Deno.env.get("SUPABASE_URL") ?? "", | |
Deno.env.get("SUPABASE_ANON_KEY") ?? "", | |
); | |
const { data: messages } = await supabase | |
.from("messages") | |
.select("*") | |
.eq("chat_id", chatId) | |
.order("created_at", { ascending: true }); | |
if (!messages) { | |
return new Response(JSON.stringify({ error: "No messages found" }), { | |
status: 404, | |
}); | |
} | |
const mappedMessages = messages.map((message) => ({ | |
role: message.role, | |
content: message.content, | |
experimental_attachments: message.attachments, | |
})); | |
const newMessages = [ | |
...mappedMessages, | |
{ | |
role: "user", | |
content: "첨부된 이미지를 보고 누구와의 대화인지 제목을 만들어줘", | |
}, | |
] as UIMessage[]; | |
try { | |
const { experimental_output } = await generateText({ | |
system, | |
messages: newMessages, | |
model: openai("gpt-4o"), | |
experimental_output: Output.object<TitleSchema>({ | |
schema: TitleOutput, | |
}), | |
}); | |
const prompt = ` | |
대화 상대방 이름: ${experimental_output.partner}, | |
주제: ${experimental_output.summary} | |
위 내용을 참고하여 | |
아래 예시와 같은 형식의 간결한 제목을 만들어줘. | |
예시) 보영과 첫인상에 대한 대화 | |
예시) 여자친구와 헤어짐에 대한 대화 | |
예시) toycrane과 영화 추천에 대한 대화 | |
`; | |
const titleGeneration = span.generation({ | |
name: "title-generation", | |
model: "gpt-4o", | |
input: { | |
system, | |
prompt, | |
}, | |
}); | |
const { object } = await generateObject({ | |
system, | |
schema: z.object({ | |
title: z.string().max(30), | |
}), | |
prompt, | |
model: openai("gpt-4o"), | |
}); | |
return new Response(JSON.stringify(experimental_output), { | |
headers: { "Content-Type": "application/json" }, | |
}); | |
} catch (error) { | |
console.error("Error generating title:", error); | |
return new Response( | |
JSON.stringify({ error: "Failed to generate title" }), | |
{ status: 500 }, | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment