File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #AUDIO OUTPUT
2+ import base64
3+ from openai import OpenAI
4+
5+ client = OpenAI ()
6+
7+ completion = client .chat .completions .create (
8+ model = "gpt-audio" ,
9+ modalities = ["text" , "audio" ],
10+ audio = {"voice" : "alloy" , "format" : "wav" },
11+ messages = [
12+ {
13+ "role" : "user" ,
14+ "content" : "Is a golden retriever a good family dog?"
15+ }
16+ ]
17+ )
18+
19+ print (completion .choices [0 ])
20+
21+ wav_bytes = base64 .b64decode (completion .choices [0 ].message .audio .data )
22+ with open ("dog.wav" , "wb" ) as f :
23+ f .write (wav_bytes )
24+
25+ #AUDIO INPUT
26+
27+ import base64
28+ import requests
29+ from openai import OpenAI
30+
31+ client = OpenAI ()
32+
33+ # Fetch the audio file and convert it to a base64 encoded string
34+ url = "https://cdn.openai.com/API/docs/audio/alloy.wav"
35+ response = requests .get (url )
36+ response .raise_for_status ()
37+ wav_data = response .content
38+ encoded_string = base64 .b64encode (wav_data ).decode ('utf-8' )
39+
40+ completion = client .chat .completions .create (
41+ model = "gpt-audio" ,
42+ modalities = ["text" , "audio" ],
43+ audio = {"voice" : "alloy" , "format" : "wav" },
44+ messages = [
45+ {
46+ "role" : "user" ,
47+ "content" : [
48+ {
49+ "type" : "text" ,
50+ "text" : "What is in this recording?"
51+ },
52+ {
53+ "type" : "input_audio" ,
54+ "input_audio" : {
55+ "data" : encoded_string ,
56+ "format" : "wav"
57+ }
58+ }
59+ ]
60+ },
61+ ]
62+ )
63+
64+ print (completion .choices [0 ].message )
Original file line number Diff line number Diff line change 1919if image_data :
2020 image_base64 = image_data [0 ]
2121 with open ("cat_and_otter.png" , "wb" ) as f :
22- f .write (base64 .b64decode (image_base64 ))
22+ f .write (base64 .b64decode (image_base64 ))
23+
24+
25+
26+
27+ #OPCIÓN 2
28+
29+ from openai import OpenAI
30+ import base64
31+ client = OpenAI ()
32+
33+ prompt = """
34+ A children's book drawing of a veterinarian using a stethoscope to
35+ listen to the heartbeat of a baby otter.
36+ """
37+
38+ result = client .images .generate (
39+ model = "gpt-image-2" ,
40+ prompt = prompt
41+ )
42+
43+ image_base64 = result .data [0 ].b64_json
44+ image_bytes = base64 .b64decode (image_base64 )
45+
46+ # Save the image to a file
47+ with open ("otter.png" , "wb" ) as f :
48+ f .write (image_bytes )
Original file line number Diff line number Diff line change 1+ from openai import OpenAI
2+
3+ client = OpenAI ()
4+
5+ response = client .responses .create (
6+ model = "gpt-4.1-mini" ,
7+ input = [{
8+ "role" : "user" ,
9+ "content" : [
10+ {"type" : "input_text" , "text" : "what's in this image?" },
11+ {
12+ "type" : "input_image" ,
13+ "image_url" : "https://api.nga.gov/iiif/a2e6da57-3cd1-4235-b20e-95dcaefed6c8/full/!800,800/0/default.jpg" ,
14+ },
15+ ],
16+ }],
17+ )
18+
19+ print (response .output_text )
You can’t perform that action at this time.
0 commit comments