Amna2024 commited on
Commit
99330c6
·
verified ·
1 Parent(s): a1e8f8b

Update rag_service.py

Browse files
Files changed (1) hide show
  1. 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
- """Build prompt with context and images"""
108
- docs_by_type = kwargs["context"]
109
- user_question = kwargs["question"]
110
-
111
- context_text = ""
112
- if len(docs_by_type["texts"]) > 0:
113
- for text_element in docs_by_type["texts"]:
114
- context_text += str(text_element)
115
-
116
- # If no relevant context found, make it fully general
117
- if not context_text.strip():
118
- prompt_template = f"""
119
- You are a helpful AI assistant. Answer the following question:
120
- Question: {user_question}
121
- """
122
- else:
123
- prompt_template = f"""
124
- You are a helpful AI assistant. You have access to the following context from uploaded documents:
125
-
126
- Context: {context_text}
127
-
128
- Question: {user_question}
129
-
130
- Instructions:
131
- - If the question is related to the provided context, use that information to answer
132
- - If the question is not related to the context or is a general query, answer based on your general knowledge
133
- - Be clear about whether you're using the provided context or general knowledge in your response
134
- """
135
-
136
- prompt_content = [{"type": "text", "text": prompt_template}]
137
-
138
- if len(docs_by_type["images"]) > 0:
139
- for image in docs_by_type["images"]:
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"""