Skip to content

Instantly share code, notes, and snippets.

@vinodjayachandran
Created April 4, 2024 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinodjayachandran/454198f9cc21ef1c7b1d559250b36838 to your computer and use it in GitHub Desktop.
Save vinodjayachandran/454198f9cc21ef1c7b1d559250b36838 to your computer and use it in GitHub Desktop.
Language translations using OpenAI
from openai import OpenAI
# Read OpenAI API Key from environment variable
api_key = os.environ.get("OPENAI_API_KEY")
client = OpenAI(api_key=api_key)
def translate_text(text):
model = "gpt-4-turbo-preview",
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[
{"role": "user", "content": f"Translate the following text to English: {text}"}
]
)
return response.choices[0].message.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment