skip to Main Content

I try to send a prompt containing raw text and HTML code in order to fix spelling errors.

E.g. a prompt would be:

fix spelling and grammar in the following html code
<p>this is som txt which needs to b fixed.<p>

But when I submit this prompt to the API via Javascript (see the code below) the only thing I receive as a response is this:

```html

The same prompt works fine when using Chat-GPT’s web page. The response is:

<p>This is some text which needs to be fixed.</p>

What am I doing wrong?

Here’s the code I’m using:


    const prompt = 'fix spelling and grammar in the following html codenn<p>this is som txt which needs to b fixed.<p>';
    
    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'Authorization': bearer,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            temperature: 1,
            top_p: 1,
            n: 1,
            stream: true,
            logprobs: null,
            stop: 'n',
            model: 'gpt-4o',
            messages: [
                { "role": "user", "content": prompt }
            ]
        }),
        signal: controller.signal
    });
    
    const reader = response.body.getReader();
    const decoder = new TextDecoder("utf-8");
    
    let result = '';
    
    while (true) {
        const { done, value } = await reader.read();
        if (done) {
            break;
        }
        // Massage and parse the chunk of data
        const chunk = decoder.decode(value);
        const lines = chunk.split("n");
        const parsedLines = lines
            .map((line) => line.replaceAll(/^data: /gi, "").trim()) // Remove the "data: " prefix
            .filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
            .map((line) => JSON.parse(line)); // Parse the JSON string
    
        for (const parsedLine of parsedLines) {
            const { choices } = parsedLine;
            const { delta } = choices[0];
            const { content } = delta;
            // Update the UI with the new content
            if (content) {
                console.log("Content: " + content);
                result += content;
            }
        }
    }
    console.log('Result:n' + result);

``

2

Answers


  1. Chosen as BEST ANSWER

    I finally found it out. The error was the stop param which caused the API to stop generating tokens.

    Furthermore, I simplified the code for better readability. This code works:

    const prompt = 'fix spelling and grammar in the following html codenn<p>this is som txt which needs to b fixed.<p>';
    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': bearer
        },
        body: JSON.stringify({
            model: 'gpt-4o',
            messages: [{ role: "user", content: prompt }],
            stream: true
        })
    });
    
    // Read the response as a stream of data
    const reader = response.body.getReader();
    const decoder = new TextDecoder("utf-8");
    result = "";
    
    while (true) {
        const { done, value } = await reader.read();
        if (done) {
            break;
        }
        // Massage and parse the chunk of data
        const chunk = decoder.decode(value);
        const lines = chunk.split("n");
        const parsedLines = lines
            .map((line) => line.replace(/^data: /gi, "").trim()) // Remove the "data: " prefix
            .filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
            .map((line) => JSON.parse(line)); // Parse the JSON string
    
        for (const parsedLine of parsedLines) {
            const { choices } = parsedLine;
            const { delta } = choices[0];
            const { content } = delta;
            // Update the UI with the new content
            if (content) {
                result += content;
                appendText(content);
            }
        }
    }
    

  2. In the past I wrote some codes that are working with ChatGPT and I edited my code. Very common issue while working with ChatGPT API is that sometimes you have to write him: You are ChatGPT, a large language model trained by OpenAI. . This code is a version for Node.js so there might be problems with running with HTML.

    async function startChat() {
    let message = "fix spelling and grammar in the following html code <p>this is som txt which needs to b fixed.<p>";
    
    // Emulate sessionStorage in Node.js
    let sessionStorage = {
      data: {},
      getItem: function (key) {
        return this.data[key] || null;
      },
      setItem: function (key, value) {
        this.data[key] = value;
      }
    };
    
    let messages = sessionStorage.getItem("bot-message");
    if (messages == null) {
      messages = [{ role: "system", content: "You are ChatGPT, a large language model trained by OpenAI." }];
    } else {
      messages = JSON.parse(messages);
    }
    messages.push({ role: "user", content: message });
    
    var res = await fetch(
      "https://api.openai.com/v1/chat/completions",
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: "API KEY",
        },
        method: "POST",
        body: JSON.stringify({
          model: "gpt-3.5-turbo",
          messages: messages,
          stop: ["nn"],
        }),
      }
    );
    
    const data = await res.json();
    let a = data.choices[0].message.content;
    
    // Store the updated messages back in our emulated sessionStorage
    sessionStorage.setItem("bot-message", JSON.stringify(messages));
    
    console.log(a);
     }startChat();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search