Can anyone help me update this davinci code? :(

Hi there!

Any help would be eternally appreciated!

I’m just working locally so no security issues to be concerned about just yet - but if I update this code to use 3.5 turbo or paste the code provided by 3.5’s playground it won’t work. I’m no expert and I’ve definitely thrown myself in at the deep end. I have picked it apart many times but can’t seem to get anywhere. Thanks for reading!

	<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        const conversationDiv = document.getElementById("conversation");
        const inputForm = document.getElementById("input-form");
        const userInput = document.getElementById("user-input");

        inputForm.addEventListener("submit", (e) => {
            e.preventDefault();
            const message = userInput.value.trim();
            if (message) {
                addMessageToConversation("user", message);
                userInput.value = "";
                sendMessageToGPT(message);
            }
        });

        function addMessageToConversation(sender, message) {
            const messageDiv = document.createElement("div");
            messageDiv.className = "message";
            messageDiv.innerHTML = `<span class="${sender}">${sender === "user" ? "You" : "ChatGPT"}:</span> ${message}`;
            conversationDiv.appendChild(messageDiv);
            conversationDiv.scrollTop = conversationDiv.scrollHeight;
        }

        async function sendMessageToGPT(message) {
                    const apiKey = "APIKEY";
                    const apiUrl = "https://api.openai.com/v1/completions";

                    try {
                        const response = await axios.post(apiUrl, {
                            "model": "text-davinci-002",
                            "prompt": message,
                            "temperature": 0.9,
                            "max_tokens": 150,
                            "top_p": 1,
                            "frequency_penalty": 0,
                            "presence_penalty": 0.6,
                            "stop": [" Human:", " AI:"],
                            }, {
                            headers: {
                                'Content-Type': 'application/json',
                                'Authorization': `Bearer ${apiKey}`
                            }
                        });
                        debugger;
                        const data = response.data.choices[0].text.trim();
                        console.log(data);
                        addMessageToConversation("gpt", data);
                    } catch (error) {
                        console.error(error);
                        addMessageToConversation("gpt", "Error: Unable to fetch response, please come back later.");
                    }
            }

    </script>

P.s APIkey removed for this post.

Many thanks!
G

Don’t tell anybody, but if you click “API reference” at the top or edge of the forum, you can learn all the secrets!

  1. https://api.openai.com/v1/completions
    this URL needs to be updated
    https://api.openai.com/v1/chat/completions

  2. prompt": message
    will need to be changed to
    "messages": a list of message arrays (dictionaries/json))

  3. then you might need to make more significant changes to how you create that list of messages, including a system role message that contains bot programming separate from a user message that contains instructions or data.

1 Like

You can also use the new gpt-3.5-turbo-instruct model with your current set-up…

More on endpoint compatability for LLM models

3 Likes