File size: 1,384 Bytes
ea99abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import requests
from typing import Dict, Any

from dotenv import load_dotenv
load_dotenv()

# Timeout in seconds
TIMEOUT = int(os.getenv("REMOTE_SERVICE_TIMEOUT", "300"))

def execute_remote_task(task_name: str, payload: Dict[str, Any]) -> Dict[str, Any]:
    """
    Execute a remote task using the configured remote service.
    
    Args:
        task_name: Name of the task to execute (e.g., 'summarization', 'translation')
        payload: Dictionary containing task-specific parameters
        
    Returns:
        Dictionary containing the task result
        
    Raises:
        requests.RequestException: If there's an error with the remote request
    """
    # Get the endpoint from environment variables
    endpoint = os.getenv(f"REMOTE_ENDPOINT_{task_name.upper()}")
    if not endpoint:
        raise ValueError(f"No endpoint configured for task: {task_name}")
    
    try:
        response = requests.post(
            url=endpoint,
            json={"task": task_name, **payload},
            timeout=TIMEOUT
        )
        response.raise_for_status()
        return response.json()
    except requests.RequestException as e:
        error_msg = f"Error calling remote service for {task_name}: {str(e)}"
        if hasattr(e, 'response') and e.response is not None:
            error_msg += f" - {e.response.text}"
        return {"error": error_msg}