To call openAI api from python (as of June 2024).
# import packages
from openai import OpenAI
import os
# add your openAI key as environment varialbe
os.environ["OPENAI_API_KEY"] = "your key here"
# create a client to make the call
client = OpenAI()
# Example to translate a sentence into Chinese
sentence='this is great, I think I like to learn more'
response = client.chat.completions.create(
model="gpt-4o",
messages = [
{"role": "system", "content": "You are a translator who can translate english into chinese."},
{"role": "user", "content": f"Translate the following English text to Chinese:{sentence}"}
]
)
print(response.choices[0].message.content)