API Error - Random nonsense answers

Hello, I’ve just started working with the OpenAI API. Initially, when I connected to the API, it gave me nonsense and random responses. After researching, I found out about the ‘temperature’ setting. I adjusted the temperature, but it didn’t help much.

Then, when I tried to use the assistant replace the davinci with assistant ID
it said ‘Unauthorized’." what I’m missing, I’m new to this space just couple hours. Soo any help will apperciated. Thanks.

rassis.html:79

POST engines/asst_WLv8CVHa8/completions 401 (Unauthorized)
(anonymous) @ rassis.html:79
rassis.html:97 Check Choice
(anonymous) @ rassis.html:97
Promise.then (async)
(anonymous) @ rassis.html:92

image

<script>
const chatBox = document.getElementById("chat-box");
const userInput = document.getElementById("user-input");

function addMessage(message, isUser = false) {
  const msgElement = document.createElement("div");
  msgElement.textContent = message;
  if (isUser) {
      msgElement.style.textAlign = "right";
  }
  chatBox.appendChild(msgElement);
  chatBox.scrollTop = chatBox.scrollHeight;
}

userInput.addEventListener("keyup", function (event) {
  if (event.key === "Enter") {
      const userMessage = userInput.value;
      addMessage("You: " + userMessage, true);
      userInput.value = "";

      fetch("https://api.openai.com/v1/engines/asst_yKa.........../completions", {
          method: "POST",
          headers: {
              "Content-Type": "application/json",
              "Authorization": "Bearer sk-V.................3311WWDoc" ,
          },
          body: JSON.stringify({
              prompt: userMessage,
              max_tokens: 50,
              temperature: 1 
          }),
      })
      .then(response => response.json())
      .then(data => {
          if (data.choices && data.choices.length > 0) {
              const rabbit_response = data.choices[0].text;
              addMessage("Rabbit: " + rabbit_response);
          } else {
              console.error("Check Choice");
          }
      })
      .catch(error => {
          console.error("There is a connection problem: " + error);
      });
  }
});
</script>
1 Like

In your case, you should replace <https://api.openai.com/v1/engines/davinci/completions > with <https://api.openai.com/v1/completions .>

<script>
const chatBox = document.getElementById("chat-box");
const userInput = document.getElementById("user-input");

function addMessage(message, isUser = false) {
  const msgElement = document.createElement("div");
  msgElement.textContent = message;
  if (isUser) {
      msgElement.style.textAlign = "right";
  }
  chatBox.appendChild(msgElement);
  chatBox.scrollTop = chatBox.scrollHeight;
}

userInput.addEventListener("keyup", function (event) {
  if (event.key === "Enter") {
      const userMessage = userInput.value;
      addMessage("You: " + userMessage, true);
      userInput.value = "";
fetch("https://api.openai.com/v1/completions", {
          method: "POST",
          headers: {
              "Content-Type": "application/json",
              "Authorization": "Bearer sk-V.................3311WWDoc" ,
          },
          body: JSON.stringify({
              prompt: userMessage,
              max_tokens: 50,
              temperature: 1,
              model: "gpt-3.5-turbo"
          }),
      })
      .then(response => response.json())
      .then(data => {
          if (data.choices && data.choices.length > 0) {
              const rabbit_response = data.choices[0].text;
              addMessage("Rabbit: " + rabbit_response);
          } else {
              console.error("Check Choice");
          }
      })
      .catch(error => {
          console.error("There is a connection problem: " + error);
      });
  }
});
</script>

I did replace but it’s givin error.

rabbit_gpt.html:79

   POST https://api.openai.com/v1/completions 400 (Bad Request)

(anonymous) @ rabbit_gpt.html:79

1 Like

In your case, it’s possible that the model you’re trying to use (gpt-3.5-turbo ) is not supported with the /v1/completions endpoint. As per the information in the extracts, you should use the /v1/chat/completions endpoint for chat models like gpt-3.5-turbo .

Try changing to gpt-3.5-instruct (check name…)

Basically, you’re calling Chat Completion endpoint with Completion code…

You can try this GPT-4 written code for Chat Completion endpoint, but check it…

Also check the model endpoint compatibility in the docs…

<script>
const chatBox = document.getElementById("chat-box");
const userInput = document.getElementById("user-input");

function addMessage(message, isUser = false) {{
  const msgElement = document.createElement("div");
  msgElement.textContent = message;
  if (isUser) {{
      msgElement.style.textAlign = "right";
  }}
  chatBox.appendChild(msgElement);
  chatBox.scrollTop = chatBox.scrollHeight;
}}

userInput.addEventListener("keyup", function (event) {{
  if (event.key === "Enter") {{
      const userMessage = userInput.value;
      addMessage("You: " + userMessage, true);
      userInput.value = "";

      fetch("<https://api.openai.com/v1/chat/completions",> {{
          method: "POST",
          headers: {{
              "Content-Type": "application/json",
              "Authorization": "Bearer sk-V.................3311WWDoc" ,
          }},
          body: JSON.stringify({{
              model: "gpt-3.5-turbo",
              messages: [{{
                  role: "user",
                  content: userMessage
              }}],
          }}),
      }})
      .then(response => response.json())
      .then(data => {{
          if (data.choices && data.choices.length > 0) {{
              const rabbit_response = data.choices[0].message.content;
              addMessage("Rabbit: " + rabbit_response);
          }} else {{
              console.error("Check Choice");
          }}
      }})
      .catch(error => {{
          console.error("There is a connection problem: " + error);
      }});
  }}
}});
</script>
3 Likes

Thank you Paul, it’s working fine now.

1 Like

@0xnerull

Is it OK if a moderator closes this topic?

1 Like