Updated engine.py to make message payload OpenAI-compatible for Qwen2.5B
Browse files
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
|
| 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
|
| 137 |
-
This
|
| 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 |
-
#
|
| 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
|
| 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 |
-
#
|
| 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,
|