pranavkv commited on
Commit
25410fe
·
verified ·
1 Parent(s): 8c5c24b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -10
app.py CHANGED
@@ -19,20 +19,64 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
19
  return "What magic will you build ?"
20
 
21
  @tool
22
- def get_current_time_in_timezone(timezone: str) -> str:
23
- """A tool that fetches the current local time in a specified timezone.
24
  Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
 
 
 
 
 
 
 
 
 
26
  """
27
  try:
28
- # Create timezone object
29
- tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
33
  except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
@@ -55,7 +99,13 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
19
  return "What magic will you build ?"
20
 
21
  @tool
22
+ def reflect_and_summarize(thought: str) -> str:
23
+ """Condenses reasoning or intermediate thoughts into a concise summary.
24
  Args:
25
+ thought: Intermediate reasoning or notes from the agent
26
+ """
27
+ summary = thought.strip()
28
+ if len(summary) > 300:
29
+ summary = summary[:300] + "..."
30
+ return f"Key insight: {summary}"
31
+
32
+ @tool
33
+ def fetch_json(url: str) -> str:
34
+ """Fetches JSON data from a public API endpoint.
35
+ Args:
36
+ url: A publicly accessible API endpoint returning JSON
37
  """
38
  try:
39
+ response = requests.get(url, timeout=5)
40
+ response.raise_for_status()
41
+ return response.text[:2000] # prevent token explosion
 
 
42
  except Exception as e:
43
+ return f"Failed to fetch data: {str(e)}"
44
+
45
 
46
+ @tool
47
+ def list_prompt_templates() -> str:
48
+ """Lists available prompt templates loaded from prompts.yaml."""
49
+ try:
50
+ return yaml.dump(prompt_templates, sort_keys=False)
51
+ except Exception as e:
52
+ return f"Error reading prompts: {str(e)}"
53
+
54
+ @tool
55
+ def get_current_time(
56
+ timezone: str = "UTC",
57
+ fmt: str = "%Y-%m-%d %H:%M:%S"
58
+ ) -> str:
59
+ """Returns current time in a specified timezone.
60
+ Args:
61
+ timezone: Timezone name (default: UTC)
62
+ fmt: Datetime format string
63
+ """
64
+ try:
65
+ tz = pytz.timezone(timezone)
66
+ return datetime.datetime.now(tz).strftime(fmt)
67
+ except Exception as e:
68
+ return f"Invalid timezone or format: {e}"
69
+ @tool
70
+ def validate_final_answer(answer: str) -> str:
71
+ """Checks final answer for clarity and completeness."""
72
+ issues = []
73
+ if len(answer) < 20:
74
+ issues.append("Answer too short")
75
+ if "I don't know" in answer:
76
+ issues.append("Uncertain response detected")
77
+ if not issues:
78
+ return "Answer looks good."
79
+ return "Potential issues: " + ", ".join(issues)
80
 
81
  final_answer = FinalAnswerTool()
82
 
 
99
 
100
  agent = CodeAgent(
101
  model=model,
102
+ tools = [
103
+ reflect_and_summarize,
104
+ fetch_json,
105
+ get_current_time,
106
+ list_prompt_templates,
107
+ validate_final_answer,
108
+ final_answer ], ## add your tools here (don't remove final answer)
109
  max_steps=6,
110
  verbosity_level=1,
111
  grammar=None,