try { | |
const response = await fetch('/api/chat', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ | |
question, | |
history, | |
}), | |
}); | |
const data = await response.json(); | |
setMessageState((state) => ({ | |
...state, | |
messages: [ | |
...state.messages, | |
{ | |
type: 'apiMessage', | |
message: data.text, | |
sourceDocs: data.sourceDocuments, | |
}, | |
], | |
history: [...state.history, [question, data.text]], | |
})); | |
} |