api接口的话可以传入图片的url地址,我们以php为例
<?php
$openaiApiKey = "YOUR_OPENAI_API_KEY"; // 替换为您的 OpenAI API 密钥
$url = "https://api.openai.com/v1/chat/completions";
$data = array(
"model" => "gpt-4-vision-preview",
"messages" => array(
array(
"role" => "user",
"content" => array(
array(
"type" => "text",
"text" => "What’s in this image?"
),
array(
"type" => "image_url",
"image_url" => array(
"url" => "https://repo.bfw.wiki/bfwrepo/image/61e27364c2151.png?x-oss-process=image/auto-orient,1/resize,m_fill,w_100,h_100,/quality,q_90"
)
)
)
)
),
"max_tokens" => 300
);
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $openaiApiKey
));
$result = curl_exec($ch);
curl_close($ch);
// 处理结果
echo $result;
?>
还支持base64及pythonimport base64
import requests
# OpenAI API Key
api_key = "YOUR_OPENAI_API_KEY"
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Path to your image
image_path = "path_to_your_image.jpg"
# Getting the base64 string
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What’s in this image?"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
print(response.json()) 网友回复
如何用html写出网页滚动视频播放卡片视觉差异效果的代码?
程序员如何低成本搭建代理进行科学上网学习技术?
threejs如何做个三维搭积木的游戏?
three如何实现标记多个起始路过地点位置后选择旅行工具(飞机汽车高铁等),最后三维模拟行驶动画导出mp4?
ai实时驱动的3d数字人可视频聊天的开源技术有吗
swoole+phpfpm如何实现不同域名指向不同目录的多租户模式?
如何用go替换nginx实现请求phpfpm解析运行php脚本?
有没有浏览器离线运行进行各种文档、图片、视频格式转换的开源工具?
如何使用go语言搭建一个web防火墙?
linux如何检测特定网络协议比如http协议中报文是否包含特点关键词并阻止返回给客户?


