Spaces:
Sleeping
Sleeping
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() | |