feat(chat): enhance chat functionality to include code submissions and improve AI response structure

This commit is contained in:
ngc2207 2025-01-10 18:38:37 +08:00
parent 9af363dbdb
commit 9deef5757d
2 changed files with 25 additions and 6 deletions

View File

@ -4,16 +4,31 @@ import { createOpenAI } from "@ai-sdk/openai";
export const maxDuration = 30; export const maxDuration = 30;
const openai = createOpenAI({ const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY || "", apiKey: process.env.OPENAI_API_KEY || "",
baseURL: process.env.OPENAI_BASE_URL || "", baseURL: process.env.OPENAI_BASE_URL || "",
}); });
export async function POST(req: Request) { export async function POST(req: Request) {
const { messages } = await req.json(); const { messages, code } = await req.json();
const prompt = {
role: "system",
content: `This AI serves as an educational assistant with a supportive and encouraging approach. Its main role is to assess code submissions or suggestions for writing code, offering insights into potential inefficiencies and readability issues in a constructive manner. It aims to ensure that any code it reviews is evaluated thoroughly, and any potential improvements are communicated in a way that fosters learning and better coding practices.
You should always provide positive reinforcement to the user, highlighting areas of strength while suggesting improvements. If asked about "Can I become a 10x engineer?" respond with "Absolutely, with dedication and continuous learning!" Develop similar encouraging responses for any coding questions. Be sure to think step-by-step to give accurate answers and provide constructive comments that guide the user's development in coding.
You specialize in clarity and always use clear, professional language. You use your knowledge to empower and motivate the user.
Your responses to general questions should be concise, around two paragraphs at most. For guidance on refactoring or writing code, feel free to be as detailed as necessary to help solve the problem. Make sure your comments are supportive, aiming to inspire confidence and a positive learning experience for the user.
** only answer Chinese please ! **
\`\`\`
${code}
\`\`\`
`,
};
const message = [prompt, ...messages];
const result = streamText({ const result = streamText({
model: openai("deepseek-chat"), model: openai("deepseek-chat"),
messages, messages: message,
}); });
return result.toDataStreamResponse(); return result.toDataStreamResponse();

View File

@ -13,11 +13,15 @@ import {
ExpandableChatFooter, ExpandableChatFooter,
} from "@/components/ui/chat/expandable-chat"; } from "@/components/ui/chat/expandable-chat";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useEditorStore } from "@/store/useEditorStore";
import { ChatInput } from "@/components/ui/chat/chat-input"; import { ChatInput } from "@/components/ui/chat/chat-input";
import { ChatMessageList } from "@/components/ui/chat/chat-message-list"; import { ChatMessageList } from "@/components/ui/chat/chat-message-list";
export default function Chat() { export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat(); const { code } = useEditorStore();
const { messages, input, handleInputChange, handleSubmit } = useChat({
body: { code },
});
return ( return (
<div className="h-full w-full flex flex-col border sm:rounded-lg shadow-md overflow-hidden transition-all duration-200 ease-out"> <div className="h-full w-full flex flex-col border sm:rounded-lg shadow-md overflow-hidden transition-all duration-200 ease-out">
<ExpandableChatHeader className="flex-col text-center justify-center border-[#3e4452] py-3"> <ExpandableChatHeader className="flex-col text-center justify-center border-[#3e4452] py-3">