HALF MOON BAY, Calif. — Ashton B. Carter, the former secretary of defense, understands why employees of technology companies have opposed partnerships and contracts with the United States government when it comes to artificial intelligence.

“My first reaction was ‘good on you’ because you are thinking morally and you are thinking about whether what you are doing is right or wrong,” Mr. Carter said at The New York Times New Work Summit in Half Moon Bay, Calif.

The sentiment comes at a time of strained relationships between Silicon Valley technology behemoths and the United States government.