I am receiving an error status code 400

I have made sure that my API key and endpoint link are correct but i am still receiving a 400 error

import React, { useState } from 'react';
import axios from 'axios';


const OpenAIIntegration = () => {
  const [userInput, setUserInput] = useState('');
  const [apiResponse, setApiResponse] = useState('');
  const apiKey = "sk-abcdef";

  const getOpenAIResponse = async () => {
    try {
      const response = await axios.post(
        '',
        {
          prompt: userInput,
          max_tokens: 200, // Adjust based on your requirements

        },
        {
          headers: {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${apiKey}`,
            model:"gpt-3.5-turbo"
          },
        }
      );
      setApiResponse(response.data.choices[0].text);
    } catch (error) {
      console.error('Error making OpenAI API request:', error);
    }
  };

  return (
    <div>
      <input
        type="text"
        value={userInput}
        onChange={(e) => setUserInput(e.target.value)}
      />
      <button onClick={getOpenAIResponse}>Get Response</button>
      <div>API Response: {apiResponse}</div>
    </div>
  );
};

export default OpenAIIntegration;

endpoint link is removed and replaced because it can’t be posted but i am using the chat completion endpoint

The Chat completion endpoint is required in order to use gpt-3.5-turbo, and its input is messages, not prompt, which is a strictly-formatted and validated list of role messages produced per the documentation.

Hi @Daffa

Welcome to the community.

I’d recommend reading the docs on chat completion as there are discrepancies in the code you shared.

I’d also recommend using the OpenAI NodeJS library.

1 Like