File size: 1,740 Bytes
1e91476
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
import requests
from .config import API_TIMEOUT, SCENARIOS, SUPPORTED_LANGUAGES
from streamlit.logger import get_logger

logger = get_logger(__name__)

def check_arguments(model_input: dict) -> None:
    """Check if the input arguments are valid."""

    # Validate the issue
    if model_input["issue"] not in SCENARIOS:
        raise ValueError(f"Invalid issue: {model_input['issue']}")

    # Validate the language
    if model_input["language"] not in SUPPORTED_LANGUAGES:
        raise ValueError(f"Invalid language: {model_input['language']}")

def generate_sim(model_input: dict, endpoint_url: str, endpoint_bearer_token: str) -> dict:
    """Generate a response from the LLM and return the raw completion response."""
    check_arguments(model_input)
    
    # Retrieve the messages history
    messages = model_input['messages']
    
    # Retrieve the temperature and max_tokens from model_input
    temperature = model_input.get("temperature", 0.7)
    max_tokens = model_input.get("max_tokens", 128)
    
    # Prepare the request body
    json_request = {
        "messages": messages,
        "max_tokens": max_tokens,
        "temperature": temperature
    }
    
    # Define headers for the request
    headers = {
        "Authorization": f"Bearer {endpoint_bearer_token}",
        "Content-Type": "application/json",
    }
    
    # Send request to Serving
    response = requests.post(url=endpoint_url, headers=headers, json=json_request, timeout=API_TIMEOUT)

    if response.status_code != 200:
        raise ValueError(f"Error in response: {response.status_code} - {response.text}")
    logger.debug(f"Default response is {response.json()}")
    # Return the raw response as a dictionary
    return response.json()