Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -27,14 +27,45 @@ class IntelligentAgent:
|
|
27 |
if self.debug:
|
28 |
print(f"IntelligentAgent initialized with model: {model_name}")
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def _should_search(self, question: str) -> bool:
|
31 |
"""
|
32 |
Use LLM to determine if search is needed for the question.
|
33 |
Returns True if search is recommended, False otherwise.
|
34 |
"""
|
35 |
-
decision_prompt = f"""
|
36 |
-
|
37 |
-
Analyze this question and decide if it requires real-time information, recent data, or specific facts that might not be in your training data.
|
38 |
|
39 |
SEARCH IS NEEDED for:
|
40 |
- Current events, news, recent developments
|
@@ -63,12 +94,7 @@ Example responses:
|
|
63 |
"""
|
64 |
|
65 |
try:
|
66 |
-
response = self.
|
67 |
-
decision_prompt,
|
68 |
-
max_new_tokens=50,
|
69 |
-
temperature=0.1,
|
70 |
-
do_sample=False
|
71 |
-
)
|
72 |
|
73 |
decision = response.strip().upper()
|
74 |
should_search = decision.startswith("SEARCH")
|
@@ -89,19 +115,15 @@ Example responses:
|
|
89 |
"""
|
90 |
Generate answer using LLM without search.
|
91 |
"""
|
92 |
-
answer_prompt = f"""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
|
|
93 |
Question: {question}
|
94 |
|
95 |
Answer:"""
|
96 |
|
97 |
try:
|
98 |
-
response = self.
|
99 |
-
|
100 |
-
max_new_tokens=500,
|
101 |
-
temperature=0.3,
|
102 |
-
do_sample=True
|
103 |
-
)
|
104 |
-
return response.strip()
|
105 |
|
106 |
except Exception as e:
|
107 |
return f"Sorry, I encountered an error generating the response: {e}"
|
@@ -149,7 +171,7 @@ Answer:"""
|
|
149 |
search_context = "\n\n".join(formatted_results)
|
150 |
|
151 |
# Generate answer using search context
|
152 |
-
answer_prompt = f"""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
153 |
|
154 |
Question: {question}
|
155 |
|
@@ -161,13 +183,8 @@ Based on the search results above, provide an answer to the question. If the sea
|
|
161 |
Answer:"""
|
162 |
|
163 |
try:
|
164 |
-
response = self.
|
165 |
-
|
166 |
-
max_new_tokens=60,
|
167 |
-
temperature=0.3,
|
168 |
-
do_sample=True
|
169 |
-
)
|
170 |
-
return response.strip()
|
171 |
|
172 |
except Exception as e:
|
173 |
if self.debug:
|
@@ -450,8 +467,6 @@ def clear_cache():
|
|
450 |
processing_status = {"is_processing": False, "progress": 0, "total": 0}
|
451 |
return "Cache cleared successfully.", None
|
452 |
|
453 |
-
|
454 |
-
|
455 |
# --- Enhanced Gradio Interface ---
|
456 |
with gr.Blocks(title="Intelligent Agent with Conditional Search") as demo:
|
457 |
gr.Markdown("# Intelligent Agent with Conditional Search")
|
|
|
27 |
if self.debug:
|
28 |
print(f"IntelligentAgent initialized with model: {model_name}")
|
29 |
|
30 |
+
def _chat_completion(self, prompt: str, max_tokens: int = 500, temperature: float = 0.3) -> str:
|
31 |
+
"""
|
32 |
+
Use chat completion instead of text generation to avoid provider compatibility issues.
|
33 |
+
"""
|
34 |
+
try:
|
35 |
+
messages = [{"role": "user", "content": prompt}]
|
36 |
+
|
37 |
+
# Try chat completion first
|
38 |
+
try:
|
39 |
+
response = self.client.chat_completion(
|
40 |
+
messages=messages,
|
41 |
+
max_tokens=max_tokens,
|
42 |
+
temperature=temperature
|
43 |
+
)
|
44 |
+
return response.choices[0].message.content.strip()
|
45 |
+
except Exception as chat_error:
|
46 |
+
if self.debug:
|
47 |
+
print(f"Chat completion failed: {chat_error}, trying text generation...")
|
48 |
+
|
49 |
+
# Fallback to text generation
|
50 |
+
response = self.client.text_generation(
|
51 |
+
prompt,
|
52 |
+
max_new_tokens=max_tokens,
|
53 |
+
temperature=temperature,
|
54 |
+
do_sample=temperature > 0
|
55 |
+
)
|
56 |
+
return response.strip()
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
if self.debug:
|
60 |
+
print(f"Both chat completion and text generation failed: {e}")
|
61 |
+
raise e
|
62 |
+
|
63 |
def _should_search(self, question: str) -> bool:
|
64 |
"""
|
65 |
Use LLM to determine if search is needed for the question.
|
66 |
Returns True if search is recommended, False otherwise.
|
67 |
"""
|
68 |
+
decision_prompt = f"""Analyze this question and decide if it requires real-time information, recent data, or specific facts that might not be in your training data.
|
|
|
|
|
69 |
|
70 |
SEARCH IS NEEDED for:
|
71 |
- Current events, news, recent developments
|
|
|
94 |
"""
|
95 |
|
96 |
try:
|
97 |
+
response = self._chat_completion(decision_prompt, max_tokens=50, temperature=0.1)
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
decision = response.strip().upper()
|
100 |
should_search = decision.startswith("SEARCH")
|
|
|
115 |
"""
|
116 |
Generate answer using LLM without search.
|
117 |
"""
|
118 |
+
answer_prompt = f"""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
119 |
+
|
120 |
Question: {question}
|
121 |
|
122 |
Answer:"""
|
123 |
|
124 |
try:
|
125 |
+
response = self._chat_completion(answer_prompt, max_tokens=500, temperature=0.3)
|
126 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
except Exception as e:
|
129 |
return f"Sorry, I encountered an error generating the response: {e}"
|
|
|
171 |
search_context = "\n\n".join(formatted_results)
|
172 |
|
173 |
# Generate answer using search context
|
174 |
+
answer_prompt = f"""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
175 |
|
176 |
Question: {question}
|
177 |
|
|
|
183 |
Answer:"""
|
184 |
|
185 |
try:
|
186 |
+
response = self._chat_completion(answer_prompt, max_tokens=600, temperature=0.3)
|
187 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
except Exception as e:
|
190 |
if self.debug:
|
|
|
467 |
processing_status = {"is_processing": False, "progress": 0, "total": 0}
|
468 |
return "Cache cleared successfully.", None
|
469 |
|
|
|
|
|
470 |
# --- Enhanced Gradio Interface ---
|
471 |
with gr.Blocks(title="Intelligent Agent with Conditional Search") as demo:
|
472 |
gr.Markdown("# Intelligent Agent with Conditional Search")
|