Spaces:
Sleeping
Sleeping
Download memory_backend.py from Kumardba224/memory-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/spaces/Kumardba224/memory-chatbot/resolve/main/memory_backend.py
- Command line
-
hf download hf://spaces/Kumardba224/memory-chatbot/memory_backend.py
-
curl -L -o memory_backend.py https://huggingface.co/spaces/Kumardba224/memory-chatbot/resolve/main/memory_backend.py
1.25 kB
| import os | |
| from dotenv import load_dotenv | |
| from langchain_groq import ChatGroq | |
| from langchain.memory import ConversationBufferMemory | |
| from langchain.chains import ConversationChain | |
| # ----------------------------------- | |
| # Load environment variables | |
| # ----------------------------------- | |
| load_dotenv() | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| if not GROQ_API_KEY: | |
| raise ValueError( | |
| "Missing GROQ_API_KEY" | |
| ) | |
| # ----------------------------------- | |
| # LLM | |
| # ----------------------------------- | |
| llm = ChatGroq( | |
| groq_api_key=GROQ_API_KEY, | |
| model_name="llama-3.1-8b-instant", | |
| temperature=0.3, | |
| max_tokens=300 | |
| ) | |
| # ----------------------------------- | |
| # Memory | |
| # ----------------------------------- | |
| memory = ConversationBufferMemory() | |
| # ----------------------------------- | |
| # Conversation Chain | |
| # ----------------------------------- | |
| conversation = ConversationChain( | |
| llm=llm, | |
| memory=memory, | |
| verbose=False | |
| ) | |
| # ----------------------------------- | |
| # Chat Function | |
| # ----------------------------------- | |
| def chat_with_memory(user_input): | |
| try: | |
| response = conversation.predict( | |
| input=user_input | |
| ) | |
| return response | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" |