2026-03-19 10:51:10 +00:00
|
|
|
import asyncio
|
|
|
|
|
import anthropic
|
|
|
|
|
|
|
|
|
|
from ..config import settings
|
|
|
|
|
|
|
|
|
|
|
2026-03-21 20:47:15 +00:00
|
|
|
def _create_anthropic_client() -> anthropic.Anthropic:
|
2026-03-19 10:51:10 +00:00
|
|
|
return anthropic.Anthropic(api_key=settings.anthropic_api_key)
|
|
|
|
|
|
|
|
|
|
|
2026-03-21 20:47:15 +00:00
|
|
|
def _create_system_prompt_summarise_text(
|
|
|
|
|
complexity_level: str,
|
|
|
|
|
from_language: str,
|
|
|
|
|
to_language: str,
|
|
|
|
|
length_preference="200-400 words",
|
|
|
|
|
) -> str:
|
|
|
|
|
return (
|
|
|
|
|
f"You are a language learning content creator.\n"
|
|
|
|
|
f"The user will provide input, you will generate an engaging realistic summary text in {to_language} "
|
|
|
|
|
f"at {complexity_level} proficiency level (CEFR scale).\n\n"
|
|
|
|
|
f"The text you generate will:\n"
|
|
|
|
|
f"- Contain ONLY the generated text in {to_language}.\n"
|
|
|
|
|
f"- Be appropriate for a {complexity_level} {to_language} speaker.\n"
|
|
|
|
|
f"- Never generate inappropriate (hateful, sexual, violent) content. It is preferable to return no text than to generate such content.\n"
|
|
|
|
|
f"- Speak directly to the reader/listener, adopting the tone and style of a semi-formal news reporter or podcaster.\n"
|
|
|
|
|
f"- Where appropriate (fluency level, content), use a small number of idiomatic expressions.\n"
|
|
|
|
|
f"- Be formatted in markdown with paragraphs and line breaks.\n"
|
|
|
|
|
f"- Be {length_preference} long.\n"
|
|
|
|
|
f"- Be inspired by the following source material "
|
|
|
|
|
f"(but written originally in {from_language}):\n\n"
|
|
|
|
|
)
|
2026-03-19 10:51:10 +00:00
|
|
|
|
2026-03-21 20:47:15 +00:00
|
|
|
|
|
|
|
|
def _create_prompt_summarise_text(
|
|
|
|
|
source_material: str,
|
|
|
|
|
) -> str:
|
|
|
|
|
return (
|
|
|
|
|
f"Source material follows: \n\n"
|
|
|
|
|
f"{source_material}"
|
|
|
|
|
)
|
2026-03-19 10:51:10 +00:00
|
|
|
|
|
|
|
|
|
2026-03-21 20:47:15 +00:00
|
|
|
async def generate_summary_text(
|
|
|
|
|
content_to_summarise: str,
|
|
|
|
|
complexity_level: str,
|
|
|
|
|
from_language: str,
|
|
|
|
|
to_language: str,
|
|
|
|
|
length_preference="200-400 words",) -> str:
|
|
|
|
|
"""Generate text using Anthropic."""
|
2026-03-19 10:51:10 +00:00
|
|
|
def _call() -> str:
|
2026-03-21 20:47:15 +00:00
|
|
|
client = _create_anthropic_client()
|
2026-03-19 10:51:10 +00:00
|
|
|
message = client.messages.create(
|
|
|
|
|
model="claude-sonnet-4-6",
|
|
|
|
|
max_tokens=1024,
|
|
|
|
|
messages=[
|
2026-03-21 20:47:15 +00:00
|
|
|
{
|
|
|
|
|
"role": "system",
|
|
|
|
|
"content": _create_system_prompt_summarise_text(
|
|
|
|
|
complexity_level=complexity_level,
|
|
|
|
|
from_language=from_language,
|
|
|
|
|
to_language=to_language,
|
|
|
|
|
length_preference=length_preference,
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": _create_prompt_summarise_text(
|
|
|
|
|
content_to_summarise
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-03-19 10:51:10 +00:00
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
return message.content[0].text
|
|
|
|
|
|
|
|
|
|
return await asyncio.to_thread(_call)
|