You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Open AI for Images无法生成图片:Node.js调用异常求助

问题:调用Azure Open AI Images API无结果,文本补全功能正常

我用Node.js写了一个调用Azure Open AI图片生成API的函数,文本补全功能正常,但发送图片生成请求后返回无结果。

以下是我的代码:

async function generateImage(paragraph) {
  const endpoint = process.env.API_AI_IMAGE_ENDPOINT;
  const azureApiKey = process.env.API_AI_IMAGE_KEY;

  const prompt = paragraph;
  const size = '256x256';
  const n = 1;  // Change to 1 for only one image

  async function main() {
    console.log('== Batch Image Generation ==');

    const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
    const response = await client.getImages(prompt, { n, size });

    if (response && response.images && response.images.length > 0) {
      const image = response.images[0];
      console.log(`Image generation result URL: ${image.url}`);
    } else {
      console.log('No images found in the results or job not yet completed.');
    }
  }

  main().catch((err) => {
    console.error('The sample encountered an error:', err);
  });
}

generateImage("a monkey eating a banana");

返回的任务详情:

Job Details: {
id: '35ccf911-cac7-4a1e-bc65-16418c7ade66',
created: undefined,
expires: undefined,
result: undefined,
status: 'notRunning',
error: undefined
}

不知道为什么无法生成结果,请求帮忙排查。


排查与修复方案

1. 确认SDK方法是否匹配

Azure OpenAI Node.js SDK中,同步生成单张图片应该使用createImage方法,而非getImages。getImages通常用于异步批量任务或查询已提交的作业状态,这会导致返回未启动的作业对象而非直接结果。

修改代码中的调用逻辑:

// 替换原有的getImages调用
const response = await client.createImage({
  prompt: prompt,
  n: n,
  size: size
});

2. 处理异步作业的状态轮询

如果确实需要使用异步生成(比如生成多张或大尺寸图片),则必须轮询作业状态直到完成:

async function main() {
  console.log('== Image Generation Job ==');

  const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
  // 提交异步作业
  const initialResponse = await client.getImages(prompt, { n, size, responseFormat: 'url' });
  const operationId = initialResponse.id;

  // 轮询作业状态
  let operationResult;
  do {
    await new Promise(resolve => setTimeout(resolve, 1000)); // 每秒查询一次
    operationResult = await client.getImageOperationResult(operationId);
    console.log(`Current job status: ${operationResult.status}`);
  } while (operationResult.status === 'running' || operationResult.status === 'notRunning');

  if (operationResult.status === 'succeeded' && operationResult.result?.images?.length > 0) {
    const image = operationResult.result.images[0];
    console.log(`Image generation result URL: ${image.url}`);
  } else {
    console.log(`Job failed or no result. Error: ${operationResult.error?.message}`);
  }
}

3. 验证端点与密钥配置

  • 确保API_AI_IMAGE_ENDPOINT是Azure OpenAI资源的完整端点(格式:https://<your-resource-name>.openai.azure.com/)
  • 确认API_AI_IMAGE_KEY是该资源的有效密钥(可在Azure门户的“密钥和终结点”页面获取)
  • 检查资源是否已部署DALL-E模型(Azure门户中OpenAI资源的“模型部署”页面确认部署存在)

4. 修复函数异步逻辑问题

外层generateImage是async函数,但未等待内部main执行完成,可能导致进程提前退出。修改为:

async function generateImage(paragraph) {
  // ... 原有代码 ...

  async function main() {
    // ... 原有代码 ...
  }

  // 等待main执行完成
  await main().catch((err) => {
    console.error('The sample encountered an error:', err);
  });
}

// 调用时也需要等待
generateImage("a monkey eating a banana").catch(console.error);

5. 检查网络与权限

  • 确认服务器/本地环境能访问Azure OpenAI端点(无防火墙、代理阻止)
  • 验证Azure OpenAI资源的网络访问策略(是否允许当前IP访问)

内容的提问来源于stack exchange,提问作者Ahmed Nizami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 04:45:10