1
2
ChatGPT clone using the T3 Stack, which includes Next.js,
3
TypeScript, Tailwind CSS, and tRPC.
4
5
The repo provides a structured foundation for creating a modern
6
web application, integrating technologies like Prisma for
7
database management and NextAuth.js for authentication.
8
9
10
12
13
async makeTitle(history: Message[]): Promise<string> {
14
const input = {
15
role: "user",
16
content:
17
"Make a title for this chat which consist of few words, no quotes",
18
} as Message;
19
const res = await openai.chat.completions.create({
20
model: this.model,
21
messages: [
22
...history,
23
{ ...input, content: `${input.content}\n---${this.constraints}\n---` },
24
],
25
});
26
const outputContent = res?.choices[0]?.message?.content ?? "";
27
return outputContent;
28
}
29
30
31
32
33
34
35
async makeTitle(history: Message[]): Promise<string> {
const input = {
role: "user",
content:
"Make a title for this chat which consist of few words, no quotes",
} as Message;
const res = await openai.chat.completions.create({
model: this.model,
messages: [
...history,
{ ...input, content: `${input.content}\n---${this.constraints}\n---` },
],
});
const outputContent = res?.choices[0]?.message?.content ?? "";
return outputContent;
}
async ask(
inputContent: string,
opts?: { history?: Message[]; stream?: boolean },
): Promise<
| Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
| OpenAI.Chat.Completions.ChatCompletion
> {
console.log("openai.ask", { inputContent, opts });
const input = { role: "user", content: inputContent } as Message;
const output = await openai.chat.completions.create({
stream: opts?.stream,
model: this.model,
messages: [
...(opts?.history ?? []),
{ ...input, content: `${input.content}\n---${this.constraints}\n---` },
],
});
console.log("openai.ask return", { input, output });
return output;
}
get: protectedProcedure
.input(z.object({ id: z.number() }))
.query(async ({ ctx, input }) => {
const userId = ctx.session.user.id;
const convId = input.id;
const conversation = await ctx.db.conversation.findUnique({
where: { id: convId },
include: {
messages: {
orderBy: { createdAt: "asc" },
},
},
});
if (conversation?.createdById !== userId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "That is not your conversation",
});
}
return conversation;
}),
export const protectedProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
// infers the `session` as non-nullable
session: { ...ctx.session, user: ctx.session.user },
},
});
});