Text Manipulation using OpenAI
Last Updated :
23 Jul, 2025
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.
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.
Similar Reads
Data Science Tutorial Data Science is a field that combines statistics, machine learning and data visualization to extract meaningful insights from vast amounts of raw data and make informed decisions, helping businesses and industries to optimize their operations and predict future trends.This Data Science tutorial offe
3 min read
Introduction to Machine Learning
What is Data Science?Data science is the study of data that helps us derive useful insight for business decision making. Data Science is all about using tools, techniques, and creativity to uncover insights hidden within data. It combines math, computer science, and domain expertise to tackle real-world challenges in a
8 min read
Top 25 Python Libraries for Data Science in 2025Data Science continues to evolve with new challenges and innovations. In 2025, the role of Python has only grown stronger as it powers data science workflows. It will remain the dominant programming language in the field of data science. Its extensive ecosystem of libraries makes data manipulation,
10 min read
Difference between Structured, Semi-structured and Unstructured dataBig Data includes huge volume, high velocity, and extensible variety of data. There are 3 types: Structured data, Semi-structured data, and Unstructured data. Structured data - Structured data is data whose elements are addressable for effective analysis. It has been organized into a formatted repos
2 min read
Types of Machine LearningMachine learning is the branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data and improve from previous experience without being explicitly programmed for every task.In simple words, ML teaches the systems to think and understand like h
13 min read
What's Data Science Pipeline?Data Science is a field that focuses on extracting knowledge from data sets that are huge in amount. It includes preparing data, doing analysis and presenting findings to make informed decisions in an organization. A pipeline in data science is a set of actions which changes the raw data from variou
3 min read
Applications of Data ScienceData Science is the deep study of a large quantity of data, which involves extracting some meaning from the raw, structured, and unstructured data. Extracting meaningful data from large amounts usesalgorithms processing of data and this processing can be done using statistical techniques and algorit
6 min read
Python for Machine Learning
Learn Data Science Tutorial With PythonData Science has become one of the fastest-growing fields in recent years, helping organizations to make informed decisions, solve problems and understand human behavior. As the volume of data grows so does the demand for skilled data scientists. The most common languages used for data science are P
3 min read
Pandas TutorialPandas is an open-source software library designed for data manipulation and analysis. It provides data structures like series and DataFrames to easily clean, transform and analyze large datasets and integrates with other Python libraries, such as NumPy and Matplotlib. It offers functions for data t
6 min read
NumPy Tutorial - Python LibraryNumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
Scikit Learn TutorialScikit-learn (also known as sklearn) is a widely-used open-source Python library for machine learning. It builds on other scientific libraries like NumPy, SciPy and Matplotlib to provide efficient tools for predictive data analysis and data mining.It offers a consistent and simple interface for a ra
3 min read
ML | Data Preprocessing in PythonData preprocessing is a important step in the data science transforming raw data into a clean structured format for analysis. It involves tasks like handling missing values, normalizing data and encoding variables. Mastering preprocessing in Python ensures reliable insights for accurate predictions
6 min read
EDA - Exploratory Data Analysis in PythonExploratory Data Analysis (EDA) is a important step in data analysis which focuses on understanding patterns, trends and relationships through statistical tools and visualizations. Python offers various libraries like pandas, numPy, matplotlib, seaborn and plotly which enables effective exploration
6 min read
Introduction to Statistics
Statistics For Data ScienceStatistics is like a toolkit we use to understand and make sense of information. It helps us collect, organize, analyze and interpret data to find patterns, trends and relationships in the world around us.From analyzing scientific experiments to making informed business decisions, statistics plays a
12 min read
Descriptive StatisticStatistics is the foundation of data science. Descriptive statistics are simple tools that help us understand and summarize data. They show the basic features of a dataset, like the average, highest and lowest values and how spread out the numbers are. It's the first step in making sense of informat
5 min read
What is Inferential Statistics?Inferential statistics is an important tool that allows us to make predictions and conclusions about a population based on sample data. Unlike descriptive statistics, which only summarize data, inferential statistics let us test hypotheses, make estimates, and measure the uncertainty about our predi
7 min read
Bayes' TheoremBayes' Theorem is a mathematical formula used to determine the conditional probability of an event based on prior knowledge and new evidence. It adjusts probabilities when new information comes in and helps make better decisions in uncertain situations.Bayes' Theorem helps us update probabilities ba
13 min read
Probability Data Distributions in Data ScienceUnderstanding how data behaves is one of the first steps in data science. Before we dive into building models or running analysis, we need to understand how the values in our dataset are spread out and thatâs where probability distributions come in.Let us start with a simple example: If you roll a f
8 min read
Parametric Methods in StatisticsParametric statistical methods are those that make assumptions regarding the distribution of the population. These methods presume that the data have a known distribution (e.g., normal, binomial, Poisson) and rely on parameters (e.g., mean and variance) to define the data.Key AssumptionsParametric t
6 min read
Non-Parametric TestsNon-parametric tests are applied in hypothesis testing when the data does not satisfy the assumptions necessary for parametric tests, such as normality or equal variances. These tests are especially helpful for analyzing ordinal data, small sample sizes, or data with outliers.Common Non-Parametric T
5 min read
Hypothesis TestingHypothesis testing compares two opposite ideas about a group of people or things and uses data from a small part of that group (a sample) to decide which idea is more likely true. We collect and study the sample data to check if the claim is correct.Hypothesis TestingFor example, if a company says i
9 min read
ANOVA for Data Science and Data AnalyticsANOVA is useful when we need to compare more than two groups and determine whether their means are significantly different. Suppose you're trying to understand which ingredients in a recipe affect its taste. Some ingredients, like spices might have a strong influence while others like a pinch of sal
9 min read
Bayesian Statistics & ProbabilityBayesian statistics sees unknown values as things that can change and updates what we believe about them whenever we get new information. It uses Bayesâ Theorem to combine what we already know with new data to get better estimates. In simple words, it means changing our initial guesses based on the
6 min read
Feature Engineering
Model Evaluation and Tuning
Data Science Practice