ollama / ollama-python

Ollama Python library
https://ollama.com
MIT License
2.71k stars 223 forks source link

Add example for chat with history #70

Open bibhas2 opened 4 months ago

bibhas2 commented 4 months ago

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)
connor-makowski commented 4 months ago

Related to:

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

And:

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

u1i commented 3 months ago

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