ProximileAdmin commited on
Commit
cc3986d
·
verified ·
1 Parent(s): 9dc0abf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -49
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
- WEATHER_API_KEY = os.environ["WEATHER_API_KEY"]
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 get_weather_data(location):
291
  """
292
- Fetch current weather data for a given location using WeatherAPI.com.
293
-
294
  Args:
295
- location (str): The location for which to retrieve weather (e.g., "London", "90210", or "48.8567,2.3510").
296
-
 
297
  Returns:
298
- dict: A dictionary containing the current weather data or an error message.
299
  """
300
- base_url = "https://api.weatherapi.com/v1/current.json"
301
- params = {
302
- "key": WEATHER_API_KEY,
303
- "q": location,
304
- "aqi": "no" # Set to "yes" to include air quality data if desired.
 
 
 
 
 
 
305
  }
306
- full_url = base_url + "?" + "&".join([f"{k}={urllib.parse.quote(str(v))}" for k, v in params.items()])
307
- try:
308
- response = requests.get(full_url)
309
- except:
310
- lgs("FAILED PARAMS: " + str(params))
311
- lgs("FAILED RESPONSE: " + str(response.text))
312
- lgs("RAW RESPONSE: " + str(response))
313
- if response.status_code != 200:
314
- return {"error": f"Failed to retrieve weather data for {location}. Status code: {response.status_code}"}
315
- data = response.json()
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
- return formatted_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
332
- class WeatherAPITool(ToolBase):
333
  def __init__(self):
334
  super().__init__(
335
- programmatic_name="get_weather_data",
336
- natural_name="Weather Report Fetcher",
337
- active_voice_description_of_capability="You can fetch real-time weather data for any location worldwide.",
338
- passive_voice_description_of_function="a service that retrieves current weather details including temperature, precipitation, humidity, and wind data.",
339
- prescriptive_conditional="When provided with a location (city, ZIP, or lat,long) call the get_weather_data function to retrieve its weather information.",
340
  input_params={
341
- "location": {
342
  "type": "string",
343
- "description": "The location to retrieve weather data for (e.g., 'London', '90210', or '48.8567,2.3510')."
 
 
 
 
344
  },
345
  },
346
- required_params=["location"],
347
  )
348
 
349
  def actual_function(self, **kwargs):
350
- return get_weather_data(kwargs["location"])
 
 
 
351
 
352
- # Instance of the weather tool.
353
- weather_tool = WeatherAPITool()
354
 
355
- tool_objects_list = [arxiv_tool, nih_ref_snp_tool,weather_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