GitHub Actions
commited on
Commit
·
2d58a98
1
Parent(s):
76d1a86
Deploy backend from GitHub Actions
Browse files🚀 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
- rag/chat.py +71 -0
rag/chat.py
CHANGED
|
@@ -113,6 +113,48 @@ class ChatHandler:
|
|
| 113 |
if not session_id:
|
| 114 |
session_id = str(uuid.uuid4())
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# Get or create conversation context
|
| 117 |
context = self._get_or_create_context(session_id)
|
| 118 |
|
|
@@ -290,6 +332,35 @@ class ChatHandler:
|
|
| 290 |
if not session_id:
|
| 291 |
session_id = str(uuid.uuid4())
|
| 292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
# Get or create conversation context
|
| 294 |
context = self._get_or_create_context(session_id)
|
| 295 |
|
|
|
|
| 113 |
if not session_id:
|
| 114 |
session_id = str(uuid.uuid4())
|
| 115 |
|
| 116 |
+
# Handle greetings and very short queries
|
| 117 |
+
query_lower = query.strip().lower()
|
| 118 |
+
greetings = ['hi', 'hello', 'hey', 'yo', 'sup', 'greetings', 'good morning', 'good afternoon', 'good evening', 'assalamualikum', 'salam', 'assalam o alaikum']
|
| 119 |
+
|
| 120 |
+
if query_lower in greetings or len(query.strip()) <= 2:
|
| 121 |
+
# For greetings, provide a friendly response without searching
|
| 122 |
+
greeting_responses = [
|
| 123 |
+
"Hello! I'm here to help you learn about Physical AI and Humanoid Robotics. What would you like to know?",
|
| 124 |
+
"Hi there! I can help you with questions about humanoid robots and physical AI. What topic interests you?",
|
| 125 |
+
"Hey! I'm your AI assistant for the Physical AI & Humanoid Robotics book. How can I assist you today?",
|
| 126 |
+
"Greetings! Feel free to ask me anything about humanoid robotics, AI, or the content of this book.",
|
| 127 |
+
"Wa Alaikum Assalam! I'm happy to help you with Physical AI and Humanoid Robotics topics. What would you like to explore?"
|
| 128 |
+
]
|
| 129 |
+
|
| 130 |
+
import random
|
| 131 |
+
response_text = random.choice(greeting_responses)
|
| 132 |
+
|
| 133 |
+
# Send the greeting response
|
| 134 |
+
yield self._format_sse_message({
|
| 135 |
+
"type": "start",
|
| 136 |
+
"session_id": session_id,
|
| 137 |
+
"sources": [],
|
| 138 |
+
"retrieved_docs": 0
|
| 139 |
+
})
|
| 140 |
+
|
| 141 |
+
# Stream the response word by word for consistency
|
| 142 |
+
words = response_text.split()
|
| 143 |
+
for word in words:
|
| 144 |
+
yield self._format_sse_message({
|
| 145 |
+
"type": "chunk",
|
| 146 |
+
"content": word + " "
|
| 147 |
+
})
|
| 148 |
+
await asyncio.sleep(0.05) # Small delay for natural effect
|
| 149 |
+
|
| 150 |
+
yield self._format_sse_message({
|
| 151 |
+
"type": "done",
|
| 152 |
+
"session_id": session_id,
|
| 153 |
+
"response_time": 0.1,
|
| 154 |
+
"tokens_used": self.count_tokens(response_text)
|
| 155 |
+
})
|
| 156 |
+
return
|
| 157 |
+
|
| 158 |
# Get or create conversation context
|
| 159 |
context = self._get_or_create_context(session_id)
|
| 160 |
|
|
|
|
| 332 |
if not session_id:
|
| 333 |
session_id = str(uuid.uuid4())
|
| 334 |
|
| 335 |
+
# Handle greetings and very short queries
|
| 336 |
+
query_lower = query.strip().lower()
|
| 337 |
+
greetings = ['hi', 'hello', 'hey', 'yo', 'sup', 'greetings', 'good morning', 'good afternoon', 'good evening', 'assalamualikum', 'salam', 'assalam o alaikum']
|
| 338 |
+
|
| 339 |
+
if query_lower in greetings or len(query.strip()) <= 2:
|
| 340 |
+
# For greetings, provide a friendly response without searching
|
| 341 |
+
greeting_responses = [
|
| 342 |
+
"Hello! I'm here to help you learn about Physical AI and Humanoid Robotics. What would you like to know?",
|
| 343 |
+
"Hi there! I can help you with questions about humanoid robots and physical AI. What topic interests you?",
|
| 344 |
+
"Hey! I'm your AI assistant for the Physical AI & Humanoid Robotics book. How can I assist you today?",
|
| 345 |
+
"Greetings! Feel free to ask me anything about humanoid robotics, AI, or the content of this book.",
|
| 346 |
+
"Wa Alaikum Assalam! I'm happy to help you with Physical AI and Humanoid Robotics topics. What would you like to explore?"
|
| 347 |
+
]
|
| 348 |
+
|
| 349 |
+
import random
|
| 350 |
+
answer = random.choice(greeting_responses)
|
| 351 |
+
|
| 352 |
+
response_time = (datetime.utcnow() - start_time).total_seconds()
|
| 353 |
+
|
| 354 |
+
return ChatResponse(
|
| 355 |
+
answer=answer,
|
| 356 |
+
sources=[],
|
| 357 |
+
session_id=session_id,
|
| 358 |
+
query=query,
|
| 359 |
+
response_time=response_time,
|
| 360 |
+
tokens_used=self.count_tokens(answer),
|
| 361 |
+
model=self.model
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
# Get or create conversation context
|
| 365 |
context = self._get_or_create_context(session_id)
|
| 366 |
|