#Please note that the model "gpt-3.5-turbo" is used in this example as the "text-davinci-003" model w

1 messages · Page 1 of 1 (latest)

sturdy lynx
#

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.

<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>

celest nicheBOT
#

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

#

From the information provided in the extracts, it seems like you're trying to use the text-davinci-003 model with the OpenAI API. However, the error message you're receiving suggests that there might be an issue with your authorization.

The Unauthorized error typically means that the API key you're using is incorrect or doesn't have the necessary permissions. Make sure that the API key you're using is correct and has the necessary permissions to access the model you're trying to use.

Here's an example of how to structure your API call correctly, based on the information provided in the extracts:

fetch("<https://api.openai.com/v1/engines/davinci/completions",> {
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${apiKey}`
    },
    body: JSON.stringify({
        prompt: userMessage,
        max_tokens: 50,
        temperature: 1 
    }),
})

In this example, replace ${apiKey} with your actual API key. Also, make sure that the model you're trying to use (text-davinci-003 or gpt-3.5-turbo) is available for your API key.

If you're still having issues, I would recommend checking the OpenAI documentation (https://platform.openai.com/docs/quickstart) or asking for help in the OpenAI community forum (https://community.openai.com).

Please note that the information provided is based on the extracts given and may not fully solve your issue.

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

#

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

sturdy lynx
celest nicheBOT
#

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

#

Yes, you are correct. According to the information provided in the extracts, OpenAI has deprecated the use of the term 'engine' in favor of 'model'. This change also affects the endpoint URL paths. Here are the updated paths:

  • The deprecated <https://api.openai.com/v1/engines/{engine_id}/completions> is now <https://api.openai.com/v1/completions.>
  • The deprecated <https://api.openai.com/v1/engines/{engine_id}/embeddings> is now <https://api.openai.com/v1/embeddings.>
  • The deprecated <https://api.openai.com/v1/engines> is now <https://api.openai.com/v1/models.>
  • The deprecated <https://api.openai.com/v1/engines/{engine_id}/edits> is now <https://api.openai.com/v1/edits.>

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

You can find more details in the API reference (https://beta.openai.com/docs/api-reference) provided by OpenAI.

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

#

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

sturdy lynx
#

@celest niche so the rest of the code is correct? full working code with all details above noted?

celest nicheBOT
#

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

#

Based on the information provided in the extracts, your code seems mostly correct. However, there are a few things to note:

  1. As you mentioned, the endpoint URL should be updated to <https://api.openai.com/v1/completions> as the old endpoint is deprecated.

  2. The model name should be included in the body of the request. For example, if you're using the gpt-3.5-turbo model, your request body should look like this:

body: JSON.stringify({
    prompt: userMessage,
    max_tokens: 50,
    temperature: 1,
    model: "gpt-3.5-turbo"
}),
  1. Make sure that the API key (Bearer sk-V.................3311WWDoc) you're using is correct and has the necessary permissions to access the model you're trying to use.

Here's the updated code:

<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>


Please note that the information provided is based on the extracts given and may not fully solve your issue. If you're still having issues, I would recommend checking the **OpenAI documentation** (<https://platform.openai.com/docs/quickstart>) or asking for help in the **OpenAI community forum** (<https://community.openai.com>).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

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

sturdy lynx
#

@celest niche

"https://api.openai.com/v1/completions"

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

celest nicheBOT
#

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

#

The HTTP 400 Bad Request error usually indicates that the server was unable to understand the request due to invalid syntax. This could be due to a variety of reasons such as incorrect headers, wrong parameters, or unsupported values in the request body.

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.

Here's how you can modify your code:

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
        }],
    }),
})

In this code, the messages parameter is an array of message objects. Each object has a role that can be 'system', 'user', or 'assistant', and content which is the text of the message from the role.

Please note that the information provided is based on the extracts given and may not fully solve your issue. If you're still having issues, I would recommend checking the OpenAI documentation (https://platform.openai.com/docs/quickstart) or asking for help in the OpenAI community forum (https://community.openai.com).

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

#

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

sturdy lynx
#

@celest niche can you change the code to use the proper for chat endpoint?

celest nicheBOT
#

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

#

Sure, here's how you can modify your code to use the /v1/chat/completions endpoint:

<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>
#

In this code, the messages parameter is an array of message objects. Each object has a role that can be 'system', 'user', or 'assistant', and content which is the text of the message from the role. The response from the chat model is accessed using data.choices[0].message.content.

Please note that the information provided is based on the extracts given and may not fully solve your issue. If you're still having issues, I would recommend checking the OpenAI documentation (https://platform.openai.com/docs/quickstart) or asking for help in the OpenAI community forum (https://community.openai.com).

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