mirror of
https://github.com/hpware/news-analyze.git
synced 2025-06-24 00:01:03 +08:00
Made a basic news summarizer. This is just to test in prod.
This commit is contained in:
parent
d21957a8f9
commit
11de0632ae
@ -4,9 +4,9 @@ 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 host = getRequestHost(event);
|
||||||
const protocol = await getRequestProtocol(event);
|
const protocol = getRequestProtocol(event);
|
||||||
const hears = await getRequestHeaders(event);
|
const hears = getRequestHeaders(event);
|
||||||
const slug = getRouterParam(event, "slug");
|
const slug = getRouterParam(event, "slug");
|
||||||
const body = await readBody(event);
|
const body = await readBody(event);
|
||||||
if (!slug) {
|
if (!slug) {
|
||||||
@ -47,8 +47,8 @@ export default defineEventHandler(async (event) => {
|
|||||||
content: body.message,
|
content: body.message,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
model: "llama-3.1-8b-instant",
|
model: "gemma2-9b-it",
|
||||||
temperature: 1,
|
temperature: 0.71,
|
||||||
max_completion_tokens: 1024,
|
max_completion_tokens: 1024,
|
||||||
top_p: 1,
|
top_p: 1,
|
||||||
stream: true,
|
stream: true,
|
||||||
@ -56,7 +56,6 @@ export default defineEventHandler(async (event) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// 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.message})
|
VALUES (${slug}, 'user', ${body.message})
|
||||||
@ -78,7 +77,6 @@ export default defineEventHandler(async (event) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// 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)
|
||||||
|
@ -4,23 +4,24 @@ 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 = getRequestHost(event);
|
||||||
|
const protocol = getRequestProtocol(event);
|
||||||
const slug = getRouterParam(event, "slug");
|
const slug = getRouterParam(event, "slug");
|
||||||
const fetchNewsArticle = await sql`
|
const buildURL = protocol + "://" + host + "/api/news/get/lt/" + slug;
|
||||||
select * from newArticle
|
const data = await fetch(buildURL);
|
||||||
where slug = ${slug}
|
const fetchNewsArticle = await data.json();
|
||||||
`;
|
|
||||||
const chatCompletion = await groq.chat.completions.create({
|
const chatCompletion = await groq.chat.completions.create({
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: "user",
|
role: "user",
|
||||||
content: `${fetchNewsArticle.title}\n${fetchNewsArticle.content}`,
|
content: `${fetchNewsArticle.title}\n${fetchNewsArticle.paragraph}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: "system",
|
role: "system",
|
||||||
content: `You are a news summarizer. You will be given a news article and you will summarize it into a short paragraph.`,
|
content: `You are a news summarizer. You will be given a news article and you will summarize it into a short paragraph.`,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
model: "llama3-70b-8192",
|
model: "gemma2-9b-it",
|
||||||
temperature: 1,
|
temperature: 1,
|
||||||
max_completion_tokens: 1024,
|
max_completion_tokens: 1024,
|
||||||
top_p: 1,
|
top_p: 1,
|
||||||
@ -28,7 +29,22 @@ export default defineEventHandler(async (event) => {
|
|||||||
stop: null,
|
stop: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
for await (const chunk of chatCompletion) {
|
const stream = new ReadableStream({
|
||||||
process.stdout.write(chunk.choices[0]?.delta?.content || "");
|
async start(controller) {
|
||||||
}
|
try {
|
||||||
|
for await (const chunk of chatCompletion) {
|
||||||
|
const content = chunk.choices[0]?.delta?.content || "";
|
||||||
|
if (content) {
|
||||||
|
controller.enqueue(new TextEncoder().encode(content));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
controller.close();
|
||||||
|
} catch (error) {
|
||||||
|
controller.error(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return sendStream(event, stream);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user