-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathopenai_multimodal_tools.py
More file actions
136 lines (125 loc) · 4.74 KB
/
Copy pathopenai_multimodal_tools.py
File metadata and controls
136 lines (125 loc) · 4.74 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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")
)
# Define tools
tools = [
{
"type": "function",
"function": {
"name": "get_image_details",
"description": "Get detailed information about an image.",
"parameters": {
"type": "object",
"properties": {
"aspect_ratio": {
"type": "string",
"description": "The aspect ratio of the image"
},
"dominant_colors": {
"type": "array",
"items": {"type": "string"},
"description": "The dominant colors in the image"
}
},
"required": ["aspect_ratio", "dominant_colors"],
"additionalProperties": False
},
"strict": True
}
},
{
"type": "function",
"function": {
"name": "get_location_info",
"description": "Get information about a location shown in an image.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location shown in the image"
},
"landmarks": {
"type": "array",
"items": {"type": "string"},
"description": "Notable landmarks in the image"
}
},
"required": ["location"],
"additionalProperties": False
},
"strict": True
}
}
]
# Example prompts and images
examples = [
{
"prompt": "Analyze this image and tell me about its composition and colors.",
"image_url": "https://samples.clarifai.com/cat1.jpeg"
},
{
"prompt": "What location is shown in this image and what landmarks can you identify?",
"image_url": "https://samples.clarifai.com/metro-north.jpg"
},
{
"prompt": "Describe this image and analyze its visual elements.",
"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 with tools
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}"
}
}
]
}
],
tools=tools,
tool_choice="auto",
temperature=0.7,
max_tokens=1024
)
# Print response and tool calls
print(f"Response: {response.choices[0].message.content}")
if response.choices[0].message.tool_calls:
print("\nTool Calls:")
for tool_call in response.choices[0].message.tool_calls:
print(f"- Function: {tool_call.function.name}")
print(f" Arguments: {tool_call.function.arguments}")
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()