Add article summarization via external API integration

Introduced a new `summarize_to_words` function to summarize articles using an external API. Integrated it into `smd_detail_article` to return summarized article content instead of the full text. Updated header key capitalization for consistency.
This commit is contained in:
ThomasTaroni 2025-06-21 21:18:32 +02:00
parent 3c1f8a2c25
commit 1d5aeb1644

View file

@ -26,18 +26,47 @@ logger = logging.getLogger(__name__)
# Initialize FastMCP server
mcp = FastMCP("SMD Researcher", host="0.0.0.0", port=8000, timeout_keep_alive=720)
async def summarize_to_words(article: dict, target_word_count: int = 1000) -> str:
url = f"https://maas.ai-2.kvant.cloud/engines/{os.getenv('SWISSDOX_SUMMARIZING_MODEL', '')}/chat/completions"
headers = {
"x-litellm-api-key": f"Bearer {os.getenv('SWISSDOX_SUMMARIZING_MODEL_APIKEY', '')}",
"Content-type": "application/json",
}
payload = {
"model": {os.getenv('SWISSDOX_SUMMARIZING_MODEL', '')},
"messages": [
{
"role": "text summarizer",
"content": f"You are summarizing the user input to a maximum of {target_word_count}"
},
{
"role": "user",
"content": f"{str(article)}"
}
]
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=payload) as response:
if response.status == 200:
return await response.json()
else:
return await response.text()
async def smd_detail_article(article_id):
url = f"https://api.swissdox.ch/api/documents/{article_id}"
headers = {
"authorization": f"Bearer {os.getenv('SWISSDOX_BEARER_TOKEN', '')}",
"content-type": "application/json",
"Authorization": f"Bearer {os.getenv('SWISSDOX_BEARER_TOKEN', '')}",
"Content-type": "application/json",
}
payload = {"filters": [], "pagination": {"pageSize": 1, "currentPage": 1}}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=payload) as response:
if response.status == 200:
return await response.json() # JSON asynchron lesen
data = await response.json()
summarized_content = await summarize_to_words(data, target_word_count=10000)
return summarized_content
else:
return {
"message": await response.text(),