Outputs from OpenAI Api is delayed and random

时间:2025-01-20 15:19:04
  • #main.JS
  • const userInput = document.querySelector('.user-input');
  • const inputText = userInput.value;
  • const data = {
  • model: 'text-davinci-003',
  • prompt: inputText,
  • temperature: 0.7,
  • max_tokens: 2048,
  • top_p: 1,
  • frequency_penalty: 0,
  • presence_penalty: 0
  • };
  • const options = {
  • method: 'POST',
  • headers: {
  • 'Content-Type': 'application/json',
  • 'Authorization': `Bearer ${openaiApiKey}`
  • },
  • body: JSON.stringify(data)
  • };
  • try {
  • const response = await fetch('/v1/completions', options);
  • const data = await response.json();
  • const choices = data.choices;
  • const text = choices[0].text.trim();
  • addMessage(text, 'bot');
  • } catch (error) {
  • console.error(error);
  • }
  • #index.html
  • <textarea class="user-input" id="user-input"></textarea>