Spaces:
Runtime error
Runtime error
Fix chat crash: Add pysqlite3-binary for Linux, improve error handling, add test script
Browse filesFiles changed (4)
- requirements.txt+1-1
- reset_key.py+4-3
- test_bot_accuracy.pyAdded+64
- website_chat_router.py+12-8
22 unmodified lines2324252627282922 unmodified lines# Force rebuild v4 - Using langchain 0.0.354 with ConversationalRetrievalChain#--- Vector Store ---pysqlite3-binarychromadb#--- Document Processing ---22 unmodified lines2324252627282922 unmodified lines# Force rebuild v4 - Using langchain 0.0.354 with ConversationalRetrievalChain#--- Vector Store ---pysqlite3-binary ; sys_platform == 'linux'chromadb#--- Document Processing ---
123456789101112import osimport sysfrom werkzeug.security import generate_password_hashfrom sqlalchemy.orm import Sessionimport database as db# Force local SQLiteos.environ["DATABASE_URL"] = "sqlite:///./data/users.db"def reset_local_key():print("π Resetting API Key for 'LocalClient'...")12345678910111213import osimport sys# Force local SQLite (MUST be before importing database)os.environ["DATABASE_URL"] = "sqlite:///./data/users.db"from werkzeug.security import generate_password_hashfrom sqlalchemy.orm import Sessionimport database as dbdef reset_local_key():print("π Resetting API Key for 'LocalClient'...")
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import requestsimport jsonimport sysimport os
# ConfigurationAPI_URL = "https://leehan23-nutribot-api.hf.space"# API_URL = "http://localhost:8000" # Uncomment for local testing
# Questions to test (based on uploaded documents)TEST_QUESTIONS = [ "What is the RNI for protein for adult men in Malaysia?", "What are the key dietary guidelines for older people?", "How much calcium is recommended for adolescents?", "Explain the food pyramid for children.", "What is the recommended water intake?"]
def run_tests(): print("π§ͺ Starting Bot Accuracy Tests...") print(f"Target: {API_URL}") # Get API Key api_key = input("\nπ Enter your API Key (from reset_key.py): ").strip() if not api_key: print("β API Key required!") return
print("\n" + "="*50) for i, question in enumerate(TEST_QUESTIONS, 1): print(f"\nβ Question {i}: {question}") print("-" * 20) try: # Note: The API streams the response, but requests.post handles it. # We'll just read the whole stream for simplicity. response = requests.post( f"{API_URL}/chat/get_response", headers={"X-API-Key": api_key}, json={"question": question, "session_id": "test_session_1"}, stream=True ) if response.status_code == 200: print("π€ Answer: ", end="", flush=True) full_answer = "" for chunk in response.iter_content(chunk_size=None): if chunk: text = chunk.decode('utf-8') print(text, end="", flush=True) full_answer += text print("\n") else: print(f"β Error {response.status_code}: {response.text}") except Exception as e: print(f"β Connection Error: {e}") print("\n" + "="*50) print("β
Tests Complete!")
if __name__ == "__main__": run_tests()17 unmodified lines181920212223242526272829303117 unmodified lines"""Streams the RAG response using the client's knowledge base."""response_data = rag.get_rag_response(question=question,client_id=client_id,chat_session_id=session_id)for chunk in response_data.get("answer", ""):yield chunkawait asyncio.sleep(0.01)@chat_router.post("/get_response")async def get_chat_response(17 unmodified lines18192021222324252627282930313233343517 unmodified lines"""Streams the RAG response using the client's knowledge base."""try:response_data = rag.get_rag_response(question=question,client_id=client_id,chat_session_id=session_id)for chunk in response_data.get("answer", ""):yield chunkawait asyncio.sleep(0.01)except Exception as e:print(f"β RAG Error: {e}")yield f"I encountered an error processing your request: {str(e)}"@chat_router.post("/get_response")async def get_chat_response(
requirements.txt+1-1
reset_key.py+4-3
test_bot_accuracy.pyAdded+64
website_chat_router.py+12-8