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

Python bindings: add possibility to clear history of a chat_session #2358

Open
woheller69 opened this issue May 16, 2024 · 4 comments
Open
Labels
enhancement New feature or request python-bindings gpt4all-bindings Python specific issues

Comments

@woheller69
Copy link
Contributor

Feature Request

Once initiating a chat session by

 with gpt4all_instance.chat_session(self.sysprompt, self.prompt):

there is no way to start a new session without leaving and restarting the CLI.

What about adding something like gpt4all_instance.chat_session.reset() to start a new chat with the same model

@simonw
Copy link
Contributor

simonw commented May 17, 2024

The session should be reset at the end of the with block. Try this:

from gpt4all import GPT4All
model = GPT4All("Phi-3-mini-4k-instruct.Q4_0.gguf")

with model.chat_session():
    print(model.generate("2 fun names for a pelican"))
    print(model.generate("2 more"))

with model.chat_session():
    print("-- should have reset --")
    print(model.generate("2 more"))

I get this:

  1. "Pelico the Plunge" - A playful take on their diving behavior, suggesting they enjoy taking plunges into water to catch fish!

  2. "Scoop Squad Pelicans" – Highlights how these birds skillfully scoop up prey with their large pouched bills while also being a fun and memorable name for an avian-themed team or group activity.


  1. "Neck Divers of the Bayou" – This title captures both the unique neck dive behavior pelicans are known for, as well as their habitat in bayous where they can often be spotted fishing and diving with ease.

  2. "The Scaly Surfers of the Sea" - A whimsical name that plays on the idea of pelicans riding ocean waves to catch food while also incorporating a nod to their scaly appearance, which is distinctive among birds.

-- should have reset --

It seems like your request was cut off. However, I'll assume you want to know what "2 more" could mean in a common context and provide an answer for that scenario: [...]

@woheller69
Copy link
Contributor Author

that does not work for me. In my tkinter app mainloop() must at the end of the with statement.
So I would have to restart the app which - with the latest Python bindings - is more time consuming than before #2354.
Therefore I would like to stay within the contextmanager and just clear history.

@simonw
Copy link
Contributor

simonw commented May 17, 2024

You might be able to achieve the same effect by calling __enter__() and __exit__() at the right moments instead of using with:

Try something like this:

from gpt4all import GPT4All

model = GPT4All("Phi-3-mini-4k-instruct.Q4_0.gguf")

# First chat session
session = model.chat_session()
session.__enter__()
try:
    print(model.generate("2 fun names for a pelican"))
    print(model.generate("2 more"))
finally:
    session.__exit__(None, None, None)

# Second chat session
session = model.chat_session()
session.__enter__()
try:
    print("-- should have reset --")
    print(model.generate("2 more"))
finally:
    session.__exit__(None, None, None)

You can keep that session object as self.session or similar.

@woheller69
Copy link
Contributor Author

Excellent, thanks! That works.

https://github.com/woheller69/gpt4all-TK-CHAT/blob/main/appGUI.py

@cebtenzzre cebtenzzre reopened this May 18, 2024
@cebtenzzre cebtenzzre added the python-bindings gpt4all-bindings Python specific issues label May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request python-bindings gpt4all-bindings Python specific issues
Projects
None yet
Development

No branches or pull requests

3 participants