blu-assistant / src /blu /agent.py
Aravindhan11's picture
Upload 33 files
9776024 verified
from typing import Optional, List, Dict
from .llm import BluLLM, make_messages
from .rag import search as rag_search
from .tools import calc, read_file
from .utils import violates_safety
SYSTEM_PROMPT = """You are Blu, a multilingual, tool-using assistant.
Rules:
1) For math, call the calculator tool.
2) For file questions, call the file reader.
3) If --rag is enabled (context provided), use it to ground answers and cite filenames.
4) Be concise by default; switch to step-by-step for complex tasks.
5) Refuse unsafe requests politely."""
def run_blu(
query: str,
use_rag: bool = False,
rag_query: Optional[str] = None,
llm: Optional[BluLLM] = None,
peft_adapter: Optional[str] = None,
) -> str:
if violates_safety(query):
return "Sorry, I can’t help with that."
llm = llm or BluLLM()
context = None
if use_rag:
rq = rag_query or query
hits = rag_search(rq, k=4)
if hits:
context = "\n\n".join([f"[Source: {p}]\n{t}" for (p, t) in hits])
if any(k in query.lower() for k in ["calc:", "calculate", "evaluate"]):
expr = query.split(":",1)[1].strip() if ":" in query else query
tool_out = calc(expr)
query = f"I computed `{expr}` and got `{tool_out}`. Please explain the steps briefly."
if query.lower().startswith("read ") or query.lower().startswith("open "):
path = query.split(" ",1)[1].strip()
tool_out = read_file(path)
query = f"I opened {path}. Contents snippet:\n{tool_out}\nNow answer the question above based on this."
msgs = make_messages(query, system_text=SYSTEM_PROMPT, context=context)
answer = llm.chat(msgs)
if use_rag and context:
answer += "\n\n(Sources consulted above.)"
return answer