minimaxir / simpleaichat

Python package for easily interfacing with chat apps, with robust features and minimal code complexity.
MIT License
3.48k stars 228 forks source link

ai.load_session() doesn't load messages #83

Open pyrotank41 opened 1 year ago

pyrotank41 commented 1 year ago

while loading a saved session, and I noticed the load_session() function never loads the messages from the saved session. Was that intended? or am i missing something?

to work around it i am using using the following code instead of using ai.load_session()

with open("chat_session.json", "rb") as f:
                sess_dict = orjson.loads(f.read())
ai.new_session(**sess_dict, api_key=OPENAI_API_KEY)

list_dict = sess_dict['messages']
chat_messages = [ChatMessage.model_validate(item) for item in list_dict]

ai.default_session.messages = chat_messages

i can create a pull request if the maintainers of this repository find this to be a bug

CyberDefend3r commented 4 months ago

It does load the messages from a saved session file. This is the main reason I use the library, it makes few-shot prompting super simple. I have saved session files with fake user and assistant messages so that it knows how to respond to future prompts.

I did try but I can't seem to replicate your issue. All the messages load for me when using the load_session() method and I verified by dumping the messages using the get_session() method. Is it possible that the session file you are loading doesn't have any messages or maybe not formatted correctly?

Here is the code I used to test:

# create an instance of AIChat class
ai = AIChat(api_key=_key_, console=False, model="gpt-4-turbo", params={"temperature": 0.78, "max_tokens": 2048})

# Load a saved session JSON file
ai.load_session(api_key=_key_, input_path="./ai_context/defender_generic.json")

# Dump the messages in that session as a list of ChatMessage objects
ai.get_session(id="defender").messages

EXAMPLE:
image