52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
import openai
|
|
from typing import List
|
|
|
|
def query_gpt4_vision(image_url, llmPrompt: List, max_tokens=150):
|
|
if not image_url or image_url == "None":
|
|
print("Image URL is empty or None.")
|
|
return False
|
|
|
|
# Initialize the messages list with system content from llmPrompt
|
|
messages = [{
|
|
"role": item["role"],
|
|
"content": item["content"]
|
|
} for item in llmPrompt if item["role"] == "system"]
|
|
|
|
# Assuming the last item in llmPrompt is always from the user and includes the prompt content
|
|
if llmPrompt[-1]["role"] == "user":
|
|
# Append a new user message that includes both the text and image URL
|
|
messages.append({
|
|
"role": "user",
|
|
"content": [
|
|
{
|
|
"type": "text",
|
|
"text": llmPrompt[-1]["content"]
|
|
},
|
|
{
|
|
"type": "image_url",
|
|
"image_url": {
|
|
"url": image_url
|
|
}
|
|
}
|
|
]
|
|
})
|
|
|
|
print(f"Submitting to GPT-4-Vision with image URL: {image_url}")
|
|
try:
|
|
response = LLM_OAI.chat.completions.create(
|
|
model="gpt-4-vision-preview", # Ensure this matches your intended model
|
|
messages=messages,
|
|
max_tokens=max_tokens
|
|
)
|
|
# Assuming the response is structured as expected; adjust accordingly
|
|
if response.choices:
|
|
comment_content = response.choices[0].message['content'] # This path may need adjustment
|
|
print(f"Generated comment: {comment_content}")
|
|
return comment_content
|
|
else:
|
|
print("OpenAI API response did not contain expected data.")
|
|
return "Failed to generate comment due to API response format."
|
|
except Exception as e:
|
|
print(f"Error during OpenAI API call: {e}")
|
|
return "Failed to generate comment due to an error with the OpenAI API."
|
|
|