sajjadpsavoji commited on
Commit
fedbab7
·
1 Parent(s): ea682f9

add default answer to agent

Browse files
Files changed (1) hide show
  1. agent.py +17 -4
agent.py CHANGED
@@ -6,11 +6,13 @@ import yaml
6
  CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
7
 
8
  class Agent:
9
- def __init__(self):
 
 
 
10
  model = InferenceClientModel(
11
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
12
  )
13
- final_answer = FinalAnswer()
14
  with open(os.path.join(CURRENT_DIR, "prompts.yaml"), 'r') as stream:
15
  prompt_templates = yaml.safe_load(stream)
16
  self.agent = CodeAgent(
@@ -28,6 +30,17 @@ class Agent:
28
  max_print_outputs_length=None,
29
  prompt_templates=prompt_templates
30
  )
 
 
31
  def __call__(self, question: str) -> str:
32
- answer = self.agent.run(question)
33
- return answer
 
 
 
 
 
 
 
 
 
 
6
  CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
7
 
8
  class Agent:
9
+ def __init__(
10
+ self,
11
+ default_answer: str = "Sorry, I don’t have an answer for that."
12
+ ):
13
  model = InferenceClientModel(
14
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
15
  )
 
16
  with open(os.path.join(CURRENT_DIR, "prompts.yaml"), 'r') as stream:
17
  prompt_templates = yaml.safe_load(stream)
18
  self.agent = CodeAgent(
 
30
  max_print_outputs_length=None,
31
  prompt_templates=prompt_templates
32
  )
33
+ self.default_answer = default_answer
34
+
35
  def __call__(self, question: str) -> str:
36
+ try:
37
+ answer = self.agent.run(question)
38
+ except Exception:
39
+ # if the agent errors out, return the fallback
40
+ return self.default_answer
41
+
42
+ # if the agent returns None or an empty string, also use fallback
43
+ if not answer:
44
+ return self.default_answer
45
+
46
+ return answer