Download chatbot.py from Ayush-003/ThinkOV2: direct link, hf CLI and curl.
- Browser
- Download file 3.11 kB
-
https://huggingface.co/spaces/Ayush-003/ThinkOV2/resolve/main/chatbot.py
- Command line
-
hf download hf://spaces/Ayush-003/ThinkOV2/chatbot.py
-
curl -L -o chatbot.py https://huggingface.co/spaces/Ayush-003/ThinkOV2/resolve/main/chatbot.py
3.11 kB
| """ | |
| Backward-compatible Chatbot class for simple Q&A | |
| For advanced features, use agent.py instead | |
| """ | |
| import asyncio | |
| from typing import Optional | |
| from agent import agent | |
| from memory import session_manager | |
| from rag_chain import RAGChain | |
| from schemas import ChatContext, ChatRequest | |
| import uuid | |
| class Chatbot: | |
| """Simple chatbot wrapper for backward compatibility""" | |
| def __init__(self, qa_chain=None, vectorstore=None): | |
| self.qa_chain = qa_chain | |
| self.vectorstore = vectorstore | |
| self.rag_chain = RAGChain(vectorstore) if vectorstore else None | |
| def get_response(self, user_input: str, session_id: Optional[str] = None) -> str: | |
| """ | |
| Get response for user input (synchronous wrapper) | |
| For simple use cases. For advanced features, use agent.process_message() | |
| """ | |
| try: | |
| # Use the old qa_chain if available | |
| if self.qa_chain: | |
| response = self.qa_chain({"query": user_input}) | |
| return response['result'] | |
| # Otherwise use the new agent system | |
| return asyncio.run(self._async_get_response(user_input, session_id)) | |
| except Exception as e: | |
| return f"An error occurred: {str(e)}" | |
| async def _async_get_response(self, user_input: str, session_id: Optional[str] = None) -> str: | |
| """Async response using new agent system""" | |
| session_id = session_id or str(uuid.uuid4()) | |
| # Get conversation history | |
| convo_history = await session_manager.get_conversation_history(session_id) | |
| # Get retrieved documents | |
| retrieved_docs = [] | |
| if self.rag_chain: | |
| retrieved_docs = await self.rag_chain.retrieve_documents(user_input) | |
| # Build context | |
| context = ChatContext( | |
| user_message=user_input, | |
| convo_history=convo_history, | |
| session_id=session_id, | |
| retrieved_docs=retrieved_docs, | |
| available_tools=[], | |
| locale="en-IN", | |
| timezone="Asia/Kolkata" | |
| ) | |
| # Process with agent | |
| response = await agent.process_message(context) | |
| return response.reply_text | |
| if __name__ == "__main__": | |
| from rag_chain import RAGChain | |
| from documentProcessor import DocumentProcessor | |
| from embedding_indexer import EmbeddingIndexer | |
| processor = DocumentProcessor("ecommerce_faq.txt") | |
| texts = processor.load_and_split() | |
| indexer = EmbeddingIndexer() | |
| vectorstore = indexer.create_vectorstore(texts) | |
| rag_chain = RAGChain(vectorstore) | |
| qa_chain = rag_chain.create_chain() | |
| chatbot = Chatbot(qa_chain) | |
| print("Chatbot initialized! Type 'exit' to quit.\n") | |
| while True: | |
| user_input = input("You: ") | |
| if user_input.lower() in ['exit', 'quit', 'bye']: | |
| print("Chatbot: Goodbye!") | |
| break | |
| response = chatbot.get_response(user_input) | |
| print(f"Chatbot: {response}\n") | |