Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for chat with history #70

Open
bibhas2 opened this issue Feb 19, 2024 · 3 comments
Open

Add example for chat with history #70

bibhas2 opened this issue Feb 19, 2024 · 3 comments

Comments

@bibhas2
Copy link

bibhas2 commented 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)
@connor-makowski
Copy link

Related to:

#63

And:

#64

@u1i
Copy link

u1i commented Mar 11, 2024

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

@Coddyy
Copy link

Coddyy commented 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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants