rdune71 commited on
Commit
0ad0d9d
·
1 Parent(s): d4c3c03
Files changed (1) hide show
  1. modules/analyzer.py +11 -17
modules/analyzer.py CHANGED
@@ -20,7 +20,7 @@ class Analyzer:
20
 
21
  # Connect to Redis cache
22
  self.cache = RedisServerStatusCache()
23
-
24
  def is_server_ready(self):
25
  # Check cache first
26
  cached_status = self.cache.get(self.cache_key)
@@ -61,7 +61,7 @@ class Analyzer:
61
  self.cache.set(self.cache_key, is_ready, ttl=60) # Cache for 1 minute
62
  log_server_status(self.cache_key, is_ready)
63
  return is_ready
64
-
65
  def wait_for_server(self, timeout=300, interval=15): # Increased timeout to 5 minutes
66
  """Wait for server to be ready with user feedback"""
67
  if self.is_server_ready():
@@ -81,7 +81,7 @@ class Analyzer:
81
 
82
  logging.warning("⏰ Server initialization timeout reached")
83
  return False
84
-
85
  def analyze_stream(self, query, search_results):
86
  """ Analyze search results using the custom LLM with streaming output
87
  Yields chunks of the response as they arrive """
@@ -92,8 +92,7 @@ class Analyzer:
92
  for result in search_results[:5] # Limit to top 5 for context
93
  ])
94
 
95
- prompt = f"""
96
- You are an expert research analyst. Analyze the following query and information to provide a comprehensive summary.
97
 
98
  Query: {query}
99
  Information: {context}
@@ -105,8 +104,7 @@ Please provide:
105
  4. Potential implications or future directions
106
  5. Any controversies or conflicting viewpoints
107
 
108
- Structure your response clearly with these sections. If there is insufficient information, state that clearly.
109
- """
110
 
111
  try:
112
  # First check if server is ready with extended timeout and user feedback
@@ -134,14 +132,12 @@ Structure your response clearly with these sections. If there is insufficient in
134
  stream=True # Enable streaming
135
  )
136
 
137
- # Yield chunks as they arrive with proper spacing
138
- accumulated_response = ""
139
  for chunk in response:
140
  if chunk.choices[0].delta.content:
141
  content = chunk.choices[0].delta.content
142
- accumulated_response += content
143
- # Yield the accumulated response for better streaming display
144
- yield accumulated_response
145
 
146
  logging.info("Analysis streaming completed successfully")
147
 
@@ -157,7 +153,7 @@ Structure your response clearly with these sections. If there is insufficient in
157
  yield "⚠️ The AI model endpoint was not found. Please check the configuration."
158
  else:
159
  yield f"Analysis failed: {str(e)}"
160
-
161
  def analyze(self, query, search_results):
162
  """ Non-streaming version for compatibility """
163
 
@@ -167,8 +163,7 @@ Structure your response clearly with these sections. If there is insufficient in
167
  for result in search_results[:5] # Limit to top 5 for context
168
  ])
169
 
170
- prompt = f"""
171
- You are an expert research analyst. Analyze the following query and information to provide a comprehensive summary.
172
 
173
  Query: {query}
174
  Information: {context}
@@ -180,8 +175,7 @@ Please provide:
180
  4. Potential implications or future directions
181
  5. Any controversies or conflicting viewpoints
182
 
183
- Structure your response clearly with these sections. If there is insufficient information, state that clearly.
184
- """
185
 
186
  try:
187
  # First check if server is ready with extended timeout
 
20
 
21
  # Connect to Redis cache
22
  self.cache = RedisServerStatusCache()
23
+
24
  def is_server_ready(self):
25
  # Check cache first
26
  cached_status = self.cache.get(self.cache_key)
 
61
  self.cache.set(self.cache_key, is_ready, ttl=60) # Cache for 1 minute
62
  log_server_status(self.cache_key, is_ready)
63
  return is_ready
64
+
65
  def wait_for_server(self, timeout=300, interval=15): # Increased timeout to 5 minutes
66
  """Wait for server to be ready with user feedback"""
67
  if self.is_server_ready():
 
81
 
82
  logging.warning("⏰ Server initialization timeout reached")
83
  return False
84
+
85
  def analyze_stream(self, query, search_results):
86
  """ Analyze search results using the custom LLM with streaming output
87
  Yields chunks of the response as they arrive """
 
92
  for result in search_results[:5] # Limit to top 5 for context
93
  ])
94
 
95
+ prompt = f"""You are an expert research analyst. Analyze the following query and information to provide a comprehensive summary.
 
96
 
97
  Query: {query}
98
  Information: {context}
 
104
  4. Potential implications or future directions
105
  5. Any controversies or conflicting viewpoints
106
 
107
+ Structure your response clearly with these sections. If there is insufficient information, state that clearly."""
 
108
 
109
  try:
110
  # First check if server is ready with extended timeout and user feedback
 
132
  stream=True # Enable streaming
133
  )
134
 
135
+ # Yield chunks as they arrive
 
136
  for chunk in response:
137
  if chunk.choices[0].delta.content:
138
  content = chunk.choices[0].delta.content
139
+ # Yield only the new content, not the accumulated response
140
+ yield content
 
141
 
142
  logging.info("Analysis streaming completed successfully")
143
 
 
153
  yield "⚠️ The AI model endpoint was not found. Please check the configuration."
154
  else:
155
  yield f"Analysis failed: {str(e)}"
156
+
157
  def analyze(self, query, search_results):
158
  """ Non-streaming version for compatibility """
159
 
 
163
  for result in search_results[:5] # Limit to top 5 for context
164
  ])
165
 
166
+ prompt = f"""You are an expert research analyst. Analyze the following query and information to provide a comprehensive summary.
 
167
 
168
  Query: {query}
169
  Information: {context}
 
175
  4. Potential implications or future directions
176
  5. Any controversies or conflicting viewpoints
177
 
178
+ Structure your response clearly with these sections. If there is insufficient information, state that clearly."""
 
179
 
180
  try:
181
  # First check if server is ready with extended timeout