Add AI chat feature
This commit is contained in:
parent
80056b3728
commit
6a5041be43
15 changed files with 1259 additions and 2 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import Elysia from "elysia";
|
||||
import { health } from "./modules/health";
|
||||
import { chat } from "./modules/chat";
|
||||
|
||||
export const app = new Elysia({ prefix: "/api" }).use(health);
|
||||
export const app = new Elysia({ prefix: "/api" }).use(health).use(chat);
|
||||
|
|
|
|||
15
server/modules/chat/index.ts
Normal file
15
server/modules/chat/index.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import { Elysia } from "elysia";
|
||||
|
||||
import { ChatModel } from "./model";
|
||||
import { Chat } from "./service";
|
||||
|
||||
export const chat = new Elysia({ prefix: "/chat" }).post(
|
||||
"/",
|
||||
async ({ body }) => {
|
||||
const stream = await Chat.streamText(body);
|
||||
return stream.toUIMessageStreamResponse();
|
||||
},
|
||||
{
|
||||
body: ChatModel.chat,
|
||||
},
|
||||
);
|
||||
21
server/modules/chat/model.ts
Normal file
21
server/modules/chat/model.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import { t, type UnwrapSchema } from "elysia";
|
||||
|
||||
export const ChatModel = {
|
||||
chat: t.Object({
|
||||
messages: t.Array(
|
||||
t.Object({
|
||||
id: t.String(),
|
||||
role: t.Union([
|
||||
t.Literal("user"),
|
||||
t.Literal("assistant"),
|
||||
t.Literal("system"),
|
||||
]),
|
||||
parts: t.Array(t.Any()),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
} as const;
|
||||
|
||||
export type ChatModel = {
|
||||
[k in keyof typeof ChatModel]: UnwrapSchema<(typeof ChatModel)[k]>;
|
||||
};
|
||||
13
server/modules/chat/service.ts
Normal file
13
server/modules/chat/service.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import { convertToModelMessages, streamText } from "ai";
|
||||
import type { ChatModel } from "./model";
|
||||
import { openai } from "@ai-sdk/openai";
|
||||
|
||||
export abstract class Chat {
|
||||
static async streamText(chat: ChatModel["chat"]) {
|
||||
return streamText({
|
||||
model: openai("gpt-5.5"),
|
||||
system: "You are Yae Miko from Genshin Impact",
|
||||
messages: await convertToModelMessages(chat.messages),
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue