Template
Extract always-on public/portal/UTM plus optional email_sms, directmail, blog, payments, social, and social_ai so new client sites can be bootstrapped from this seed. Refs #1 Refs #2 Co-authored-by: Cursor <cursoragent@cursor.com>
64 lines
1.6 KiB
Python
64 lines
1.6 KiB
Python
"""Ollama client for drafting social posts."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
import requests
|
|
from django.conf import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class OllamaError(RuntimeError):
|
|
pass
|
|
|
|
|
|
def generate_social_post(
|
|
prompt: str,
|
|
*,
|
|
platform: str = "",
|
|
tone: str = "professional, warm, on-brand",
|
|
) -> str:
|
|
"""
|
|
Call the LAN Ollama endpoint (default http://10.0.0.128:11434) to draft copy.
|
|
|
|
Uses the standard Ollama /api/generate HTTP API.
|
|
"""
|
|
base = (settings.OLLAMA_BASE_URL or "").rstrip("/")
|
|
if not base:
|
|
raise OllamaError("OLLAMA_BASE_URL is not configured")
|
|
|
|
system = (
|
|
"You write short social media posts for a local business. "
|
|
f"Tone: {tone}. "
|
|
"Return only the post text, no preamble."
|
|
)
|
|
if platform:
|
|
system += f" Optimize for {platform}."
|
|
|
|
full_prompt = f"{system}\n\nUser request:\n{prompt}"
|
|
|
|
url = f"{base}/api/generate"
|
|
payload = {
|
|
"model": settings.OLLAMA_MODEL,
|
|
"prompt": full_prompt,
|
|
"stream": False,
|
|
}
|
|
try:
|
|
response = requests.post(
|
|
url,
|
|
json=payload,
|
|
timeout=settings.OLLAMA_TIMEOUT_SECONDS,
|
|
)
|
|
response.raise_for_status()
|
|
except requests.RequestException as exc:
|
|
logger.exception("Ollama request failed")
|
|
raise OllamaError(f"Ollama unreachable at {url}: {exc}") from exc
|
|
|
|
data = response.json()
|
|
text = (data.get("response") or "").strip()
|
|
if not text:
|
|
raise OllamaError("Ollama returned empty response")
|
|
return text
|