Summarize text using openai-api?

Hello - i try to cretae a summary of a text like the Earth description on wikipedia -

using the following code:

   model="text-davinci-003",
    prompt=wPrompt,
    max_tokens=1024,
    temperature=0.5
  )
  print(resp)

But it seems i don´t get a summary.
It seems that i only get more or less the same text back as a result.

What i am doing here wrong?
How can i get a summary of some longer text?

Welcome to the forum.

Are you specifically asking it to summarize in your prompt?

The full prompt is this:

wPrompt = “”"
Make a summary of the following text:

        Earth is the third planet from the Sun and the only astronomical object known to harbor life. This is enabled by Earth being a water world, the only one in the Solar System sustaining liquid surface water. Almost all of Earth's water is contained in its global ocean, covering 70.8% of Earth's surface. The remaining 29.2% of Earth's surface is land, most of which is located in the form of continental landmasses within one hemisphere, Earth's land hemisphere. Most of Earth's land is somewhat humid and covered by vegetation, while large sheets of ice at Earth's polar deserts retain more water than Earth's groundwater, lakes, rivers and atmospheric water together. Earth's land is part of Earth's crust, consisting of several slowly moving tectonic plates, which interact to produce mountain ranges, volcanoes, and earthquakes. Inside Earth's crust is a liquid outer core that generates the magnetosphere, deflecting most of the destructive solar winds and cosmic radiation.

Earth has a dynamic atmosphere, which sustains Earth’s surface conditions and protects it from most meteoroids and UV-light at entry. It has a composition of primarily nitrogen and oxygen. Water vapor is widely present in the atmosphere, forming clouds that cover most of the planet. The water vapor acts as a greenhouse gas and, together with other greenhouse gases in the atmosphere, particularly carbon dioxide (CO2), creates the conditions for both liquid surface water and water vapor to persist via the capturing of energy from the Sun’s light. This process maintains the current average surface temperature of 14.76 °C, at which water is liquid under atmospheric pressure. Differences in the amount of captured energy between geographic regions (as with the equatorial region receiving more sunlight than the polar regions) drive atmospheric and ocean currents, producing a global climate system with different climate regions, and a range of weather phenomena such as precipitation, allowing components such as nitrogen to cycle.

Earth is rounded into an ellipsoid with a circumference of about 40,000 km. It is the densest planet in the Solar System. Of the four rocky planets, it is the largest and most massive. Earth is about eight light-minutes away from the Sun and orbits it, taking a year (about 365.25 days) to complete one revolution. Earth rotates around its own axis in slightly less than a day (in about 23 hours and 56 minutes). Earth’s axis of rotation is tilted with respect to the perpendicular to its orbital plane around the Sun, producing seasons. Earth is orbited by one permanent natural satellite, the Moon, which orbits Earth at 384,400 km (1.28 light seconds) and is roughly a quarter as wide as Earth. Through tidal locking, the Moon always faces Earth with the same side, which causes tides, stabilizes Earth’s axis, and gradually slows its rotation.

