puter.ai.chat()


Given a prompt returns the completion that best matches the prompt.

Syntax

Code copied
puter.ai.chat(prompt, testMode = false)
puter.ai.chat(prompt, imageURL, testMode = false)
puter.ai.chat(prompt, [imageURLArray], testMode = false)
puter.ai.chat([messages], testMode = false)

Parameters

prompt (String) (Required)

A string containing the prompt you want to complete.

testMode (Boolean) (Optional)

A boolean indicating whether you want to use the test API. Defaults to false. This is useful for testing your code without using up API credits.

imageURL (String)

A string containing the URL of an image you want to provide as context for the completion. Also known as "GPT Vision".

imageURLArray (Array)

An array of strings containing the URLs of images you want to provide as context for the completion.

messages (Array)

An array of objects containing the messages you want to complete. Each object must have a role and a content property. The role property must be one of system, assistant, user, or function. The content property must be a string containing the message. An example of a valid messages parameter is:

Code copied
[
    {
        role: 'system',
        content: 'Hello, how are you?'
    },
    {
        role: 'user',
        content: 'I am doing well, how are you?'
    },
]

Providing a messages array is especially useful for building chatbots where you want to provide context to the completion.

Return value

Will resolve to a string containing the completion that best matches the prompt.

In case of an error, the Promise will reject with an error message.

Examples

Ask GPT-3.5 Turbo a question

Code copied
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        puter.ai.chat(`What color was Napoleon's white horse?`).then((response) => {
            puter.print(response);
        });
    </script>
</body>
</html>

GPT-4 Vision

Code copied
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <img src="https://assets.puter.site/doge.jpeg" style="display:block;">
    <script>
        puter.ai.chat(
            `What do you see?`, 
            `https://assets.puter.site/doge.jpeg`)
        .then((response) => {
            puter.print(response);
        });
    </script>
</body>
</html>