Welcome back to the sixth article of our Free AI Course on prompt engineering for software developers!
In the previous article, we explored the basics of inferring with large language models (LLMs). We also demonstrated how to perform sentiment analysis and extract specific information from text.
We provided practical examples to show how LLMs can streamline these tasks, making your AI applications more efficient and effective.
This article will dive into the basics of text transformation using LLMs.
Text transformation involves:
- converting input from one format to another
- Language translation
- Grammar correction
- Data format conversion.
Prompting accordingly ensures your LLM performs these text transformations seamlessly. This approach is simpler and more proficient compared to traditional text transformation methods.
We will explore how LLMs like ChatGPT can take input in one format and output it in another, enhancing your development process and efficiency.
Ready to perform the aforementioned tasks faster than any human could before!? Let’s see how!
The Basics of Text Transformation
Let’s kick it off with an overview of what text transformation involves.
LLMs are adept at converting text from one format to another. This could be translation, transculturation, fixing grammar and spelling errors, or changing data formats (like converting HTML to JSON).
Traditionally, these tasks would require complex algorithms and regular expressions. However, LLMs achieve the same results much more efficiently with simple prompts.
Language Translation
LLMs have been trained on vast amounts of text from the internet in different languages. This training enables LLMs to navigate and surf languages smoothly and eloquently.
De facto, LLMs can now identify and translate across almost all documented and spoken languages of the 21st century. They can translate almost anything, from a simple text to Shakespeare’s most emotional poems.
Additionally, they can manage multiple translations simultaneously and adapt translations based on context.
Let’s look at some examples.
Example 1: Translating English to Spanish
Here’s a basic translation task:
# Prompt to translate text from English to French
prompt = f"""
Translate the following English text to French:
```Hello, I need some help with my laptop.```
"""
response = get_completion(prompt)
print(response)text transformation output (response):
Bonjour, j'ai besoin d'aide avec mon ordinateur portable.Example 2: Identifying the Language
Next, let’s identify the language of a given text:
prompt = f"""
Tell me what language this is: "Combien coûte le lampadaire?"
"""
response = get_completion(prompt)
print(response)The model responds with:
"This is French."Example 3: Formal and Informal Translations
LLMs can adapt translations based on formality:
prompt = f"""
Translate the following text to Spanish in both formal and informal forms: "Would you like to order a pillow?"
"""
response = get_completion(prompt)You get both formal and informal Spanish translations. This reflects how you would speak in different social contexts.
Formal: ¿Le gustaría ordenar una almohada?
Informal: ¿Te gustaría ordenar una almohada?Tone Transformation
Writing for different audiences requires adjusting the tone of your text. LLMs can help with this, too.
This comes in handy when repurposing a formal business letter or adapting the tone of an email. LLMs can seamlessly make these adjustments and text transformations.
Let’s see how to transform a casual message into a formal business letter:
Example: Translating from Slang to Formal Language
# Prompt to translate slang to a business letter
prompt = f"""
Translate the following from slang to a business letter:
'Hey, I was wondering if you could send over the details about that project.'
"""
response = get_completion(prompt)
print(response)The output of this text transformation prompt is:
Dear [Recipient],
I hope this message finds you well. I am writing to request the details regarding the project we discussed. Would you be able to send them over at your earliest convenience?
Thank you in advance for your assistance.
Sincerely,
[Your Name]Data Format Conversion
LLMs can also convert data between formats, such as JSON and HTML. This is especially handy for developers and data analysts who need to transform data for various applications.
You can easily convert complex data structures by describing the input and output formats in your prompt. This reduces the need for manual coding and makes your workflow more efficient.
Example: Converting JSON to HTML
Here’s an example prompt to turn a JSON object into an HTML table:
# JSON data
data_json = {
"restaurant employees": [
{"name": "Alice", "email": "alice@example.com"},
{"name": "Bob", "email": "bob@example.com"},
{"name": "Charlie", "email": "charlie@example.com"}
]
}
# Prompt to translate JSON to an HTML table
prompt = f"""
Translate the following Python dictionary from JSON to an HTML \
table with column headers and title: {data_json}
"""
response = get_completion(prompt)
print(response)
from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))Response:
Name | |
Alice | alice@example.com |
Bob | bob@example.com |
Charlie | charlie@example.com |
Grammar and Spell Checking
One of the most popular uses of LLMs is proofreading and correcting text. This is especially useful when writing in a second language.
By leveraging the language model’s native-like capabilities, you can quickly identify and correct grammatical errors and typos. You can even run the extra mile, and with proper LLM prompt engineering, you can see how you can make a given text more eloquent.
This tool is invaluable for anyone looking to improve their writing quality.
Example 1: Proofreading Sentences
Let’s correct a list of sentences with grammatical errors:
sentences = [
"I has a apple.",
"She go to school yesterday.",
"He dont like playing soccer."
]
prompt = f"""
Proofread and correct the following sentences: {sentences}
"""
response = get_completion(prompt)
print(response)The model provides grammatically correct versions of these sentences.
Response:
response:
Corrected sentences:
1. I have an apple.
2. She went to school yesterday.
3. He doesn't like playing soccer.Example 2
text = f"""
Got this for my daughter for her birthday cuz she keeps taking \
mine from my room. Yes, adults also like pandas too. She takes \
it everywhere with her, and it's super soft and cute. One of the \
ears is a bit lower than the other, and I don't think that was \
designed to be asymmetrical. It's a bit small for what I paid for it \
though. I think there might be other options that are bigger for \
the same price. It arrived a day earlier than expected, so I got \
to play with it myself before I gave it to my daughter.
"""
prompt = f"proofread and correct this review: ```{text}```"
response = get_completion(prompt)
print(response)
from redlines import Redlines
diff = Redlines(text,response)
display(Markdown(diff.output_markdown))Got I got this for my daughter for her birthday cuz because she keeps taking mine from my room. room. Yes, adults also like pandas too. too. She takes it everywhere with her, and it’s super soft and cute. cute. One of the ears is a bit lower than the other, and I don’t think that was designed to be asymmetrical. It’s a bit small for what I paid for it though. I think there might be other options that are bigger for the same price. price. It arrived a day earlier than expected, so I got to play with it myself before I gave it to my daughter. Daughter
Beyond basic corrections, LLMs can enhance your text by changing its style or making it more compelling. Think of text transformation with LLMs as the perfect wings for your muse and writing horizons.
With LLMs and AI advancement, our writing capabilities are set to reach new heights.
Final Thoughts
This article delved into the transformative magic of large language models (LLMs) in text expansion. We showcased how LLMs can effortlessly turn a brief set of instructions into a full-blown email or spin a simple list of topics into a detailed essay.
These models don’t just stretch your words; they elevate them, creating richer, more nuanced content from mere fragments.
We demonstrated practical applications, including personalized email generation, and examined the impact of the temperature parameter on the model’s responses.
In the next article of our prompt engineering series, we will explore the world of transforming text using LLMs even more. We will cover various tasks such as language translation, tone transformation, data format conversion, and advanced grammar and spell checking.
Discover more from AI For Developers
Subscribe to get the latest posts sent to your email.