Earth, like most other bodies in the Solar System, formed 4.5 billion years ago from gas in the early Solar System. During the first billion years of Earth’s history, the ocean formed and then life developed within it. Life spread globally and has been altering Earth’s atmosphere and surface, leading to the Great Oxidation Event two billion years ago. Humans emerged 300,000 years ago in Africa and have spread across every continent on Earth with the exception of Antarctica. Humans depend on Earth’s biosphere and natural resources for their survival, but have increasingly impacted the planet’s environment. Humanity’s current impact on Earth’s climate and biosphere is unsustainable, threatening the livelihood of humans and many other forms of life, and causing widespread extinctions.[24]

        """

And this is the summary:

“\nEarth is the third planet from the Sun and the only known astronomical object to harbor life. It is a water world,
with 70.8% of its surface covered by oceans and 29.2% by land. Its atmosphere, composed of nitrogen and oxygen, is able to sustain Earth’s surface conditions and protect it from solar winds and cosmic radiation. Earth is round, with a circumference of 40,000 km, and is the densest planet in the Solar System. It is orbited by one permanent natural satellite, the Moon, and rotates around its axis in 23 hours and 56 minutes. It formed 4.5 billion years ago and has been home to life for the past 3 billion years. Humans have spread across every continent on Earth and are now impacting the planet’s environment in an unsustainable way, threatening the livelihood of humans and many other forms of life.”,

Try that…

You might try raising temp a bit higher… though you have to worry more about hallucinations…

ok thanks - but generally the handling using this code

  resp = openai.Completion.create(
    model="text-davinci-003",
    prompt=wPrompt,
    max_tokens=1024,
    temperature=0.5
  )

So openai.Completion and as model “text-davinci-003” is correct generally?
(i was not sure about that cause there are so much options and models in openai…)

and regarding the temperatur -

i thought lower temperatur-setting will make the results more focused - wouldn´t this be good in my case?

You can try with different models. GPT-4 will be better likely - but also more expensive.

If you can get good output with gpt-turbo, you can get it really cheap.

Lower temperature can also cause it to not be as creative - ie sound like a wikipedia page.

Let us know how it goes and if you need more help.

You can find more on all the models here

Hello - can i see somewhere which models i can use for the Completion endpoint?

When i try to use this model “gpt-3.5-turbo”
i get this error

openai.error.InvalidRequestError: This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?

gpt-3.5-turbo and gpt-4 run on the chat endpoint

So you’d adjust it to something like

resp = openai.ChatCompletion.create(
    model="text-davinci-003",
    messages=[{"role":"user", "content":wPrompt}],
    max_tokens=1024,
    temperature=0.5
  )

also note that the prompt field is changed into a messages field

1 Like

Hello - @smuzani -
I think this was what i need
(you only used the wrong model in your response)

This code seems to work for me:

    resp = openai.ChatCompletion.create(
      model = "gpt-3.5-turbo",
      messages=[{"role":"user", "content":q}],
      temperature = 0.5,
      max_tokens = 1024
    )

What is still not clear for me is why i get different results when using the online-chat:

The provided text outlines the features and services of "2% Realty West Coast." It covers:

* Contact details: Phone number, email, and address.
* Areas served: Vancouver, Langley, Fraser Valley, and surrounding regions.
* Services: Home buying and selling guides, VIP buyer program, free home evaluation, 2% commission video, insights, and a video blog.
* Commission savings: A comparison of potential savings using their 2% commission structure.
* Key offerings: Full real estate agent services, Realtor.ca exposure, comprehensive marketing, real estate agent representation, in-home consultation.
* Location: Address, contact information, and privacy policy.
* Mission: The agency's mission statement.
* Copyright: Copyright information.
* Website design: Credit for the website's design.

Overall, the text emphasizes the agency's unique 2% commission, extensive services, and mission-driven approach in the real estate industry.
```.

And when use with exactly the same question the gpt-3.5-turbo model i get this summary:


  • 2% Realty West Coast offers real estate agent services in Vancouver, Langley, and the Fraser Valley.
  • They provide comprehensive marketing and exposure for properties through social media and their website.
  • They offer real estate agent representation and assistance with paperwork.
  • They offer free in-home consultations to understand clients’ expectations and requirements.
  • They charge a 2% commission, which can result in significant savings for sellers compared to traditional commissions.
  • They emphasize their commitment to giving back to the community.
  • They have a mission statement and testimonials from satisfied clients.
  • They have a video blog and provide insights on the real estate industry.
  • They have a team of sales associates and offer opportunities to join their team.
  • They provide contact information for inquiries and offer a free home evaluation service.

Why are the results so different when using online vs. the api?

ChatGPT (the site) is designed to be very newbie friendly, while the API (gpt-3.5-turbo) is designed to be more steerable and suited for a wide range of applications.

The API will give very neutral answers, often answering it exactly. While the site tends to give verbose answers. It has a habit of repeating what you ask it, probably to guide itself. If you give it a statement like “this is my code, this are the logs”, it tends to propose solutions. It’s more likely to give a disclaimer that it’s not human and such.

You can guide the API to be more like the web version, using the system messages. Though I suspect there might be a bit more hardcoded changes in the web version to prevent some kinds of prompt attacks.

2 Likes

Hello developer, @smuzani @smuzani @rapid1898
I got this err

--------------------------------------------------------------------------- APIRemovedInV1 Traceback (most recent call last) /tmp/ipykernel_799/2644331792.py in <cell line: 11>() 9 10 # Use the OpenAI API for summarization ---> 11 response = openai.Completion.create( 12 engine="text-davinci-003", # You may use a different engine based on your subscription 13 prompt=text_to_summarize, /usr/local/lib/python3.10/dist-packages/openai/lib/_old_api.py in __call__(self, *_args, **_kwargs) 37 38 def __call__(self, *_args: Any, **_kwargs: Any) -> Any: ---> 39 raise APIRemovedInV1(symbol=self._symbol) 40 41 APIRemovedInV1:

this is my code

import openai

Set your OpenAI API key

openai.api_key = ‘■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■elCxRySX’

Example text to be summarized

text_to_summarize = “”“Older adults form a fast-increasing proportion of the world population. However, gains in increasing quantity of life have not been accompanied by similar gains in quality of life. Older people frequently experience frailty, memory problems, and chronic diseases including cardiovascular disease (CVD) and neurodegenerative diseases. Recent trials have demonstrated the efficacy of anti-hypertensive therapy in older populations but failed to show benefits for aspirin. Statins clearly reduce CVD events in middle-aged populations. There seems to be evidence that the effect is similar in primary prevention older populations based on meta-analyses mainly from sub-groups in large trials, but this becomes less clear with increasing age. However, given differences in drug metabolism and possibly efficacy, competing co-morbidities, their effects on mortality, disability, and dementia in this age group remain to be determined.
“””

Use the OpenAI API for summarization

response = openai.Completion.create(
engine=“text-davinci-003”, # You may use a different engine based on your subscription
prompt=text_to_summarize,
max_tokens=150,
temperature=0.8
)

Extract the summarized text from the API response

summary = response[‘choices’][0][‘text’]

Print the summary

print(summary)

That doesn’t appear to be “your code”. Looks like something an AI wrote using obsolete methods and models.

This is your code:

from openai import OpenAI
client = OpenAI(api_key="sk-12345")  # empty = env variable OPENAI_API_KEY

text = """Older adults form a fast-increasing proportion of the world population. However, gains in increasing quantity of life have not been accompanied by similar gains in quality of life. Older people frequently experience frailty, memory problems, and chronic diseases including cardiovascular disease (CVD) and neurodegenerative diseases. Recent trials have demonstrated the efficacy of anti-hypertensive therapy in older populations but failed to show benefits for aspirin. Statins clearly reduce CVD events in middle-aged populations. There seems to be evidence that the effect is similar in primary prevention older populations based on meta-analyses mainly from sub-groups in large trials, but this becomes less clear with increasing age. However, given differences in drug metabolism and possibly efficacy, competing co-morbidities, their effects on mortality, disability, and dementia in this age group remain to be determined."""
system = [{"role": "system", "content": "You are Summary AI."}]
user = [{"role": "user", "content": f"Summarize this briefly:\n\n{text}"}]
chat_history = []  # past user and assistant turns, for AI memory

chat_completion = client.chat.completions.create(
  messages = system + chat_history + user,
  model="gpt-3.5-turbo",
  max_tokens=500, top_p=0.9,
  )
print(chat_completion.choices[0].message.content)