Spaces:
Sleeping
Sleeping
Update rag_service.py
Browse files- rag_service.py +34 -45
rag_service.py
CHANGED
|
@@ -103,52 +103,41 @@ class RAGService:
|
|
| 103 |
text.append(doc)
|
| 104 |
return {"images": b64, "texts": text}
|
| 105 |
|
|
|
|
| 106 |
def build_prompt(self, kwargs):
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
prompt_content.append(
|
| 141 |
-
{
|
| 142 |
-
"type": "image_url",
|
| 143 |
-
"image_url": {"url": f"data:image/jpeg;base64,{image}"},
|
| 144 |
-
}
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
-
return ChatPromptTemplate.from_messages(
|
| 148 |
-
[
|
| 149 |
-
HumanMessage(content=prompt_content),
|
| 150 |
-
]
|
| 151 |
-
)
|
| 152 |
|
| 153 |
def ask_question(self, question: str):
|
| 154 |
"""Process a question and return response"""
|
|
|
|
| 103 |
text.append(doc)
|
| 104 |
return {"images": b64, "texts": text}
|
| 105 |
|
| 106 |
+
|
| 107 |
def build_prompt(self, kwargs):
|
| 108 |
+
"""Build prompt with context and images"""
|
| 109 |
+
docs_by_type = kwargs["context"]
|
| 110 |
+
user_question = kwargs["question"]
|
| 111 |
+
|
| 112 |
+
context_text = ""
|
| 113 |
+
if len(docs_by_type["texts"]) > 0:
|
| 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.
|
| 120 |
+
|
| 121 |
+
Context from documents (use if relevant): {context_text}
|
| 122 |
+
|
| 123 |
+
Question: {user_question}
|
| 124 |
+
|
| 125 |
+
Instructions: Answer the question. If the provided context is relevant to the question, use it. If not, answer using your general knowledge.
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
prompt_content = [{"type": "text", "text": prompt_template}]
|
| 129 |
+
|
| 130 |
+
# Add images only if context exists
|
| 131 |
+
if len(docs_by_type["images"]) > 0:
|
| 132 |
+
for image in docs_by_type["images"]:
|
| 133 |
+
prompt_content.append(
|
| 134 |
+
{
|
| 135 |
+
"type": "image_url",
|
| 136 |
+
"image_url": {"url": f"data:image/jpeg;base64,{image}"},
|
| 137 |
+
}
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
return ChatPromptTemplate.from_messages([HumanMessage(content=prompt_content)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
def ask_question(self, question: str):
|
| 143 |
"""Process a question and return response"""
|