ollama-js
ollama-js copied to clipboard
How to create a separate chat via API?
import ollama from 'ollama'
const message = { role: 'user', content: 'What color of the sky?' }
const response = await ollama.chat({ model: 'llama2', messages: [message], stream: true })
for await (const part of response) {
process.stdout.write(part.message.content)
}
const message2 = { role: 'user', content: 'What was my first prompt?' }
const response = await ollama.chat({ model: 'llama2', messages: [message2], stream: true })
for await (const part of response) {
process.stdout.write(part.message.content)
}
//result: your first prompt was "What color of the sky?"
The documentation is not clear enough, How am I supposed to save a session of the current conversation (to continue talking later) and create a new separate chat conversation?
Just tested this exact same code and the same doesn't happen for me
for one separate chat (in pseudo code):
- create empty chat=[] or [{role:'system', content:'...you system prompt here...'}]
- add new message: chat=[...chat, message]
- await ollama.chat({ model: 'llama2', messages: chat, stream: true })
- after get response add answer: chat=[...chat, answer] (answer must be {role:'assistant', content: 'ai bla-bla-bla'})
- goto 2
save this chat for late as you wish
import ollama from 'ollama' const message = { role: 'user', content: 'What color of the sky?' } const response = await ollama.chat({ model: 'llama2', messages: [message], stream: true }) for await (const part of response) { process.stdout.write(part.message.content) } const message2 = { role: 'user', content: 'What was my first prompt?' } const response = await ollama.chat({ model: 'llama2', messages: [message2], stream: true }) for await (const part of response) { process.stdout.write(part.message.content) } //result: your first prompt was "What color of the sky?"The documentation is not clear enough, How am I supposed to save a session of the current conversation (to continue talking later) and create a new separate chat conversation?
What do you mean by your example? Do you mean that even when you do the ollama.chat for the second time, ollama remeber your first prompt? Or you want Ollama to remember the first prompt the second time you call it? Be cause if it's the second case the answer of imareo seem to be what's you need. If it's the first case, well you need aother solution. So please answer back to this issue to we know if your problem is solve or not and what was your need clearly.