Add example for chat with history #36

Open
opened 2026-02-15 16:27:36 -05:00 by yindo · 3 comments
Owner

Originally created by @bibhas2 on GitHub (Feb 19, 2024).

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)
Originally created by @bibhas2 on GitHub (Feb 19, 2024). 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: ```python 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) ```
Author
Owner

@connor-makowski commented on GitHub (Feb 20, 2024):

Related to:

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

And:

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

@connor-makowski commented on GitHub (Feb 20, 2024): Related to: https://github.com/ollama/ollama-python/issues/63 And: https://github.com/ollama/ollama-python/pull/64
Author
Owner

@u1i commented on GitHub (Mar 10, 2024):

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

@u1i commented on GitHub (Mar 10, 2024): Examples are always great! How about adding: system prompt & temperature setting?
Author
Owner

@Coddyy commented on GitHub (Dec 9, 2024):

I am trying to integrate tool calling with chat history, if some function(tools) parameters are missing in user's question then it should come back and ask for the parameter, to get this working I need the previous context.

response = ollama.chat(
            'llama3.2',
            messages=[{'role': 'user', 'content': query}],
            tools=[add_posts, add_numbers, substract_numbers, general_query],
            stream=True # without this whole code works fine! but need chat history
        )

        print('***** Checking available tools')
        if response.message.tool_calls:
            for tool in response.message.tool_calls:
                if function_to_call := available_functions.get(tool.function.name):
                    print('Calling Tool:', tool.function.name)
                    print('Output', function_to_call(**tool.function.arguments))
                else:
                    print('No Tools Found, Activating RAG......')
                    print(general_query(query))

Getting
AttributeError: 'generator' object has no attribute 'message'

Thanks in advance!!

@Coddyy commented on GitHub (Dec 9, 2024): I am trying to integrate tool calling with chat history, if some function(tools) parameters are missing in user's question then it should come back and ask for the parameter, to get this working I need the previous context. ``` response = ollama.chat( 'llama3.2', messages=[{'role': 'user', 'content': query}], tools=[add_posts, add_numbers, substract_numbers, general_query], stream=True # without this whole code works fine! but need chat history ) print('***** Checking available tools') if response.message.tool_calls: for tool in response.message.tool_calls: if function_to_call := available_functions.get(tool.function.name): print('Calling Tool:', tool.function.name) print('Output', function_to_call(**tool.function.arguments)) else: print('No Tools Found, Activating RAG......') print(general_query(query)) ``` Getting `AttributeError: 'generator' object has no attribute 'message'` Thanks in advance!!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#36