Spaces:
Runtime error
Runtime error
from llama_cpp import Llama | |
from duckduckgo_search import DDGS | |
from e2b import Sandbox | |
import gradio as gr | |
llm = Llama(model_path="models/Sam-reason-A1.Q4_K_S.gguf", n_ctx=2048) | |
def search_tool(q): | |
with DDGS() as ddgs: | |
results = ddgs.text(q) | |
return "\n".join([r["title"] + ": " + r["href"] for r in results[:3]]) | |
def calc_tool(expr): | |
try: return str(eval(expr)) | |
except Exception as e: return f"Math error: {e}" | |
def run_tool(command): | |
with Sandbox(template="base") as sb: | |
out = sb.run(command) | |
return out.stdout or out.stderr or "No output." | |
tools = { | |
"search": search_tool, | |
"calc": calc_tool, | |
"run": run_tool | |
} | |
def parse_tools(text): | |
for key in tools: | |
if f"<tool:{key}>" in text: | |
start = text.find(f"<tool:{key}>") + len(f"<tool:{key}>") | |
end = text.find(f"</tool:{key}>") | |
arg = text[start:end].strip() | |
return tools[key](arg) | |
return None | |
def agent_chat(user_input, history=[]): | |
history.append({"role": "user", "content": user_input}) | |
prompt = "\n".join([f"{m['role']}: {m['content']}" for m in history]) | |
output = llm(prompt=prompt, stop=["user:", "system:"], echo=False) | |
response = output["choices"][0]["text"].strip() | |
result = parse_tools(response) | |
if result: response += f"\n🔧 Tool Output:\n{result}" | |
history.append({"role": "assistant", "content": response}) | |
return response | |
gr.ChatInterface(fn=agent_chat).launch() | |