Update rag_service.py
Browse files- rag_service.py +16 -18
rag_service.py
CHANGED
|
@@ -114,6 +114,16 @@ class RAGService:
|
|
| 114 |
for text_element in docs_by_type["texts"]:
|
| 115 |
context_text += str(text_element)
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# Always use this flexible prompt
|
| 118 |
prompt_template = f"""
|
| 119 |
You are a helpful AI assistant.
|
|
@@ -126,31 +136,19 @@ class RAGService:
|
|
| 126 |
"""
|
| 127 |
|
| 128 |
prompt_content = [{"type": "text", "text": prompt_template}]
|
|
|
|
|
|
|
| 129 |
|
| 130 |
else:
|
| 131 |
-
#
|
| 132 |
prompt_template = f"""
|
| 133 |
You are a helpful AI assistant. Answer the following question using your general knowledge:
|
| 134 |
-
|
| 135 |
Question: {user_question}
|
| 136 |
"""
|
| 137 |
-
|
| 138 |
-
prompt_content = [{"type": "text", "text": prompt_template}]
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# Add images only if context exists
|
| 144 |
-
if len(docs_by_type["images"]) > 0:
|
| 145 |
-
for image in docs_by_type["images"]:
|
| 146 |
-
prompt_content.append(
|
| 147 |
-
{
|
| 148 |
-
"type": "image_url",
|
| 149 |
-
"image_url": {"url": f"data:image/jpeg;base64,{image}"},
|
| 150 |
-
}
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
return ChatPromptTemplate.from_messages([HumanMessage(content=prompt_content)])
|
| 154 |
|
| 155 |
def ask_question(self, question: str):
|
| 156 |
"""Process a question and return response"""
|
|
|
|
| 114 |
for text_element in docs_by_type["texts"]:
|
| 115 |
context_text += str(text_element)
|
| 116 |
|
| 117 |
+
# Add images only if context exists
|
| 118 |
+
if len(docs_by_type["images"]) > 0:
|
| 119 |
+
for image in docs_by_type["images"]:
|
| 120 |
+
prompt_content.append(
|
| 121 |
+
{
|
| 122 |
+
"type": "image_url",
|
| 123 |
+
"image_url": {"url": f"data:image/jpeg;base64,{image}"},
|
| 124 |
+
}
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
# Always use this flexible prompt
|
| 128 |
prompt_template = f"""
|
| 129 |
You are a helpful AI assistant.
|
|
|
|
| 136 |
"""
|
| 137 |
|
| 138 |
prompt_content = [{"type": "text", "text": prompt_template}]
|
| 139 |
+
|
| 140 |
+
return ChatPromptTemplate.from_messages([HumanMessage(content=prompt_content)])
|
| 141 |
|
| 142 |
else:
|
| 143 |
+
# Generic question with no context or images
|
| 144 |
prompt_template = f"""
|
| 145 |
You are a helpful AI assistant. Answer the following question using your general knowledge:
|
|
|
|
| 146 |
Question: {user_question}
|
| 147 |
"""
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
return ChatPromptTemplate.from_messages(
|
| 150 |
+
[HumanMessage(content=prompt_template.strip())] # plain string
|
| 151 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
def ask_question(self, question: str):
|
| 154 |
"""Process a question and return response"""
|