文档地址: 豆包大模型-火山引擎
模型广场地址: 账号登录-火山引擎
首先来到模型广场,选取你需要的模型,我这边要做图片理解的应用,所以选用了Doubao-1.5.vision-pro.
点立即体验,进入一个新的页面,可以上传图片,然后输入提示词体验
上面有个api接入,点击可以创建app key
然后点击开通
开通后会有代码示例,看了我上篇对接阿里云大模型的应该知道,这种对接用http库最好,可以兼容所有大模型,如果选择大模型提供的sdk反而麻烦
curl https://ark.cn-beijing.volces.com/api/v3/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer 你的appky" \
-d $'{
"model": "doubao-1.5-vision-pro-250328",
"messages": [
{
"content": [
{
"text": "图片主要讲了什么?",
"type": "text"
},
{
"image_url": {
"url": "https://ark-project.tos-cn-beijing.ivolces.com/images/view.jpeg"
},
"type": "image_url"
}
],
"role": "user"
}
]
}'
我们还是让他来识别这张图片
代码:需要注意的是这个api key不是一开始给你的,而是在示例里面的
public void testDoubaoImage() {
long time = System.currentTimeMillis();
String url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
String appKey = "你的appkey";
JSONArray finalJsonArray = new JSONArray()
.set(new JSONObject()
.set("role", "user")
.set("content", new JSONArray()
.set(new JSONObject()
.set("type", "image_url")
.set("image_url", new JSONObject()
.set("url", "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg")
)
)
.set(new JSONObject()
.set("type", "text")
.set("text", "图片主要讲了什么?")
)
)
);
try {
String requestBody = new JSONObject()
.putOpt("model", "doubao-1.5-vision-pro-250328")
.putOpt("messages", finalJsonArray)
.toString();
Request okhttpRequest = new Request.Builder()
.url(url)
.post(RequestBody.create(requestBody, MediaType.get(ContentType.JSON.getValue())))
.addHeader("Authorization", "Bearer " + appKey)
.build();
Call call = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.build()
.newCall(okhttpRequest);
Response okhttpResponse = call.execute();
JSONObject data = JSONUtil.parseObj(IoUtil.read(okhttpResponse.body().charStream()));
System.out.println(data.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getStr("content"));
System.out.println(System.currentTimeMillis() - time);
} catch (Exception e) {
e.printStackTrace();
}
}
输出:
图片主要讲述了一位女子与她的狗在海滩上互动的温馨场景。女子坐在沙滩上,面带微笑,似乎在与狗玩耍。狗则坐在她面前,前爪搭在她的手上,显得非常亲密和友好。背景是广阔的海洋和柔和的阳光,营造出一种宁静而愉快的氛围。这张图片传达了人与宠物之间深厚的情感联系以及在自然环境中享受美好时光的快乐。