-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai_api_test.py
33 lines (32 loc) · 1.01 KB
/
openai_api_test.py
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
import os
from openai import OpenAI
import base64
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
base64_image = encode_image("image_path")
client = OpenAI(
api_key="", # your api key
base_url="https://api.openai.com/v1/chat/completions",
# # https://docs.openkey.cloud/api/model-chat
# api_key="sk-N7hXtAFOVWiHROgy7b5810F25b6b4998998fBfAbE46533F5",
# base_url="https://openkey.cloud/v1"
)
chat_completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "assistant",
"content": [
{"type": "text", "text": "What’s in this image?"},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}},
],
}
],
temperature=0.7,
max_tokens=256,
)
print (chat_completion)
response = chat_completion.choices[0].message.content
response = response.strip()
print(response)