mirror of
https://github.com/hpware/news-analyze.git
synced 2025-06-23 21:14:23 +00:00
Made a basic news summarizer. This is just to test in prod.
This commit is contained in:
parent
d21957a8f9
commit
11de0632ae
2 changed files with 30 additions and 16 deletions
|
@ -4,23 +4,24 @@ import sql from "~/server/components/postgres";
|
|||
const groq = new Groq();
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const host = getRequestHost(event);
|
||||
const protocol = getRequestProtocol(event);
|
||||
const slug = getRouterParam(event, "slug");
|
||||
const fetchNewsArticle = await sql`
|
||||
select * from newArticle
|
||||
where slug = ${slug}
|
||||
`;
|
||||
const buildURL = protocol + "://" + host + "/api/news/get/lt/" + slug;
|
||||
const data = await fetch(buildURL);
|
||||
const fetchNewsArticle = await data.json();
|
||||
const chatCompletion = await groq.chat.completions.create({
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: `${fetchNewsArticle.title}\n${fetchNewsArticle.content}`,
|
||||
content: `${fetchNewsArticle.title}\n${fetchNewsArticle.paragraph}`,
|
||||
},
|
||||
{
|
||||
role: "system",
|
||||
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,
|
||||
max_completion_tokens: 1024,
|
||||
top_p: 1,
|
||||
|
@ -28,7 +29,22 @@ export default defineEventHandler(async (event) => {
|
|||
stop: null,
|
||||
});
|
||||
|
||||
for await (const chunk of chatCompletion) {
|
||||
process.stdout.write(chunk.choices[0]?.delta?.content || "");
|
||||
}
|
||||
const stream = new ReadableStream({
|
||||
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…
Add table
Add a link
Reference in a new issue