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

Update rag_service.py

Browse files
Files changed (1) hide show
  1. rag_service.py +29 -29
rag_service.py CHANGED
@@ -105,39 +105,39 @@ class RAGService:
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"""
 
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"""