31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
|
|
import requests
|
|
import os
|
|
from journal.core.config import CLOUDAI_API_KEY, CLOUDAI_API_URL, CLOUDAI_TIMEOUT
|
|
|
|
def get_cloud_ai_response(prompt: str) -> str:
|
|
"""
|
|
Gets a response from the cloud AI service.
|
|
"""
|
|
api_key = os.getenv("JOURNAL_CLOUDAI_API_KEY", CLOUDAI_API_KEY).strip()
|
|
api_url = os.getenv("JOURNAL_CLOUDAI_API_URL", CLOUDAI_API_URL).strip()
|
|
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
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {api_key}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
payload = {"prompt": prompt}
|
|
try:
|
|
response = requests.post(api_url, headers=headers, json=payload, timeout=timeout_seconds)
|
|
response.raise_for_status()
|
|
return response.json().get("response", "No response from AI.")
|
|
except requests.exceptions.RequestException as e:
|
|
return f"Error communicating with Cloud AI: {e}"
|