Spaces:
Sleeping
Sleeping
Update agent_engine.py
Browse files- agent_engine.py +36 -9
agent_engine.py
CHANGED
@@ -18,22 +18,49 @@ CHAT_MODEL = os.environ.get("CHAT_MODEL", "HuggingFaceH4/zephyr-7b-beta")
|
|
18 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")
|
19 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
20 |
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def step_turn(conversation: list, turn: int, topic: str, params: dict) -> list:
|
30 |
"""Advance one turn of the multi-agent conversation."""
|
31 |
-
# Choose agent by sequence
|
32 |
sequence = ["Initiator", "Responder", "Guardian", "Provocateur", "Cultural"]
|
33 |
agent = sequence[turn % len(sequence)]
|
34 |
prompt = PROMPTS.get(agent, "")
|
35 |
-
|
36 |
-
history = [{"role": "user", "content": msg['text']} for msg in conversation[-5:]]
|
37 |
response = safe_chat(prompt, history, temperature=params[agent]['creativity'])
|
38 |
embed_and_store(response, agent, topic)
|
39 |
conversation.append({"agent": agent, "text": response, "turn": turn + 1})
|
|
|
18 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")
|
19 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
20 |
|
21 |
+
def safe_chat(system_prompt: str, history: list, temperature: float = 0.7) -> str:
|
22 |
+
"""Call HF inference API with fallback formatting."""
|
23 |
+
start = perf_counter()
|
24 |
+
chat_history = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in history])
|
25 |
+
full_prompt = f"{system_prompt}\n\n{chat_history}\n\nAssistant:"
|
26 |
|
27 |
+
payload = {
|
28 |
+
"inputs": full_prompt,
|
29 |
+
"parameters": {"max_new_tokens": 300, "temperature": temperature}
|
30 |
+
}
|
31 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} if HF_API_TOKEN else {}
|
32 |
+
|
33 |
+
try:
|
34 |
+
resp = requests.post(
|
35 |
+
f"https://api-inference.huggingface.co/models/{CHAT_MODEL}",
|
36 |
+
json=payload,
|
37 |
+
headers=headers,
|
38 |
+
timeout=60
|
39 |
+
)
|
40 |
+
if resp.status_code == 200:
|
41 |
+
data = resp.json()
|
42 |
+
text = data[0].get("generated_text", "").strip() if isinstance(data, list) else data.get("generated_text", "").strip()
|
43 |
+
elif resp.status_code == 503:
|
44 |
+
logging.warning("Model loading… retrying after 15s.")
|
45 |
+
sleep(15)
|
46 |
+
return safe_chat(system_prompt, history, temperature)
|
47 |
+
else:
|
48 |
+
logging.error(f"HF error {resp.status_code}: {resp.text}")
|
49 |
+
text = f"⚠️ API Error {resp.status_code}"
|
50 |
+
except Exception as e:
|
51 |
+
logging.error(f"safe_chat exception: {e}")
|
52 |
+
text = f"⚠️ System Error: {e}"
|
53 |
+
|
54 |
+
elapsed = perf_counter() - start
|
55 |
+
logging.info(f"safe_chat: {elapsed:.3f}s for prompt '{system_prompt[:30]}…'")
|
56 |
+
return text
|
57 |
|
58 |
def step_turn(conversation: list, turn: int, topic: str, params: dict) -> list:
|
59 |
"""Advance one turn of the multi-agent conversation."""
|
|
|
60 |
sequence = ["Initiator", "Responder", "Guardian", "Provocateur", "Cultural"]
|
61 |
agent = sequence[turn % len(sequence)]
|
62 |
prompt = PROMPTS.get(agent, "")
|
63 |
+
history = [{"role": "user", "content": msg['text']} for msg in conversation[-5:] if msg['agent'] != "System"]
|
|
|
64 |
response = safe_chat(prompt, history, temperature=params[agent]['creativity'])
|
65 |
embed_and_store(response, agent, topic)
|
66 |
conversation.append({"agent": agent, "text": response, "turn": turn + 1})
|