-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_processor.py
91 lines (74 loc) · 2.95 KB
/
ai_processor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# ai_processor.py
from typing import List
import groq
from models import Article
import asyncio
import logging
class AIProcessor:
def __init__(self, api_key: str):
self.client = groq.Groq(api_key=api_key)
self.logger = logging.getLogger(__name__)
async def generate_summary(self, content: str) -> str:
"""Generate a summary of the article content using Claude."""
try:
prompt = f"""
Please provide a concise summary of the following article.
Focus on the key points and main takeaways. Keep the summary under 200 words.
Article:
{content}
"""
completion = await self.client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model="mixtral-8x7b-32768",
temperature=0.3,
max_tokens=500,
)
return completion.choices[0].message.content
except Exception as e:
self.logger.error(f"Error generating summary: {str(e)}")
return "Error generating summary"
async def extract_keywords(self, content: str) -> List[str]:
"""Extract relevant keywords from the article content."""
try:
prompt = f"""
Please extract 5-7 relevant keywords from the following article content.
Return only the keywords as a comma-separated list.
Article:
{content}
"""
completion = await self.client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model="mixtral-8x7b-32768",
temperature=0.3,
max_tokens=100,
)
keywords = completion.choices[0].message.content.split(",")
return [keyword.strip() for keyword in keywords]
except Exception as e:
self.logger.error(f"Error extracting keywords: {str(e)}")
return []
async def process_article(self, article: Article) -> Article:
"""Process a single article by generating summary and extracting keywords."""
try:
summary = await self.generate_summary(article.content)
keywords = await self.extract_keywords(article.content)
article.summary = summary
article.keywords = keywords
return article
except Exception as e:
self.logger.error(f"Error processing article: {str(e)}")
return article
async def process_articles(self, articles: List[Article]) -> List[Article]:
"""Process multiple articles concurrently."""
tasks = [self.process_article(article) for article in articles]
return await asyncio.gather(*tasks)