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

Azure Functions GET请求404,Postman调用无响应求助

Azure HTTP函数无响应排查方案

已核对代码文件与Azure网站的函数名称一致,API Key也已配置,但通过Postman发送GET请求 https://disney-clone-portfolio.azurewebsites.net/api/getaisuggestion?term=action 时完全无响应,以下是具体排查方向及修复建议:

相关代码

//https://disney-clone-portfolio.azurewebsites.net/api/getaisuggestion?term=action

import {
  app,
  HttpRequest,
  HttpResponseInit,
  InvocationContext,
} from "@azure/functions";

import OpenAI from "openai";

const openAI = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

export async function getAISuggestion(
  request: HttpRequest,
  context: InvocationContext
): Promise<HttpResponseInit> {
  context.log(`Http function processed request for url "${request.url}"`);

  const term = request.query.get("term");

  const completion = await openAI.chat.completions.create({
    messages: [
      {
        role: "system",
        content: `You are a digital video assistant working for services such as Netflix, Disney Plus & Amazon Prime Video. Your job is to provide suggestions based on the videos the user specifies. Provide an quirky breakdown of what the user should watch next! It should only list the names of the films after the introduction. Keep the response short and sweet! Always list at least 3 films as suggestions. If the user mentions a genre, you should provide a suggestion based on that genre.`,
      },
      {
        role: "user",
        content: `I like: ${term}`,
      },
    ],
    model: "gpt-3.5-turbo-16k",
  });

  return {
    body: completion.choices[0].message.content || "No suggestions found",
  };
}

app.http("getAISuggestion", {
  methods: ["GET"],
  authLevel: "anonymous",
  handler: getAISuggestion,
});

排查方向及修复建议

  • 添加异常捕获逻辑:当前代码未处理任何错误,一旦OpenAI API调用失败(如Key无效、网络中断、模型不存在),会导致函数崩溃无响应。修改代码加入try-catch块,捕获错误并返回明确的响应:
    export async function getAISuggestion(
      request: HttpRequest,
      context: InvocationContext
    ): Promise<HttpResponseInit> {
      context.log(`Http function processed request for url "${request.url}"`);
    
      try {
        const term = request.query.get("term");
        if (!term) {
          return { status: 400, body: "缺少必填查询参数 'term'" };
        }
    
        const completion = await openAI.chat.completions.create({
          messages: [
            {
              role: "system",
              content: `You are a digital video assistant working for services such as Netflix, Disney Plus & Amazon Prime Video. Your job is to provide suggestions based on the videos the user specifies. Provide an quirky breakdown of what the user should watch next! It should only list the names of the films after the introduction. Keep the response short and sweet! Always list at least 3 films as suggestions. If the user mentions a genre, you should provide a suggestion based on that genre.`,
            },
            {
              role: "user",
              content: `I like: ${term}`,
            },
          ],
          model: "gpt-3.5-turbo-16k",
        });
    
        return {
          body: completion.choices[0].message.content || "No suggestions found",
        };
      } catch (error) {
        context.error(`请求处理失败: ${error}`);
        return { status: 500, body: `服务器内部错误: ${(error as Error).message}` };
      }
    }
    
  • 验证环境变量有效性:确认Azure函数应用配置中的OPENAI_API_KEY拼写正确、权限充足,且能正常调用gpt-3.5-turbo-16k模型。可在代码中添加日志验证变量是否存在:
    context.log(`OPENAI_API_KEY 已配置: ${!!process.env.OPENAI_API_KEY}`);
    
  • 设置请求超时:OpenAI API调用可能因网络延迟或模型处理耗时过长触发Azure函数超时(默认5分钟)。给OpenAI请求添加超时限制,同时在Azure门户延长函数超时时间:
    const completion = await openAI.chat.completions.create({
      // 现有参数
    }, { timeout: 30000 }); // 设置30秒超时
    
  • 检查网络连通性:若函数部署在Azure中国区,需确认环境能访问OpenAI国际版API(可能需配置虚拟网络或代理)。查看函数执行日志是否有网络相关错误提示。
  • 查看函数执行日志:登录Azure门户,进入函数应用的「监测」->「日志」页面,排查是否有API调用失败、参数错误等具体报错信息。
  • 本地测试验证:在本地启动函数,用Postman请求本地地址(如http://localhost:7071/api/getaisuggestion?term=action),若本地正常则问题出在Azure部署环境;若本地也无响应,需检查本地OpenAI版本、API Key有效性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 09:06:00