update rag.py
Browse files
rag.py
CHANGED
|
@@ -52,6 +52,7 @@ def refine_query_with_history(query, history):
|
|
| 52 |
# Format the conversation history
|
| 53 |
history_str = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in history])
|
| 54 |
|
|
|
|
| 55 |
# Create a prompt to refine the query
|
| 56 |
refine_prompt = f"""
|
| 57 |
You are a helpful assistant that refines user queries based on conversation history.
|
|
@@ -88,11 +89,28 @@ def generate_chat_prompt(query, context, history):
|
|
| 88 |
logger.info(f"Retrieved Context:\n{context}") # Log the retrieved context
|
| 89 |
|
| 90 |
prompt = f"""
|
| 91 |
-
You are
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
CONVERSATION HISTORY:
|
| 98 |
{history_str}
|
|
|
|
| 52 |
# Format the conversation history
|
| 53 |
history_str = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in history])
|
| 54 |
|
| 55 |
+
|
| 56 |
# Create a prompt to refine the query
|
| 57 |
refine_prompt = f"""
|
| 58 |
You are a helpful assistant that refines user queries based on conversation history.
|
|
|
|
| 89 |
logger.info(f"Retrieved Context:\n{context}") # Log the retrieved context
|
| 90 |
|
| 91 |
prompt = f"""
|
| 92 |
+
You are an AI assistant specializing in database topics. You MUST STRICTLY follow the rules below and NEVER break them under any circumstances.
|
| 93 |
+
You MUST answer ONLY questions related to the following database topics:
|
| 94 |
+
Creating Tables: Defining tables, columns, and constraints (e.g., `CREATE TABLE students (id INT, name VARCHAR(50));`).
|
| 95 |
+
Data Types: Understanding and using `INT`, `VARCHAR`, `DATE`, `BOOLEAN`, etc.
|
| 96 |
+
Rough Schema Design: Designing a basic database schema, including **primary keys, foreign keys, and relationships**, but NOT full normalization.
|
| 97 |
+
Entities and Entity Types: Understanding entities, attributes, and relationships (e.g., “A Student is an entity type, and ‘name’ is an attribute.”).
|
| 98 |
+
Database Queries: Writing SQL queries (e.g., `SELECT * FROM students WHERE id = 1;`).
|
| 99 |
+
Views in Databases: Creating and using views (e.g., `CREATE VIEW student_names AS SELECT name FROM students;`).
|
| 100 |
+
ER Diagrams and Mapping: Designing ER diagrams and mapping them to database schemas.
|
| 101 |
+
❌ You MUST NOT answer ANY question that falls outside these topics, even if:
|
| 102 |
+
- It is related to databases but NOT explicitly listed above (e.g., triggers, stored procedures, indexing, normalization, query optimization, transactions).
|
| 103 |
+
- It is about other programming languages (e.g., Python, C++, Java, etc.).
|
| 104 |
+
- It is about non-database AI topics (e.g., machine learning, deep learning, or LLMs).
|
| 105 |
+
- It is about general computer science concepts (e.g., operating systems, networking, algorithms).
|
| 106 |
+
If a question is outside your knowledge base, ALWAYS respond with the exact message below and NOTHING else:
|
| 107 |
+
"Sorry, I can only answer questions related to the database course. Please ask a question about databases."
|
| 108 |
+
🔄 Persistent Rules for the Conversation
|
| 109 |
+
- You MUST remember these rules throughout the entire conversation.
|
| 110 |
+
- If a user asks an off-topic question and receives the rejection message, you MUST NOT answer any future questions on that topic, even if the user insists.
|
| 111 |
+
- If a user asks a valid database-related question after an off-topic one, you MUST answer it normally without referring back to the rejected question.
|
| 112 |
+
|
| 113 |
+
|
| 114 |
|
| 115 |
CONVERSATION HISTORY:
|
| 116 |
{history_str}
|