File size: 13,417 Bytes
8d38779 d61d72d 8d38779 d61d72d 8d38779 d61d72d 8d38779 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
import os
import logging
from typing import List, Dict, Any, Optional, Tuple, Callable, Union
from dotenv import load_dotenv
from llm_providers import LLMProvider
from langchain.schema import HumanMessage
from tantivy_search_agent import TantivySearchAgent
load_dotenv()
class SearchAgent:
def __init__(self, tantivy_agent: TantivySearchAgent, provider_name: str = "Gemini", api_keys: Dict[str, str] = None):
"""Initialize the search agent with Tantivy agent and LLM client"""
self.tantivy_agent = tantivy_agent
self.logger = logging.getLogger(__name__)
# Initialize LLM provider with API keys
self.llm_provider = LLMProvider(api_keys)
self.llm = None
self.set_provider(provider_name)
self.min_confidence_threshold = 0.7
def set_provider(self, provider_name: str) -> None:
self.llm = self.llm_provider.get_provider(provider_name)
if not self.llm:
raise ValueError(f"Provider {provider_name} not available")
self.current_provider = provider_name
def get_available_providers(self) -> list[str]:
return self.llm_provider.get_available_providers()
def get_query(self, query: str, failed_queries: List[Dict[str, str]] = []) -> str:
"""Generate a Tantivy query using Claude, considering previously failed queries"""
try:
if not self.llm:
raise ValueError("LLM provider not initialized")
prompt = (
"Create a query for this search request with the following restrictions:\n"+
self.tantivy_agent.get_query_instructions()+
"\n\nAdditional instructions: \n"
"1. return only the search query without any other text\n"
"2. Use only Hebrew terms for the search query\n"
"3. the corpus to search in is an ancient Hebrew corpus - Tora and Talmud. so Try to use ancient Hebrew terms and or Talmudic expressions."
"4. prevent modern words that are not common in talmudic texts \n"
f"the search request: {query}"
)
if failed_queries:
prompt += (
f"\n\nPrevious failed queries:\n"+
"------------------------\n"+
'\n'.join(f"Query: {q['query']}, Reason: {q['reason']}" for q in failed_queries)+
"\n\n"
"Please generate an alternative query that:\n"
"1. Uses different Hebrew synonyms or related terms\n"
"2. Tries broader or more general terms\n"
"3. Adjusts proximity values or uses wildcards\n"
"4. Prevents using modern words that are not common in ancient hebrew and talmud texts\n"
)
response = self.llm.invoke([HumanMessage(content=prompt)])
tantivy_query = response.content.strip()
self.logger.info(f"Generated Tantivy query: {tantivy_query}")
return tantivy_query
except Exception as e:
self.logger.error(f"Error generating query: {e}")
# Fallback to basic quoted search
return f'"{query}"'
def _evaluate_results(self, results: List[Dict[str, Any]], query: str) -> Dict[str, Any]:
"""Evaluate search results using Claude with confidence scoring"""
if not self.llm:
raise ValueError("LLM provider not initialized")
# Prepare context from results
context = "\n".join(f"Result {i}. Source: {r.get('reference',[])}\n Text: {r.get('text', [])}"
for i, r in enumerate(results)
)
try:
message = self.llm.invoke([HumanMessage(content=f"""Evaluate the search results for answering this question:
Question: {query}
Search Results:
{context}
Provide evaluation in this format (3 lines):
Confidence score (0.0 to 1.0) indicating how well the results can answer the question. this line should include only the number return, don't include '[line 1]'
ACCEPT if score >= {self.min_confidence_threshold}, REFINE if score < {self.min_confidence_threshold}. return only the word ACCEPT or REFINE.
Detailed explanation of what information is present or missing, don't include '[line 3]'. it should be only in Hebrew
""")])
lines = message.content.strip().replace('\n\n', '\n').split('\n')
confidence = float(lines[0])
decision = lines[1].upper()
explanation = lines[2]
is_good = decision == 'ACCEPT'
self.logger.info(f"Evaluation: Confidence={confidence}, Decision={decision}")
self.logger.info(f"Explanation: {explanation}")
return {
"confidence": confidence,
"is_sufficient": is_good,
"explanation": explanation,
}
except Exception as e:
self.logger.error(f"Error evaluating results: {e}")
# Fallback to simple evaluation
return {
"confidence": 0.0,
"is_sufficient": False,
"explanation": "",
}
def _generate_answer(self, query: str, results: List[Dict[str, Any]]) -> str:
"""Generate answer using Claude with improved context utilization"""
if not self.llm:
raise ValueError("LLM provider not initialized")
if not results:
return "ืื ื ืืฆืื ืชืืฆืืืช"
# Prepare context from results
context = "\n".join(f"Result {i+1}. Source: {r.get('reference',[])}\n Text: {r.get('text', [])}"
for i, r in enumerate(results)
)
try:
message = self.llm.invoke([HumanMessage(content=f"""Based on these search results, answer this question:
Question: {query}
Search Results:
{context}
Requirements for your answer:
1. Use only information from the search results
2. Be comprehensive but concise
3. Structure the answer clearly
4. If any aspect of the question cannot be fully answered, acknowledge this
5. cite sources for each fact or information you use
6. The answer should be only in Hebrew
""")])
return message.content.strip()
except Exception as e:
self.logger.error(f"Error generating answer: {e}")
return f"I encountered an error generating the answer: {str(e)}"
def search_and_answer(self, query: str, num_results: int = 10, max_iterations: int = 3,
on_step: Optional[Callable[[Dict[str, Any]], None]] = None) -> Dict[str, Any]:
"""Execute multi-step search process using Tantivy with streaming updates"""
steps = []
all_results = []
# Step 1: Generate Tantivy query
initial_query = self.get_query(query)
step = {
'action': 'ืืฆืืจืช ืฉืืืืชืช ืืืคืืฉ',
'description': 'ื ืืฆืจื ืฉืืืืชืช ืืืคืืฉ ืขืืืจ ืื ืืข ืืืืคืืฉ',
'results': [{'type': 'query', 'content': initial_query}]
}
steps.append(step)
if on_step:
on_step(step)
# Step 2: Initial search with Tantivy query
results = self.tantivy_agent.search(initial_query, num_results)
step = {
'action': 'ืืืคืืฉ ืืืืืจ',
'description': f'ืืืคืืฉ ืืืืืจ ืขืืืจ ืฉืืืืชืช ืืืคืืฉ: {initial_query}',
'results': [{'type': 'document', 'content': {
'title': r['title'],
'reference': r['reference'],
'topics': r['topics'],
'highlights': r['highlights'],
'score': r['score']
}} for r in results]
}
steps.append(step)
if on_step:
on_step(step)
failed_queries = []
if results.__len__() == 0:
failed_queries.append({'query': initial_query, 'reason': 'no results'})
is_sufficient = False
else:
all_results.extend(results)
# Step 3: Evaluate results
evaluation = self._evaluate_results(results, query)
confidence = evaluation['confidence']
is_sufficient = evaluation['is_sufficient']
explanation = evaluation['explanation']
step = {
'action': 'ืืืจืื ืชืืฆืืืช',
'description': 'ืืืจืื ืชืืฆืืืช ืืืคืืฉ',
'results': [{
'type': 'evaluation',
'content': {
'status': 'accepted' if is_sufficient else 'insufficient',
'confidence': confidence,
'explanation': explanation,
}
}]
}
steps.append(step)
if on_step:
on_step(step)
if not is_sufficient:
failed_queries.append({'query': initial_query, 'reason': explanation})
# Step 4: Additional searches if needed
attempt = 2
while not is_sufficient and attempt < max_iterations:
# Generate new query
new_query = self.get_query(query, failed_queries)
step = {
'action': f'ืืฆืืจืช ืฉืืืืชื ืืืืฉ (ื ืืกืืื {attempt})',
'description': 'ื ืืฆืจื ืฉืืืืชืช ืืืคืืฉ ื ืืกืคืช ืขืืืจ ืื ืืข ืืืืคืืฉ',
'results': [
{'type': 'new_query', 'content': new_query}
]
}
steps.append(step)
if on_step:
on_step(step)
# Search with new query
results = self.tantivy_agent.search(new_query, num_results)
step = {
'action': f'ืืืคืืฉ ื ืืกืฃ (ื ืืกืืื {attempt}) ',
'description': f'ืืืคืฉ ืืืืืจ ืขืืืจ ืฉืืืืชืช ืืืคืืฉ: {new_query}',
'results': [{'type': 'document', 'content': {
'title': r['title'],
'reference': r['reference'],
'topics': r['topics'],
'highlights': r['highlights'],
'score': r['score']
}} for r in results]
}
steps.append(step)
if on_step:
on_step(step)
if results.__len__() == 0:
failed_queries.append({'query': new_query, 'reason': 'no results'})
else:
all_results.extend(results)
# Re-evaluate with current results
evaluation = self._evaluate_results(results, query)
confidence = evaluation['confidence']
is_sufficient = evaluation['is_sufficient']
explanation = evaluation['explanation']
step = {
'action': f'ืืืจืื ืชืืฆืืืช (ื ืืกืืื {attempt})',
'description': 'ืืืจืื ืชืืฆืืืช ืืืคืืฉ ืื ืืกืืื ืื',
'explanation': explanation,
'results': [{
'type': 'evaluation',
'content': {
'status': 'accepted' if is_sufficient else 'insufficient',
'confidence': confidence,
'explanation': explanation,
}
}]
}
steps.append(step)
if on_step:
on_step(step)
if not is_sufficient:
failed_queries.append({'query': new_query, 'reason': explanation})
attempt += 1
# Step 5: Generate final answer
answer = self._generate_answer(query, all_results)
final_result = {
'steps': steps,
'answer': answer,
'sources': [{
'title': r['title'],
'reference': r['reference'],
'topics': r['topics'],
'path': r['file_path'],
'highlights': r['highlights'],
'text': r['text'],
'score': r['score']
} for r in all_results]
}
# Send final result through callback
if on_step:
on_step({
'action': 'ืกืืื',
'description': 'ืืืืคืืฉ ืืืฉืื',
'final_result': final_result
})
return final_result
|