ollama-js icon indicating copy to clipboard operation
ollama-js copied to clipboard

How to create a separate chat via API?

Open Aviv1000 opened this issue 1 year ago • 3 comments

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?

Aviv1000 avatar Jun 13 '24 12:06 Aviv1000

Just tested this exact same code and the same doesn't happen for me

hopperelec avatar Jun 13 '24 16:06 hopperelec

for one separate chat (in pseudo code):

  1. create empty chat=[] or [{role:'system', content:'...you system prompt here...'}]
  2. add new message: chat=[...chat, message]
  3. await ollama.chat({ model: 'llama2', messages: chat, stream: true })
  4. after get response add answer: chat=[...chat, answer] (answer must be {role:'assistant', content: 'ai bla-bla-bla'})
  5. goto 2

save this chat for late as you wish

imareo avatar Jun 15 '24 06:06 imareo

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.

ZaMeR12 avatar Sep 12 '24 03:09 ZaMeR12