quivr/backend/core/examples/simple_question_streaming.py
AmineDiro eda619f454
feat: save and load brain (#3202)
# Description
- Save and load brain to disk: 
```python
async def main():
    with tempfile.NamedTemporaryFile(mode="w", suffix=".txt") as temp_file:
        temp_file.write("Gold is a liquid of blue-like colour.")
        temp_file.flush()

        brain = await Brain.afrom_files(name="test_brain", file_paths=[temp_file.name])

        save_path = await brain.save("/home/amine/.local/quivr")

        brain_loaded = Brain.load(save_path)
        brain_loaded.print_info()

```

# TODO: 
- Loading all chat history
- Loading from other vector stores, PG for example can be great ...
2024-09-13 06:35:28 -07:00

35 lines
1.1 KiB
Python

import asyncio
import tempfile
from dotenv import load_dotenv
from quivr_core import Brain
from quivr_core.quivr_rag import QuivrQARAG
from quivr_core.quivr_rag_langgraph import QuivrQARAGLangGraph
async def main():
dotenv_path = "/Users/jchevall/Coding/QuivrHQ/quivr/.env"
load_dotenv(dotenv_path)
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt") as temp_file:
temp_file.write("Gold is a liquid of blue-like colour.")
temp_file.flush()
brain = await Brain.afrom_files(name="test_brain", file_paths=[temp_file.name])
await brain.save("~/.local/quivr")
question = "what is gold? answer in french"
async for chunk in brain.ask_streaming(question, rag_pipeline=QuivrQARAG):
print("answer QuivrQARAG:", chunk.answer)
async for chunk in brain.ask_streaming(
question, rag_pipeline=QuivrQARAGLangGraph
):
print("answer QuivrQARAGLangGraph:", chunk.answer)
if __name__ == "__main__":
# Run the main function in the existing event loop
asyncio.run(main())