mohantest commited on
Commit
f6c3f4b
·
verified ·
1 Parent(s): 57111d2

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +30 -18
agent.py CHANGED
@@ -3,12 +3,19 @@ import hashlib
3
  import json
4
  import logging
5
  from smolagents import CodeAgent, tool
6
- from smolagents.models import Model # <-- base class for models
7
  from huggingface_hub import InferenceClient
8
 
9
  logging.basicConfig(level=logging.INFO)
10
  logger = logging.getLogger(__name__)
11
 
 
 
 
 
 
 
 
12
  # Cache for answers
13
  CACHE_FILE = "answer_cache.json"
14
  if os.path.exists(CACHE_FILE):
@@ -69,12 +76,18 @@ def web_search(query: str) -> str:
69
  except Exception as e:
70
  return f"Search error: {e}"
71
 
72
- # ---------- Custom model that inherits from smolagents.Model ----------
73
  class CustomHFModel(Model):
74
- def __init__(self, model_id="HuggingFaceH4/zephyr-7b-beta", **kwargs):
75
  super().__init__(**kwargs)
76
- self.client = InferenceClient(model=model_id, token=os.getenv("HF_TOKEN"))
 
 
 
 
 
77
  self.model_id = model_id
 
78
 
79
  def generate(
80
  self,
@@ -85,19 +98,18 @@ class CustomHFModel(Model):
85
  temperature=0.7,
86
  **kwargs
87
  ):
88
- """
89
- Required by smolagents: takes a list of messages
90
- (e.g., [{"role": "user", "content": "..."}])
91
- and returns the assistant's reply as a string.
92
- """
93
- response = self.client.chat_completion(
94
- messages=messages,
95
- max_tokens=max_tokens,
96
- temperature=temperature,
97
- stop=stop_sequences,
98
- **kwargs
99
- )
100
- return response.choices[0].message.content
101
 
102
  # ---------- Assemble the agent ----------
103
  tools = [calculator]
@@ -108,7 +120,7 @@ try:
108
  except ImportError:
109
  logger.warning("duckduckgo-search not installed, web_search disabled.")
110
 
111
- model = CustomHFModel() # you can change the model_id if desired
112
  agent = CodeAgent(tools=tools, model=model)
113
 
114
  # ---------- The class expected by app.py ----------
 
3
  import json
4
  import logging
5
  from smolagents import CodeAgent, tool
6
+ from smolagents.models import Model
7
  from huggingface_hub import InferenceClient
8
 
9
  logging.basicConfig(level=logging.INFO)
10
  logger = logging.getLogger(__name__)
11
 
12
+ # Check HF_TOKEN at startup
13
+ hf_token = os.getenv("HF_TOKEN")
14
+ if hf_token:
15
+ logger.info("HF_TOKEN is set (first 4 chars: %s...)", hf_token[:4])
16
+ else:
17
+ logger.warning("HF_TOKEN is NOT set - inference may fail")
18
+
19
  # Cache for answers
20
  CACHE_FILE = "answer_cache.json"
21
  if os.path.exists(CACHE_FILE):
 
76
  except Exception as e:
77
  return f"Search error: {e}"
78
 
79
+ # ---------- Custom model with explicit Hugging Face endpoint ----------
80
  class CustomHFModel(Model):
81
+ def __init__(self, model_id="mistralai/Mistral-7B-Instruct-v0.2", **kwargs):
82
  super().__init__(**kwargs)
83
+ # Force Hugging Face inference endpoint
84
+ self.client = InferenceClient(
85
+ model=model_id,
86
+ token=os.getenv("HF_TOKEN"),
87
+ base_url="https://api-inference.huggingface.co" # Explicit HF URL
88
+ )
89
  self.model_id = model_id
90
+ logger.info(f"CustomHFModel initialized with model: {model_id}")
91
 
92
  def generate(
93
  self,
 
98
  temperature=0.7,
99
  **kwargs
100
  ):
101
+ try:
102
+ response = self.client.chat_completion(
103
+ messages=messages,
104
+ max_tokens=max_tokens,
105
+ temperature=temperature,
106
+ stop=stop_sequences,
107
+ **kwargs
108
+ )
109
+ return response.choices[0].message.content
110
+ except Exception as e:
111
+ # Wrap error with model info for debugging
112
+ raise Exception(f"Model {self.model_id} inference failed: {e}")
 
113
 
114
  # ---------- Assemble the agent ----------
115
  tools = [calculator]
 
120
  except ImportError:
121
  logger.warning("duckduckgo-search not installed, web_search disabled.")
122
 
123
+ model = CustomHFModel() # You can change model_id here if needed
124
  agent = CodeAgent(tools=tools, model=model)
125
 
126
  # ---------- The class expected by app.py ----------