45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
import fs from "fs";
|
||
import OpenAI from "openai";
|
||
|
||
// 1. 初始化客户端(记得设置环境变量 HUNYUAN_API_KEY)
|
||
const client = new OpenAI({
|
||
apiKey: "sk-LVfG90qgdhf9kKQUucqBSLioxamDu7gBeW9boXqKOxIDJt7H",
|
||
baseURL: "https://api.hunyuan.cloud.tencent.com/v1",
|
||
});
|
||
|
||
async function run() {
|
||
try {
|
||
// 2. 读取本地图片,转成 base64
|
||
const imagePath = "./test.png"; // 👈 换成你自己的图片路径
|
||
const base64Image = fs.readFileSync(imagePath).toString("base64");
|
||
|
||
// 3. 调用 hunyuan-vision
|
||
const completion = await client.chat.completions.create({
|
||
model: "hunyuan-vision",
|
||
stream: false, // 先关掉流式,调试更清晰
|
||
messages: [
|
||
{
|
||
role: "user",
|
||
content: [
|
||
{ type: "text", text: "请描述这张图片的主要内容。" },
|
||
{
|
||
type: "image_url",
|
||
image_url: {
|
||
url: `data:image/png;base64,${base64Image}`, // 👈 必须加前缀
|
||
},
|
||
},
|
||
],
|
||
},
|
||
],
|
||
});
|
||
|
||
// 4. 打印结果
|
||
console.log(completion.choices[0].message.content);
|
||
|
||
} catch (err) {
|
||
console.error("调用出错:", err.response?.data || err.message);
|
||
}
|
||
}
|
||
|
||
run();
|