forked from onlyphantom/llm-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_openai.py
More file actions
48 lines (41 loc) · 1.24 KB
/
08_openai.py
File metadata and controls
48 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from dotenv import load_dotenv
import os
load_dotenv()
from openai import OpenAI
import webbrowser
# Initialize OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# list all models
try:
models = client.models.list()
print(models.data[0].id)
print([model.id for model in models.data])
except Exception as e:
print(f"Error listing models: {e}")
# create our completion (modern syntax)
try:
completion = client.completions.create(model="gpt-3.5-turbo-instruct", prompt="Bill Gates is a")
print(completion.choices[0].text)
except Exception as e:
print(f"Error creating completion: {e}")
# image generation (modern syntax)
try:
image_gen = client.images.generate(
prompt="Zwei Hunde spielen unter einem Baum, cartoon",
n=2,
size="512x512"
)
for img in image_gen.data:
webbrowser.open_new_tab(img.url)
except Exception as e:
print(f"Error generating images: {e}")
# Audio transcription (modern syntax)
try:
with open("audio/donda.mp3", "rb") as audio:
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio
)
print(transcript.text)
except Exception as e:
print(f"Error transcribing audio: {e}")