Search code examples
typescriptazureazure-functions

404 on Azure Functions GET Request


The following file is absolutely right, I double checked it. The name matches the Azure website, the API Key is in there, why am I getting no response at all when trying to use this on Postman as a GET?

//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,
});

Solution

    • You need to execute the function locally before deploying to the function app to ensure that the function is not throwing any error and works as expected.
    • I have modified the code to use Azure OpenAI.
    import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
    import OpenAI from "openai";
    
    const apiVersion = '2024-02-15-preview';
    const apiKey = process.env.OPENAI_API_KEY;
    
    const openAI = new OpenAI({
      apiKey: apiKey,
      baseURL: process.env.OPENAI_BASE_URL,
      defaultQuery: { 'api-version': apiVersion },
      defaultHeaders: { 'api-key': apiKey },
    });
    
    
    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: 'user',
            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-35-turbo-16k",
      });
    
      return {
        body: completion.choices[0].message.content || "No suggestions found",
      };
    };
    
    app.http('getAISuggestion', {
        methods: ['GET'],
        authLevel: 'anonymous',
        handler: getAISuggestion
    });
    
    • Added API key and base URL in local settings file.
    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "node",
        "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
        "OPENAI_API_KEY": "a0ee****2a3",
        "OPENAI_BASE_URL": "https://{resource_name}.openai.azure.com/openai/deployments/{model}"
      }
    }
    
    • I am getting expected output locally.

    enter image description here

    enter image description here

    • I deployed the function to the function app using vs code or you can use func azure functionapp publish {func-app-name} command too.

    enter image description here

    • Added the key and base url in App Settings post deployment.

    enter image description here

    • I am able to invoke the function URL successfully.

    enter image description here