Leonydis137 commited on
Commit
75afe96
·
verified ·
1 Parent(s): 68c2148

Update agent_engine.py

Browse files
Files changed (1) hide show
  1. agent_engine.py +6 -32
agent_engine.py CHANGED
@@ -19,38 +19,12 @@ 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."""
 
19
  logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
20
 
21
 
22
+ chat_history = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in history])
23
+ full_prompt = f"{system_prompt}\n\n{chat_history}\n\nAssistant:"
24
+ payload = {
25
+ "inputs": full_prompt,
26
+ "parameters": {"max_new_tokens": 300, "temperature": temperature}
27
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def step_turn(conversation: list, turn: int, topic: str, params: dict) -> list:
30
  """Advance one turn of the multi-agent conversation."""