My API with OpenAI is not working properly

18 Views Asked by At

I tring to call OpenAI api using node but it is not working as expected i am asking for keywords from paragraph but it is giving me sentance which is not event part of this paragraph i want important keywords from paragraph.

import express from "express";
import axios from "axios";

const app = express();
const port = 3000;

// Endpoint for SEO
app.get("/seo", async (req, res) => {
  try {
    const requestData = {
      model: "davinci-002",
      prompt:
        "Please extract important keywords from the following text. only 5 words from it. Provide the keywords as an array of strings. Avoid generating full sentences. Here's the text:" +
        "React.js, developed by Facebook, stands as a pivotal JavaScript library revolutionizing front-end web development. At its core lies a component-based architecture, empowering developers to construct reusable, encapsulated UI components, leading to cleaner and more maintainable codebases. Central to React's efficiency is its virtual DOM implementation, enabling lightning-fast rendering by selectively updating only the portions of the DOM that have changed. This approach enhances performance and user experience, particularly in complex applications. With JSX, a syntax extension blending HTML-like code within JavaScript, React facilitates the creation of declarative and expressive UIs. " +
        "",
      temperature: 0.5,
      max_tokens: 60,
      top_p: 1.0,
      best_of: 1,
      frequency_penalty: 0.8,
      presence_penalty: 0.0,
    };

    const requestConfig = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${api_key}`,
      },
    };

    const response = await axios.post(
      "https://api.openai.com/v1/completions",
      requestData,
      requestConfig
    );
    console.log("Request Data:", response.data.choices);

    res.json({ keywords: response.data.choices[0].text.trim() });
  } catch (error) {
    console.error("Error:", error);
    res.status(500).json({ error: "An error occurred" });
  }
});

// Start the server
app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

I tried different command, params but noting works

0

There are 0 best solutions below