Skip to content

Commit 1c7dd2e

Browse files
committed
fix: Error processing query: 'str' object does not support item assignment
1 parent ba48033 commit 1c7dd2e

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

agentic_rag/gradio_app.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,31 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
198198
# Add final formatted response to history
199199
history.append([message, formatted_response])
200200
else:
201+
# For standard response (no CoT)
201202
formatted_response = response["answer"]
202203
print("\nStandard Response:")
203204
print("-" * 50)
204205
print(formatted_response)
206+
207+
# Add sources if available
208+
if response.get("context"):
209+
print("\nSources Used:")
210+
print("-" * 50)
211+
sources_text = "\n\n📚 Sources used:\n"
212+
formatted_response += sources_text
213+
print(sources_text)
214+
215+
for ctx in response["context"]:
216+
source = ctx["metadata"].get("source", "Unknown")
217+
if "page_numbers" in ctx["metadata"]:
218+
pages = ctx["metadata"].get("page_numbers", [])
219+
source_line = f"- {source} (pages: {pages})\n"
220+
else:
221+
file_path = ctx["metadata"].get("file_path", "Unknown")
222+
source_line = f"- {source} (file: {file_path})\n"
223+
formatted_response += source_line
224+
print(source_line)
225+
205226
history.append([message, formatted_response])
206227

207228
print("\n" + "="*50)

agentic_rag/local_rag_agent.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,12 @@ def _generate_response(self, query: str, context: List[Dict[str, Any]]) -> Dict[
431431
Answer:"""
432432

433433
prompt = template.format(context=context_str, query=query)
434-
response = self._generate_text(prompt)
434+
response_text = self._generate_text(prompt)
435435

436436
# Add sources to response if available
437+
sources = {}
437438
if context:
438439
# Group sources by document
439-
sources = {}
440440
for item in context:
441441
source = item['metadata'].get('source', 'Unknown')
442442
if source not in sources:
@@ -457,12 +457,11 @@ def _generate_response(self, query: str, context: List[Dict[str, Any]]) -> Dict[
457457
print(f"Document: {source} (pages: {pages})")
458458
else: # Code with file path
459459
print(f"Code file: {source}")
460-
461-
response['sources'] = sources
462460

463461
return {
464-
"answer": response,
465-
"context": context
462+
"answer": response_text,
463+
"context": context,
464+
"sources": sources
466465
}
467466

468467
def _generate_general_response(self, query: str) -> Dict[str, Any]:

0 commit comments

Comments
 (0)