Open In App

Text Manipulation using OpenAI

Last Updated : 08 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Open AI is a leading organization in the field of Artificial Intelligence and Machine Learning, they have provided the developers with state-of-the-art innovations like ChatGPT, WhisperAI, DALL-E, and many more to work on the vast unstructured data available. For text manipulation, OpenAI has compiled a Completions model which helps you to generate new text data, fill masks in strings, carry conversations, translate, and summarize. The completion module uses the power of GPT-3 to perform these tasks and give out fascinating results. In today’s article, we will be going through this Completions module and see how one can use this in Python.

Steps to perform text manipulation using OpenAI

Now let’s explore how one can use GPT-3 to manipulate text data

Step 1: Install the Openai library in your python environment

!pip install openai

Step 2: Import the openai library and assign the API key to openai environment

Python3




# import the library
import openai
# assign the API key to the environment by replacing
# API_KEY with your generated key
openai.api_key = "API_KEY"


If you do not have an API key, then log in to your OpenAI account after creating one. After logging in, select Personal from the top-right menu, and then select “View API keys”. A page containing API keys is displayed, and the button “Create new secret key” is visible. A secret key is generated when you click on that, copy it and save it somewhere else because it will be needed in further steps.

Step 3: Define a function to perform text manipulation using OpenAI API in python

Python3




# function that takes in string argument as parameter
def comp(PROMPT, MaxToken=50, outputs=3):
    # using OpenAI's Completion module that helps perform
    # text manipulations
    response = openai.Completion.create(
        # model name used here is text-davinci-003
        # there are many other models available under the 
        # umbrella of GPT-3
        model="text-davinci-003",
        # passing the user input 
        prompt=PROMPT,
        # generated output can have "max_tokens" number of tokens 
        max_tokens=MaxToken,
        # number of outputs generated in one call
        n=outputs
    )
    # creating a list to store all the outputs
    output = list()
    for k in response['choices']:
        output.append(k['text'].strip())
    return output


Here, we have used the Completions module from OpenAI library and generate text for the given user prompt. Here are the important parameters involved with Completions module:

  • model [required]: ID of the model to use can find out using the below command openai.Model.list().data where the value of ‘id’ represent the model name. we need to select a suitable model as per our use.
  • prompt: The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays. Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document.
  • max_tokens: The maximum number of tokens to generate in the completion. The default value is 16.
  • temperature: Sampling temperature ranges between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
  • n: number of completions to generate for each prompt.

Example prompts for text manipulation

Now let’s try out some prompts with the completions module and see their results.

Prompt 1: Text Generation

Python3




p = """Tell me whether the tweet is positive, neutral, or negative.
Tweet: I don't like tomato!
Sentiment:"""
  
comp(p, outputs=1)


Output:

['Negative']

Prompt 2: Text Completion

Python3




p = """hey doctor, ____"""
  
comp(p,outputs=5)


Output:

['What seems to be the problem?',
'Good morning! How can I help you today?',
'What seems to be the trouble?',
'Good to see you! How can I help you today?',
'What seems to be the problem?']

Prompt 3: Text Generation

Python3




p = "Write a tagline for my shoe company"
Output = comp(p,outputs=3)
print(Output[0])
print(Output[1])
print(Output[2])


Output:

"Walk in Comfort, Walk with Us!"
"Walking in Style, Step by Step"
"Walk in style with our designer shoes!"

Prompt 4: Text Completion

Python3




p = """What is the sentiment of these tweets:
  
1. "GFG has the best DSA course"
2. "Got late for work and got fired for that"
3. "Can't wait for Diwali!!!"
4. "What do you have in mind?"
5. "I hate strawberry"
  
Tweet sentiment ratings:"""
  
print(comp(p, outputs=1)[0])


Output:

1. Positive
2. Negative
3. Positive
4. Neutral
5. Negative

Prompt 5: Text Completion

Python3




