80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
|
|
import os
|
|
|
|
import requests
|
|
|
|
from journal.ai.api_compat import (
|
|
build_text_payload,
|
|
detect_text_endpoint_kind,
|
|
extract_text_response,
|
|
normalize_endpoint_url,
|
|
)
|
|
from journal.core.config import (
|
|
CLOUDAI_API_KEY,
|
|
CLOUDAI_API_URL,
|
|
CLOUDAI_CONNECT_TIMEOUT,
|
|
CLOUDAI_TIMEOUT,
|
|
LLAMA_CPP_MODEL,
|
|
LLAMA_CPP_URL,
|
|
)
|
|
|
|
def get_cloud_ai_response(prompt: str) -> str:
|
|
"""
|
|
Gets a response from the configured AI endpoint.
|
|
|
|
Supports:
|
|
- OpenAI-compatible chat/completions and responses APIs
|
|
- LM Studio native /api/v1/chat
|
|
- Legacy prompt APIs returning {"response": ...}
|
|
"""
|
|
api_key = os.getenv("JOURNAL_CLOUDAI_API_KEY", CLOUDAI_API_KEY).strip()
|
|
raw_api_url = (
|
|
os.getenv("JOURNAL_CLOUDAI_API_URL", CLOUDAI_API_URL).strip()
|
|
or os.getenv("JOURNAL_LLAMA_CPP_URL", "").strip()
|
|
or LLAMA_CPP_URL
|
|
)
|
|
api_url = normalize_endpoint_url(raw_api_url, "/v1/chat/completions")
|
|
model = (
|
|
os.getenv("JOURNAL_CLOUDAI_MODEL", "").strip()
|
|
or os.getenv("JOURNAL_LLAMA_CPP_MODEL", "").strip()
|
|
or LLAMA_CPP_MODEL
|
|
)
|
|
timeout_raw = os.getenv("JOURNAL_CLOUDAI_TIMEOUT", str(CLOUDAI_TIMEOUT)).strip()
|
|
try:
|
|
timeout_seconds = int(timeout_raw)
|
|
except ValueError:
|
|
timeout_seconds = CLOUDAI_TIMEOUT
|
|
if timeout_seconds <= 0:
|
|
timeout_seconds = CLOUDAI_TIMEOUT
|
|
|
|
connect_timeout_raw = os.getenv(
|
|
"JOURNAL_CLOUDAI_CONNECT_TIMEOUT", str(CLOUDAI_CONNECT_TIMEOUT)
|
|
).strip()
|
|
try:
|
|
connect_timeout_seconds = int(connect_timeout_raw)
|
|
except ValueError:
|
|
connect_timeout_seconds = CLOUDAI_CONNECT_TIMEOUT
|
|
if connect_timeout_seconds <= 0:
|
|
connect_timeout_seconds = CLOUDAI_CONNECT_TIMEOUT
|
|
|
|
headers = {"Content-Type": "application/json"}
|
|
if api_key:
|
|
headers["Authorization"] = f"Bearer {api_key}"
|
|
|
|
endpoint_kind = detect_text_endpoint_kind(api_url)
|
|
payload = build_text_payload(prompt, model, endpoint_kind)
|
|
|
|
try:
|
|
response = requests.post(
|
|
api_url,
|
|
headers=headers,
|
|
json=payload,
|
|
timeout=(connect_timeout_seconds, timeout_seconds),
|
|
)
|
|
response.raise_for_status()
|
|
body = response.json()
|
|
text = extract_text_response(body)
|
|
return text or "No response from AI."
|
|
except requests.exceptions.RequestException as e:
|
|
return f"Error communicating with Cloud AI ({api_url}): {e}"
|