#Using Ai Api in next.js but it is not generating the content

1 messages · Page 1 of 1 (latest)

somber vale
#
import { NextApiRequest, NextApiResponse } from "next";
// Server-side API handler
const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
export async function GET(req: NextApiRequest, res: NextApiResponse) {
  try {
    const chatCompletion = await groq.chat.completions.create({
      "messages": [
        {  "role": "user",
          "content": "Suggest one friendly question to ask."   }  ],
      "model": "llama3-8b-8192",
      "temperature": 1,
      "max_tokens": 1024,
      "top_p": 1,
      "stream": true,
      "stop": null
    });
    res.status(200).json(chatCompletion);
  } catch (error: any) {
    console.error("Error fetching Groq completion:", error); // Log the error
    res.status(500).json({ message: "Error fetching Groq completion", error: error.message || "Unknown error" });
  }
}
``` the environment variable is already added still it is not generating content showing this error- ```{
    "message": "Request failed with status code 500",
    "name": "AxiosError",
    "stack": "AxiosError: Request failed with status code 500\n    at settle (webpack-internal:///(app-pages-br
        "headers": {
            "Accept": "application/json, text/plain, */*"
        },
        "method": "get",
        "url": "/api/suggest-messages"
    },
    "code": "ERR_BAD_RESPONSE",
    "status": 500
}```` calling t here - ```    const response = await axios.get("/api/suggest-messages"); // Call the API route
      if (response.status == 200) {
        const suggestedMessage = response.data.choices[0]?.message?.content || "";
        setMessage(suggestedMessage);
      }
snow cradleBOT
#

Hi @somber vale. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided, it seems like the error is occurring when you're trying to call the /api/suggest-messages endpoint. The error message indicates a server-side issue (status code 500), which suggests that the problem might be in your server-side code.

The code you provided is using the Groq SDK to generate chat completions. However, the Groq SDK is not mentioned in the knowledge sources provided. Instead, the knowledge sources mention the use of OpenAI, Replicate, Anyscale, and Perplexity APIs.

If you're using the Groq SDK, you need to ensure that you have correctly set up the Groq API key in your environment variables and that the key has the necessary permissions. Also, make sure that the model llama3-8b-8192 you're trying to use is available and correctly spelled.

If you're supposed to use one of the APIs mentioned in the knowledge sources, you need to adjust your code accordingly. For example, if you're using OpenAI, your code might look something like this (based on the OpenAI example (https://vercel.com/docs/integrations/ai/openai#add-the-provider-to-your-project) in the knowledge sources):

import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';

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

export async function GET(req: NextApiRequest, res: NextApiResponse) {
  try {
    const chatCompletion = await openai.chat.completions.create({
      messages: [
        { role: "user", content: "Suggest one friendly question to ask." }
      ],
      model: 'gpt-3.5-turbo',
      stream: true,
    });
#

const stream = OpenAIStream(chatCompletion);
return new StreamingTextResponse(stream);
} catch (error: any) {
console.error("Error fetching OpenAI completion:", error);
res.status(500).json({ message: "Error fetching OpenAI completion", error: error.message || "Unknown error" });
}
}


Please replace `'gpt-3.5-turbo'` with the model you're supposed to use.

If none of these suggestions solve your issue, I recommend checking the Groq SDK documentation or reaching out to their support for further assistance.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

@somber vale kapa.ai is still learning and improving, please let us know how it did by reacting below

somber vale
#

it shows 'OpenAIStream' is deprecated.'StreamingTextResponse' is deprecated.

#

@snow cradle it shows 'OpenAIStream' is deprecated.'StreamingTextResponse' is deprecated.

snow cradleBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The deprecation warning you're seeing suggests that the OpenAIStream and StreamingTextResponse functions from the ai package are no longer recommended for use and may be removed in future versions of the package.

Deprecation is a process in software development where certain features or aspects of the software are phased out. It's a warning that the feature or aspect is no longer in favor or will be removed in future updates of the software. It's usually a good idea to move away from using deprecated features if possible, as their support might be dropped in the future (source (https://vercel.com/docs/release-phases#deprecated-and-sunset)).

Unfortunately, the provided knowledge sources do not provide an alternative to these deprecated functions. It's recommended to check the official documentation of the ai package or reach out to the package maintainers for the recommended alternatives.

If you're using Next.js with OpenAI, you might want to consider using the openai package directly, as shown in the examples from the knowledge sources (https://vercel.com/docs/integrations/ai/openai#add-the-provider-to-your-project). However, please note that this might require changes in how you handle the API responses.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: