-1

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);
   }
 };
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
davR7
  • 63
  • 7
  • 1
    How long is `newMessage`? – 0stone0 Mar 14 '23 at 14:54
  • @0stone0 yes. Does the API have any limitations regarding text size? – davR7 Mar 14 '23 at 15:07
  • 1
    Yes it does, since you're passing `max_token` to, the prompt will be cut of if the limit is reached/ – 0stone0 Mar 14 '23 at 15:11
  • 1
    Does this answer your question? [OpenAI GPT-3 API: Why do I get only partial completion? Why is the completion cut off?](https://stackoverflow.com/questions/75648132/openai-gpt-3-api-why-do-i-get-only-partial-completion-why-is-the-completion-cu) – Rok Benko Mar 15 '23 at 07:58
  • 1
    @RokBenko Thank you very much for the comment :), but I already managed to clear my doubts. I'll leave the link here: https://platform.openai.com/docs/api-reference/completions/create – davR7 Mar 15 '23 at 10:20

1 Answers1

1

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

davR7
  • 63
  • 7