def send_question_to_rest_api()

in evaluations/generate_evaluation_input.py [0:0]


def send_question_to_rest_api(uri, function_key, question, conversation_id):
    """
    Sends the question to the REST API endpoint and returns its streaming text response,
    while extracting the conversation ID from the first chunk.
    
    Args:
        uri (str): The API endpoint URL.
        function_key (str): The API access key.
        question (str): The question to process.
        conversation_id (str): The conversation identifier to send (may be empty).
    
    Returns:
        tuple: (extracted_conversation_id (str or None), complete text response (str))
    """
    headers = {
        "x-functions-key": function_key,
        "Content-Type": "application/json"
    }
    payload = {
        "conversation_id": conversation_id,
        "question": question
    }
    try:
        response = requests.post(uri, headers=headers, json=payload, stream=True)
        response.raise_for_status()
        result_text = ""
        extracted_conv_id = None
        for chunk in response.iter_lines(decode_unicode=True):
            if chunk:
                # Try to extract the conversation ID from the first non-empty chunk.
                if extracted_conv_id is None:
                    extracted_conv_id, chunk = extract_conversation_id_from_chunk(chunk)
                result_text += chunk + "\n"
        return extracted_conv_id, result_text.strip()
    except Exception as e:
        error_msg = f"Error: {str(e)}"
        return None, error_msg