tomhflau commited on
Commit
72f6881
Β·
1 Parent(s): 36ca4f3

fixing agent

Browse files
Files changed (1) hide show
  1. app.py +41 -8
app.py CHANGED
@@ -35,8 +35,11 @@ class BasicAgent:
35
  raise e
36
 
37
  def __call__(self, question: str) -> str:
38
- print(f"Agent received question (first 50 chars): {question[:50]}...")
 
39
  try:
 
 
40
  # Create a more detailed prompt for better responses
41
  system_prompt = "You are a helpful AI assistant. Provide clear, accurate, and concise answers to questions."
42
 
@@ -56,14 +59,33 @@ class BasicAgent:
56
  temperature=0.7,
57
  )
58
 
59
- answer = completion.choices[0].message.content
60
- print(f"Agent returning answer: {answer[:100]}...")
 
 
 
 
 
 
61
  return answer
62
 
63
  except Exception as e:
64
- error_msg = f"Error processing question: {str(e)}"
65
- print(f"❌ {error_msg}")
66
- return error_msg
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  def run_and_submit_all(profile: gr.OAuthProfile | None):
69
  """
@@ -94,10 +116,21 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
94
 
95
  # 1. Instantiate Agent
96
  try:
 
97
  agent = BasicAgent()
 
 
 
 
 
 
 
 
 
98
  except Exception as e:
99
- print(f"Error instantiating agent: {e}")
100
- return f"Error initializing agent: {e}", None
 
101
 
102
  # 2. Fetch Questions
103
  print(f"Fetching questions from: {questions_url}")
 
35
  raise e
36
 
37
  def __call__(self, question: str) -> str:
38
+ print(f"πŸ€– Agent received question: {question[:100]}...")
39
+
40
  try:
41
+ print("πŸ”„ Making API call to HF Inference...")
42
+
43
  # Create a more detailed prompt for better responses
44
  system_prompt = "You are a helpful AI assistant. Provide clear, accurate, and concise answers to questions."
45
 
 
59
  temperature=0.7,
60
  )
61
 
62
+ print("βœ… API call successful!")
63
+ answer = completion.choices[0].message.content.strip()
64
+ print(f"πŸ“ Agent returning answer: {answer[:200]}...")
65
+
66
+ # Ensure we're not returning empty or None answers
67
+ if not answer or answer.lower().strip() == "":
68
+ return "I apologize, but I couldn't generate a proper response to your question."
69
+
70
  return answer
71
 
72
  except Exception as e:
73
+ error_msg = f"❌ HF API Error: {str(e)}"
74
+ print(error_msg)
75
+ print(f"πŸ“‹ Full error details: {repr(e)}")
76
+
77
+ # Return a more informative error message
78
+ return f"Sorry, I encountered an error while processing your question: {str(e)}"
79
+
80
+ def test_connection(self):
81
+ """Test if the HF API connection is working"""
82
+ try:
83
+ test_response = self("What is 2+2?")
84
+ print(f"πŸ§ͺ Test response: {test_response}")
85
+ return True, test_response
86
+ except Exception as e:
87
+ print(f"🚫 Test failed: {e}")
88
+ return False, str(e)
89
 
90
  def run_and_submit_all(profile: gr.OAuthProfile | None):
91
  """
 
116
 
117
  # 1. Instantiate Agent
118
  try:
119
+ print("πŸš€ Initializing BasicAgent...")
120
  agent = BasicAgent()
121
+
122
+ # Test the agent before proceeding
123
+ print("πŸ§ͺ Testing agent connection...")
124
+ test_success, test_result = agent.test_connection()
125
+ if not test_success:
126
+ return f"❌ Agent test failed: {test_result}\nPlease check your HF_TOKEN in environment variables.", None
127
+
128
+ print(f"βœ… Agent test successful: {test_result[:100]}...")
129
+
130
  except Exception as e:
131
+ error_msg = f"❌ Error initializing agent: {e}"
132
+ print(error_msg)
133
+ return error_msg, None
134
 
135
  # 2. Fetch Questions
136
  print(f"Fetching questions from: {questions_url}")