ishan-25 commited on
Commit
215d5f8
Β·
verified Β·
1 Parent(s): 2611a9e

Updated engine.py to make message payload OpenAI-compatible for Qwen2.5B

Browse files
Files changed (1) hide show
  1. engine.py +6 -9
engine.py CHANGED
@@ -128,30 +128,28 @@ import os
128
  from config import HF_MODEL
129
  from agents import ValidatorAgent, QuestionGenAgent, ScorerAgent
130
 
131
- # Initialize the official serverless client at the module level
132
  client = InferenceClient(model=HF_MODEL, token=os.environ.get("HF_TOKEN"))
133
 
134
  def ask_llm(prompt: str, temperature: float = 0.7, max_tokens: int = 512) -> str:
135
  """
136
- Hugging Face Inference Provider API call using the modern chat_completion pipeline.
137
- This routes requests via active serverless clusters natively and eliminates task errors.
138
  """
139
  try:
140
- # Hugging Face API requirements demand a temperature reading strictly > 0
141
  safe_temp = temperature if temperature > 0 else 0.01
142
 
143
- # ── SWAPPED FROM text_generation TO chat_completion ──
144
  response = client.chat_completion(
145
  messages=[{"role": "user", "content": prompt}],
146
  max_tokens=max_tokens,
147
  temperature=safe_temp
148
  )
149
 
150
- # Extract the text string from the standard OpenAI choice payload structure
151
  cleaned = response.choices[0].message.content
152
  cleaned = cleaned.replace("<s>", "").replace("</s>", "").strip()
153
 
154
- # Diagnostic print visible inside your Hugging Face Live Logs screen
155
  print("\n" + "="*40)
156
  print(f"[LLM RAW OUTPUT]: '{cleaned}'")
157
  print("="*40 + "\n")
@@ -165,12 +163,11 @@ def ask_llm(prompt: str, temperature: float = 0.7, max_tokens: int = 512) -> str
165
 
166
 
167
  # ── Agent instances (singletons, created once at startup) ─────────────────────
168
- # These MUST stay directly below ask_llm to prevent initialization NameErrors
169
  _validator = ValidatorAgent(ask_llm)
170
  _q_gen = QuestionGenAgent(ask_llm)
171
  _scorer = ScorerAgent(ask_llm)
172
 
173
-
174
  # ── Main orchestration functions (called by Gradio UI) ────────────────────────
175
 
176
  def generate_all_questions(job_desc: str, mode_label: str,
 
128
  from config import HF_MODEL
129
  from agents import ValidatorAgent, QuestionGenAgent, ScorerAgent
130
 
131
+ # Initialize the inference client globally using your global configuration string
132
  client = InferenceClient(model=HF_MODEL, token=os.environ.get("HF_TOKEN"))
133
 
134
  def ask_llm(prompt: str, temperature: float = 0.7, max_tokens: int = 512) -> str:
135
  """
136
+ Hugging Face Inference Provider call utilizing the modern chat completion pipeline.
137
+ This fulfills partner routing rules cleanly and bypasses task metadata limitations.
138
  """
139
  try:
 
140
  safe_temp = temperature if temperature > 0 else 0.01
141
 
142
+ # Format the unstructured string prompt into a compliant chat completion message schema
143
  response = client.chat_completion(
144
  messages=[{"role": "user", "content": prompt}],
145
  max_tokens=max_tokens,
146
  temperature=safe_temp
147
  )
148
 
149
+ # Extract the content text directly from the returned choices payload array
150
  cleaned = response.choices[0].message.content
151
  cleaned = cleaned.replace("<s>", "").replace("</s>", "").strip()
152
 
 
153
  print("\n" + "="*40)
154
  print(f"[LLM RAW OUTPUT]: '{cleaned}'")
155
  print("="*40 + "\n")
 
163
 
164
 
165
  # ── Agent instances (singletons, created once at startup) ─────────────────────
166
+ # Retained directly below the function layout to protect against local NameErrors
167
  _validator = ValidatorAgent(ask_llm)
168
  _q_gen = QuestionGenAgent(ask_llm)
169
  _scorer = ScorerAgent(ask_llm)
170
 
 
171
  # ── Main orchestration functions (called by Gradio UI) ────────────────────────
172
 
173
  def generate_all_questions(job_desc: str, mode_label: str,