puter.ai.chat()
Given a prompt returns the completion that best matches the prompt.
puter.ai.chat(prompt)
puter.ai.chat(prompt, options = {})
puter.ai.chat(prompt, testMode = false, options = {})
puter.ai.chat(prompt, imageURL, testMode = false, options = {})
puter.ai.chat(prompt, [imageURLArray], testMode = false, options = {})
puter.ai.chat([messages], testMode = false, options = {})
prompt
(String)
A string containing the prompt you want to complete.
options
(Object) (Optional)
An object containing the following properties:
model
(String) - The model you want to use for the completion. Defaults to gpt-4o-mini
. The following models are available:gpt-4o-mini
(default)gpt-4o
claude-3-5-sonnet
meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo
meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo
meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo
mistral-large-latest
codestral-latest
google/gemma-2-27b-it
stream
(Boolean) - A boolean indicating whether you want to stream the completion. Defaults to false
.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:
[
{
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.
When stream
is set to false
(default):
When stream
is set to true
:
for await...of
loop to receive the response in parts as they become available.In case of an error, the Promise
will reject with an error message.
Ask GPT-4o mini a question
<html>
<body>
<script src="https://js.puter.com/v2/"></script>
<script>
puter.ai.chat(`What is life?`).then(puter.print);
</script>
</body>
</html>
GPT-4 Vision
<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(puter.print);
</script>
</body>
</html>
Stream the response
<html>
<body>
<script src="https://js.puter.com/v2/"></script>
<script>
(async () => {
const resp = await puter.ai.chat('Tell me in detail what Rick and Morty is all about.', {model: 'claude', stream: true });
for await ( const part of resp ) document.write(part?.text.replaceAll('\n', '<br>'));
})();
</script>
</body>
</html>