@@ -824,3 +824,158 @@ def test_evaluate_with_new_model(self):
824824 import os
825825 if os .path .exists (dataset_path ):
826826 os .remove (dataset_path )
827+
828+ def test_must_compute_validation (self ):
829+ """Test that the must_compute parameter is properly validated."""
830+ print ("\n === Testing must_compute validation ===" )
831+
832+ # Test config with both hallucination and completeness
833+ test_config = {
834+ "hallucination" : {
835+ "detector_name" : "default"
836+ },
837+ "completeness" : {
838+ "detector_name" : "default"
839+ }
840+ }
841+ print (f"Test Config: { test_config } " )
842+
843+ # Test valid values
844+ valid_values = ['all_or_none' , 'ignore_failures' ]
845+ print (f"Testing valid must_compute values: { valid_values } " )
846+
847+ for value in valid_values :
848+ print (f"Testing valid must_compute value: { value } " )
849+ detect = Detect (
850+ values_returned = ["context" , "generated_text" ],
851+ api_key = self .api_key ,
852+ config = test_config ,
853+ must_compute = value
854+ )
855+ assert detect .must_compute == value
856+ print (f"✅ Successfully validated must_compute value: { value } " )
857+
858+ # Test invalid string value
859+ invalid_string_value = "invalid_value"
860+ print (f"Testing invalid must_compute string value: { invalid_string_value } " )
861+ try :
862+ Detect (
863+ values_returned = ["context" , "generated_text" ],
864+ api_key = self .api_key ,
865+ config = test_config ,
866+ must_compute = invalid_string_value
867+ )
868+ print ("❌ ERROR: Expected ValueError but none was raised - This should not happen" )
869+ assert False , "Expected ValueError for invalid string value"
870+ except ValueError as e :
871+ print (f"✅ Successfully caught ValueError for invalid string: { str (e )} " )
872+ assert "`must_compute` must be either 'all_or_none' or 'ignore_failures'" in str (e )
873+
874+ # Test non-string value
875+ non_string_value = 123
876+ print (f"Testing non-string must_compute value: { non_string_value } " )
877+ try :
878+ Detect (
879+ values_returned = ["context" , "generated_text" ],
880+ api_key = self .api_key ,
881+ config = test_config ,
882+ must_compute = non_string_value
883+ )
884+ print ("❌ ERROR: Expected ValueError but none was raised - This should not happen" )
885+ assert False , "Expected ValueError for non-string value"
886+ except ValueError as e :
887+ print (f"✅ Successfully caught ValueError for non-string: { str (e )} " )
888+ assert "`must_compute` must be a string value" in str (e )
889+
890+ # Test default value
891+ print ("Testing default must_compute value: default" )
892+ detect_default = Detect (
893+ values_returned = ["context" , "generated_text" ],
894+ api_key = self .api_key ,
895+ config = test_config
896+ )
897+ assert detect_default .must_compute == 'all_or_none'
898+ print (f"✅ Successfully validated default must_compute value: { detect_default .must_compute } " )
899+
900+ print ("🎉 Result: must_compute validation working correctly" )
901+
902+ def test_must_compute_with_actual_service (self ):
903+ """Test must_compute functionality with actual service calls."""
904+ print ("\n === Testing must_compute with actual service ===" )
905+
906+ # Test config with both hallucination and completeness
907+ test_config = {
908+ "hallucination" : {
909+ "detector_name" : "default"
910+ },
911+ "completeness" : {
912+ "detector_name" : "default"
913+ }
914+ }
915+ print (f"Test Config: { test_config } " )
916+
917+ # Test both must_compute values
918+ for must_compute_value in ['all_or_none' , 'ignore_failures' ]:
919+ print (f"\n --- Testing must_compute: { must_compute_value } ---" )
920+
921+ detect = Detect (
922+ values_returned = ["context" , "generated_text" , "user_query" ],
923+ api_key = self .api_key ,
924+ config = test_config ,
925+ must_compute = must_compute_value
926+ )
927+
928+ @detect
929+ def generate_summary (context , query ):
930+ generated_text = f"Summary of { context } based on query: { query } "
931+ return context , generated_text , query
932+
933+ # Test data
934+ context = "Machine learning is a subset of artificial intelligence that enables computers to learn without being explicitly programmed."
935+ query = "What is machine learning?"
936+
937+ print (f"Input Context: { context } " )
938+ print (f"Input Query: { query } " )
939+ print (f"Must Compute: { must_compute_value } " )
940+
941+ try :
942+ # Call the decorated function
943+ context_ret , generated_text , query_ret , result = generate_summary (context , query )
944+
945+ print (f"✅ API Call Successful!" )
946+ print (f"Status Code: { result .status } " )
947+ print (f"Generated Text: { generated_text } " )
948+
949+ # Display response details
950+ if hasattr (result .detect_response , 'hallucination' ):
951+ hallucination = result .detect_response .hallucination
952+ print (f"Hallucination Score: { hallucination .get ('score' , 'N/A' )} " )
953+ print (f"Is Hallucinated: { hallucination .get ('is_hallucinated' , 'N/A' )} " )
954+
955+ if hasattr (result .detect_response , 'completeness' ):
956+ completeness = result .detect_response .completeness
957+ print (f"Completeness Score: { completeness .get ('score' , 'N/A' )} " )
958+
959+ # Show the full response structure
960+ print (f"Response Object Type: { type (result .detect_response )} " )
961+ if hasattr (result .detect_response , '__dict__' ):
962+ print (f"Response Attributes: { list (result .detect_response .__dict__ .keys ())} " )
963+
964+ except Exception as e :
965+ error_message = str (e )
966+ print (f"API Call Result: { error_message } " )
967+ print (f"Error Type: { type (e ).__name__ } " )
968+
969+ # For all_or_none, 503 is expected when services are unavailable
970+ if must_compute_value == 'all_or_none' and '503' in error_message :
971+ print ("✅ Expected behavior: all_or_none returns 503 when services unavailable" )
972+ # For ignore_failures, we expect success or different error handling
973+ elif must_compute_value == 'ignore_failures' :
974+ if '503' in error_message :
975+ print ("❌ Unexpected: ignore_failures should handle service unavailability" )
976+ else :
977+ print ("✅ Expected behavior: ignore_failures handled the error appropriately" )
978+ else :
979+ print (f"❌ Unexpected error for { must_compute_value } : { error_message } " )
980+
981+ print ("\n 🎉 All must_compute service tests completed!" )
0 commit comments