404

问题描述 投票:0回答:1

以下文件绝对正确,我仔细检查过。该名称与 Azure 网站匹配,API 密钥就在那里,为什么当我尝试在 Postman 上使用它作为 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,
});
typescript azure azure-functions
1个回答
0
投票
  • 您需要在部署到函数应用程序之前在本地执行函数,以确保函数不会抛出任何错误并按预期工作。
  • 我已修改代码以使用 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
});
  • 在本地设置文件中添加了 API 密钥和基本 URL。
{
  "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}"
  }
}
  • 我在本地得到了预期的输出。

enter image description here

enter image description here

  • 我使用 vs code 将函数部署到函数应用程序,或者您也可以使用
    func azure functionapp publish {func-app-name}
    命令。

enter image description here

  • 在部署后应用程序设置中添加了密钥和基本 URL。

enter image description here

  • 我能够成功调用函数 URL。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.