Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,9 +14,8 @@ import requests
|
|
| 14 |
VERBOSE_SHELL = True
|
| 15 |
ENDPOINT_URL = "https://api.hyperbolic.xyz/v1"
|
| 16 |
OAI_API_KEY = os.environ['HYPERBOLIC_XYZ_API_KEY']
|
| 17 |
-
|
| 18 |
MODEL_NAME = "meta-llama/Llama-3.3-70B-Instruct"
|
| 19 |
-
#MODEL_NAME = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
| 20 |
|
| 21 |
def lgs(log_string):
|
| 22 |
if VERBOSE_SHELL:
|
|
@@ -287,72 +286,104 @@ class NIHRefSNPTool(ToolBase):
|
|
| 287 |
|
| 288 |
nih_ref_snp_tool=NIHRefSNPTool()
|
| 289 |
|
| 290 |
-
def
|
| 291 |
"""
|
| 292 |
-
|
| 293 |
-
|
| 294 |
Args:
|
| 295 |
-
|
| 296 |
-
|
|
|
|
| 297 |
Returns:
|
| 298 |
-
|
| 299 |
"""
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
}
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
formatted_data = {
|
| 317 |
-
"location": data.get("location", {}),
|
| 318 |
-
"current": {
|
| 319 |
-
"last_updated": data.get("current", {}).get("last_updated"),
|
| 320 |
-
"temp_c": data.get("current", {}).get("temp_c"),
|
| 321 |
-
"temp_f": data.get("current", {}).get("temp_f"),
|
| 322 |
-
"precip_mm": data.get("current", {}).get("precip_mm"),
|
| 323 |
-
"precip_in": data.get("current", {}).get("precip_in"),
|
| 324 |
-
"humidity": data.get("current", {}).get("humidity"),
|
| 325 |
-
"wind_kph": data.get("current", {}).get("wind_kph"),
|
| 326 |
-
"wind_mph": data.get("current", {}).get("wind_mph"),
|
| 327 |
-
"condition": data.get("current", {}).get("condition", {})
|
| 328 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
}
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
-
class
|
| 333 |
def __init__(self):
|
| 334 |
super().__init__(
|
| 335 |
-
programmatic_name="
|
| 336 |
-
natural_name="
|
| 337 |
-
active_voice_description_of_capability="You can
|
| 338 |
-
passive_voice_description_of_function="a service that retrieves
|
| 339 |
-
prescriptive_conditional="When
|
| 340 |
input_params={
|
| 341 |
-
"
|
| 342 |
"type": "string",
|
| 343 |
-
"description": "The
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
},
|
| 345 |
},
|
| 346 |
-
required_params=["
|
| 347 |
)
|
| 348 |
|
| 349 |
def actual_function(self, **kwargs):
|
| 350 |
-
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
-
|
| 353 |
-
weather_tool = WeatherAPITool()
|
| 354 |
|
| 355 |
-
tool_objects_list = [arxiv_tool, nih_ref_snp_tool,
|
| 356 |
system_prompt = build_sys_prompt(tool_objects_list)
|
| 357 |
functions_dict = {t.json_name: t.actual_function for t in tool_objects_list}
|
| 358 |
|
|
|
|
| 14 |
VERBOSE_SHELL = True
|
| 15 |
ENDPOINT_URL = "https://api.hyperbolic.xyz/v1"
|
| 16 |
OAI_API_KEY = os.environ['HYPERBOLIC_XYZ_API_KEY']
|
| 17 |
+
PERPLEXITY_API_KEY = os.environ["PERPLEXITY_API_KEY"]
|
| 18 |
MODEL_NAME = "meta-llama/Llama-3.3-70B-Instruct"
|
|
|
|
| 19 |
|
| 20 |
def lgs(log_string):
|
| 21 |
if VERBOSE_SHELL:
|
|
|
|
| 286 |
|
| 287 |
nih_ref_snp_tool=NIHRefSNPTool()
|
| 288 |
|
| 289 |
+
def query_perplexity(query: str, api_key: str) -> Dict:
|
| 290 |
"""
|
| 291 |
+
Query the Perplexity API for research information using default settings.
|
| 292 |
+
|
| 293 |
Args:
|
| 294 |
+
query: The research question or topic to query
|
| 295 |
+
api_key: Your Perplexity API key
|
| 296 |
+
|
| 297 |
Returns:
|
| 298 |
+
Dictionary containing the response from Perplexity API
|
| 299 |
"""
|
| 300 |
+
if not api_key:
|
| 301 |
+
return {
|
| 302 |
+
"status": "error",
|
| 303 |
+
"message": "API key is required. Please provide your Perplexity API key."
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
url = "https://api.perplexity.ai/chat/completions"
|
| 307 |
+
|
| 308 |
+
headers = {
|
| 309 |
+
"Authorization": f"Bearer {api_key}",
|
| 310 |
+
"Content-Type": "application/json"
|
| 311 |
}
|
| 312 |
+
|
| 313 |
+
# Using Perplexity's recommended defaults
|
| 314 |
+
messages = [
|
| 315 |
+
{
|
| 316 |
+
"role": "system",
|
| 317 |
+
"content": "You are a helpful AI research assistant. Provide accurate, detailed information with relevant citations."
|
| 318 |
+
},
|
| 319 |
+
{
|
| 320 |
+
"role": "user",
|
| 321 |
+
"content": query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
}
|
| 323 |
+
]
|
| 324 |
+
|
| 325 |
+
payload = {
|
| 326 |
+
"model": "sonar",
|
| 327 |
+
"messages": messages,
|
| 328 |
+
"temperature": 0.2,
|
| 329 |
+
"top_p": 0.9,
|
| 330 |
}
|
| 331 |
+
|
| 332 |
+
try:
|
| 333 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 334 |
+
response.raise_for_status() # Raise exception for 4XX/5XX responses
|
| 335 |
+
data = response.json()
|
| 336 |
+
|
| 337 |
+
# Format the response for easier consumption
|
| 338 |
+
result = {
|
| 339 |
+
"status": "success",
|
| 340 |
+
"content": data["choices"][0]["message"]["content"] if data.get("choices") else None,
|
| 341 |
+
"citations": data.get("citations", []),
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
return result
|
| 345 |
+
|
| 346 |
+
except requests.exceptions.RequestException as e:
|
| 347 |
+
return {
|
| 348 |
+
"status": "error",
|
| 349 |
+
"message": f"API request failed: {str(e)}"
|
| 350 |
+
}
|
| 351 |
+
except Exception as e:
|
| 352 |
+
return {
|
| 353 |
+
"status": "error",
|
| 354 |
+
"message": f"An unexpected error occurred: {str(e)}"
|
| 355 |
+
}
|
| 356 |
|
| 357 |
+
class PerplexityQueryTool(ToolBase):
|
| 358 |
def __init__(self):
|
| 359 |
super().__init__(
|
| 360 |
+
programmatic_name="query_perplexity",
|
| 361 |
+
natural_name="Perplexity Research Assistant",
|
| 362 |
+
active_voice_description_of_capability="You can search for up-to-date information using the Perplexity API.",
|
| 363 |
+
passive_voice_description_of_function="a service that retrieves information from the web using the Perplexity AI model with proper citations.",
|
| 364 |
+
prescriptive_conditional="When you need to find current information or answer research questions, you should use the query_perplexity function.",
|
| 365 |
input_params={
|
| 366 |
+
"query": {
|
| 367 |
"type": "string",
|
| 368 |
+
"description": "The research question or topic to query."
|
| 369 |
+
},
|
| 370 |
+
"api_key": {
|
| 371 |
+
"type": "string",
|
| 372 |
+
"description": "Your Perplexity API key."
|
| 373 |
},
|
| 374 |
},
|
| 375 |
+
required_params=["query", "api_key"],
|
| 376 |
)
|
| 377 |
|
| 378 |
def actual_function(self, **kwargs):
|
| 379 |
+
"""
|
| 380 |
+
Query the Perplexity API for research information.
|
| 381 |
+
"""
|
| 382 |
+
return query_perplexity(**kwargs)
|
| 383 |
|
| 384 |
+
perplexity_tool = PerplexityQueryTool()
|
|
|
|
| 385 |
|
| 386 |
+
tool_objects_list = [arxiv_tool, nih_ref_snp_tool, perplexity_tool]
|
| 387 |
system_prompt = build_sys_prompt(tool_objects_list)
|
| 388 |
functions_dict = {t.json_name: t.actual_function for t in tool_objects_list}
|
| 389 |
|