The scene is a Boston bar, where the regulars hoot, back-slap and dissolve into giggles when somebody makes a joke about sex. Diane Chambers (Shelley Long), a former graduate student working as a waitress, once again is offended. She begins to lecture the customers in her most professorial tone.

DIANE: Ah yes, unlimited sex. The adult male version of owning a candy store. But once you've consumed as much sex as you want for as long as you want, what do you do then?

(The crowd is silent.)

NORM (a regular): I'd help the poor.

So goes the dialogue in ''Cheers,'' a half-hour NBC television comedy that is one of the network's few success stories this season. After months of dismally low ratings last year, the show has managed this season to gather a respectable audience. And it has done so, critics say, with good acting and witty writing, the type of quality programming that Grant Tinker, the president of NBC, has said he wants on the network.

Won Five Emmy Awards

The show was a particular point of pride for NBC executives this fall when it won five Emmy Awards, including the categories for outstanding comedy, for writing and for direction. Miss Long received the award for outstanding actress in a comedy series for her portrayal of Diane.

'' 'Cheers' is a very important comedy for us,'' said Brandon Tartikoff, the president of the NBC entertainment division. ''It is classy, sophisticated and for adults. We never for a second doubted that we would renew it for this year.''

The show revolves around the inhabitants of the bar, including the owner, a retired baseball pitcher, and Diane, the snobbish but vulnerable former graduate student.

So far this season, ''Cheers'' has scored a modest success in the ratings. But to NBC executives, who have seen program after program go down to defeat in recent years, ''Cheers'' is a major victory. (The shelves of the NBC gift shop in Rockefeller Center are stocked with ''Cheers'' T-shirts these days.) Has Competed Well

The program surpassed ABC's entry in the 9:30 P.M. time period, the now-canceled comedy, ''It's Not Easy,'' and has competed well with one of the most popular shows on prime-time television, CBS's ''Simon and Simon.'' In the first seven weeks of the season it has averaged a 17.6 rating and a 27 share of the audience, according to the A. C. Nielsen Company.

By comparison, eight of NBC's nine new prime-time shows have averaged at or below an 11 rating and an 18 share and most have finished third in their time periods. ''Simon and Simon'' received a 24.3 rating and a 37 share. A rating point represents one percent of all the households with televisions in the United States, or 838,000 homes. A share is the percent of all televisions turned on at a specific time.

Another point of pride for NBC is that the dialogue on the show at times reaches a sophistication that is rare on network television. Jokes have included references to Shakespeare, John Donne and Spinoza. Among other affectations, Diane has the unfortunate habit of dropping phrases in French.

''John Cheever is pretty small pommes de terre,'' she offers as other bar inhabitants wince. Developed by Three Men

The show was created and developed by three men who have had considerable experience with award-winning television programs, such as ''M*A*S*H'' and ''Taxi'' - two brothers, Glen and Les Charles, and James Burrows, who directs the program.

''We wanted to create a show around a Katharine Hepburn-Spencer Tracy-type relationship,'' Mr. Burrows said in a recent telephone interview, referring to the characters of the bar owner, Sam Malone, and Diane. ''She is uptown, he is downtown.''

Throughout the program's development, Glen Charles said, the creators attempted to draw ideas for the set, the characters and the dialogue from reality. Glen Charles traveled to Boston to find a bar that could be used as a model for the set. They settled on an establishment named The Bull & Finch. In the program, it took the name ''Cheers.''

Rhea Perlman, the actress in ''Cheers'' who plays a tiny, sharp- tongued waitress named Carla Tortelli, was sent to The Bull & Finch to watch the bar in operation. Ted Danson, who plays Sam Malone, the handsome, ex-alcoholic bar owner, spent two weeks attending a bartending school in Burbank, Calif., preparing for his part. The character of Norm, a bloated, unemployed accountant on a regimen of one beer every half hour, is based on a real character Les Charles said he encountered when he worked as a bartender in college.

The three creators of the show also visited bars around Los Angeles, picking up bits of conversation that would later be used in the show. ''Cheers'' dialogue that was taken from real bars, included a discussion of which is ''the sweatiest movie ever made,'' and what is the best flavor of canned soup.

photo of Shelley Long