p="""Complete this conversation with an AI assistant. 
The assistant is helpful, sarcastic, clever, and very nice in nature. 
Human want to know some good places to eat.
  
Human: Hello, who are you?
AI: I am an AI created by OpenAI. How can I help you today?
Human:"""
Output = comp(p,MaxToken =35, outputs=3)
print(Output[0])
print('\n',Output[1])
print('\n',Output[2])


Output:

I'm looking for a good place to eat.
AI: Sounds like you've got a bit of a craving for something delicious. I know the perfect place! Let me
I'm looking for some good places to eat. Do you know any?
AI: Sure! Are you looking for something specific, like Italian, or do you just want
Could you recommend some good places to eat in this area?
AI: Of course! There are lots of great restaurants in the area. I recommend checking out the Seafood

Prompt 6: Translation

Python3




p="""
Translate this sentence in hindi, german and japanese:
'I am learning from GeeksforGeeks.'
"""
Output = comp(p,MaxToken=500,outputs=2)
print(Output[0])
print('\n',Output[1])


Output:

Hindi: मैं GeeksforGeeks से सीख रहा हूँ।
German: Ich lerne von GeeksforGeeks.
Japanese: 私はGeeksforGeeksから学んでいます。
Hindi: मैं GeeksforGeeks से सीख रहा हूँ।
German: Ich lerne von GeeksforGeeks.
Japanese: 私はGeeksforGeeksから学んでいます。

Prompt 7: Summarization

Python3




p="""
Summarize the below paragraph for a pre-school student explaining 
how dangerous Mt. Everest is:
  
Mount Everest, located in the Himalayas, is the highest 
peak in the world and a popular destination for mountaineers. 
Standing at an impressive elevation of 29,029 feet (8,848 meters), 
it poses a significant challenge to climbers due to its extreme 
altitude and unpredictable weather conditions. The mountain is 
situated on the border between Nepal and China, and it attracts 
adventurers from around the globe who are determined to conquer 
its formidable slopes. Mount Everest has a rich history of expeditions 
and has been a subject of fascination for explorers and adventurers
for centuries. Despite its allure, scaling Mount Everest is a 
dangerous undertaking that requires meticulous planning, physical
fitness, and mountaineering expertise.
"""
Output = comp(p,MaxToken=20,outputs=3)
print(Output[0])
print(Output[1])
print(Output[2])


Output:

Mount Everest is the highest mountain in the world. It's very dangerous- you have to be
Mount Everest is a very tall mountain located in the Himalayas. People from all around the
Mt. Everest is the highest peak in the world and it is very dangerous and hard to

Prompt 8: Translation

Python3




p="""
How would Indian say this paragraph:
  
Mount Everest, located in the Himalayas, is the highest 
peak in the world and a popular destination for mountaineers. 
Standing at an impressive elevation of 29,029 feet (8,848 meters), 
it poses a significant challenge to climbers due to its extreme 
altitude and unpredictable weather conditions. The mountain is 
situated on the border between Nepal and China, and it attracts 
adventurers from around the globe who are determined to conquer 
its formidable slopes. Mount Everest has a rich history of expeditions 
and has been a subject of fascination for explorers and adventurers
for centuries. Despite its allure, scaling Mount Everest is a 
dangerous undertaking that requires meticulous planning, physical
fitness, and mountaineering expertise.
"""
Output = comp(p,MaxToken=1200,outputs=1)
print(Output[0])


Output:

माउंट एवरेस्ट, हिमालयों में स्थित, विश्व में सर्वोच्च चोटी है और पर्यावरण में श्रमिकों के लिए एक लोकप्रिय गंतव्य है। 
29,029 फीट (8,848 मीटर) की सुंदर ऊँचाई के साथ, यह अत्यधिक ऊँचाई और अनपेक्षित मौसम के कारण पर्यावरण में श्रमिकों को एक महत्वपूर्ण चुनौती देता है।
यह पर्वत नेपाल और चीन के सीमा पर स्थित है और यह पृथ्वी के हर कोणस्थल से ऊर्जा और अहंकार से प्रतीक्षा करने वाले अभ्यासीयकों को आकर्षित करता है।
माउंट एवरेस्ट का एक अत्यंत अधिक प्रयासों और आगमनों का अत्यधिक इतिहास है और यह दो घंटों से अन्वेषकों और आगमनाकारों के लिए आकर्षण रहा है।
माउंट एवरेस्ट के आकर्षण के बावजूद, उसे छोड़ने के लिए सख्त योजना की आवश्यकता, शारीरिक स्वस्थता और पर्वतारोहण के विद्या की आवश्यकता होती है।

