mirror of
https://github.com/hpware/news-analyze.git
synced 2025-06-24 00:01:03 +08:00
- Updated DraggableWindow.vue to improve shadow effects. - Refactored AboutWindow.vue for better structure and readability. - Added chatbot functionality in chatbot.vue with cookie management. - Improved navigation component for better code clarity. - Created a new chat history table in the database schema. - Modified error handling in error.vue to display error messages correctly. - Integrated ChatbotWindow into the desktop application layout. - Implemented accordion component in home.vue for Q/A section. - Enhanced API for chat functionality with improved error handling. - Removed unused routes for cleaner codebase. - Added custom animations for accordion components in tailwind.config.js. - Developed accordion UI components (Accordion, AccordionContent, AccordionItem, AccordionTrigger) for better user interaction.
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import { Groq } from "groq-sdk";
|
|
import sql from "~/server/components/postgres";
|
|
|
|
const groq = new Groq();
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const slug = getRouterParam(event, "slug");
|
|
if (!slug) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: "A UUID is required for this action.",
|
|
});
|
|
}
|
|
const getChatHistory = await sql`
|
|
select * from chatHistory
|
|
where uuid = ${slug}
|
|
order by created_ata asc
|
|
`;
|
|
if (getChatHistory.length === 0) {
|
|
}
|
|
const body = await readBody(event);
|
|
const fetchNewsArticle = await sql`
|
|
select * from newArticle
|
|
where newsid = ${body.newsid}
|
|
`;
|
|
const chatCompletion = await groq.chat.completions.create({
|
|
messages: [
|
|
{
|
|
role: "system",
|
|
content: `You are a news chat, the following content will be used to chat with the user title: ${fetchNewsArticle.title}\n content: ${fetchNewsArticle.content}`,
|
|
},
|
|
...getChatHistory.map((chat) => ({
|
|
role: chat.role,
|
|
content: chat.content,
|
|
})),
|
|
{
|
|
role: "user",
|
|
content: `${body}`,
|
|
},
|
|
],
|
|
model: "llama-3.1-8b-instant",
|
|
temperature: 1,
|
|
max_completion_tokens: 1024,
|
|
top_p: 1,
|
|
stream: true,
|
|
stop: null,
|
|
});
|
|
await sql`
|
|
INSERT INTO chat_history (uuid, role, content)
|
|
VALUES (${slug}, 'user', ${body})
|
|
`;
|
|
let assistantResponse = "";
|
|
for await (const chunk of chatCompletion) {
|
|
const content = chunk.choices[0]?.delta?.content || "";
|
|
assistantResponse += content;
|
|
process.stdout.write(content);
|
|
}
|
|
if (assistantResponse) {
|
|
await sql`
|
|
INSERT INTO chat_history (uuid, role, content)
|
|
VALUES (${slug}, 'assistant', ${assistantResponse})
|
|
`;
|
|
}
|
|
});
|