Download app/memory.py from Remostartdev/FARMLINGUA_FARMAI: direct link, hf CLI and curl.
- Browser
- Download file 698 Bytes
-
https://huggingface.co/spaces/Remostartdev/FARMLINGUA_FARMAI/resolve/main/app/memory.py
- Command line
-
hf download hf://spaces/Remostartdev/FARMLINGUA_FARMAI/app/memory.py
-
curl -L -o memory.py https://huggingface.co/spaces/Remostartdev/FARMLINGUA_FARMAI/resolve/main/app/memory.py
698 Bytes
| from collections import defaultdict | |
| from typing import Dict, List | |
| from app.config import config | |
| # { uid: { "text": [...], "stt": [...] } } | |
| _store: Dict[str, Dict[str, List[dict]]] = defaultdict( | |
| lambda: {"text": [], "stt": []} | |
| ) | |
| def get_history(uid: str, channel: str) -> List[dict]: | |
| return list(_store[uid][channel]) | |
| def append_turn(uid: str, channel: str, role: str, content: str) -> None: | |
| history = _store[uid][channel] | |
| history.append({"role": role, "content": content}) | |
| max_messages = config.MAX_HISTORY_TURNS * 2 | |
| if len(history) > max_messages: | |
| _store[uid][channel] = history[-max_messages:] | |
| def clear_session(uid: str) -> None: | |
| _store.pop(uid, None) |