Spaces:
Sleeping
Sleeping
File size: 5,050 Bytes
a6ea1e1 4133259 a6ea1e1 4133259 85c613a a6ea1e1 0ceac21 a6ea1e1 0ceac21 a6ea1e1 0ceac21 9f4ee55 0ceac21 5e00653 0ceac21 9f4ee55 e1bd82f 0ceac21 e1bd82f 5e00653 0ceac21 9f4ee55 0ceac21 9f4ee55 0ceac21 5e00653 0ceac21 9f4ee55 0ceac21 9f4ee55 0ceac21 9f4ee55 a6ea1e1 b56404a 0ceac21 b56404a 0ceac21 e1bd82f 0ceac21 9f4ee55 4133259 9f4ee55 e1bd82f 9f4ee55 4133259 e1bd82f 0ceac21 4133259 9f4ee55 b56404a 9f4ee55 a6ea1e1 85c613a 0ceac21 85c613a 598819e a6ea1e1 0ceac21 9f4ee55 5e00653 e1bd82f 5e00653 85c613a 0ceac21 85c613a 0ceac21 85c613a 0ceac21 598819e 0ceac21 43bd2be 85c613a 0ceac21 85c613a 4133259 85c613a e1bd82f 85c613a 0ceac21 85c613a 4133259 85c613a a6ea1e1 9f4ee55 43bd2be 9f4ee55 a6ea1e1 0ceac21 a6ea1e1 4133259 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import requests
import json
from config import Config
from planner import create_plan
from executor import Executor
from tools_runtime import execute_tool_call
# =====================================================
# 🔥 UNIVERSAL LLM ROUTER (INSIDE agent.py)
# =====================================================
class LLMRouter:
def __init__(self):
self.providers = [
{
"url": Config.LLM_API_URL,
"key": Config.LLM_API_KEY,
"model": Config.MODEL
}
]
def call(self, messages):
last_error = None
for p in self.providers:
try:
res = requests.post(
p["url"],
headers={
"Authorization": f"Bearer {p['key']}",
"Content-Type": "application/json"
},
json={
"model": p["model"],
"messages": messages,
"temperature": 0.2
},
timeout=30
)
data = res.json()
# =========================
# NORMALIZER (IMPORTANT FIX)
# =========================
if isinstance(data, list):
data = data[0] if data else {}
if isinstance(data, dict) and "result" in data:
data = data["result"]
# =========================
# VALIDATION
# =========================
if isinstance(data, dict) and "choices" in data:
return data
except Exception as e:
last_error = str(e)
continue
return {
"choices": [{
"message": {
"content": "LLM_ERROR"
}
}],
"error": last_error
}
# =====================================================
# AGENT
# =====================================================
class Agent:
def __init__(self):
self.llm = LLMRouter().call
self.executor = Executor(llm=self.llm)
self.history = []
# =====================================================
# GOAL MODE
# =====================================================
def run_goal(self, goal: str):
try:
plan = create_plan(goal)
if not isinstance(plan, dict):
return {"mode": "error", "error": "invalid_plan"}
except Exception as e:
return {
"mode": "error",
"error": f"plan_failed: {str(e)}"
}
try:
results = self.executor.run_plan(plan)
except Exception as e:
results = {"error": str(e)}
return {
"mode": "goal_execution",
"goal": goal,
"plan": plan,
"results": results
}
# =====================================================
# CHAT / TOOL MODE
# =====================================================
def run(self, user_input):
messages = [
{
"role": "system",
"content": Config.SYSTEM_PROMPT
},
{
"role": "user",
"content": user_input
}
]
response = self.llm(messages)
# =========================
# SAFE EXTRACTION
# =========================
try:
content = (
response.get("choices", [{}])[0]
.get("message", {})
.get("content", "")
)
except Exception:
content = ""
if not content:
return {
"mode": "error",
"response": "empty_llm_response"
}
# =========================
# TOOL MODE
# =========================
try:
tool = json.loads(content)
if isinstance(tool, dict) and "tool" in tool:
result = execute_tool_call(tool)
self.history.append({
"input": user_input,
"tool_call": tool,
"result": result
})
return {
"mode": "tool_execution",
"result": result
}
except Exception:
pass
# =========================
# NORMAL MODE
# =========================
return {
"mode": "normal_response",
"response": content
}
# =====================================================
# DEBUG
# =====================================================
if __name__ == "__main__":
agent = Agent()
print(agent.run("List files in current directory"))
print(agent.run_goal("Create a simple python web server")) |