Reputation: 21
I have developed a Python script to utilize information collected from a text file as input for the ChatGPT API, specifically using the 3.5-turbo model.
However, I am encountering an issue where I am not consistently receiving the expected output or answers from the API, even when I include preset text before inserting the collected data, such as:
import openai
default_parameters = r'''The answer from ChatGPT must comply with the following requirements ("n" an integer representing each answer number. Each answer headline, subheadline, and body must have the same "n"):--Headline (n) Begin--<Insert here the headline generated by ChatGPT (n)>--Headline (n) End--\n--Subheadline (n) Begin--<Insert here the subheadline generated by ChatGPT>--Subheadline (n) End--\n--Body (n) Begin--<Insert here the body generated by ChatGPT>--Body (n) End--.'''
default_prompt = r'''- Task: Generate 10 'news article' from the subject:'''
question = r'''Most important laws created in the US from 1980 to 2000.'''
prompt_a = {"role": "user", "content": f'''{default_parameters}\n{default_prompt}"""{question}"""'''}
conversation = [prompt_a]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=conversation,
max_tokens=50
)
answer_gpt = response['choices'][0]['message']['content']
print("Answer:", answer_gpt)
Despite setting up the input with a structured prompt and system message, it seems like the model's responses are not consistently aligning with my expectations. How to improve the consistency of outputs and ensure that I receive more accurate and relevant responses from the OpenAI API.
I've even tried incorporating specific formatting instructions like "--<.Insert the answer here.>--" within my prompts, but sometimes the API generates answers in a different format than what I would like.
How can I ensure that I consistently receive answers from the OpenAI API in the specified format I require? Are there any additional techniques or best practices to achieve this consistency?
In my attempts to obtain consistent and desired outputs from the OpenAI API with a specified format, I initially structured my Python script as follows:
I expected that by setting a clear system message and providing a structured prompt, the AI model would consistently generate responses in the specified format. Specifically, I aimed to receive answers that followed a predetermined structure and included the necessary details as per the prompts. For example, I wanted the answers to be in a format like "--<.Insert the answer here.>--" or in a similar structured manner.
However, despite these efforts, I encountered inconsistencies in the responses generated by the API. Sometimes, the answers provided were not in the desired format, and the formatting instructions were not consistently followed. This led to variations in the presentation and structure of the generated answers, which did not align with my expectations.
Response:
--Headline (1) End--
--Subheadline (1) Begin--A Look at the Key Environmental Legislation from 1980 to 2000--Subheadline (1) End--
--Body (1) Begin--During the two decades spanning from 1980 to 2000, the United States saw the enactment of several pivotal environmental laws that left an indelible mark on the nation's commitment to environmental protection. These laws laid the foundation for a more sustainable future and set standards for clean air, clean water, and the preservation of natural ecosystems. In this article, we delve into some of the most important environmental legislation that emerged during this transformative period.--Body (1) End--
--Headline (1) Begin--The Digital Revolution: A Closer Look at the Tech Laws of the 1980s and 1990s--Headline (1) End--
--Subheadline (2) Begin--Exploring the Legal Framework That Shaped the Information Age--Subheadline (2) End--
--Body (2) Begin--The late 20th century was marked by an unprecedented surge in technological innovation, and the United States played a leading role in this digital revolution. To regulate and adapt to this rapid transformation, a series of groundbreaking technology laws were enacted during the 1980s and 1990s. From the birth of the internet to the protection of intellectual property, these laws paved the way for the modern digital landscape we know today.--Body (2) End--
--Headline (3) Begin--Reforming the Criminal Justice System: Landmark Laws from 1980 to 2000--Headline (3) End--
--Subheadline (3) Begin--Examining Key Criminal Justice Reforms That Shaped an Era--Subheadline (3) End--
--Body (1) Begin--The 1980s and 1990s witnessed significant changes in the U.S. criminal justice system. Amidst rising crime rates, lawmakers and policymakers crafted a series of laws aimed at reforming the system, addressing drug epidemics, and improving public safety. This article delves into the most influential criminal justice reforms of that era, exploring their impact on American society and the criminal justice landscape.
--Headline (5) Begin--Healthcare Transformation: Landmark Laws from the Late 20th Century--Headline (5) End--
--Subheadline (5) Begin--Analyzing Key Healthcare Legislation That Shaped Access and Coverage--Subheadline (5) End--
--Body (5) Begin--The late 20th century brought about significant changes in the U.S. healthcare landscape. From the establishment of Medicare's prospective payment system to the passage of the Americans with Disabilities Act, these decades witnessed pivotal healthcare legislation that reshaped access, coverage, and patient rights. This article explores the most important healthcare laws from 1980 to 2000 and their lasting impact on the nation's healthcare system.--Body (4) End--
You can see that the answers identation mismatch a lot, there are missing --Headline (n) Begin--
and other errors too.
Upvotes: 0
Views: 1973
Reputation: 2046
When working with these LLMs, it often works better to give examples than descriptions of what you want it to produce.
This system prompt:
You are a bot that will generate news articles for the user in a structured format. The user will provide you with a topic and the number of articles they want you to generate and you will generate these news article in the format like this one (the number in (1) is the ordinal number of the news article that was provided in the response). Do keep the news articles short (at about 100 words).
--Headline (1) Begin--Empowering Accessibility: The Impact of the Americans with Disabilities Act--Headline (1) End--
--Subheadline (1) Begin--Exploring ADA's Journey: A Beacon of Hope and Equality for Millions, Offering Protection, Accessibility, and Inclusive Opportunities--Subheadline (1) End--
--Body (1) Begin--The Americans with Disabilities Act (ADA), signed into law in 1990, was a groundbreaking legislation that aimed to protect the rights of individuals with disabilities. It prohibited discrimination against people with disabilities in various areas, including employment, public accommodations, transportation, and telecommunications. The ADA played a crucial role in promoting equal opportunities and accessibility for millions of Americans, ensuring that they could participate fully in society.--Body (1) End--
combined with this user prompt
provide 3 articles on the laws passed around offshore drilling
Gives me this answer, which appears to be correctly formatted (no clue if the laws are real or hallucinated):
--Headline (1) Begin--Protecting the Seas: The Impact of Laws Passed Around Offshore Drilling--Headline (1) End--
--Subheadline (1) Begin--A Closer Look at the Laws Aimed at Safeguarding the Environment and Balancing Energy Needs--Subheadline (1) End--
--Body (1) Begin--Over the years, several laws have been passed to regulate and govern offshore drilling activities. These laws aim to strike a balance between meeting energy demands and protecting the environment. One such law is the Outer Continental Shelf Lands Act, which grants the federal government authority over offshore resources. Additionally, the Clean Water Act sets pollution control standards for drilling operations, while the National Environmental Policy Act requires environmental impact assessments. These laws ensure that offshore drilling is conducted responsibly and with consideration for the fragile marine ecosystem.--Body (1) End--
--Headline (2) Begin--The Deepwater Horizon Incident: A Catalyst for Stricter Offshore Drilling Regulations--Headline (2) End--
--Subheadline (2) Begin--Examining the Reforms and Safety Measures Implemented After the Tragic Oil Spill--Subheadline (2) End--
--Body (2) Begin--The Deepwater Horizon incident in 2010 was a wake-up call for the offshore drilling industry. The disaster prompted the enactment of stricter regulations to prevent similar accidents and protect the environment. The Blowout Preventer Systems and Well Control Rule was implemented to enhance the safety of drilling operations, while the Oil Pollution Act of 1990 was amended to increase liability and response requirements for oil spills. These reforms have led to improved safety standards, emergency response capabilities, and increased accountability in the offshore drilling sector.--Body (2) End--
--Headline (3) Begin--Preserving Marine Life: The Role of Marine Mammal Protection Laws in Offshore Drilling--Headline (3) End--
--Subheadline (3) Begin--Understanding the Measures in Place to Mitigate the Impact on Marine Mammals--Subheadline (3) End--
--Body (3) Begin--Offshore drilling activities can have a significant impact on marine mammal populations. To protect these vulnerable species, laws such as the Marine Mammal Protection Act and the Endangered Species Act have been established. These laws require companies to obtain permits and conduct surveys to assess potential impacts on marine mammals. They also mandate the implementation of mitigation measures, such as using acoustic deterrent devices and establishing exclusion zones. These laws play a crucial role in safeguarding marine life and ensuring that offshore drilling is carried out in an environmentally responsible manner.--Body (3) End--
Note: I used gpt-3.5-turbo-0613
since it is better at following instructions in the system message.
This would be the whole thing in code:
import openai
system_message = {
"role": "system",
"content": """
You are a bot that will generate news articles for the user in a structured format. The user will provide you with a topic and the number of articles they want you to generate and you will generate these news article in the format like this one (the number in (1) is the ordinal number of the news article that was provided in the response). Do keep the news articles short (at about 100 words).
--Headline (1) Begin--Empowering Accessibility: The Impact of the Americans with Disabilities Act--Headline (1) End--
--Subheadline (1) Begin--Exploring ADA's Journey: A Beacon of Hope and Equality for Millions, Offering Protection, Accessibility, and Inclusive Opportunities--Subheadline (1) End--
--Body (1) Begin--The Americans with Disabilities Act (ADA), signed into law in 1990, was a groundbreaking legislation that aimed to protect the rights of individuals with disabilities. It prohibited discrimination against people with disabilities in various areas, including employment, public accommodations, transportation, and telecommunications. The ADA played a crucial role in promoting equal opportunities and accessibility for millions of Americans, ensuring that they could participate fully in society.--Body (1) End--
"""
}
user_message = {
"role": "user",
"content": "provide 3 articles on the laws passed around offshore drilling"
}
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[system_message, user_message],
max_tokens=3800
)
answer_gpt = response['choices'][0]['message']['content']
print("Answer:", answer_gpt)
Upvotes: 1