feat: add summary using Anthropic API

This commit is contained in:
Riccardo
2024-09-20 02:52:16 +02:00
parent 1917b040eb
commit c0a669dc0a
8 changed files with 325 additions and 59 deletions

13
utils/anthropic.ts Normal file
View File

@@ -0,0 +1,13 @@
import Anthropic from '@anthropic-ai/sdk';
export async function message(text: string) {
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY
});
return anthropic.messages.create({
model: 'claude-3-5-sonnet-20240620',
max_tokens: 1024,
messages: [{ role: 'user', content: text }]
});
}

21
utils/summarize.ts Normal file
View File

@@ -0,0 +1,21 @@
import { message } from './anthropic';
import { NewsType } from './validationSchemas';
export async function summirize(news: NewsType[]) {
const newsInput = news
.map(n => `TITLE: ${n.title}, CONTENT: ${n.text}, LINK: ${n.url}`)
.join('\n\n');
const promptSetup =
"You are a tech journalist with a technology degree and background. Summarize the following list of posts from an online forum as a TL;DR (Too Long; Didn't Read) summary. Your summary should:\n\n1. Be 200-500 words long.\n\n2. Consist multiple phrases in one single paragraph, combining related news items where possible.\n\n3. Highlight the 2-3 most significant or impactful news items.\n\n4. Provide context within broader tech trends or developments.\n\n5. Use a tone that is informative and slightly enthusiastic, aimed at tech-savvy general readers.\n\n6. Group news items by themes or technology areas if applicable.\n\n7. Be formatted for HTML use, with links incorporated as follows: <a href='[LINK]'>[linked text]</a>\n\nFocus on conveying the key points and their potential impact on the tech landscape. Your response should consist of the summary only.\n\nThe news items are structured as follows:\n\nTITLE: <title>\n\nCONTENT: <content>\n\nLINK: <link>\n\nPlease summarize the following news:";
try {
const response = await message(promptSetup + newsInput);
const summary = response.content[0] as { text: string };
return summary.text;
} catch (error) {
console.error(error);
return;
}
}