import openai
import os
from dotenv import load_dotenv, find_dotenv
= load_dotenv(find_dotenv()) # read local .env file
_
= os.getenv('OPENAI_API_KEY') openai.api_key
Transforming
Use Large Language Models for text transformation tasks such as language translation, spelling and grammar checking, tone adjustment, and format conversion.
Setup
def get_completion(prompt, model="gpt-3.5-turbo", temperature=0):
= [{"role": "user", "content": prompt}]
messages = openai.ChatCompletion.create(
response =model,
model=messages,
messages=temperature,
temperature
)return response.choices[0].message["content"]
Translation
ChatGPT is trained with sources in many languages. This gives the model the ability to do translation. Here are some examples of how to use this capability.
= f"""
prompt Translate the following English text to Spanish: \
```Hi, I would like to order a blender```
"""
= get_completion(prompt)
response print(response)
= f"""
prompt Tell me which language this is:
```Combien coûte le lampadaire?```
"""
= get_completion(prompt)
response print(response)
= f"""
prompt Translate the following text to French and Spanish
and English pirate: \
```I want to order a basketball```
"""
= get_completion(prompt)
response print(response)
= f"""
prompt Translate the following text to Spanish in both the \
formal and informal forms:
'Would you like to order a pillow?'
"""
= get_completion(prompt)
response print(response)
Universal Translator
Imagine you are in charge of IT at a large multinational e-commerce company. Users are messaging you with IT issues in all their native languages. Your staff is from all over the world and speaks only their native languages. You need a universal translator!
= [
user_messages "La performance du système est plus lente que d'habitude.", # System performance is slower than normal
"Mi monitor tiene píxeles que no se iluminan.", # My monitor has pixels that are not lighting
"Il mio mouse non funziona", # My mouse is not working
"Mój klawisz Ctrl jest zepsuty", # My keyboard has a broken control key
"我的屏幕在闪烁" # My screen is flashing
]
for issue in user_messages:
= f"Tell me what language this is: ```{issue}```"
prompt = get_completion(prompt)
lang print(f"Original message ({lang}): {issue}")
= f"""
prompt Translate the following text to English \
and Korean: ```{issue}```
"""
= get_completion(prompt)
response print(response, "\n")
Try it yourself!
Try some translations on your own!
Tone Transformation
Writing can vary based on the intended audience. ChatGPT can produce different tones.
= f"""
prompt Translate the following from slang to a business letter:
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
= get_completion(prompt)
response print(response)
Format Conversion
ChatGPT can translate between formats. The prompt should describe the input and output formats.
= { "resturant employees" :[
data_json "name":"Shyam", "email":"shyamjaiswal@gmail.com"},
{"name":"Bob", "email":"bob32@gmail.com"},
{"name":"Jai", "email":"jai87@gmail.com"}
{
]}
= f"""
prompt Translate the following python dictionary from JSON to an HTML \
table with column headers and title: {data_json}
"""
= get_completion(prompt)
response print(response)
from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))
Spellcheck/Grammar check.
Here are some examples of common grammar and spelling problems and the LLM’s response.
To signal to the LLM that you want it to proofread your text, you instruct the model to ‘proofread’ or ‘proofread and correct’.
= [
text "The girl with the black and white puppies have a ball.", # The girl has a ball.
"Yolanda has her notebook.", # ok
"Its going to be a long day. Does the car need it’s oil changed?", # Homonyms
"Their goes my freedom. There going to bring they’re suitcases.", # Homonyms
"Your going to need you’re notebook.", # Homonyms
"That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # Homonyms
"This phrase is to cherck chatGPT for speling abilitty" # spelling
]for t in text:
= f"""Proofread and correct the following text
prompt and rewrite the corrected version. If you don't find
and errors, just say "No errors found". Don't use
any punctuation around the text:
```{t}```"""
= get_completion(prompt)
response print(response)
= f"""
text 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.
"""
= f"proofread and correct this review: ```{text}```"
prompt = get_completion(prompt)
response print(response)
from redlines import Redlines
= Redlines(text,response)
diff display(Markdown(diff.output_markdown))
= f"""
prompt proofread and correct this review. Make it more compelling.
Ensure it follows APA style guide and targets an advanced reader.
Output in markdown format.
Text: ```{text}```
"""
= get_completion(prompt)
response display(Markdown(response))
Try it yourself!
Try changing the instructions to form your own review.
import json
with open('l2-guidelines.ipynb') as f:
print(f.read())
Thanks to the following sites:
https://writingprompts.com/bad-grammar-examples/