This endpoint generates a summary in English for a given text.
In this example, we want to summarize a passage from a news article into its main point.
1. Set up
Install the SDK, if you haven't already.
$ pip install cohere
Next, set up the Cohere client.
import cohere co = cohere.Client(api_key)
2. Create prompt
Store the document you want to summarize into a variable
text ="""Ice cream is a sweetened frozen food typically eaten as a snack or dessert. It may be made from milk or cream and is flavoured with a sweetener, either sugar or an alternative, and a spice, such as cocoa or vanilla, or with fruit such as strawberries or peaches. It can also be made by whisking a flavored cream base and liquid nitrogen together. Food coloring is sometimes added, in addition to stabilizers. The mixture is cooled below the freezing point of water and stirred to incorporate air spaces and to prevent detectable ice crystals from forming. The result is a smooth, semi-solid foam that is solid at very low temperatures (below 2 °C or 35 °F). It becomes more malleable as its temperature increases.The meaning of the name "ice cream" varies from one country to another. In some countries, such as the United States, "ice cream" applies only to a specific variety, and most governments regulate the commercial use of the various terms according to the relative quantities of the main ingredients, notably the amount of cream. Products that do not meet the criteria to be called ice cream are sometimes labelled "frozen dairy dessert" instead. In other countries, such as Italy and Argentina, one word is used fo all variants. Analogues made from dairy alternatives, such as goat's or sheep's milk, or milk substitutes (e.g., soy, cashew, coconut, almond milk or tofu), are available for those who are lactose intolerant, allergic to dairy protein or vegan."""
3. Define model settings
The endpoint has a number of settings you can use to control the kind of output it generates. The full list is available in the API reference, but let’s look at a few:
summarize-medium. Generally, medium models are faster while larger models will perform better.
temperature- Ranges from 0 to 5. Controls the randomness of the output. Higher values tend to generate more creative outcomes, and gives you the opportunity of generating various summaries for the same input text. It also might include more hallucinations. Use a higher value if for example you plan to perform a selection of various summaries afterwards
length- You can choose between
long. Short summaries are roughly up to 2 sentences long,
mediumbetween 3 and 5 and
longmight have more 6 or more sentences.
format- You can choose between
bullet. Paragraph generates a coherent sequence of sentences, while
bulletoutputs the summary in bullet points
additional_command- You can input additional direction for a command.
4. Generate the summary
Call the endpoint via the
co.summarize() method, specifying the prompt and the rest of the model settings.
response = co.summarize( model='summarize-xlarge', length='medium', abstractiveness='medium' ) summary = response.summary