From 9deef5757d63c4a48671b5424a2085fcd04af432 Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Fri, 10 Jan 2025 18:38:37 +0800 Subject: [PATCH] feat(chat): enhance chat functionality to include code submissions and improve AI response structure --- src/app/api/chat/route.ts | 25 ++++++++++++++++++++----- src/app/playground/components/chat.tsx | 6 +++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts index 60ccbb3..45a94fb 100644 --- a/src/app/api/chat/route.ts +++ b/src/app/api/chat/route.ts @@ -4,16 +4,31 @@ import { createOpenAI } from "@ai-sdk/openai"; export const maxDuration = 30; const openai = createOpenAI({ - apiKey: process.env.OPENAI_API_KEY || "", - baseURL: process.env.OPENAI_BASE_URL || "", - }); + apiKey: process.env.OPENAI_API_KEY || "", + baseURL: process.env.OPENAI_BASE_URL || "", +}); 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({ model: openai("deepseek-chat"), - messages, + messages: message, }); return result.toDataStreamResponse(); diff --git a/src/app/playground/components/chat.tsx b/src/app/playground/components/chat.tsx index d308c6d..a5a6d79 100644 --- a/src/app/playground/components/chat.tsx +++ b/src/app/playground/components/chat.tsx @@ -13,11 +13,15 @@ import { ExpandableChatFooter, } from "@/components/ui/chat/expandable-chat"; import { Button } from "@/components/ui/button"; +import { useEditorStore } from "@/store/useEditorStore"; import { ChatInput } from "@/components/ui/chat/chat-input"; import { ChatMessageList } from "@/components/ui/chat/chat-message-list"; export default function Chat() { - const { messages, input, handleInputChange, handleSubmit } = useChat(); + const { code } = useEditorStore(); + const { messages, input, handleInputChange, handleSubmit } = useChat({ + body: { code }, + }); return (