mirror of
https://litchi.icu/ngc2207/judge4c-demo.git
synced 2025-05-18 22:46:49 +00:00
feat: 添加聊天支持API和组件,集成OpenAI模型
This commit is contained in:
parent
3b2f2d3d2a
commit
b529255006
20
src/app/api/chat/route.ts
Normal file
20
src/app/api/chat/route.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { streamText } from "ai";
|
||||||
|
import { createOpenAI } from "@ai-sdk/openai";
|
||||||
|
|
||||||
|
const openai = createOpenAI({
|
||||||
|
apiKey: process.env.OPENAI_API_KEY || "",
|
||||||
|
baseURL: process.env.OPENAI_BASE_URL || "",
|
||||||
|
});
|
||||||
|
|
||||||
|
export const maxDuration = 30;
|
||||||
|
|
||||||
|
export async function POST(req: Request) {
|
||||||
|
const { messages } = await req.json();
|
||||||
|
|
||||||
|
const result = await streamText({
|
||||||
|
model: openai("gpt-4o-mini"),
|
||||||
|
messages,
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.toDataStreamResponse();
|
||||||
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ChatBubble,
|
ChatBubble,
|
||||||
ChatBubbleAvatar,
|
ChatBubbleAvatar,
|
||||||
@ -13,8 +15,10 @@ import {
|
|||||||
import { ChatMessageList } from "@/components/ui/chat/chat-message-list";
|
import { ChatMessageList } from "@/components/ui/chat/chat-message-list";
|
||||||
import { Button } from "./ui/button";
|
import { Button } from "./ui/button";
|
||||||
import { Send } from "lucide-react";
|
import { Send } from "lucide-react";
|
||||||
|
import { useChat } from "ai/react";
|
||||||
|
|
||||||
export default function ChatSupport() {
|
export default function ChatSupport() {
|
||||||
|
const { messages, input, handleInputChange, handleSubmit } = useChat();
|
||||||
return (
|
return (
|
||||||
<ExpandableChat size="lg" position="bottom-right">
|
<ExpandableChat size="lg" position="bottom-right">
|
||||||
<ExpandableChatHeader className="flex-col text-center justify-center">
|
<ExpandableChatHeader className="flex-col text-center justify-center">
|
||||||
@ -27,17 +31,33 @@ export default function ChatSupport() {
|
|||||||
</ExpandableChatHeader>
|
</ExpandableChatHeader>
|
||||||
<ExpandableChatBody>
|
<ExpandableChatBody>
|
||||||
<ChatMessageList>
|
<ChatMessageList>
|
||||||
<ChatBubble>
|
{messages.map((message, index) => (
|
||||||
<ChatBubbleAvatar />
|
<ChatBubble
|
||||||
<ChatBubbleMessage>{/* {message.content} */}</ChatBubbleMessage>
|
key={index}
|
||||||
</ChatBubble>
|
variant={message.role == "user" ? "sent" : "received"}
|
||||||
|
>
|
||||||
|
<ChatBubbleAvatar
|
||||||
|
src=""
|
||||||
|
fallback={message.role == "user" ? "US" : "AI"}
|
||||||
|
/>
|
||||||
|
<ChatBubbleMessage
|
||||||
|
variant={message.role == "user" ? "sent" : "received"}
|
||||||
|
>
|
||||||
|
{message.content}
|
||||||
|
</ChatBubbleMessage>
|
||||||
|
</ChatBubble>
|
||||||
|
))}
|
||||||
</ChatMessageList>
|
</ChatMessageList>
|
||||||
</ExpandableChatBody>
|
</ExpandableChatBody>
|
||||||
<ExpandableChatFooter>
|
<ExpandableChatFooter>
|
||||||
<ChatInput />
|
<form onSubmit={handleSubmit}>
|
||||||
<Button type="submit" size="icon">
|
<div className="flex items-center justify-between gap-2">
|
||||||
<Send className="size-4" />
|
<ChatInput value={input} onChange={handleInputChange} />
|
||||||
</Button>
|
<Button type="submit" size="icon">
|
||||||
|
<Send className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
</ExpandableChatFooter>
|
</ExpandableChatFooter>
|
||||||
</ExpandableChat>
|
</ExpandableChat>
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user