-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathopenai_multimodal.py
More file actions
76 lines (66 loc) · 2.37 KB
/
Copy pathopenai_multimodal.py
File metadata and controls
76 lines (66 loc) · 2.37 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import base64
import requests
from openai import OpenAI
def get_image_base64(image_url):
"""Download image and convert to base64."""
response = requests.get(image_url)
return base64.b64encode(response.content).decode('utf-8')
def main():
# Initialize the client
client = OpenAI(
base_url="https://api.clarifai.com/v2/ext/openai/v1",
api_key=os.getenv("CLARIFAI_PAT")
)
# Example prompts and images
examples = [
{
"prompt": "Describe what you see in this image.",
"image_url": "https://samples.clarifai.com/cat1.jpeg"
},
{
"prompt": "What's happening in this scene?",
"image_url": "https://samples.clarifai.com/metro-north.jpg"
},
{
"prompt": "Analyze this image and tell me what you observe.",
"image_url": "https://samples.clarifai.com/dog1.jpeg"
}
]
# Process each example
for example in examples:
print(f"\nPrompt: {example['prompt']}")
print(f"Image: {example['image_url']}")
print("-" * 50)
try:
# Get image as base64
image_base64 = get_image_base64(example["image_url"])
# Get completion
response = client.chat.completions.create(
model="https://clarifai.com/openai/chat-completion/models/gpt-4_1",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": example["prompt"]},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_base64}"
}
}
]
}
],
temperature=0.7,
max_tokens=1024
)
print(f"Response: {response.choices[0].message.content}")
except Exception as e:
print(f"Error processing example: {str(e)}")
if __name__ == "__main__":
# Ensure PAT is set
if not os.getenv("CLARIFAI_PAT"):
print("Please set your CLARIFAI_PAT environment variable")
exit(1)
main()