import requests
import json
def analyze_image(image_url, api_url, openai_key):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {openai_key}'
}
data = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What’s in this image?"},
{
"type": "image_url",
"image_url": {
"url": image_url,
},
},
],
}
],
"max_tokens": 4000,
}
response = requests.post(api_url, headers=headers, data=json.dumps(data))
print(response.json())
# 使用方法
analyze_image("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", "https://4.0.wokaai.com/v1/chat/completions", "sk-xxxxxxx")