AlexDGenu commited on
Commit
68a9996
Β·
1 Parent(s): 3528f7a

Refactor SmolAgent to use chat completion format

Browse files
Files changed (1) hide show
  1. app.py +32 -7
app.py CHANGED
@@ -27,17 +27,26 @@ class SmolAgent:
27
  print("SmolAgent initialized with direct inference client.")
28
 
29
  def __call__(self, question: str) -> str:
30
- prompt = f"{SYSTEM_PROMPT}\n\nQuestion: {question}\n\nAnswer:"
31
  print(f"\nπŸͺ Running on question:\n{question}\n")
32
  try:
33
- response = self.client.text_generation(
34
- prompt,
35
- max_new_tokens=100,
 
 
 
 
 
 
36
  temperature=0.1,
37
  stop=["\n"],
38
  )
39
- cleaned_response = response.strip()
40
- print(f"βœ… Raw model response:\n{response}\n")
 
 
 
 
41
  print(f"βœ… Cleaned response to submit:\n{cleaned_response}\n")
42
 
43
  # Parse the response to extract the final answer if it follows the template
@@ -56,7 +65,23 @@ class SmolAgent:
56
  import traceback
57
  traceback.print_exc()
58
  print(f"❌ AGENT ERROR: {e}")
59
- return f"AGENT ERROR: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
 
62
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
27
  print("SmolAgent initialized with direct inference client.")
28
 
29
  def __call__(self, question: str) -> str:
 
30
  print(f"\nπŸͺ Running on question:\n{question}\n")
31
  try:
32
+ # Use chat completion format which is more reliable
33
+ messages = [
34
+ {"role": "system", "content": SYSTEM_PROMPT},
35
+ {"role": "user", "content": question}
36
+ ]
37
+
38
+ response = self.client.chat_completion(
39
+ messages=messages,
40
+ max_tokens=100,
41
  temperature=0.1,
42
  stop=["\n"],
43
  )
44
+
45
+ # Extract the assistant's response
46
+ assistant_message = response.choices[0].message.content
47
+ cleaned_response = assistant_message.strip()
48
+
49
+ print(f"βœ… Raw model response:\n{assistant_message}\n")
50
  print(f"βœ… Cleaned response to submit:\n{cleaned_response}\n")
51
 
52
  # Parse the response to extract the final answer if it follows the template
 
65
  import traceback
66
  traceback.print_exc()
67
  print(f"❌ AGENT ERROR: {e}")
68
+ # Fallback: try text_generation with different parameters
69
+ try:
70
+ print("πŸ”„ Trying fallback text_generation method...")
71
+ prompt = f"{SYSTEM_PROMPT}\n\nQuestion: {question}\n\nAnswer:"
72
+ response = self.client.text_generation(
73
+ prompt,
74
+ max_new_tokens=50,
75
+ temperature=0.1,
76
+ do_sample=True,
77
+ return_full_text=False,
78
+ )
79
+ cleaned_response = response.strip()
80
+ print(f"βœ… Fallback response: {cleaned_response}")
81
+ return cleaned_response
82
+ except Exception as fallback_error:
83
+ print(f"❌ Fallback also failed: {fallback_error}")
84
+ return f"AGENT ERROR: {e}"
85
 
86
 
87
  def run_and_submit_all( profile: gr.OAuthProfile | None):