Skip to content

Instantly share code, notes, and snippets.

@johnnieskywalker
Last active April 12, 2024 15:47
Show Gist options
  • Save johnnieskywalker/8ec729d2cf610cf2e81ff0c9273c8831 to your computer and use it in GitHub Desktop.
Save johnnieskywalker/8ec729d2cf610cf2e81ff0c9273c8831 to your computer and use it in GitHub Desktop.
Elvish Translator
python3 openai-test.py
Enter the language to translate from: polski
Enter the text to translate: Gandalfie którędy do najbliższej gospody ?
The sentence "Gandalfie którędy do najbliższej gospody?" translates to Elvish as "Mithrandir, man cerir tenest i orthad?" in the language of the Elves from The Lord of the Rings.
from openai import OpenAI
from dotenv import load_dotenv
import os
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=OPENAI_API_KEY)
language = input("Enter the language to translate from: ")
text = input("Enter the text to translate: ")
try:
completion = client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Translate the following {text} from {language} to Elfish from LOTR"}
]
)
print(completion.choices[0].message.content)
except AttributeError:
print("The method you are trying to use is not available. Check the latest API documentation.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment