Skip to content

Instantly share code, notes, and snippets.

@fnando1995
Created April 16, 2024 21:11
Show Gist options
  • Save fnando1995/201881021a8a81aa0e3041bc8d62c82a to your computer and use it in GitHub Desktop.
Save fnando1995/201881021a8a81aa0e3041bc8d62c82a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "e3993515-9710-4ac4-89e9-b35ebb81e920",
"metadata": {
"id": "rhqRhkElpJ0z"
},
"source": [
"# Transforming\n",
"\n",
"In this notebook, we will explore how to use Large Language Models for text transformation tasks such as language translation, spelling and grammar checking, tone adjustment, and format conversion.\n",
"\n",
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41df0348",
"metadata": {
"height": 133,
"tags": []
},
"outputs": [],
"source": [
"import openai\n",
"import os\n",
"\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv()) # read local .env file\n",
"\n",
"openai.api_key = os.getenv('OPENAI_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a85ee0f",
"metadata": {
"height": 150,
"tags": []
},
"outputs": [],
"source": [
"def get_completion(prompt, model=\"gpt-3.5-turbo\", temperature=0): \n",
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=temperature, \n",
" )\n",
" return response.choices[0].message[\"content\"]"
]
},
{
"cell_type": "markdown",
"id": "1d8f0bd8-628e-4c36-bcd0-2110162f25fc",
"metadata": {
"id": "zdxC4c6pwqA5"
},
"source": [
"## Translation\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c4df6ff",
"metadata": {
"height": 116,
"tags": []
},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"Translate the following English text to Spanish: \\ \n",
"```Hi, I would like to order a blender```\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7300ed9b",
"metadata": {
"height": 116,
"tags": []
},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"Tell me which language this is: \n",
"```Combien coûte le lampadaire?```\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "791e789b",
"metadata": {
"height": 133,
"tags": []
},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"Translate the following text to French and Spanish\n",
"and English pirate: \\\n",
"```I want to order a basketball```\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fcf7eb63",
"metadata": {
"height": 133,
"tags": []
},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"Translate the following text to Spanish in both the \\\n",
"formal and informal forms: \n",
"'Would you like to order a pillow?'\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "8edb56d2-a32a-470f-9f40-4fc5b1ea0849",
"metadata": {
"id": "-hN2bczQrRC1"
},
"source": [
"### Universal Translator\n",
"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!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68a40bf0",
"metadata": {
"height": 156,
"tags": []
},
"outputs": [],
"source": [
"user_messages = [\n",
" \"La performance du système est plus lente que d'habitude.\", # System performance is slower than normal \n",
" \"Mi monitor tiene píxeles que no se iluminan.\", # My monitor has pixels that are not lighting\n",
" \"Il mio mouse non funziona\", # My mouse is not working\n",
" \"Mój klawisz Ctrl jest zepsuty\", # My keyboard has a broken control key\n",
" \"我的屏幕在闪烁\" # My screen is flashing\n",
"] "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "552d0db9",
"metadata": {
"height": 201,
"tags": []
},
"outputs": [],
"source": [
"for issue in user_messages:\n",
" prompt = f\"Tell me what language this is: ```{issue}```\"\n",
" lang = get_completion(prompt)\n",
" print(f\"Original message ({lang}): {issue}\")\n",
"\n",
" prompt = f\"\"\"\n",
" Translate the following text to English \\\n",
" and Korean: ```{issue}```\n",
" \"\"\"\n",
" response = get_completion(prompt)\n",
" print(response, \"\\n\")"
]
},
{
"cell_type": "markdown",
"id": "18e660eb-324f-474c-acf3-7e3bf5b7c70e",
"metadata": {},
"source": [
"## Try it yourself!\n",
"Try some translations on your own!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa57158f-d77d-42d1-94fe-17fa59c012f8",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "1d9e54ca-f93a-43c8-a295-bff7a89f77f5",
"metadata": {
"id": "JH3-0vdjsILh"
},
"source": [
"## Tone Transformation\n",
"Writing can vary based on the intended audience. ChatGPT can produce different tones.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2deac328",
"metadata": {
"height": 116,
"tags": []
},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"Translate the following from slang to a business letter: \n",
"'Dude, This is Joe, check out this spec on this standing lamp.'\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "a2c7eb73-6b82-442d-b4f8-251c308e89d5",
"metadata": {
"id": "p3e9sZh5tWIa"
},
"source": [
"## Format Conversion\n",
"ChatGPT can translate between formats. The prompt should describe the input and output formats."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a37f0a0",
"metadata": {
"height": 218,
"tags": []
},
"outputs": [],
"source": [
"data_json = { \"resturant employees\" :[ \n",
" {\"name\":\"Shyam\", \"email\":\"shyamjaiswal@gmail.com\"},\n",
" {\"name\":\"Bob\", \"email\":\"bob32@gmail.com\"},\n",
" {\"name\":\"Jai\", \"email\":\"jai87@gmail.com\"}\n",
"]}\n",
"\n",
"prompt = f\"\"\"\n",
"Translate the following python dictionary from JSON to an HTML \\\n",
"table with column headers and title: {data_json}\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "481a46b7",
"metadata": {
"height": 48,
"tags": []
},
"outputs": [],
"source": [
"from IPython.display import display, Markdown, Latex, HTML, JSON\n",
"display(HTML(response))"
]
},
{
"cell_type": "markdown",
"id": "2df1824c-534b-45cb-b0c1-3000bba5adbe",
"metadata": {
"id": "qLTz16qEzyT_"
},
"source": [
"## Spellcheck/Grammar check.\n",
"\n",
"Here are some examples of common grammar and spelling problems and the LLM's response. \n",
"\n",
"To signal to the LLM that you want it to proofread your text, you instruct the model to 'proofread' or 'proofread and correct'."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52d77283",
"metadata": {
"height": 326,
"tags": []
},
"outputs": [],
"source": [
"text = [ \n",
" \"The girl with the black and white puppies have a ball.\", # The girl has a ball.\n",
" \"Yolanda has her notebook.\", # ok\n",
" \"Its going to be a long day. Does the car need it’s oil changed?\", # Homonyms\n",
" \"Their goes my freedom. There going to bring they’re suitcases.\", # Homonyms\n",
" \"Your going to need you’re notebook.\", # Homonyms\n",
" \"That medicine effects my ability to sleep. Have you heard of the butterfly affect?\", # Homonyms\n",
" \"This phrase is to cherck chatGPT for speling abilitty\" # spelling\n",
"]\n",
"for t in text:\n",
" prompt = f\"\"\"Proofread and correct the following text\n",
" and rewrite the corrected version. If you don't find\n",
" and errors, just say \"No errors found\". Don't use \n",
" any punctuation around the text:\n",
" ```{t}```\"\"\"\n",
" response = get_completion(prompt)\n",
" print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7543fe7d",
"metadata": {
"height": 258,
"tags": []
},
"outputs": [],
"source": [
"text = f\"\"\"\n",
"Got this for my daughter for her birthday cuz she keeps taking \\\n",
"mine from my room. Yes, adults also like pandas too. She takes \\\n",
"it everywhere with her, and it's super soft and cute. One of the \\\n",
"ears is a bit lower than the other, and I don't think that was \\\n",
"designed to be asymmetrical. It's a bit small for what I paid for it \\\n",
"though. I think there might be other options that are bigger for \\\n",
"the same price. It arrived a day earlier than expected, so I got \\\n",
"to play with it myself before I gave it to my daughter.\n",
"\"\"\"\n",
"prompt = f\"proofread and correct this review: ```{text}```\"\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11ac80a0",
"metadata": {
"height": 82,
"tags": []
},
"outputs": [],
"source": [
"from redlines import Redlines\n",
"\n",
"diff = Redlines(text,response)\n",
"display(Markdown(diff.output_markdown))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b4e73fd",
"metadata": {
"height": 150,
"tags": []
},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"proofread and correct this review. Make it more compelling. \n",
"Ensure it follows APA style guide and targets an advanced reader. \n",
"Output in markdown format.\n",
"Text: ```{text}```\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"display(Markdown(response))"
]
},
{
"cell_type": "markdown",
"id": "63fb76bc-a742-4b35-9dc2-f7fbc12d38fb",
"metadata": {},
"source": [
"## Try it yourself!\n",
"Try changing the instructions to form your own review."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2b2ca58",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "3dbf5020-7d7f-4ba5-840b-20e883cd7c99",
"metadata": {
"id": "unsf1JnRr2IC"
},
"source": [
"Thanks to the following sites:\n",
"\n",
"https://writingprompts.com/bad-grammar-examples/\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment