Skip to content

Instantly share code, notes, and snippets.

@ymoslem
Last active May 4, 2024 16:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ymoslem/cc46564d23857883aeeec136436fac23 to your computer and use it in GitHub Desktop.
Save ymoslem/cc46564d23857883aeeec136436fac23 to your computer and use it in GitHub Desktop.
Minimal working code for translation with GPT-4, "gpt-3.5-turbo" (a.k.a. ChatGPT) and "text-davinci-003"
# pip3 install openai
import openai
import time
OPENAI_API_KEY = "your_api_key_here"
openai.api_key = OPENAI_API_KEY
prompt = """French: La semaine dernière, quelqu’un m’a fait part de sa gratitude envers notre travail.
English:"""
# GPT-4
start_time = time.time()
output = openai.ChatCompletion.create(
model="gpt-4",
temperature=0.3,
max_tokens=50,
messages=[
{"role": "user",
"content": prompt}
]
)
time_spent = round((time.time() - start_time), 2)
print("• Translation by GPT-4 in %s seconds" % time_spent,
output["choices"][0]["message"]["content"].strip(),
sep="\n")
# ChatGPT (3.5)
start_time = time.time()
output = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
temperature=0.3,
max_tokens=50,
messages=[
{"role": "user",
"content": prompt}
]
)
time_spent = round((time.time() - start_time), 2)
print("• Translation by ChatGPT 3.5 in %s seconds" % time_spent,
output["choices"][0]["message"]["content"].strip(),
sep="\n")
# text-davinci-003
start_time = time.time()
output = openai.Completion.create(
model="text-davinci-003",
temperature=0.3,
max_tokens=50,
prompt=[prompt]
)
time_spent = round((time.time() - start_time), 2)
print("\n• Translation by GPT-3.5 'text-davinci-003' in %s seconds" % time_spent,
output["choices"][0]["text"].strip(),
sep="\n")
# ---- Example Output ----
# • Translation by GPT-4 in 2.44 seconds
# Last week, someone expressed their gratitude towards our work.
# • Translation by ChatGPT in 0.36 second(s)
# Last week, someone expressed their gratitude towards our work to me.
# • Translation by GPT-3.5 'text-davinci-003' in 0.57 second(s)
# Last week, someone expressed their gratitude for our work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment