admin管理员组文章数量:1632157
一、API调用现状
跟据https://platform.openai/docs/api-reference/chat/create所述,我们可以通过API调用其多模态大模型,而目前国内支持多模态API调用的几乎没有。所以本文后续提供一个通过API调用GPT-4o模型,进行多模态(图片)交互的示例。
二、获取图片的URL
由于openai的官方文档中对图片参数的需求是统一资源定位符(URL),我们需要先将自己本地的图片通过一定方式转换成URL,这里我使用的是sm.ms图床,它提供5GB的免费空间,且它还支持API调用,我们可以通过API调用批量提交图片获取其对应的URL,具体操作流程参考【python】调用sm.ms图床api接口,实现上传图片并返回url_smms图床网页接口-CSDN博客
三 、调用CHAT_Gpt API
首先拿到自己的API_KEY,将自己的API_KEY替换掉YOUR_API_KEY,点击运行就能与GPT-4o进行多模态的交互啦
import requests
def chat_with_gpt(prompt,img_url):
api_key = "YOUR_API_KEY"
model = "gpt-4o"
url = "https://api.openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": model,
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url":img_url
}
}
]
},
{
"role": "system",
"content": [
{
"type": "text",
"text": prompt
}
]
}
]
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
response_data = response.json()
return response_data['choices'][0]['message']['content']
else:
return f"Error: {response.status_code}, {response.text}"
if __name__=="__main":
img_url="https://dashscope.oss-cn-beijing.aliyuncs/images/dog_and_girl.jpeg"
prompt="图片里面有什么"
response = chat_with_gpt(prompt,img_url)
print(response)
#response=这张图片展示了一位坐在沙滩上的女性和她的狗狗。她们正在互动,狗狗举起一只前爪,她伸出手与狗狗互动。背景是一个波光粼粼的大海,天空是明亮的日落时分,整个场景显得温馨而美好。
版权声明:本文标题:Chat_Gpt API调用上传图片示例 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729103040a1186750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论