Spaces:
Sleeping
Sleeping
Update src/simple_rag.py
Browse files- src/simple_rag.py +41 -26
src/simple_rag.py
CHANGED
|
@@ -40,21 +40,21 @@ CHROMA_PATH = os.path.join(WRITABLE_DIR, "src", "chroma")
|
|
| 40 |
|
| 41 |
embedding_model = HuggingFaceEmbeddings(model_name="intfloat/multilingual-e5-base")
|
| 42 |
|
| 43 |
-
PROMPT_TEMPLATE = """
|
| 44 |
-
You are a helpful assistant.
|
| 45 |
-
Answer the question based ONLY on the context below.
|
| 46 |
-
If the user asks in Khmer, respond in Khmer.
|
| 47 |
-
If the user asks in English, respond in English.
|
| 48 |
-
Use clear, concise sentences, no more than 50 word. Do not mention the existence of context.
|
| 49 |
|
| 50 |
-
Context:
|
| 51 |
-
{context}
|
| 52 |
|
| 53 |
-
Question:
|
| 54 |
-
{question}
|
| 55 |
|
| 56 |
-
Answer:
|
| 57 |
-
"""
|
| 58 |
|
| 59 |
def load_documents():
|
| 60 |
loader = PyPDFDirectoryLoader(DATA_PATH)
|
|
@@ -102,24 +102,39 @@ def ask_question(query_text: str, k: int = 3):
|
|
| 102 |
})
|
| 103 |
|
| 104 |
context_text = "\n\n".join(chunk["text"] for chunk in context_chunks)
|
| 105 |
-
prompt = PROMPT_TEMPLATE.format(context=context_text, question=query_text)
|
| 106 |
-
logging.info(f"Prompt: {prompt}")
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
messages,
|
| 115 |
-
add_generation_prompt=True,
|
| 116 |
-
tokenize=False,
|
| 117 |
-
thinking_mode="off"
|
| 118 |
-
)
|
| 119 |
-
logging.info(f"Prompts: {prompts}")
|
| 120 |
|
| 121 |
output = pipeline(
|
| 122 |
-
|
| 123 |
max_new_tokens=128,
|
| 124 |
do_sample=False,
|
| 125 |
return_full_text=False,
|
|
|
|
| 40 |
|
| 41 |
embedding_model = HuggingFaceEmbeddings(model_name="intfloat/multilingual-e5-base")
|
| 42 |
|
| 43 |
+
# PROMPT_TEMPLATE = """
|
| 44 |
+
# You are a helpful assistant.
|
| 45 |
+
# Answer the question based ONLY on the context below.
|
| 46 |
+
# If the user asks in Khmer, respond in Khmer.
|
| 47 |
+
# If the user asks in English, respond in English.
|
| 48 |
+
# Use clear, concise sentences, no more than 50 word. Do not mention the existence of context.
|
| 49 |
|
| 50 |
+
# Context:
|
| 51 |
+
# {context}
|
| 52 |
|
| 53 |
+
# Question:
|
| 54 |
+
# {question}
|
| 55 |
|
| 56 |
+
# Answer:
|
| 57 |
+
# """
|
| 58 |
|
| 59 |
def load_documents():
|
| 60 |
loader = PyPDFDirectoryLoader(DATA_PATH)
|
|
|
|
| 102 |
})
|
| 103 |
|
| 104 |
context_text = "\n\n".join(chunk["text"] for chunk in context_chunks)
|
| 105 |
+
#prompt = PROMPT_TEMPLATE.format(context=context_text, question=query_text)
|
| 106 |
+
#logging.info(f"Prompt: {prompt}")
|
| 107 |
|
| 108 |
+
# Construct structured messages instead of using PROMPT_TEMPLATE
|
| 109 |
+
messages = [
|
| 110 |
+
{
|
| 111 |
+
"role": "system",
|
| 112 |
+
"content": (
|
| 113 |
+
"You are a helpful assistant. "
|
| 114 |
+
"Answer the question based ONLY on the context provided. "
|
| 115 |
+
"If the user asks in Khmer, respond in Khmer. "
|
| 116 |
+
"If the user asks in English, respond in English. "
|
| 117 |
+
"Use clear, concise sentences, max 50 words. "
|
| 118 |
+
"Do not mention context or metadata."
|
| 119 |
+
)
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"role": "user",
|
| 123 |
+
"content": f"Context:\n{context_text}\n\nQuestion:\n{query_text}"
|
| 124 |
+
}
|
| 125 |
+
]
|
| 126 |
+
|
| 127 |
+
prompt = tokenizer.apply_chat_template(
|
| 128 |
+
messages,
|
| 129 |
+
add_generation_prompt=True,
|
| 130 |
+
tokenize=False,
|
| 131 |
+
thinking_mode="off"
|
| 132 |
+
)
|
| 133 |
|
| 134 |
+
logging.info(f"Prompts: {prompt}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
output = pipeline(
|
| 137 |
+
prompt,
|
| 138 |
max_new_tokens=128,
|
| 139 |
do_sample=False,
|
| 140 |
return_full_text=False,
|