Spaces:
Sleeping
Sleeping
Commit
Β·
4697ce0
1
Parent(s):
eeaffec
Mixtral 8x7B
Browse files- app.py +33 -2
- requirements.txt +2 -1
app.py
CHANGED
@@ -6,6 +6,7 @@ import datetime
|
|
6 |
from langchain.tools import tool
|
7 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
8 |
from langchain.agents import initialize_agent, AgentType
|
|
|
9 |
|
10 |
## # Load environment variables from .env file
|
11 |
# --- Constants ---
|
@@ -195,6 +196,27 @@ def classify_image(image_url: str) -> str:
|
|
195 |
except Exception:
|
196 |
return "error"
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
##-- Tool Discovery ---
|
200 |
# Use @tool for each function.
|
@@ -212,7 +234,8 @@ tools_list = [
|
|
212 |
currency_convert,
|
213 |
image_caption,
|
214 |
ocr_image,
|
215 |
-
classify_image
|
|
|
216 |
]
|
217 |
|
218 |
tool_descriptions = "\n".join(f"- {tool.name}: {tool.description}" for tool in tools_list)
|
@@ -227,6 +250,8 @@ You are an intelligent assistant with access to the following tools:
|
|
227 |
|
228 |
For every question, you must do your internal reasoning using the Thought β Action β Observation β Answer process, but your output to the user should be ONLY the final answer as a single value (number, string, or comma-separated list), with no extra explanation, thoughts, actions, or observations.
|
229 |
|
|
|
|
|
230 |
**Your output must be only the answer. Do not include any reasoning, tool calls, or explanations.**
|
231 |
|
232 |
Examples:
|
@@ -240,6 +265,10 @@ Your Output: 22
|
|
240 |
Q: What is the capital of France?
|
241 |
Your Output: Paris
|
242 |
|
|
|
|
|
|
|
|
|
243 |
Q: Convert 10 meters to feet.
|
244 |
Your Output: 32.81
|
245 |
|
@@ -247,6 +276,7 @@ Instructions:
|
|
247 |
- Always do your internal reasoning (Thought β Action β Observation β Answer) before producing the answer, but DO NOT show this reasoning to the user.
|
248 |
- Use a tool only if necessary, and don't use multiple tools in a call. Don't use a tool if you can answer directly.
|
249 |
- Your output must be a single value (number, string, or comma-separated list) with no extra explanation or formatting.
|
|
|
250 |
- Be concise and accurate.
|
251 |
"""
|
252 |
|
@@ -254,7 +284,8 @@ Instructions:
|
|
254 |
# Generate the chat interface, including the tools
|
255 |
|
256 |
llm = HuggingFaceEndpoint(
|
257 |
-
repo_id="
|
|
|
258 |
huggingfacehub_api_token=HF_ACCESS_KEY,
|
259 |
# model_kwargs={'prompt': system_prompt}
|
260 |
# system_prompt=system_prompt,
|
|
|
6 |
from langchain.tools import tool
|
7 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
8 |
from langchain.agents import initialize_agent, AgentType
|
9 |
+
from bs4 import BeautifulSoup
|
10 |
|
11 |
## # Load environment variables from .env file
|
12 |
# --- Constants ---
|
|
|
196 |
except Exception:
|
197 |
return "error"
|
198 |
|
199 |
+
# --- TOOL 12: Web Scraping Tool ---
|
200 |
+
@tool
|
201 |
+
def web_scrape_tool(url: str) -> str:
|
202 |
+
"""
|
203 |
+
Scrape the main textual content from a given website URL and return a concise summary or answer.
|
204 |
+
Input: A valid URL (e.g., 'https://en.wikipedia.org/wiki/Python_(programming_language)')
|
205 |
+
"""
|
206 |
+
try:
|
207 |
+
headers = {
|
208 |
+
"User-Agent": "Mozilla/5.0 (compatible; WebScrapeTool/1.0)"
|
209 |
+
}
|
210 |
+
resp = requests.get(url, headers=headers, timeout=20)
|
211 |
+
resp.raise_for_status()
|
212 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
213 |
+
# Try to extract main content from common tags
|
214 |
+
paragraphs = soup.find_all("p")
|
215 |
+
text = " ".join(p.get_text() for p in paragraphs)
|
216 |
+
# Limit to first 1000 characters for brevity
|
217 |
+
return text[:1000] if text else "No textual content found."
|
218 |
+
except Exception as e:
|
219 |
+
return f"error: {e}"
|
220 |
|
221 |
##-- Tool Discovery ---
|
222 |
# Use @tool for each function.
|
|
|
234 |
currency_convert,
|
235 |
image_caption,
|
236 |
ocr_image,
|
237 |
+
classify_image,
|
238 |
+
web_scrape_tool
|
239 |
]
|
240 |
|
241 |
tool_descriptions = "\n".join(f"- {tool.name}: {tool.description}" for tool in tools_list)
|
|
|
250 |
|
251 |
For every question, you must do your internal reasoning using the Thought β Action β Observation β Answer process, but your output to the user should be ONLY the final answer as a single value (number, string, or comma-separated list), with no extra explanation, thoughts, actions, or observations.
|
252 |
|
253 |
+
**If a tool returns a long text or description (such as from a web scraping tool), you must carefully read and process that output, and extract or identify ONLY the most relevant, concise answer to the user's question, and provide a single string as output. Do not return the full text or irrelevant details.**
|
254 |
+
|
255 |
**Your output must be only the answer. Do not include any reasoning, tool calls, or explanations.**
|
256 |
|
257 |
Examples:
|
|
|
265 |
Q: What is the capital of France?
|
266 |
Your Output: Paris
|
267 |
|
268 |
+
Q: Which year was python 3.0 released as per the website https://en.wikipedia.org/wiki/Python_(programming_language)?
|
269 |
+
(Tool returns a long description about Python.)
|
270 |
+
Your Output: 2008
|
271 |
+
|
272 |
Q: Convert 10 meters to feet.
|
273 |
Your Output: 32.81
|
274 |
|
|
|
276 |
- Always do your internal reasoning (Thought β Action β Observation β Answer) before producing the answer, but DO NOT show this reasoning to the user.
|
277 |
- Use a tool only if necessary, and don't use multiple tools in a call. Don't use a tool if you can answer directly.
|
278 |
- Your output must be a single value (number, string, or comma-separated list) with no extra explanation or formatting.
|
279 |
+
- If you cannot answer the question or if you couldn't process the input question just answer as "no_answer".
|
280 |
- Be concise and accurate.
|
281 |
"""
|
282 |
|
|
|
284 |
# Generate the chat interface, including the tools
|
285 |
|
286 |
llm = HuggingFaceEndpoint(
|
287 |
+
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
288 |
+
# repo_id="Qwen/Qwen2.5-32B-Instruct",
|
289 |
huggingfacehub_api_token=HF_ACCESS_KEY,
|
290 |
# model_kwargs={'prompt': system_prompt}
|
291 |
# system_prompt=system_prompt,
|
requirements.txt
CHANGED
@@ -7,4 +7,5 @@ huggingface-hub
|
|
7 |
langchain-huggingface
|
8 |
langchain-community
|
9 |
transformers
|
10 |
-
langchain-openai
|
|
|
|
7 |
langchain-huggingface
|
8 |
langchain-community
|
9 |
transformers
|
10 |
+
langchain-openai
|
11 |
+
beautifulsoup4
|