def count_words(string):
words = string.split()
return len(words)
Using gpt-4-0613
def get_summary(text):
try:
# Making a request to the chat API endpoint
response = openai.chat.completions.create(
model=“gpt-4-0613”,
messages=[
{“role”: “system”, “content”: “You are a helpful assistant.”},
{“role”: “user”, “content”: f"Summarize the following text in under 50 words: {text}"}
]
)
Could you try to write a summary of the wizard of oz in 50 words or less?
assistant:
Kansas girl Dorothy is whisked away by a tornado to the magical land of Oz. With her dog Toto and friends Scarecrow, Tin Man, and Cowardly Lion, she follows the Yellow Brick Road to seek the Wizard’s help to return home, confronts the Wicked Witch, and learns the power of self-reliance.
if < 50, great!
else:
user:
Unfortunately, that’s 51 words. Can we optimize it?
assistant:
Kansas girl Dorothy, with Toto, meets Scarecrow, Tin Man, Lion in magical Oz. They seek the Wizard, confront a witch, and discover inner strength on the Yellow Brick Road.
In summary: sometimes things work out in a single go. Sometimes, you need to allow the AI to refine the result.
Thank you!
Your suggestion looks like it is using the web interface of ChatGPT, correct? I am hoping to integrate the word limitations directly inside my Python code for the API.
I used the api playground with gpt-4-1106-preview i think
in the if not escalation your message structure would look something like this:
messages = [
{“role”: “system”, “content”: “You are a helpful assistant.”},
{“role”: “user”, “content”: f"Summarize the following text in under 50 words: {text}"}
{"role": "assistant", previous_output}
{"role": "user", "Unfortunately, that’s " + word_count + " words. Can we optimize it?"}
]
I think what is going on here is that you are triggering a particular part of finely-tuned behavior, where a “summary”, when mentioned by name, results in a particular type of output by the AI model.
The AI, dealing internally with tokenized parts of language, doesn’t have a full perception of words as entities. Planning to end at a particular point, without going back to edit your work, requires preplanning of the presentation of contents, which is another high bar for the AI.
I will provide you with a method for success.
system
You are a helpful AI writing assistant.
user
Document:
{document_text}
-–
AI Task:
From the text above, create a five sentence overview of the topic and contents. Each sentence of the digest shall be about 10 words long, and shall progress the presentation of information extracted from the text logically, so that the final response product can arrive at 50 words total.
No other chat is desired, just the abstract of the document.
NASA’s James Webb Space Telescope captured detailed images of Uranus. The telescope revealed Uranus’ rings, moons, and dynamic atmospheric features. Infrared views show a seasonal polar cap and various bright storms. Webb’s data will help understand Uranus’ extreme seasons and atmospheric phenomena. These insights aid the study of exoplanets and our solar system.
Chimpanzees in Ivory Coast exhibit unique tool-use filmed by researchers. They create special water-dipping sticks with chewed, brush-like tips. These tools are specifically designed for drinking, a behavior not seen before. An absorption experiment confirmed the efficiency of the longer brush tips. This “drinking culture” aids survival in dry conditions for this endangered sub-species.
Word Count: 54
Modify sentence instruction from 10 words to 9 words if the target is below instead of around 50 words.