mohantest commited on
Commit
53d79dc
·
verified ·
1 Parent(s): a2d7807

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +40 -9
agent.py CHANGED
@@ -78,9 +78,9 @@ def web_search(query: str) -> str:
78
  except Exception as e:
79
  return f"Search error: {e}"
80
 
81
- # ---------- Custom Model with Direct HTTP ----------
82
  class CustomHFModel(Model):
83
- def __init__(self, model_id="HuggingFaceH4/zephyr-7b-beta", **kwargs):
84
  super().__init__(**kwargs)
85
  self.model_id = model_id
86
  self.api_url = f"https://api-inference.huggingface.co/models/{model_id}"
@@ -97,12 +97,19 @@ class CustomHFModel(Model):
97
  **kwargs
98
  ):
99
  """
100
- Convert ChatMessage objects or dicts to a prompt and call HF Inference API.
 
 
101
  """
102
- # Build prompt in Zephyr's chat format
103
  prompt = ""
 
 
 
 
 
 
104
  for msg in messages:
105
- # Handle both ChatMessage objects and plain dicts
106
  if hasattr(msg, 'role'):
107
  role = msg.role
108
  content = msg.content
@@ -111,12 +118,36 @@ class CustomHFModel(Model):
111
  content = msg['content']
112
 
113
  if role == "system":
114
- prompt += f"<|system|>\n{content}\n</s>\n"
115
  elif role == "user":
116
- prompt += f"<|user|>\n{content}\n</s>\n"
117
  elif role == "assistant":
118
- prompt += f"<|assistant|>\n{content}\n</s>\n"
119
- prompt += "<|assistant|>\n" # start assistant response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  payload = {
122
  "inputs": prompt,
 
78
  except Exception as e:
79
  return f"Search error: {e}"
80
 
81
+ # ---------- Custom Model with Direct HTTP (Mistral Instruct) ----------
82
  class CustomHFModel(Model):
83
+ def __init__(self, model_id="mistralai/Mistral-7B-Instruct-v0.3", **kwargs):
84
  super().__init__(**kwargs)
85
  self.model_id = model_id
86
  self.api_url = f"https://api-inference.huggingface.co/models/{model_id}"
 
97
  **kwargs
98
  ):
99
  """
100
+ Convert ChatMessage objects to a Mistral Instruct prompt.
101
+ Format: <s>[INST] Instruction [/INST]</s>
102
+ For multi-turn: <s>[INST] First [/INST] First answer</s>[INST] Second [/INST] Second answer</s>
103
  """
104
+ # Build prompt in Mistral Instruct format
105
  prompt = ""
106
+ # Mistral expects alternating user/assistant, starting with user.
107
+ # If first message is system, we can prepend it to the first user message.
108
+ system_prompt = ""
109
+ user_msgs = []
110
+ assistant_msgs = []
111
+
112
  for msg in messages:
 
113
  if hasattr(msg, 'role'):
114
  role = msg.role
115
  content = msg.content
 
118
  content = msg['content']
119
 
120
  if role == "system":
121
+ system_prompt = content # store system prompt to prepend later
122
  elif role == "user":
123
+ user_msgs.append(content)
124
  elif role == "assistant":
125
+ assistant_msgs.append(content)
126
+
127
+ # Pair user and assistant messages
128
+ # Start with system if present
129
+ if system_prompt:
130
+ # Prepend system to the first user message (if any)
131
+ if user_msgs:
132
+ first_user = system_prompt + "\n\n" + user_msgs[0]
133
+ user_msgs[0] = first_user
134
+ else:
135
+ # No user message? treat system as first user
136
+ user_msgs = [system_prompt]
137
+
138
+ # Build conversation
139
+ for i in range(max(len(user_msgs), len(assistant_msgs))):
140
+ if i < len(user_msgs):
141
+ prompt += f"<s>[INST] {user_msgs[i]} [/INST]"
142
+ if i < len(assistant_msgs):
143
+ prompt += f" {assistant_msgs[i]}</s>"
144
+ else:
145
+ # Last assistant not yet generated; we'll add closing later
146
+ break
147
+
148
+ # If we're expecting an assistant response (last turn incomplete), prompt should end with the last [INST] ... [/INST] without closing.
149
+ # Actually, the pattern is: ... [/INST] (assistant will generate). So we leave as is.
150
+ # The API will continue the assistant part.
151
 
152
  payload = {
153
  "inputs": prompt,