Let’s explore generating topic names for news articles using a generative model, specifically, Azure OpenAI.
Topic generation is a powerful application of NLP that involves creating relevant and coherent content based on a given prompt. In the context of Azure OpenAI prompts, the ability to generate topics is demonstrated using a news headline classification example.
In this code snippet, the task is to categorize a news headline into one of the predefined categories, which are Business, Tech, Politics, Sport, and Entertainment. The news headline, provided as input, is “Trump is ready to contest in Nov 2024 elections.” The code uses the Azure OpenAI API to generate a response that predicts the most appropriate category for the given headline.
The completion engine is configured with specific parameters, such as temperature, max tokens, and penalties for frequency and presence. After generating the response, the code extracts and prints the predicted category from the output.
This example showcases how Azure OpenAI prompts can be utilized for the automatic categorization of news headlines, demonstrating the versatility and effectiveness of NLP in topic-generation tasks:
news_headline=”
Label the following news headline into 1 of the following categories: Business, Tech, Politics, Sport, Entertainment\n\n Headline 1: Trump is ready to contest in nov 2024 elections\nCategory:
“,
response = openai.Completion.create(
engine=model_deployment_name,
prompt= news_headline,
temperature=0,
max_tokens=118,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=None)
index_of_newline=response.choice[0].text.find(‘\n’)
print(‘category:’,response.choices[0].text[:index_of_newline])
Here’s the output:
category: Politics