Spaces:
Build error
Build error
Create agent_engine.py
Browse files- agent_engine.py +66 -0
agent_engine.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
+
import requests
|
| 4 |
+
from time import perf_counter, sleep
|
| 5 |
+
from .memory_manager import embed_and_store, retrieve_relevant
|
| 6 |
+
|
| 7 |
+
# Agent prompts
|
| 8 |
+
PROMPTS = {
|
| 9 |
+
"Initiator": "You are the Discussion Initiator...",
|
| 10 |
+
"Responder": "You are the Critical Responder...",
|
| 11 |
+
"Guardian": "You are the Depth Guardian...",
|
| 12 |
+
"Provocateur": "You are the Cross-Disciplinary Provocateur...",
|
| 13 |
+
"Cultural": "You are the Cultural Perspective...",
|
| 14 |
+
"Judge": "You are the Impartial Judge..."
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
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 |
+
def safe_chat(system_prompt: str, history: list, temperature: float = 0.7) -> str:
|
| 23 |
+
"""Call HF inference API with timing and error handling."""
|
| 24 |
+
start = perf_counter()
|
| 25 |
+
payload = {
|
| 26 |
+
"inputs": [{"role": "system", "content": system_prompt}] + history,
|
| 27 |
+
"parameters": {"max_new_tokens": 300, "temperature": temperature}
|
| 28 |
+
}
|
| 29 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} if HF_API_TOKEN else {}
|
| 30 |
+
try:
|
| 31 |
+
resp = requests.post(
|
| 32 |
+
f"https://api-inference.huggingface.co/models/{CHAT_MODEL}",
|
| 33 |
+
json=payload,
|
| 34 |
+
headers=headers,
|
| 35 |
+
timeout=60
|
| 36 |
+
)
|
| 37 |
+
if resp.status_code == 200:
|
| 38 |
+
data = resp.json()
|
| 39 |
+
text = data[0].get('generated_text', '').strip()
|
| 40 |
+
elif resp.status_code == 503:
|
| 41 |
+
logging.warning("Model loading, retrying...")
|
| 42 |
+
sleep(15)
|
| 43 |
+
return safe_chat(system_prompt, history, temperature)
|
| 44 |
+
else:
|
| 45 |
+
logging.error(f"HF error {resp.status_code}: {resp.text}")
|
| 46 |
+
text = f"⚠️ API Error {resp.status_code}"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
logging.error(f"safe_chat exception: {e}")
|
| 49 |
+
text = f"⚠️ System Error: {e}"
|
| 50 |
+
elapsed = perf_counter() - start
|
| 51 |
+
logging.info(f"safe_chat: {elapsed:.3f}s for '{system_prompt[:30]}...'")
|
| 52 |
+
return text
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def step_turn(conversation: list, turn: int, topic: str, params: dict) -> list:
|
| 56 |
+
"""Advance one turn of the multi-agent conversation."""
|
| 57 |
+
# Choose agent by sequence
|
| 58 |
+
sequence = ["Initiator", "Responder", "Guardian", "Provocateur", "Cultural"]
|
| 59 |
+
agent = sequence[turn % len(sequence)]
|
| 60 |
+
prompt = PROMPTS.get(agent, "")
|
| 61 |
+
# Prepare history
|
| 62 |
+
history = [{"role": "user", "content": msg['text']} for msg in conversation[-5:]]
|
| 63 |
+
response = safe_chat(prompt, history, temperature=params[agent]['creativity'])
|
| 64 |
+
embed_and_store(response, agent, topic)
|
| 65 |
+
conversation.append({"agent": agent, "text": response, "turn": turn + 1})
|
| 66 |
+
return conversation
|