HERRAMIENTAS = { “calc”: expresión lambda: _safe_calc(expresión), “kb_search”: consulta lambda, k=3: _kb_search(consulta, int(k)), “extract_json”: texto lambda: _extract_json(texto), “write_file”: ruta lambda, contenido: _write_file(ruta, contenido), } TOOL_SCHEMAS = [
{“type”: “function”,”function”:{“name”:”calc”,”description”:”Safely compute a numeric expression.”,”parameters”:{“type”:”object”,”properties”:{“expression”:{“type”:”string”}},”required”:[“expression”]}}}, {“type”: “function”,”function”:{“name”:”kb_search”,”description”:”Buscar mini base de conocimiento interna.”,”parameters”:{“type”:”object”,”properties”:{“query”:{“type”:”string”},”k”:{“type”:”integer”,”default”:3}},”required”:[“query”]}}}, {“type”: “function”,”function”:{“name”:”extract_json”,”description”:”Extrae y analiza el primer objeto JSON del texto.”,”parameters”:{“type”:”object”,”properties”:{“text”:{“type”:”string”}},”required”:[“text”]}}}, {“type”: “function”,”function”:{“name”:”write_file”,”description”:”Escribe contenido en una ruta de archivo.”,”parameters”:{“type”:”object”,”properties”:{“path”:{“type”:”string”},”content”:{“type”:”string”}},”required”:[“path”,”content”]}}}, ]@dataclass clase AgentState: objetivo: memoria str: Lista[str] = campo(default_factory=lista) seguimiento: Lista[Dict[str, Any]]= campo(default_factory=lista) def chat(mensajes, herramientas=Ninguno, herramienta_choice=”auto”, temperatura=0.2): kwargs = dict( modelo=MODELO, mensajes=mensajes, temperatura=temperatura, ) si herramientas no es Ninguna: kwargs[“tools”] = herramientas kwargs[“tool_choice”] = tool_choice return client.chat.completions.create(**kwargs) def run_tool(name, args): fn = TOOLS.get(name) if not fn: return {“ok”: False, “error”: f”Herramienta desconocida: {name}”} intente: return fn(**args) excepto Excepción como e: return {“ok”: False, “error”: str(e), “args”: args}
{“type”: “function”,”function”:{“name”:”calc”,”description”:”Safely compute a numeric expression.”,”parameters”:{“type”:”object”,”properties”:{“expression”:{“type”:”string”}},”required”:[“expression”]}}}, {“type”: “function”,”function”:{“name”:”kb_search”,”description”:”Buscar mini base de conocimiento interna.”,”parameters”:{“type”:”object”,”properties”:{“query”:{“type”:”string”},”k”:{“type”:”integer”,”default”:3}},”required”:[“query”]}}}, {“type”: “function”,”function”:{“name”:”extract_json”,”description”:”Extrae y analiza el primer objeto JSON del texto.”,”parameters”:{“type”:”object”,”properties”:{“text”:{“type”:”string”}},”required”:[“text”]}}}, {“type”: “function”,”function”:{“name”:”write_file”,”description”:”Escribe contenido en una ruta de archivo.”,”parameters”:{“type”:”object”,”properties”:{“path”:{“type”:”string”},”content”:{“type”:”string”}},”required”:[“path”,”content”]}}}, ]@dataclass clase AgentState: objetivo: memoria str: Lista[str] = campo(default_factory=lista) seguimiento: Lista[Dict[str, Any]]= campo(default_factory=lista) def chat(mensajes, herramientas=Ninguno, herramienta_choice=”auto”, temperatura=0.2): kwargs = dict( modelo=MODELO, mensajes=mensajes, temperatura=temperatura, ) si herramientas no es Ninguna: kwargs[“tools”] = herramientas kwargs[“tool_choice”] = tool_choice return client.chat.completions.create(**kwargs) def run_tool(name, args): fn = TOOLS.get(name) if not fn: return {“ok”: False, “error”: f”Herramienta desconocida: {name}”} intente: return fn(**args) excepto Excepción como e: return {“ok”: False, “error”: str(e), “args”: args}