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

Add example for chat with history

Open bibhas2 opened this issue 4 months ago • 2 comments

Chat with history is perhaps the most common use case. In fact ollama run works like that. An example with that use case will be great for the newcomers. Here's a sample code:

import ollama

messages = []

def send(chat):
  messages.append(
    {
      'role': 'user',
      'content': chat,
    }
  )
  stream = ollama.chat(model='mistral:instruct', 
    messages=messages,
    stream=True,
  )

  response = ""
  for chunk in stream:
    part = chunk['message']['content']
    print(part, end='', flush=True)
    response = response + part

  messages.append(
    {
      'role': 'assistant',
      'content': response,
    }
  )

  print("")

while True:
    chat = input(">>> ")

    if chat == "/exit":
        break
    elif len(chat) > 0:
        send(chat)

bibhas2 avatar Feb 19 '24 08:02 bibhas2

Related to:

https://github.com/ollama/ollama-python/issues/63

And:

https://github.com/ollama/ollama-python/pull/64

connor-makowski avatar Feb 20 '24 15:02 connor-makowski

Examples are always great! How about adding: system prompt & temperature setting?

u1i avatar Mar 11 '24 00:03 u1i