Text Summarisation with ChatGPT API: A Python Implementation

Text Summarisation with ChatGPT API: A Python Implementation

Introduction

In the world of natural language processing, text summarization is a powerful tool that allows us to condense lengthy documents into concise, meaningful summaries. It is particularly valuable for distilling the essential information from articles, reports, or any form of extended text. In this article, we will explore how to efficiently perform text summarization using OpenAI's ChatGPT API, a state-of-the-art language model that can generate human-like text.

Why Text Summarization Matters

Text summarization serves multiple purposes in today's information-driven world. It can help readers save time by providing quick overviews of long documents. For content creators, it aids in generating concise abstracts, making their work more accessible and digestible. Businesses and researchers use text summarization to sift through vast amounts of text data, extracting key insights efficiently.

Using OpenAI's ChatGPT API

OpenAI's ChatGPT is a versatile language model that can be fine-tuned for various natural language processing tasks. To use it for text summarization, you need to set up your Python environment and make API requests to the GPT-3 model.

STEP 1 - Create API Key

To create the API key, follow key steps below:

  1. Navigate to https://platform.openai.com/account/api-keys
  2. Click on 'Create new secret key' button
  3. Once you have created an API Key, copy and save it somewhere safe. For security reasons,?you won't be able to view it again?through your OpenAI account.

ChatGPT API Key

STEP 2 - Python Code (Text Summarisation & Sentiment Analysis)

Let's walk through a Python code example of how to utilize the ChatGPT API for text summarization and sentiment analysis. Ensure that you have the openai Python package installed, which you can install using "pip install openai"

#pip install openai

import openai        
def generate_summary_and_sentiment(input_text, api_key, max_tokens=50):
    
    # Specify the summarization prompt
    summarization_prompt = f"Summarize the following text: '{input_text}'"
    
    # Specify the sentiment analysis prompt
    sentiment_prompt = f"Analyze the sentiment of the following text: '{input_text}'"

    # Request the summarization using ChatGPT
    summarization_response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=summarization_prompt,
        max_tokens=max_tokens,
        api_key=api_key
    )

    # Request sentiment analysis using ChatGPT
    sentiment_response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=sentiment_prompt,
        max_tokens=max_tokens,
        api_key=api_key
    )

    # Extract and return the summary and sentiment analysis
    summary = summarization_response.choices[0].text
    sentiment = sentiment_response.choices[0].text
    
    return {'summary': summary, 'sentiment': sentiment}
        
YOUR_API_KEY = '<YOUR SECRET KEY>'         
text_to_summarise = "Your input text goes here."


result = generate_summary_and_sentiment(text_to_summarise,YOUR_API_KEY)

print("Summary:", result['summary'])
print("Sentiment:", result['sentiment'])

        

Summary of the code


Code

  1. Import the openai library line is an import statement that brings the "openai" module into our Python program. It allows us to use functions and features provided by OpenAI.
  2. Define a function named generate_summary_and_sentiment that takes three parameters: input_text (the text to be summarized), api_key (your OpenAI GPT-3 API key), and max_tokens (the maximum number of tokens for the summary). Functions are like mini-programs that you can use to perform specific tasks.
  3. Create a summarization prompt by using an f-string to include the input_text in the prompt. This is the instruction for ChatGPT. These lines send a request to OpenAI's ChatGPT to generate a text summary.
  4. Make an API request using openai.Completion.create to generate the summary. The engine parameter specifies the GPT-3 engine, prompt is the summarization instruction, and max_tokens controls the length of the summary. The api_key parameter is used to authenticate with the API.
  5. Extract the generated summary from the API response and store it in the summary variable.
  6. Return the generated summary as the result of the generate_summary function.

Function above will allow you to easily generate text summaries and sentiment analysis by providing the input text, your API key, and optionally specifying the maximum number of tokens for the summary.


Function

Generate a text summary using OpenAI's ChatGPT API.

Args:

input_text (str): The text you want to summarize.

api_key (str): Your OpenAI GPT-3 API key.

max_tokens (int): The maximum number of tokens for the summary.

Returns:

str: The generated text summary.


Understanding the Results

It's important to note that while ChatGPT can generate impressive summaries, the quality and length of the summaries depend on your specific request and the input text. You may need to fine-tune your prompt, adjust the max_tokens parameter, and experiment to achieve the desired level of summarization.

It comes at a cost

Using the ChatGPT API involves charges based on a pricing model that's structured per 1000 tokens. You can find further information at this link https://openai.com/pricing#language-models.

OpenAI cost calculation example - "Multiple models, each with different capabilities and price points. Prices are per 1,000 tokens. You can think of tokens as pieces of words, where 1,000 tokens is about 750 words. This paragraph is 35 tokens."

Conclusion

Text summarization is a valuable technique in many domains, from content creation to data analysis. OpenAI's ChatGPT API empowers developers to perform efficient and accurate text summarization tasks. By following the code example provided in this article, you can harness the power of state-of-the-art language models to extract essential information from lengthy documents, making your work more accessible and efficient.

Julien Salinas

Fondateur/CTO chez NLP Cloud. Ingénieur full-stack. Entraineur de boxe fran?aise.

3 个月

Thanks for the how-to! If you are looking for an alternative to OpenAI, I recommend our NLP Cloud summarization API: https://nlpcloud.com/nlp-text-summarization-api.html It is based on LLaMA 3 405B and other advanced LLMs that compete with GPT-4.

回复

要查看或添加评论,请登录

Eshan Sharma的更多文章

  • The Role of Compliance in Responsible Machine Learning for Loan Underwriting

    The Role of Compliance in Responsible Machine Learning for Loan Underwriting

    Introduction Machine learning (ML) is rapidly transforming the lending industry, including the loan underwriting…

  • (NLP) Python Libraries - A Comprehensive Guide

    (NLP) Python Libraries - A Comprehensive Guide

    Natural Language Processing (NLP) is a fascinating field that focuses on the interaction between humans and computers…

  • Behind the Data Curtain - Hypothesis Testing

    Behind the Data Curtain - Hypothesis Testing

    Introduction In the realm of statistics, hypothesis testing stands as a cornerstone for making informed decisions and…

    1 条评论
  • Topic Modeling

    Topic Modeling

    Introduction: Businesses and professionals are continually looking for effective ways to make sense of massive amounts…

    1 条评论

社区洞察

其他会员也浏览了