Spaces:
Sleeping
Sleeping
Commit ·
ab7d91b
1
Parent(s): ca7bcdc
improved prompt
Browse files
cosmicforge_ai_chatbot/cosmicforge_chatbot.py
CHANGED
|
@@ -21,15 +21,14 @@ class CosmicForgeMedicalChat:
|
|
| 21 |
await self.model.load_model()
|
| 22 |
|
| 23 |
def create_medical_chat_prompt(self, user_input: str) -> str:
|
| 24 |
-
return f"""You are CosmicForge, an advanced medical AI assistant
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
User Input: {user_input}
|
| 27 |
|
| 28 |
-
If the input is related to
|
| 29 |
-
Provide a thorough, professional response addressing the user's query. Include relevant medical information, potential considerations, general advice, and always emphasize the importance of professional medical consultation.
|
| 30 |
-
|
| 31 |
-
If the input is not related to medical or health topics, respond ONLY with:
|
| 32 |
-
"I'm CosmicForge, a medical AI assistant. I can only provide information and advice related to health and medical topics. For other subjects, please consult appropriate resources or experts."
|
| 33 |
"""
|
| 34 |
|
| 35 |
async def process_chat(self, user_input: str) -> Tuple[str, str]:
|
|
@@ -61,23 +60,38 @@ If the input is not related to medical or health topics, respond ONLY with:
|
|
| 61 |
clean_response = response.replace(prompt, "").strip()
|
| 62 |
lines = clean_response.split('\n')
|
| 63 |
clean_lines = [line for line in lines if not line.startswith("User Input:") and "CosmicForge" not in line]
|
| 64 |
-
final_response = '
|
| 65 |
return final_response
|
| 66 |
|
| 67 |
async def _post_process_response(self, response: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
clean_lines = []
|
|
|
|
| 69 |
for line in response.split('\n'):
|
| 70 |
line = line.strip()
|
| 71 |
-
if line:
|
| 72 |
-
clean_lines
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
clean_output = " ".join(clean_lines)
|
| 75 |
|
| 76 |
if "I'm CosmicForge, a medical AI assistant." in clean_output:
|
| 77 |
return clean_output
|
| 78 |
|
| 79 |
-
|
| 80 |
-
clean_output += " Important note: This information is for educational purposes only and does not constitute medical advice. Please consult a healthcare professional for personalized medical guidance, diagnosis, and treatment."
|
| 81 |
|
| 82 |
return clean_output
|
| 83 |
|
|
@@ -132,4 +146,4 @@ If the input is not related to medical or health topics, respond ONLY with:
|
|
| 132 |
except Exception as e:
|
| 133 |
logger.error(f"Unexpected error retrieving chat response status: {str(e)}", exc_info=True)
|
| 134 |
raise HTTPException(status_code=500, detail="Failed to retrieve chat response status")
|
| 135 |
-
return "not found"
|
|
|
|
| 21 |
await self.model.load_model()
|
| 22 |
|
| 23 |
def create_medical_chat_prompt(self, user_input: str) -> str:
|
| 24 |
+
return f"""You are CosmicForge, an advanced medical AI assistant. Provide concise, professional responses to medical queries including:
|
| 25 |
+
1. Key Information
|
| 26 |
+
2. Brief Explanation
|
| 27 |
+
3. Recommendations
|
| 28 |
|
| 29 |
User Input: {user_input}
|
| 30 |
|
| 31 |
+
If the input is not related to medical or health topics, respond ONLY with: "I'm CosmicForge, a medical AI assistant. I can only provide information on health and medical topics."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
async def process_chat(self, user_input: str) -> Tuple[str, str]:
|
|
|
|
| 60 |
clean_response = response.replace(prompt, "").strip()
|
| 61 |
lines = clean_response.split('\n')
|
| 62 |
clean_lines = [line for line in lines if not line.startswith("User Input:") and "CosmicForge" not in line]
|
| 63 |
+
final_response = '\n'.join(clean_lines).strip()
|
| 64 |
return final_response
|
| 65 |
|
| 66 |
async def _post_process_response(self, response: str) -> str:
|
| 67 |
+
response = response.replace('Response:', '').replace('"', '').replace("'", "").strip()
|
| 68 |
+
first_section = "Key Information:"
|
| 69 |
+
if first_section in response:
|
| 70 |
+
response = response[response.index(first_section):]
|
| 71 |
+
sections = ["Key Information:", "Brief Explanation:", "Recommendations:"]
|
| 72 |
clean_lines = []
|
| 73 |
+
current_section = ""
|
| 74 |
for line in response.split('\n'):
|
| 75 |
line = line.strip()
|
| 76 |
+
if any(section in line for section in sections):
|
| 77 |
+
if clean_lines:
|
| 78 |
+
clean_lines.append(" ") # Single space between sections
|
| 79 |
+
current_section = line.strip(':')
|
| 80 |
+
clean_lines.append(current_section + ":")
|
| 81 |
+
elif line:
|
| 82 |
+
if current_section == "Key Information":
|
| 83 |
+
clean_lines.append(line.strip('* '))
|
| 84 |
+
elif current_section == "Recommendations":
|
| 85 |
+
clean_lines.append("- " + line.lstrip('0123456789. '))
|
| 86 |
+
else:
|
| 87 |
+
clean_lines.append(line)
|
| 88 |
|
| 89 |
clean_output = " ".join(clean_lines)
|
| 90 |
|
| 91 |
if "I'm CosmicForge, a medical AI assistant." in clean_output:
|
| 92 |
return clean_output
|
| 93 |
|
| 94 |
+
clean_output += " Important note: This information is for educational purposes only and does not constitute medical advice. Please consult a healthcare professional for personalized medical guidance."
|
|
|
|
| 95 |
|
| 96 |
return clean_output
|
| 97 |
|
|
|
|
| 146 |
except Exception as e:
|
| 147 |
logger.error(f"Unexpected error retrieving chat response status: {str(e)}", exc_info=True)
|
| 148 |
raise HTTPException(status_code=500, detail="Failed to retrieve chat response status")
|
| 149 |
+
return "not found"
|