Chat

The module allows to start a chat very easily. The history is persisted in a ref() and therefore automatically updates in the UI.

const { messages, send } = useGPT().createChat()

.createChat(options)

Creates a new chat instance.

type options = {
   systemPrompt?: string
   stream?: boolean
}

Parameters

keyTypeDefaultDescription
systemPromptstringoptionalLets you start the chat with a priming-message of role system.
streambooleanoptionalAdds the answer word-by-word with a delay of 50ms.

systemPrompt can be used to define the style of the conversation like .createChat({ systemPrompt: 'You are a polite sales person' })

Return values

Returns a messages variable and a send(prompt, onSubmit) function

keyTypeDefaultDescription
idstringThe internal uuid of the chat
messagesref<Message[]>[]Contains all messages from both the user and the assistant in historical order.
send(prompt, onSubmit):FunctionIs used to submit a new message to the chat. Accepts a callback function onSubmit e.g. for reseting an input field.
statusstringidleGives the current state of the assistant as idle, thinking or streaming (if stream: true).
type Message = {
   id: string
   role: 'system' | 'assistant' | 'user'
   message: string
   datetime_js: number
}

Example:

const { messages, send } = useGPT().createChat()

await send('Hi how are you?')

Minimal Demo

<script setup lang="ts">
const { messages, send, status } = useGPT().createChat({ stream: true })
const input = ref<string>('')
</script>

<template>
   <pre>Status: {{ status }}</pre>
   <pre>{{ messages }}</pre>
   <input
      type="text"
      v-model="input"
      @keyup.enter="send(input, () => (input = ''))" />
</template>