Prompt 9: Text Conversion

Python3




PROMPT = """
Convert the below text to emoji's
Text:###
1. Hi
2. Geeks
3. For
###
Answer:
"""
print(comp(PROMPT, MaxToken=300, outputs=1)[0])


Output:

1. ????
2. ????
3. ????

Prompt 10: Text Conversion

Python3




PROMPT = """
Convert the below text to only special characters
Text:###
1. Hi
2. Geeks
3. For
###
Answer:
"""
print(comp(PROMPT, MaxToken=300, outputs=1)[0])


Output:

§1. ㅎㅣ
2. ㄱㅣㅇㄱㅅ
3. ㄹㅗ

Prompt 11: Retrieve factual information

Python3




PROMPT = """
How many states are in India?
Answer:
"""
print(comp(PROMPT, MaxToken=300, outputs=1)[0])


Output:

There are 28 states and 8 union territories in India.

Prompt 12: Retrieve factual information

Python3




PROMPT = """
How many countries are there in world?
Answer:
"""
print(comp(PROMPT, MaxToken=300, outputs=1)[0])


Output:

There are currently 195 countries in the world, according to the United Nations. This number includes 193 member states that are UN members, as well as 2 non-member observer states.

Frequently Asked Questions (FAQs)

1. What is completion in OpenAI?

The most fundamental OpenAI model is the Completions API, which has a simple interface yet is highly versatile and powerful. It answers with a text completion that you’ve instructed it to prepare when you prompt it.

2. What is the difference between chat completion and completion in OpenAI?

The /chat/completions endpoint completes a specific dialogue and demands input in a specific way that matches the message history, in contrast to the /completions endpoint, which completes a single prompt and accepts a single string.

To use chatGPT models, you must use the /chat/completions API, but your request must be modified accordingly.

3. What is text completion AI?

Complete the sentence, a specialised model or system created to automatically create text or complete phrases from a partial input or prompt is referred to as having artificial intelligence (AI). In order to identify the statistical patterns, linguistic structures, and semantic linkages present in the training data, these models are trained on large volumes of text data. The text completion AI makes use of this newly discovered knowledge to create a logical and contextually appropriate continuation of the text when given an unfinished sentence or prompt.

4. How does OpenAI generate text?

The GPT-3 language model is used in the Open AI text generator to create intelligent text. This tool enables you to create a wide range of texts. The Open AI text generator is among the newest and most well-liked forms of text generators, however there are many different varieties.

5. Can Completions API write an essay?

You may ask it to write anything for you, in any style, including business taglines, essays, research papers, software code, song lyrics about your dog, and poems using the name of your child.

6. Can Completions API generate images?

No, the completions API can only generate text. It does not have image generating capabilities. If you want to generate images using text prompts you can check out DALL-E by OpenAI.

Conclusion

The Completions module, which uses the power of GPT-3 to generate new text, fill in masked strings, facilitate discussions, translate languages, and summarise content, is one of the primary features provided by OpenAI, as we saw in this article. Python programmers may make use of the OpenAI library and unlock the power of the Completions module by following a few straightforward steps.

The article’s prompts show off the versatility of the Completions module by providing illustrations of how to create text responses for various scenarios and contexts. The factors temperature, maximum tokens, prompt structure, and model choice all help to give the outputs that are produced flexibility and control.

Although the findings from OpenAI’s text completion AI models can be rather interesting, it is vital to use caution and evaluate the generated text for correctness, coherence, and appropriateness. Text completion AI models that rely on statistical patterns rather than actual comprehension may occasionally produce incorrect or absurd results. Developers may take advantage of text completion AI’s potential while assuring the accuracy and dependability of the generated text by understanding its capabilities and constraints.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads