Coder Social home page Coder Social logo

Comments (5)

dosubot avatar dosubot commented on June 8, 2024 2

To ensure your conversation records are permanently saved in the chat_stores.json file with SimpleChatStore, you need to explicitly call the persist method after your chat interactions. Here's how you can do it:

chat_store.persist(persist_path="chat_stores.json")

This method should be called at points in your application where you want to save the chat history, such as after a conversation session ends or at regular intervals.

Additionally, there's a critical detail to be aware of regarding the persist method implementation. If you're encountering issues with the chat history not being saved, it might be due to incorrect indentation in the persist method within the SimpleChatStore class. Ensure that the block of code responsible for writing to the file is correctly indented to execute outside of any conditional directory existence checks. This adjustment ensures that the chat store data is written to the chat_stores.json file as intended.

For more detailed implementation and to verify the correct structure, you might want to review the source code of the SimpleChatStore class. This will help you ensure that your chat history is being saved correctly.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

from llama_index.

dosubot avatar dosubot commented on June 8, 2024 2

The issue with the chat_stores.json file not saving the conversation records correctly seems to stem from the serialization process of the store attribute within the SimpleChatStore. To ensure that the conversation records are properly saved, you need to serialize the store dictionary directly to JSON format before writing it to the file. This involves using json.dumps(self.store) to correctly serialize the store dictionary, which holds the conversation records, instead of relying on a potentially undefined self.json() method.

Here's how you can adjust the persist method to correctly handle the serialization:

def persist(
    self,
    persist_path: str = "chat_store.json",
    fs: Optional[fsspec.AbstractFileSystem] = None,
) -> None:
    """Persist the chat store to a file."""
    fs = fs or fsspec.filesystem("file")
    dirpath = os.path.dirname(persist_path)
    if not fs.exists(dirpath):
        fs.makedirs(dirpath)
    with fs.open(persist_path, "w") as f:
        f.write(json.dumps(self.store))  # Serialize the store dictionary correctly

This modification ensures that the store dictionary is properly converted to a JSON string and written to the specified file, addressing the issue with saving conversation records.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

from llama_index.

union-cmd avatar union-cmd commented on June 8, 2024

The data stored in stores.json is as follows

"{"store": {}, "class_name": "SimpleChatStore"}"

from llama_index.

union-cmd avatar union-cmd commented on June 8, 2024

This was not saved successfully

from llama_index.

logan-markewich avatar logan-markewich commented on June 8, 2024

@union-cmd You didn't persist the chat store.

This works fine for me

from llama_index.core.memory import ChatMemoryBuffer
from llama_index.core.storage.chat_store import SimpleChatStore
from llama_index.core import Document, VectorStoreIndex

index = VectorStoreIndex.from_documents([Document.example()])

chat_store = SimpleChatStore()
memory = ChatMemoryBuffer.from_defaults(
    token_limit=5000, 
    chat_store=chat_store, 
    chat_store_key="user1"
)

chat_engine = index.as_chat_engine(
    chat_mode="condense_plus_context",
    memory=memory
)

response = chat_engine.chat("hello!")
chat_store.persist("chat_store.json")

chat_store = SimpleChatStore.from_persist_path("chat_store.json")
memory = ChatMemoryBuffer.from_defaults(
    token_limit=5000, 
    chat_store=chat_store, 
    chat_store_key="user1"
)

print(memory.get_all())

from llama_index.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.