on1onmangoes commited on
Commit
66c6128
·
verified ·
1 Parent(s): 13469d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -62,33 +62,26 @@ def stream_chat_with_rag(
62
 
63
  def format_answer_string(answer: str):
64
  """
65
- This function takes the full string returned by the API (which includes both the assistant's answer and documents)
66
- and splits it into separate sections: the assistant's answer and the relevant documents.
67
  """
68
- # Step 1: Split the answer by the point where document context starts
69
- split_marker = "Extracted Documents with Descriptive Context:"
70
  if split_marker in answer:
71
- assistant_answer, documents_section = answer.split(split_marker, 1)
 
72
  else:
73
- # If no documents found, return the whole answer as the assistant's response
74
- return f"Assistant: {answer.strip()}"
75
 
76
- # Step 2: Clean and format the assistant's answer
77
- formatted_answer = f"Assistant: {assistant_answer.strip()}\n\n"
78
-
79
- # Step 3: Format the documents section
80
- formatted_answer += "Relevant Documents:\n"
81
- document_entries = documents_section.split("Document ")
82
 
83
- # Step 4: Reformat each document entry
84
- for entry in document_entries[1:]: # Skip the first empty split
85
- doc_number, rest = entry.split(":", 1)
86
- formatted_answer += f"{doc_number}. {rest.strip()}\n\n"
87
-
88
  return formatted_answer
89
 
90
 
91
 
 
92
  # this version works just issue with formatting
93
  # def stream_chat_with_rag(
94
  # message: str,
 
62
 
63
  def format_answer_string(answer: str):
64
  """
65
+ This function extracts and formats the assistant's response before document metadata.
66
+ Anything after the marker `[(` (where documents are listed) is ignored.
67
  """
68
+ # Step 1: Split the response at the start of the document metadata
69
+ split_marker = "[("
70
  if split_marker in answer:
71
+ # Everything before the marker is the relevant answer
72
+ answer_before_docs = answer.split(split_marker)[0]
73
  else:
74
+ # If no documents metadata, return the entire answer
75
+ answer_before_docs = answer
76
 
77
+ # Step 2: Clean up formatting by replacing escaped newline characters
78
+ formatted_answer = answer_before_docs.replace("\\n", "\n").strip()
 
 
 
 
79
 
 
 
 
 
 
80
  return formatted_answer
81
 
82
 
83
 
84
+
85
  # this version works just issue with formatting
86
  # def stream_chat_with_rag(
87
  # message: str,