mirror of
https://github.com/hpware/news-analyze.git
synced 2025-06-24 00:01:03 +08:00
Made a basic AI chat streaming content, but there is still no auth yet,
should be added soon. And add the test file test.vue to be ignored. This file is just a poc (it is AI generated based on my API file)
This commit is contained in:
parent
13c9b7ecc8
commit
d21957a8f9
3
.gitignore
vendored
3
.gitignore
vendored
@ -33,3 +33,6 @@ __pycache__
|
|||||||
*.sql
|
*.sql
|
||||||
!database/*.sql
|
!database/*.sql
|
||||||
_dt_*.py
|
_dt_*.py
|
||||||
|
|
||||||
|
# Testing files
|
||||||
|
test.vue
|
||||||
|
@ -4,30 +4,39 @@ import sql from "~/server/components/postgres";
|
|||||||
const groq = new Groq();
|
const groq = new Groq();
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
|
const host = await getRequestHost(event);
|
||||||
|
const protocol = await getRequestProtocol(event);
|
||||||
|
const hears = await getRequestHeaders(event);
|
||||||
const slug = getRouterParam(event, "slug");
|
const slug = getRouterParam(event, "slug");
|
||||||
|
const body = await readBody(event);
|
||||||
if (!slug) {
|
if (!slug) {
|
||||||
throw createError({
|
throw createError({
|
||||||
statusCode: 400,
|
statusCode: 400,
|
||||||
message: "A UUID is required for this action.",
|
message: "A UUID is required for this action.",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const getChatHistory = await sql`
|
const getChatHistory = await sql`
|
||||||
select * from chatHistory
|
select * from chat_history
|
||||||
where uuid = ${slug}
|
where uuid = ${slug}
|
||||||
order by created_ata asc
|
order by created_at asc
|
||||||
`;
|
|
||||||
if (getChatHistory.length === 0) {
|
|
||||||
}
|
|
||||||
const body = await readBody(event);
|
|
||||||
const fetchNewsArticle = await sql`
|
|
||||||
select * from newArticle
|
|
||||||
where newsid = ${body.newsid}
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
const buildURL = protocol + "://" + host + "/api/news/get/lt/" + "LX30VwG";
|
||||||
|
const data = await fetch(buildURL);
|
||||||
|
const fetchNewsArticle = await data.json();
|
||||||
|
|
||||||
|
// Set headers for Server-Sent Events
|
||||||
|
setHeader(event, "Content-Type", "text/plain; charset=utf-8");
|
||||||
|
setHeader(event, "Cache-Control", "no-cache");
|
||||||
|
setHeader(event, "Connection", "keep-alive");
|
||||||
|
setHeader(event, "Access-Control-Allow-Origin", "*");
|
||||||
|
|
||||||
const chatCompletion = await groq.chat.completions.create({
|
const chatCompletion = await groq.chat.completions.create({
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: "system",
|
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}`,
|
content: `You are a news chat, the following content will be used to chat with the user title: ${fetchNewsArticle.title} article: ${fetchNewsArticle.paragraph} origin: ${fetchNewsArticle.origin} author: ${fetchNewsArticle.author}`,
|
||||||
},
|
},
|
||||||
...getChatHistory.map((chat) => ({
|
...getChatHistory.map((chat) => ({
|
||||||
role: chat.role,
|
role: chat.role,
|
||||||
@ -35,7 +44,7 @@ export default defineEventHandler(async (event) => {
|
|||||||
})),
|
})),
|
||||||
{
|
{
|
||||||
role: "user",
|
role: "user",
|
||||||
content: `${body}`,
|
content: body.message,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
model: "llama-3.1-8b-instant",
|
model: "llama-3.1-8b-instant",
|
||||||
@ -45,20 +54,44 @@ export default defineEventHandler(async (event) => {
|
|||||||
stream: true,
|
stream: true,
|
||||||
stop: null,
|
stop: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Save user message
|
||||||
await sql`
|
await sql`
|
||||||
INSERT INTO chat_history (uuid, role, content)
|
INSERT INTO chat_history (uuid, role, content)
|
||||||
VALUES (${slug}, 'user', ${body})
|
VALUES (${slug}, 'user', ${body.message})
|
||||||
`;
|
`; */
|
||||||
|
|
||||||
let assistantResponse = "";
|
let assistantResponse = "";
|
||||||
|
|
||||||
|
// Create a readable stream
|
||||||
|
const stream = new ReadableStream({
|
||||||
|
async start(controller) {
|
||||||
|
try {
|
||||||
for await (const chunk of chatCompletion) {
|
for await (const chunk of chatCompletion) {
|
||||||
const content = chunk.choices[0]?.delta?.content || "";
|
const content = chunk.choices[0]?.delta?.content || "";
|
||||||
|
if (content) {
|
||||||
assistantResponse += content;
|
assistantResponse += content;
|
||||||
process.stdout.write(content);
|
// Send chunk to client
|
||||||
|
controller.enqueue(new TextEncoder().encode(content));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Save complete assistant response
|
||||||
if (assistantResponse) {
|
if (assistantResponse) {
|
||||||
await sql`
|
await sql`
|
||||||
INSERT INTO chat_history (uuid, role, content)
|
INSERT INTO chat_history (uuid, role, content)
|
||||||
VALUES (${slug}, 'assistant', ${assistantResponse})
|
VALUES (${slug}, 'assistant', ${assistantResponse})
|
||||||
`;
|
`;
|
||||||
|
} */
|
||||||
|
|
||||||
|
controller.close();
|
||||||
|
} catch (error) {
|
||||||
|
controller.error(error);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return sendStream(event, stream);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user