Search code examples
javascriptnode.jschatgpt-api

ApiChatGPT Cutting Text


The chatGPT API is clipping the response text. Is there a way to resolve this? If there is no way to solve it, how can I remove the paragraph that had the text cut off. Can someone help me?

// API_URL = https://api.openai.com/v1/completions

async function newUserMessage(newMessage) {
  try {
    const response = await axios.post(API_URL, {
      prompt: newMessage,
      model: 'text-davinci-003',
      max_tokens: 150
     }, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${API_KEY}`,
      },
    });
    
    const { text } = response.data.choices[0];
    const newText = text.replace(/(\r\n|\n|\r)/gm, "");
    setResponse(newText);
    setQuery("");
   } catch (error) {
     console.error(error);
   }
 };

Solution

  • OpenAI language model processes text by dividing it into tokens. The API response was getting clipped because the text sent was going over the 100 token limit. To avoid this problem, I set the max_tokens property to its maximum value.

    This was my solution:

    const settings = {
      prompt: newMessage,
      model: 'text-davinci-003',
      temperature: 0.5,
      max_tokens: 2048,
      frequency_penalty: 0.5,
      presence_penalty: 0,
     }
    

    Here is the documentation I used: platform.openai.com/docs/api-reference/completions/create