Last active
May 4, 2024 16:50
-
-
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"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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