Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,24 +1,31 @@
|
|
1 |
from llama_cpp import Llama
|
2 |
from duckduckgo_search import DDGS
|
3 |
from e2b import Sandbox
|
4 |
-
|
5 |
import gradio as gr
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
9 |
def search_tool(q):
|
10 |
with DDGS() as ddgs:
|
11 |
results = ddgs.text(q)
|
12 |
-
return "\n".join([r[
|
13 |
|
14 |
def calc_tool(expr):
|
15 |
-
try:
|
16 |
-
|
|
|
|
|
17 |
|
18 |
def run_tool(command):
|
19 |
with Sandbox(template="base") as sb:
|
20 |
-
|
21 |
-
return
|
22 |
|
23 |
tools = {
|
24 |
"search": search_tool,
|
@@ -26,22 +33,25 @@ tools = {
|
|
26 |
"run": run_tool
|
27 |
}
|
28 |
|
|
|
29 |
def parse_tools(text):
|
30 |
-
for
|
31 |
-
if f"<tool:{
|
32 |
-
start = text.find(f"<tool:{
|
33 |
-
end = text.find(f"</tool:{
|
34 |
arg = text[start:end].strip()
|
35 |
-
return tools[
|
36 |
return None
|
37 |
|
|
|
38 |
def agent_chat(user_input, history=[]):
|
39 |
history.append({"role": "user", "content": user_input})
|
40 |
prompt = "\n".join([f"{m['role']}: {m['content']}" for m in history])
|
41 |
output = llm(prompt=prompt, stop=["user:", "system:"], echo=False)
|
42 |
response = output["choices"][0]["text"].strip()
|
43 |
result = parse_tools(response)
|
44 |
-
if result:
|
|
|
45 |
history.append({"role": "assistant", "content": response})
|
46 |
return response
|
47 |
|
|
|
1 |
from llama_cpp import Llama
|
2 |
from duckduckgo_search import DDGS
|
3 |
from e2b import Sandbox
|
|
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Load GGUF model (Sam-reason-S2.1, gemma3 arch)
|
7 |
+
llm = Llama(
|
8 |
+
model_path="models/Sam-reason-S2.1.Q4_K_S.gguf",
|
9 |
+
n_ctx=2048,
|
10 |
+
verbose=False
|
11 |
+
)
|
12 |
|
13 |
+
# Tools
|
14 |
def search_tool(q):
|
15 |
with DDGS() as ddgs:
|
16 |
results = ddgs.text(q)
|
17 |
+
return "\n".join([f"{r['title']}: {r['href']}" for r in results[:3]])
|
18 |
|
19 |
def calc_tool(expr):
|
20 |
+
try:
|
21 |
+
return str(eval(expr))
|
22 |
+
except Exception as e:
|
23 |
+
return f"Math error: {e}"
|
24 |
|
25 |
def run_tool(command):
|
26 |
with Sandbox(template="base") as sb:
|
27 |
+
result = sb.run(command)
|
28 |
+
return result.stdout or result.stderr or "No output."
|
29 |
|
30 |
tools = {
|
31 |
"search": search_tool,
|
|
|
33 |
"run": run_tool
|
34 |
}
|
35 |
|
36 |
+
# Tool parser
|
37 |
def parse_tools(text):
|
38 |
+
for name in tools:
|
39 |
+
if f"<tool:{name}>" in text:
|
40 |
+
start = text.find(f"<tool:{name}>") + len(f"<tool:{name}>")
|
41 |
+
end = text.find(f"</tool:{name}>")
|
42 |
arg = text[start:end].strip()
|
43 |
+
return tools[name](arg)
|
44 |
return None
|
45 |
|
46 |
+
# Agent loop
|
47 |
def agent_chat(user_input, history=[]):
|
48 |
history.append({"role": "user", "content": user_input})
|
49 |
prompt = "\n".join([f"{m['role']}: {m['content']}" for m in history])
|
50 |
output = llm(prompt=prompt, stop=["user:", "system:"], echo=False)
|
51 |
response = output["choices"][0]["text"].strip()
|
52 |
result = parse_tools(response)
|
53 |
+
if result:
|
54 |
+
response += f"\n🔧 Tool Output:\n{result}"
|
55 |
history.append({"role": "assistant", "content": response})
|
56 |
return response
|
57 |
|