Spanish Prime Minister Felipe Gonzalez predicted yesterday that his country will vote to retain its NATO membership in a forthcoming referendum.

After meeting briefly with President Reagan and holding lengthy talks with Vice President Bush, Gonzalez said:

"The Spanish people have amply demonstrated their maturity over the past few years. I am sure that once again they will take the most sensible decision and will reject any risky adventure."

Gonzalez and his governing Socialist Party last year reversed their initial opposition to membership in the North Atlantic Treaty Organization, which Spain joined in 1982. A national referendum is to be held next year.

The Spanish premier, addressing the Wilson Center, a private study group here, said that a referendum was the only way to heal a divisive public debate over the issue.

During his speech, Gonzalez also alluded to his country's intention to reduce the number of U.S. troops on Spanish soil. "Now we can, in common agreement, start a gradual process of transfer of assignments and missions, without decreasing the level of efficiency of common security," he said.

The Pentagon estimates that just over 9,000 U.S. servicemen are in Spain with levels governed by a 1983 accord which runs out in 1988, but which allows both sides to reopen negotiations. A House delegation returned from Spain in August and recommended that troop levels be reduced, but only if Spain remains in NATO.

Spain is to become a member of the European Community Jan. 1, which Gonzalez said would, sooner or later, have an effect on security.

"It is reasonable to suppose that the community countries will ultimately develop closer cooperation in this sphere," he said. " . . . We wish to do everything possible in order to reinforce Europe's identity in the area of security."