import json
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI(
base_url="https://kymaapi.com/v1",
api_key="ky-your-api-key"
)
async def generate_article(topic: str) -> dict:
# Step 1: Generate structured outline
outline_resp = await client.chat.completions.create(
model="deepseek-v3",
messages=[{
"role": "user",
"content": f"Create a blog post outline for: {topic}. "
"Return JSON: {{\"title\": str, \"sections\": [str]}}"
}],
response_format={"type": "json_object"},
temperature=0.7
)
outline = json.loads(outline_resp.choices[0].message.content)
# Step 2: Expand each section
sections = []
for section in outline["sections"]:
resp = await client.chat.completions.create(
model="deepseek-v3",
messages=[{"role": "user", "content": f"Write 2 paragraphs for: {section}"}],
temperature=0.8
)
sections.append(resp.choices[0].message.content)
return {"title": outline["title"], "sections": sections}
# Batch multiple articles in parallel
topics = ["AI in healthcare", "Future of remote work", "Climate tech startups"]
articles = asyncio.run(asyncio.gather(*[generate_article(t) for t in topics]))
for a in articles:
print(f"# {a['title']}\n")